@jkcfg/std
Advanced tools
Comparing version 0.3.2 to 0.4.0
import * as std from '../index'; | ||
declare type ValidateFn = (value: any) => string; | ||
export interface Value { | ||
import { WriteOptions } from '../write'; | ||
import { ValidateFn } from './validate'; | ||
/** | ||
* File is the basic unit of input for generate; it represents a | ||
* configuration item to output to a file. | ||
*/ | ||
export interface File { | ||
path: string; | ||
@@ -12,5 +17,11 @@ value: any | Promise<any>; | ||
overwrite?: std.Overwrite; | ||
writeFile?: (v: any, p: string, o?: WriteOptions) => void; | ||
} | ||
declare type ValueArg = Value[] | Promise<Value[]> | (() => Value[]); | ||
declare function generate(definition: ValueArg, params: GenerateParams): void; | ||
export { generate }; | ||
declare type GenerateArg = File[] | Promise<File[]> | (() => File[]); | ||
/** | ||
* generate is the entry point for the module; it accepts the input | ||
* configuration, and outputs validated values to the files as | ||
* specified. | ||
*/ | ||
export declare function generate(definition: GenerateArg, params: GenerateParams): Promise<void>; | ||
export {}; |
@@ -11,2 +11,3 @@ var __rest = (this && this.__rest) || function (s, e) { | ||
import * as std from '../index'; | ||
import { normaliseResult, formatError } from '../validation'; | ||
const helpMsg = ` | ||
@@ -26,3 +27,3 @@ To use generate, export a default value with the list of files to generate: | ||
function error(msg) { | ||
std.log(`error: ${msg}`); | ||
std.log(msg); | ||
} | ||
@@ -77,4 +78,4 @@ function help() { | ||
const isString = (s) => typeof s === 'string' || s instanceof String; | ||
// Compute the output format of a value. | ||
function valueFormat(o) { | ||
// Compute the output format of a file spec. | ||
function fileFormat(o) { | ||
let { path, format, value } = o; | ||
@@ -91,19 +92,3 @@ if (format === undefined || format === std.Format.FromExtension) { | ||
} | ||
function formatSummary(values) { | ||
const formats = Array(Object.keys(std.Format).length).fill(0); | ||
values.forEach((e) => { | ||
formats[valueFormat(e)] += 1; | ||
}); | ||
return formats; | ||
} | ||
const formatNames = [ | ||
'FromExtension', | ||
'JSON', | ||
'YAML', | ||
'Raw', | ||
'YAMLStream', | ||
'JSONStream', | ||
'HCL', | ||
]; | ||
const formatName = (f) => formatNames[f]; | ||
const formatName = (f) => std.Format[f]; | ||
function usedFormats(summary) { | ||
@@ -118,5 +103,5 @@ const augmented = summary.map((n, i) => ({ format: formatName(i), n })); | ||
} | ||
function validate(values, params) { | ||
function validateFormat(files, params) { | ||
/* we have an array */ | ||
if (!Array.isArray(values)) { | ||
if (!Array.isArray(files)) { | ||
error('default value is not an array'); | ||
@@ -127,3 +112,3 @@ return { valid: false, showHelp: true }; | ||
let valid = true; | ||
values.forEach((e, i) => { | ||
files.forEach((e, i) => { | ||
/* 'file' is the old 'path' property name. Fixup things */ | ||
@@ -140,38 +125,57 @@ if (e.file !== undefined) { | ||
}); | ||
if (!valid) { | ||
return { valid, showHelp: true }; | ||
} | ||
/* when outputting to stdout, ensure that: */ | ||
return { valid, showHelp: !valid }; | ||
} | ||
function assembleForStdout(values) { | ||
// When writing to stdout, we need to | ||
// 1. make sure everything is a mutually compatible format (e.g., | ||
// YAMLs and YAMLStreams, but not JSON with YAMLStreams); | ||
// 2. collate the values to be printed as a streamable array, which | ||
// means anything that's _already_ a stream value is inlined. | ||
let stdoutFormat; | ||
if (params.stdout === true) { | ||
/* there's a single output format defined */ | ||
const summary = formatSummary(values); | ||
const formats = usedFormats(summary); | ||
if (formats.length > 1) { | ||
error(`stdout output requires using a single format but got: ${formats.join(',')}`); | ||
return { valid: false, showHelp: false }; | ||
} | ||
/* | ||
* If we have more than one file to generate, make sure it's either JSON or | ||
* YAML so we can output a stream of documents. | ||
*/ | ||
if (values.length > 1 && formats[0] !== 'JSON' && formats[0] !== 'YAML') { | ||
error(`stdout output for multiple files requires either JSON or YAML format but got: ${formats[0]}`); | ||
return { valid: false, showHelp: false }; | ||
} | ||
if (values.length > 1) { | ||
if (formats[0] === 'JSON') { | ||
let stream = []; | ||
const formatsSeen = Array(Object.keys(std.Format).length).fill(0); | ||
for (const v of values) { | ||
const format = fileFormat(v); | ||
formatsSeen[format] += 1; | ||
switch (format) { | ||
case std.Format.YAML: | ||
case std.Format.YAMLStream: | ||
if (stdoutFormat !== undefined && stdoutFormat !== std.Format.YAMLStream) { | ||
error(`stdout requires compatible formats, but have seen ${usedFormats(formatsSeen).join(',')}`); | ||
return { valid: false }; | ||
} | ||
stdoutFormat = std.Format.YAMLStream; | ||
stream = (format === std.Format.YAML) ? | ||
stream.push(v.value) && stream : stream.concat(v.value); | ||
break; | ||
case std.Format.JSON: | ||
case std.Format.JSONStream: | ||
if (stdoutFormat !== undefined && stdoutFormat !== std.Format.JSONStream) { | ||
error(`stdout requires compatible formats, but have seen ${usedFormats(formatsSeen).join(',')}`); | ||
return { valid: false }; | ||
} | ||
stdoutFormat = std.Format.JSONStream; | ||
} | ||
else if (formats[0] === 'YAML') { | ||
stdoutFormat = std.Format.YAMLStream; | ||
} | ||
stream = (format === std.Format.JSON) ? | ||
stream.push(v.value) && stream : stream.concat(v.value); | ||
break; | ||
default: | ||
// for anything else, only one value is allowed; therefore keep | ||
// the value as it is, but check that this is the only value. | ||
if (stdoutFormat !== undefined) { | ||
error(`stdout requires compatible formats, but have seen ${usedFormats(formatsSeen).join(',')}`); | ||
return { valid: false }; | ||
} | ||
stdoutFormat = format; | ||
stream = v.value; | ||
break; | ||
} | ||
else { | ||
stdoutFormat = valueFormat(values[0]); | ||
} | ||
} | ||
return { valid: true, stdoutFormat, showHelp: false }; | ||
return { valid: true, stdoutFormat, stream }; | ||
} | ||
function generate(definition, params) { | ||
/** | ||
* generate is the entry point for the module; it accepts the input | ||
* configuration, and outputs validated values to the files as | ||
* specified. | ||
*/ | ||
export function generate(definition, params) { | ||
/* | ||
@@ -190,35 +194,46 @@ * The default export can be: | ||
} | ||
const { stdout = false, overwrite = false } = params; | ||
inputs.then((files) => { | ||
/* values can be promises as well */ | ||
const justValues = files.map(f => f.value); | ||
Promise.all(justValues).then((resolved) => { | ||
resolved.forEach((v, i) => { | ||
// eslint-disable-next-line no-param-reassign | ||
files[i].value = v; | ||
}); | ||
const { valid, stdoutFormat, showHelp } = validate(files, params); | ||
if (showHelp) { | ||
help(); | ||
const { stdout = false, overwrite = false, writeFile = std.write } = params; | ||
return async function () { | ||
const files = await inputs; | ||
const resolved = await Promise.all(files.map(f => f.value)); | ||
resolved.forEach((v, i) => { | ||
// eslint-disable-next-line no-param-reassign | ||
files[i].value = v; | ||
}); | ||
// check the format of the generated value | ||
const { valid: formatValid, showHelp } = validateFormat(files, params); | ||
if (showHelp) { | ||
help(); | ||
} | ||
if (!formatValid) { | ||
throw new Error('jk-internal-skip: format invalid'); | ||
} | ||
let results = await Promise.all(files.map(({ path, value, validate = (() => 'ok') }) => { | ||
return Promise.resolve(validate(value)) | ||
.then(r => ({ path, result: normaliseResult(r) })); | ||
})); | ||
let valuesValid = true; | ||
results.forEach(({ path, result }) => { | ||
if (result !== 'ok') { | ||
result.forEach(err => error(formatError(path, err))); | ||
valuesValid = false; | ||
} | ||
}); | ||
if (!valuesValid) { | ||
throw new Error('jk-internal-skip: values failed validation'); | ||
} | ||
if (stdout) { | ||
const { valid, stdoutFormat, stream } = assembleForStdout(files); | ||
if (!valid) { | ||
throw new Error('jk-internal-skip: validation failed'); | ||
} | ||
if (stdout) { | ||
if (files.length > 1) { | ||
std.write(justValues, '', { format: stdoutFormat }); | ||
} | ||
else { | ||
std.write(justValues[0], '', { format: stdoutFormat }); | ||
} | ||
std.write(stream, std.stdout, { format: stdoutFormat }); | ||
} | ||
else { | ||
for (const o of files) { | ||
const { path, value } = o, args = __rest(o, ["path", "value"]); | ||
writeFile(value, path, Object.assign({ overwrite }, args)); | ||
} | ||
else { | ||
for (const o of files) { | ||
const { path, value } = o, args = __rest(o, ["path", "value"]); | ||
std.write(value, path, Object.assign({ overwrite }, args)); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
}(); | ||
} | ||
export { generate }; |
@@ -1,45 +0,41 @@ | ||
import * as std from '../index'; | ||
import { Format, Overwrite } from '../index'; | ||
import * as host from '@jkcfg/std/internal/host'; // magic module | ||
import * as param from '../param'; | ||
import { generate } from './generate'; | ||
import { valuesFormatFromPath } from '../read'; | ||
const inputParams = { | ||
stdout: param.Boolean('jk.transform.stdout', false), | ||
overwrite: param.Boolean('jk.transform.overwrite', false) ? std.Overwrite.Write : std.Overwrite.Err, | ||
overwrite: param.Boolean('jk.transform.overwrite', false) ? Overwrite.Write : Overwrite.Err, | ||
}; | ||
function readFormatFromPath(path) { | ||
const ext = path.split('.').pop(); | ||
switch (ext) { | ||
case 'yaml': | ||
case 'yml': | ||
return std.Format.YAMLStream; | ||
case 'json': | ||
return std.Format.JSONStream; | ||
default: | ||
return std.Format.FromExtension; | ||
} | ||
// If we're told to overwrite, we need to be able to write to the | ||
// files mentioned on the command-line; but not otherwise. | ||
if (inputParams.overwrite == Overwrite.Write) { | ||
inputParams.writeFile = host.write; | ||
} | ||
function transformOne(fn, file, obj) { | ||
let txObj = fn(obj); | ||
txObj = (txObj === undefined) ? obj : txObj; | ||
return { | ||
value: txObj, | ||
path: file, | ||
}; | ||
} | ||
function transform(fn) { | ||
function transformOne(obj) { | ||
let txObj = fn(obj); | ||
txObj = (txObj === undefined) ? obj : txObj; | ||
return txObj; | ||
} | ||
const inputFiles = param.Object('jk.transform.input', {}); | ||
const outputs = []; | ||
for (const file of Object.keys(inputFiles)) { | ||
const format = readFormatFromPath(file); | ||
outputs.push(std.read(file, { format }).then((obj) => { | ||
for (const path of Object.keys(inputFiles)) { | ||
const format = valuesFormatFromPath(path); | ||
outputs.push(host.read(path, { format }).then((obj) => { | ||
switch (format) { | ||
case std.Format.YAMLStream: | ||
case std.Format.JSONStream: | ||
return obj.map((v) => transformOne(fn, file, v)); | ||
case Format.YAMLStream: | ||
case Format.JSONStream: | ||
return { | ||
path, | ||
format, | ||
value: Array.prototype.map.call(obj, transformOne), | ||
}; | ||
default: | ||
return [transformOne(fn, file, obj)]; | ||
return { path, value: transformOne(obj) }; | ||
} | ||
})); | ||
} | ||
generate(Promise.all(outputs).then((vs) => Array.prototype.concat(...vs)), inputParams); | ||
generate(Promise.all(outputs), inputParams); | ||
} | ||
export default transform; |
@@ -1,4 +0,3 @@ | ||
declare type ValidateFnResult = boolean | string | string[]; | ||
declare type ValidateFn = (obj: any) => ValidateFnResult; | ||
import { ValidateFnResult } from '../validation'; | ||
export declare type ValidateFn = (obj: any) => ValidateFnResult | Promise<ValidateFnResult>; | ||
export default function validate(fn: ValidateFn): void; | ||
export {}; |
@@ -1,20 +0,14 @@ | ||
import * as std from '../index'; | ||
import { log, Format } from '../index'; | ||
import * as host from '@jkcfg/std/internal/host'; // magic module | ||
import * as param from '../param'; | ||
function normaliseResult(result) { | ||
switch (typeof result) { | ||
case 'string': | ||
if (result === 'ok') | ||
return result; | ||
return [result]; | ||
case 'boolean': | ||
if (result) | ||
return 'ok'; | ||
return ['value not valid']; | ||
case 'object': | ||
if (Array.isArray(result)) | ||
return result; | ||
break; | ||
default: | ||
} | ||
throw new Error(`unrecognised result from validation function: ${result}`); | ||
import { formatError, normaliseResult } from '../validation'; | ||
import { valuesFormatFromPath } from '../read'; | ||
function reduce(results) { | ||
return results.reduce((a, b) => { | ||
if (a == 'ok') | ||
return b; | ||
if (b == 'ok') | ||
return a; | ||
return Array.prototype.concat(a, b); | ||
}, 'ok'); | ||
} | ||
@@ -24,6 +18,19 @@ export default function validate(fn) { | ||
const files = Object.keys(inputFiles); | ||
const validateFile = async function vf(path) { | ||
const obj = await std.read(path); | ||
return { path, result: normaliseResult(fn(obj)) }; | ||
}; | ||
function validateValue(v) { | ||
return Promise.resolve(fn(v)).then(normaliseResult); | ||
} | ||
async function validateFile(path) { | ||
const format = valuesFormatFromPath(path); | ||
const obj = await host.read(path, { format }); | ||
switch (format) { | ||
case Format.YAMLStream: | ||
case Format.JSONStream: | ||
const results = obj.map(validateValue); | ||
const resolvedResults = await Promise.all(results); | ||
return { path, result: reduce(resolvedResults) }; | ||
default: | ||
const result = await validateValue(obj); | ||
return { path, result }; | ||
} | ||
} | ||
const objects = files.map(validateFile); | ||
@@ -33,8 +40,7 @@ Promise.all(objects).then((results) => { | ||
if (result === 'ok') { | ||
std.log(`${path}: ok`); | ||
log(`${path}: ok`); | ||
} | ||
else { | ||
std.log(`${path}:`); | ||
for (const err of result) { | ||
std.log(` error: ${err}`); | ||
log(formatError(path, err)); | ||
} | ||
@@ -41,0 +47,0 @@ } |
18
fs.d.ts
@@ -25,7 +25,21 @@ /** | ||
export declare function join(base: string, name: string): string; | ||
interface WalkOpts { | ||
export interface WalkOpts { | ||
pre?: (f: FileInfo) => boolean; | ||
post?: () => void; | ||
} | ||
/** walk is a generator function that yields the files and directories | ||
* under a given path, in a preorder traversal. "Preorder" means that | ||
* the traversal is depth-first, and a directory is yielded | ||
* immediately before the traversal examines its contents. | ||
* | ||
* @param path the starting point for the traversal, which should be a | ||
* directory. | ||
* | ||
* @param opts pre- and post-hooks for the walk. The pre-hook is | ||
* called for each directory after it is yielded; if it returns a | ||
* falsey value, the directory is not traversed. The post-hook is | ||
* called after a directory's contents have all been traversed. The | ||
* starting point does not get treated as part of the traversal, i.e., | ||
* it starts with the contents of the directory at `path`. | ||
*/ | ||
export declare function walk(path: string, opts?: WalkOpts): IterableIterator<FileInfo>; | ||
export {}; |
39
fs.js
@@ -42,2 +42,18 @@ /** | ||
const noop = () => { }; | ||
/* eslint-disable no-labels */ | ||
/** walk is a generator function that yields the files and directories | ||
* under a given path, in a preorder traversal. "Preorder" means that | ||
* the traversal is depth-first, and a directory is yielded | ||
* immediately before the traversal examines its contents. | ||
* | ||
* @param path the starting point for the traversal, which should be a | ||
* directory. | ||
* | ||
* @param opts pre- and post-hooks for the walk. The pre-hook is | ||
* called for each directory after it is yielded; if it returns a | ||
* falsey value, the directory is not traversed. The post-hook is | ||
* called after a directory's contents have all been traversed. The | ||
* starting point does not get treated as part of the traversal, i.e., | ||
* it starts with the contents of the directory at `path`. | ||
*/ | ||
export function* walk(path, opts = {}) { | ||
@@ -49,27 +65,23 @@ const { pre = always, post = noop } = opts; | ||
let next = top.files; | ||
while (next !== undefined) { | ||
// eslint-disable-next-line no-restricted-syntax | ||
runNext: while (next !== undefined) { | ||
let i = 0; | ||
for (; i < next.length; i += 1) { | ||
const f = next[i]; | ||
yield f; | ||
if (f.isdir && pre(f)) { | ||
const d = dir(f.path); | ||
// If we need to recurse into the subdirectory, push the work | ||
// yet to do here, then the subdirectory's files. If not, we | ||
// can just continue as before. | ||
// yet to do here, then process the subdirectory's files. | ||
if (d.files.length > 0) { | ||
stack.push(next.slice(i + 1)); | ||
stack.push(d.files); | ||
yield f; | ||
break; | ||
next = d.files; | ||
continue runNext; | ||
} | ||
else { | ||
yield f; | ||
post(); | ||
continue; | ||
} | ||
// If not, we can just continue through the current directory. | ||
post(); | ||
} | ||
yield f; | ||
} | ||
// If we've exhausted the slice, we're popping a directory | ||
if (i === next.length) | ||
if (i === next.length && stack.length > 0) | ||
post(); | ||
@@ -79,1 +91,2 @@ next = stack.pop(); | ||
} | ||
/* eslint-enable */ |
export { log } from './log'; | ||
export { Format, Overwrite, write, print, } from './write'; | ||
export { Encoding, read } from './read'; | ||
export { stdout, Format, Overwrite, write, print, } from './write'; | ||
export { Encoding, read, stdin } from './read'; | ||
export { parse, stringify } from './parse'; |
export { log } from './log'; | ||
export { Format, Overwrite, write, print, } from './write'; | ||
export { Encoding, read } from './read'; | ||
export { stdout, Format, Overwrite, write, print, } from './write'; | ||
export { Encoding, read, stdin } from './read'; | ||
export { parse, stringify } from './parse'; |
import { flatbuffers } from "./flatbuffers"; | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -13,3 +13,3 @@ export declare namespace __std { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -25,3 +25,3 @@ export declare namespace __std { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -40,3 +40,3 @@ export declare namespace __std { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -59,3 +59,3 @@ export declare namespace __std { | ||
* | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -70,3 +70,3 @@ export declare namespace __std { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -82,3 +82,3 @@ export declare namespace __std { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -93,3 +93,3 @@ export declare namespace __std { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -106,3 +106,3 @@ export declare namespace __std { | ||
* | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -117,3 +117,3 @@ export declare namespace __std { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -128,3 +128,3 @@ export declare namespace __std { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -184,2 +184,3 @@ export declare namespace __std { | ||
static endError(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createError(builder: flatbuffers.Builder, messageOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -226,2 +227,3 @@ } | ||
static endCancelArgs(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createCancelArgs(builder: flatbuffers.Builder, serial: flatbuffers.Long): flatbuffers.Offset; | ||
} | ||
@@ -268,2 +270,3 @@ } | ||
static endDeferred(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createDeferred(builder: flatbuffers.Builder, serial: flatbuffers.Long): flatbuffers.Offset; | ||
} | ||
@@ -320,2 +323,3 @@ } | ||
static endDeferredResponse(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createDeferredResponse(builder: flatbuffers.Builder, retvalType: __std.DeferredRetval, retvalOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -382,2 +386,3 @@ } | ||
static endData(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createData(builder: flatbuffers.Builder, bytesOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -415,2 +420,3 @@ } | ||
static endEndOfStream(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createEndOfStream(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
} | ||
@@ -476,2 +482,3 @@ } | ||
static endFulfilment(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createFulfilment(builder: flatbuffers.Builder, serial: flatbuffers.Long, valueType: __std.FulfilmentValue, valueOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -523,2 +530,8 @@ } | ||
/** | ||
* @param flatbuffers.Encoding= optionalEncoding | ||
* @returns string|Uint8Array|null | ||
*/ | ||
module(): string | null; | ||
module(optionalEncoding: flatbuffers.Encoding): string | Uint8Array | null; | ||
/** | ||
* @param flatbuffers.Builder builder | ||
@@ -554,5 +567,11 @@ */ | ||
* @param flatbuffers.Builder builder | ||
* @param flatbuffers.Offset moduleOffset | ||
*/ | ||
static addModule(builder: flatbuffers.Builder, moduleOffset: flatbuffers.Offset): void; | ||
/** | ||
* @param flatbuffers.Builder builder | ||
* @returns flatbuffers.Offset | ||
*/ | ||
static endWriteArgs(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createWriteArgs(builder: flatbuffers.Builder, pathOffset: flatbuffers.Offset, valueOffset: flatbuffers.Offset, format: __std.Format, indent: number, overwrite: __std.Overwrite, moduleOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -637,2 +656,3 @@ } | ||
static endReadArgs(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createReadArgs(builder: flatbuffers.Builder, pathOffset: flatbuffers.Offset, timeout: number, encoding: __std.Encoding, format: __std.Format, moduleOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -699,2 +719,3 @@ } | ||
static endParamArgs(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createParamArgs(builder: flatbuffers.Builder, pathOffset: flatbuffers.Offset, type: __std.ParamType, defaultValueOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -741,2 +762,3 @@ } | ||
static endParamValue(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createParamValue(builder: flatbuffers.Builder, jsonOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -791,2 +813,3 @@ } | ||
static endParamResponse(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createParamResponse(builder: flatbuffers.Builder, retvalType: __std.ParamRetval, retvalOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -842,2 +865,3 @@ } | ||
static endParseArgs(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createParseArgs(builder: flatbuffers.Builder, charsOffset: flatbuffers.Offset, format: __std.Format): flatbuffers.Offset; | ||
} | ||
@@ -884,2 +908,3 @@ } | ||
static endParseUnparseData(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createParseUnparseData(builder: flatbuffers.Builder, dataOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -934,2 +959,3 @@ } | ||
static endParseUnparseResponse(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createParseUnparseResponse(builder: flatbuffers.Builder, retvalType: __std.ParseUnparseRetval, retvalOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -985,2 +1011,3 @@ } | ||
static endUnparseArgs(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createUnparseArgs(builder: flatbuffers.Builder, objectOffset: flatbuffers.Offset, format: __std.Format): flatbuffers.Offset; | ||
} | ||
@@ -1029,2 +1056,3 @@ } | ||
static endRPCSerialised(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createRPCSerialised(builder: flatbuffers.Builder, valueOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -1079,2 +1107,3 @@ } | ||
static endRPCArg(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createRPCArg(builder: flatbuffers.Builder, argType: __std.RPCValue, argOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -1156,2 +1185,3 @@ } | ||
static endRPCArgs(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createRPCArgs(builder: flatbuffers.Builder, methodOffset: flatbuffers.Offset, argsOffset: flatbuffers.Offset, sync: boolean): flatbuffers.Offset; | ||
} | ||
@@ -1206,2 +1236,3 @@ } | ||
static endRPCSyncResponse(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createRPCSyncResponse(builder: flatbuffers.Builder, retvalType: __std.RPCSyncRetval, retvalOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
@@ -1256,3 +1287,4 @@ } | ||
static endMessage(builder: flatbuffers.Builder): flatbuffers.Offset; | ||
static createMessage(builder: flatbuffers.Builder, argsType: __std.Args, argsOffset: flatbuffers.Offset): flatbuffers.Offset; | ||
} | ||
} |
// automatically generated by the FlatBuffers compiler, do not modify | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -16,3 +16,3 @@ export var __std; | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -30,3 +30,3 @@ (function (__std) { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -47,3 +47,3 @@ (function (__std) { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -68,3 +68,3 @@ (function (__std) { | ||
* | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -81,3 +81,3 @@ (function (__std) { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -95,3 +95,3 @@ (function (__std) { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -108,3 +108,3 @@ (function (__std) { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -123,3 +123,3 @@ (function (__std) { | ||
* | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -136,3 +136,3 @@ (function (__std) { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -149,3 +149,3 @@ (function (__std) { | ||
/** | ||
* @enum | ||
* @enum {number} | ||
*/ | ||
@@ -226,2 +226,7 @@ (function (__std) { | ||
; | ||
static createError(builder, messageOffset) { | ||
Error.startError(builder); | ||
Error.addMessage(builder, messageOffset); | ||
return Error.endError(builder); | ||
} | ||
} | ||
@@ -293,2 +298,7 @@ __std.Error = Error; | ||
; | ||
static createCancelArgs(builder, serial) { | ||
CancelArgs.startCancelArgs(builder); | ||
CancelArgs.addSerial(builder, serial); | ||
return CancelArgs.endCancelArgs(builder); | ||
} | ||
} | ||
@@ -360,2 +370,7 @@ __std.CancelArgs = CancelArgs; | ||
; | ||
static createDeferred(builder, serial) { | ||
Deferred.startDeferred(builder); | ||
Deferred.addSerial(builder, serial); | ||
return Deferred.endDeferred(builder); | ||
} | ||
} | ||
@@ -444,2 +459,8 @@ __std.Deferred = Deferred; | ||
; | ||
static createDeferredResponse(builder, retvalType, retvalOffset) { | ||
DeferredResponse.startDeferredResponse(builder); | ||
DeferredResponse.addRetvalType(builder, retvalType); | ||
DeferredResponse.addRetval(builder, retvalOffset); | ||
return DeferredResponse.endDeferredResponse(builder); | ||
} | ||
} | ||
@@ -549,2 +570,7 @@ __std.DeferredResponse = DeferredResponse; | ||
; | ||
static createData(builder, bytesOffset) { | ||
Data.startData(builder); | ||
Data.addBytes(builder, bytesOffset); | ||
return Data.endData(builder); | ||
} | ||
} | ||
@@ -600,2 +626,6 @@ __std.Data = Data; | ||
; | ||
static createEndOfStream(builder) { | ||
EndOfStream.startEndOfStream(builder); | ||
return EndOfStream.endEndOfStream(builder); | ||
} | ||
} | ||
@@ -700,2 +730,9 @@ __std.EndOfStream = EndOfStream; | ||
; | ||
static createFulfilment(builder, serial, valueType, valueOffset) { | ||
Fulfilment.startFulfilment(builder); | ||
Fulfilment.addSerial(builder, serial); | ||
Fulfilment.addValueType(builder, valueType); | ||
Fulfilment.addValue(builder, valueOffset); | ||
return Fulfilment.endFulfilment(builder); | ||
} | ||
} | ||
@@ -767,2 +804,7 @@ __std.Fulfilment = Fulfilment; | ||
; | ||
module(optionalEncoding) { | ||
var offset = this.bb.__offset(this.bb_pos, 14); | ||
return offset ? this.bb.__string(this.bb_pos + offset, optionalEncoding) : null; | ||
} | ||
; | ||
/** | ||
@@ -772,3 +814,3 @@ * @param flatbuffers.Builder builder | ||
static startWriteArgs(builder) { | ||
builder.startObject(5); | ||
builder.startObject(6); | ||
} | ||
@@ -818,2 +860,10 @@ ; | ||
* @param flatbuffers.Builder builder | ||
* @param flatbuffers.Offset moduleOffset | ||
*/ | ||
static addModule(builder, moduleOffset) { | ||
builder.addFieldOffset(5, moduleOffset, 0); | ||
} | ||
; | ||
/** | ||
* @param flatbuffers.Builder builder | ||
* @returns flatbuffers.Offset | ||
@@ -826,2 +876,12 @@ */ | ||
; | ||
static createWriteArgs(builder, pathOffset, valueOffset, format, indent, overwrite, moduleOffset) { | ||
WriteArgs.startWriteArgs(builder); | ||
WriteArgs.addPath(builder, pathOffset); | ||
WriteArgs.addValue(builder, valueOffset); | ||
WriteArgs.addFormat(builder, format); | ||
WriteArgs.addIndent(builder, indent); | ||
WriteArgs.addOverwrite(builder, overwrite); | ||
WriteArgs.addModule(builder, moduleOffset); | ||
return WriteArgs.endWriteArgs(builder); | ||
} | ||
} | ||
@@ -949,2 +1009,11 @@ __std.WriteArgs = WriteArgs; | ||
; | ||
static createReadArgs(builder, pathOffset, timeout, encoding, format, moduleOffset) { | ||
ReadArgs.startReadArgs(builder); | ||
ReadArgs.addPath(builder, pathOffset); | ||
ReadArgs.addTimeout(builder, timeout); | ||
ReadArgs.addEncoding(builder, encoding); | ||
ReadArgs.addFormat(builder, format); | ||
ReadArgs.addModule(builder, moduleOffset); | ||
return ReadArgs.endReadArgs(builder); | ||
} | ||
} | ||
@@ -1040,2 +1109,9 @@ __std.ReadArgs = ReadArgs; | ||
; | ||
static createParamArgs(builder, pathOffset, type, defaultValueOffset) { | ||
ParamArgs.startParamArgs(builder); | ||
ParamArgs.addPath(builder, pathOffset); | ||
ParamArgs.addType(builder, type); | ||
ParamArgs.addDefaultValue(builder, defaultValueOffset); | ||
return ParamArgs.endParamArgs(builder); | ||
} | ||
} | ||
@@ -1102,2 +1178,7 @@ __std.ParamArgs = ParamArgs; | ||
; | ||
static createParamValue(builder, jsonOffset) { | ||
ParamValue.startParamValue(builder); | ||
ParamValue.addJson(builder, jsonOffset); | ||
return ParamValue.endParamValue(builder); | ||
} | ||
} | ||
@@ -1184,2 +1265,8 @@ __std.ParamValue = ParamValue; | ||
; | ||
static createParamResponse(builder, retvalType, retvalOffset) { | ||
ParamResponse.startParamResponse(builder); | ||
ParamResponse.addRetvalType(builder, retvalType); | ||
ParamResponse.addRetval(builder, retvalOffset); | ||
return ParamResponse.endParamResponse(builder); | ||
} | ||
} | ||
@@ -1262,2 +1349,8 @@ __std.ParamResponse = ParamResponse; | ||
; | ||
static createParseArgs(builder, charsOffset, format) { | ||
ParseArgs.startParseArgs(builder); | ||
ParseArgs.addChars(builder, charsOffset); | ||
ParseArgs.addFormat(builder, format); | ||
return ParseArgs.endParseArgs(builder); | ||
} | ||
} | ||
@@ -1324,2 +1417,7 @@ __std.ParseArgs = ParseArgs; | ||
; | ||
static createParseUnparseData(builder, dataOffset) { | ||
ParseUnparseData.startParseUnparseData(builder); | ||
ParseUnparseData.addData(builder, dataOffset); | ||
return ParseUnparseData.endParseUnparseData(builder); | ||
} | ||
} | ||
@@ -1406,2 +1504,8 @@ __std.ParseUnparseData = ParseUnparseData; | ||
; | ||
static createParseUnparseResponse(builder, retvalType, retvalOffset) { | ||
ParseUnparseResponse.startParseUnparseResponse(builder); | ||
ParseUnparseResponse.addRetvalType(builder, retvalType); | ||
ParseUnparseResponse.addRetval(builder, retvalOffset); | ||
return ParseUnparseResponse.endParseUnparseResponse(builder); | ||
} | ||
} | ||
@@ -1484,2 +1588,8 @@ __std.ParseUnparseResponse = ParseUnparseResponse; | ||
; | ||
static createUnparseArgs(builder, objectOffset, format) { | ||
UnparseArgs.startUnparseArgs(builder); | ||
UnparseArgs.addObject(builder, objectOffset); | ||
UnparseArgs.addFormat(builder, format); | ||
return UnparseArgs.endUnparseArgs(builder); | ||
} | ||
} | ||
@@ -1548,2 +1658,7 @@ __std.UnparseArgs = UnparseArgs; | ||
; | ||
static createRPCSerialised(builder, valueOffset) { | ||
RPCSerialised.startRPCSerialised(builder); | ||
RPCSerialised.addValue(builder, valueOffset); | ||
return RPCSerialised.endRPCSerialised(builder); | ||
} | ||
} | ||
@@ -1630,2 +1745,8 @@ __std.RPCSerialised = RPCSerialised; | ||
; | ||
static createRPCArg(builder, argType, argOffset) { | ||
RPCArg.startRPCArg(builder); | ||
RPCArg.addArgType(builder, argType); | ||
RPCArg.addArg(builder, argOffset); | ||
return RPCArg.endRPCArg(builder); | ||
} | ||
} | ||
@@ -1755,2 +1876,9 @@ __std.RPCArg = RPCArg; | ||
; | ||
static createRPCArgs(builder, methodOffset, argsOffset, sync) { | ||
RPCArgs.startRPCArgs(builder); | ||
RPCArgs.addMethod(builder, methodOffset); | ||
RPCArgs.addArgs(builder, argsOffset); | ||
RPCArgs.addSync(builder, sync); | ||
return RPCArgs.endRPCArgs(builder); | ||
} | ||
} | ||
@@ -1837,2 +1965,8 @@ __std.RPCArgs = RPCArgs; | ||
; | ||
static createRPCSyncResponse(builder, retvalType, retvalOffset) { | ||
RPCSyncResponse.startRPCSyncResponse(builder); | ||
RPCSyncResponse.addRetvalType(builder, retvalType); | ||
RPCSyncResponse.addRetval(builder, retvalOffset); | ||
return RPCSyncResponse.endRPCSyncResponse(builder); | ||
} | ||
} | ||
@@ -1919,4 +2053,10 @@ __std.RPCSyncResponse = RPCSyncResponse; | ||
; | ||
static createMessage(builder, argsType, argsOffset) { | ||
Message.startMessage(builder); | ||
Message.addArgsType(builder, argsType); | ||
Message.addArgs(builder, argsOffset); | ||
return Message.endMessage(builder); | ||
} | ||
} | ||
__std.Message = Message; | ||
})(__std || (__std = {})); |
{ | ||
"name": "@jkcfg/std", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "jk standard library", | ||
@@ -31,10 +31,10 @@ "main": "std.js", | ||
"@typescript-eslint/eslint-plugin": "^1.7.0", | ||
"babel-jest": "^24.8.0", | ||
"babel-jest": "^24.9.0", | ||
"eslint": "^5.12.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.17.2", | ||
"eslint-plugin-jest": "^22.4.1", | ||
"jest": "^24.8.0", | ||
"eslint-plugin-jest": "^23.0.4", | ||
"jest": "^24.9.0", | ||
"to-fast-properties": "^2.0.0", | ||
"ts-jest": "^24.0.2", | ||
"ts-jest": "^24.1.0", | ||
"typedoc": "^0.14.2", | ||
@@ -41,0 +41,0 @@ "typedoc-plugin-external-module-name": "^2.1.0", |
@@ -5,2 +5,3 @@ /** | ||
import { Format } from './write'; | ||
export declare const stdin: unique symbol; | ||
export declare enum Encoding { | ||
@@ -16,2 +17,5 @@ Bytes = 0, | ||
} | ||
export declare function read(path: string, opts?: ReadOptions): Promise<any>; | ||
export declare function valuesFormatFromPath(path: string): Format; | ||
declare type ReadPath = string | typeof stdin; | ||
export declare function read(path?: ReadPath, opts?: ReadOptions): Promise<any>; | ||
export {}; |
23
read.js
@@ -9,2 +9,3 @@ /** | ||
import { Format } from './write'; | ||
export const stdin = Symbol('<stdin>'); | ||
/* we re-define Encoding from the generated __std.Encoding to document it */ | ||
@@ -17,8 +18,26 @@ export var Encoding; | ||
})(Encoding || (Encoding = {})); | ||
// valuesFormatFromPath guesses, for a path, the format that will | ||
// return all values in a file. In other words, it prefers YAML | ||
// streams and concatenated JSON. You may need to treat the read value | ||
// differently depending on the format you got here, since YAMLStream | ||
// and JSONStream will both result in an array of values. | ||
export function valuesFormatFromPath(path) { | ||
const ext = path.split('.').pop(); | ||
switch (ext) { | ||
case 'yaml': | ||
case 'yml': | ||
return Format.YAMLStream; | ||
case 'json': | ||
return Format.JSONStream; | ||
default: | ||
return Format.FromExtension; | ||
} | ||
} | ||
// read requests the path and returns a promise that will be resolved | ||
// with the contents at the path, or rejected. | ||
export function read(path, opts = {}) { | ||
export function read(path = stdin, opts = {}) { | ||
const { encoding = Encoding.JSON, format = Format.FromExtension, module } = opts; | ||
const pathArg = (path === stdin) ? '' : path; | ||
const builder = new flatbuffers.Builder(512); | ||
const pathOffset = builder.createString(path); | ||
const pathOffset = builder.createString(pathArg); | ||
let moduleOffset = 0; | ||
@@ -25,0 +44,0 @@ if (module !== undefined) { |
@@ -5,5 +5,8 @@ # jk | ||
> `jk` is still in very early stages. The standard library API isn't frozen | ||
> and will most likely change. Feedback is most definitely welcome! | ||
Caveat: jk is pre-1.0, and as such, features, commands, and APIs inevitably | ||
will change. | ||
Releases within a minor version should be stable; we try to signal breaking | ||
changes ahead of time in [./docs/deprecations.md](./docs/deprecations.md). | ||
## jk - configuration as code | ||
@@ -10,0 +13,0 @@ |
/** | ||
* @module std/schema | ||
*/ | ||
interface Location { | ||
line: number; | ||
column: number; | ||
} | ||
import { ValidationResult } from './validation'; | ||
/** | ||
* ValidationError represents a specific problem encountered when | ||
* validating a value. | ||
*/ | ||
export interface ValidationError { | ||
msg: string; | ||
path?: string; | ||
start?: Location; | ||
end?: Location; | ||
} | ||
export declare type Result = 'ok' | ValidationError[]; | ||
/** | ||
* validateWithObject validates a value using a JSON schema supplied | ||
@@ -27,3 +13,3 @@ * as object. | ||
*/ | ||
export declare function validateWithObject(obj: any, schema: Record<string, any>): Result; | ||
export declare function validateWithObject(obj: any, schema: Record<string, any>): ValidationResult; | ||
/** | ||
@@ -33,3 +19,3 @@ * validateWithFile validates a value using a schema located at | ||
*/ | ||
export declare function validateWithFile(obj: any, path: string): Promise<Result>; | ||
export declare function validateWithFile(obj: any, path: string): Promise<ValidationResult>; | ||
/** | ||
@@ -49,3 +35,2 @@ * validateWithResource validates a value using a schema location at | ||
*/ | ||
export declare function validateWithResource(obj: any, path: string, moduleRef: string): Promise<Result>; | ||
export {}; | ||
export declare function validateWithResource(obj: any, path: string, moduleRef: string): Promise<ValidationResult>; |
/** | ||
* @module std | ||
*/ | ||
export declare const stdout: unique symbol; | ||
export declare enum Format { | ||
@@ -22,4 +23,7 @@ FromExtension = 0, | ||
overwrite?: Overwrite | boolean; | ||
module?: string; | ||
} | ||
export declare function write(value: any, path?: string, { format, indent, overwrite }?: WriteOptions): void; | ||
export declare function print(value: any, opts: WriteOptions): void; | ||
declare type WritePath = string | typeof stdout; | ||
export declare function write(value: any, path?: WritePath, opts?: WriteOptions): void; | ||
export declare function print(value: any, opts?: WriteOptions): void; | ||
export {}; |
30
write.js
@@ -7,2 +7,3 @@ /** | ||
import { sendRequest } from './internal/deferred'; | ||
export const stdout = Symbol('<stdout>'); | ||
/* we re-define Format from the generated __std.Format to document it */ | ||
@@ -25,6 +26,8 @@ export var Format; | ||
})(Overwrite || (Overwrite = {})); | ||
export function write(value, path = '', { format = Format.FromExtension, indent = 2, overwrite = Overwrite.Write } = {}) { | ||
export function write(value, path = stdout, opts = {}) { | ||
if (value === undefined) { | ||
throw TypeError('cannot write undefined value'); | ||
} | ||
const { format = Format.FromExtension, indent = 2, overwrite = Overwrite.Write, module, } = opts; | ||
const pathArg = (path === stdout) ? '' : path; | ||
let overwriteVal; | ||
@@ -39,10 +42,17 @@ if (typeof overwrite === 'boolean') { | ||
const str = (format === Format.Raw) ? value.toString() : JSON.stringify(value); | ||
const strOff = builder.createString(str); | ||
const pathOff = builder.createString(path); | ||
const strOffset = builder.createString(str); | ||
const pathOffset = builder.createString(pathArg); | ||
let moduleOffset = 0; | ||
if (module !== undefined) { | ||
moduleOffset = builder.createString(module); | ||
} | ||
__std.WriteArgs.startWriteArgs(builder); | ||
__std.WriteArgs.addValue(builder, strOff); | ||
__std.WriteArgs.addPath(builder, pathOff); | ||
__std.WriteArgs.addValue(builder, strOffset); | ||
__std.WriteArgs.addPath(builder, pathOffset); | ||
__std.WriteArgs.addFormat(builder, format); | ||
__std.WriteArgs.addIndent(builder, indent); | ||
__std.WriteArgs.addOverwrite(builder, overwriteVal); | ||
if (module !== undefined) { | ||
__std.WriteArgs.addModule(builder, moduleOffset); | ||
} | ||
const args = __std.WriteArgs.endWriteArgs(builder); | ||
@@ -63,8 +73,12 @@ __std.Message.startMessage(builder); | ||
// print is a convenience for printing any value to stdout | ||
export function print(value, opts) { | ||
export function print(value, opts = {}) { | ||
if (arguments.length === 0) { | ||
write('\n', stdout, { format: Format.Raw }); | ||
return; | ||
} | ||
if (value === undefined) { | ||
write('undefined\n', '', { format: Format.Raw }); | ||
write('undefined\n', stdout, { format: Format.Raw }); | ||
return; | ||
} | ||
write(value, '', opts); | ||
write(value, stdout, opts); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
218113
45
6295
135