zorb 0.0.1-alpha.0 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/zorb ADDED
Binary file
package/package.json CHANGED
@@ -1,30 +1,14 @@
1
1
  {
2
2
  "name": "zorb",
3
- "description": "Structure data workflow runner",
4
- "version": "0.0.1-alpha.0",
5
- "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
7
- },
8
- "main": "./index.js",
9
- "types": "./types.d.ts",
10
- "bin": "./bin.js",
11
- "author": "James D <james@jdrydn.com> (https://jdrydn.com)",
3
+ "version": "0.0.2",
4
+ "description": "Standalone workflow runner",
5
+ "bin": "./dist/zorb",
6
+ "author": "jdrydn <james@jdrydn.com> (https://jdrydn.com)",
12
7
  "license": "MIT",
13
- "homepage": "https://github.com/someimportantcompany/zorb",
14
- "bugs": "https://github.com/someimportantcompany/zorb/issues",
15
- "repository": "https://github.com/someimportantcompany/zorb.git",
16
- "dependencies": {
17
- "@someimportantcompany/utils": "^1.3.1",
18
- "http-assert-plus": "^2.0.1",
19
- "yaml": "^2.3.4",
20
- "yargs": "^17.7.2",
21
- "yup": "^1.3.3"
22
- },
23
- "devDependencies": {
24
- "@types/yargs": "^17.0.29"
25
- },
26
- "engines": {
27
- "node": ">16",
28
- "npm": ">7"
29
- }
8
+ "repository": "https://github.com/jdrydn/zorb",
9
+ "bugs": "https://github.com/jdrydn/zorb/issues",
10
+ "homepage": "https://github.com/jdrydn/zorb",
11
+ "files": [
12
+ "./dist/zorb"
13
+ ]
30
14
  }
package/.editorconfig DELETED
@@ -1,9 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- charset = utf-8
5
- indent_style = space
6
- indent_size = 2
7
- end_of_line = lf
8
- insert_final_newline = true
9
- trim_trailing_whitespace = true
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # zorb
2
-
3
- Structure data workflow runner.
package/bin.js DELETED
@@ -1,74 +0,0 @@
1
- const yargs = require('yargs');
2
- const { hideBin } = require('yargs/helpers');
3
-
4
- const { runWorkflow } = require('./lib/runner');
5
- const { validateSchemaFile } = require('./lib/schemas');
6
-
7
- const { loadFile } = require('./lib/files');
8
- const { createLogger } = require('./lib/logger');
9
-
10
- const parser = yargs(hideBin(process.argv))
11
- .count('verbose')
12
- .alias('v', 'verbose')
13
- .option('cwd', {
14
- type: 'string',
15
- default: process.env.PWD ?? '/',
16
- })
17
- .command('run <script>', 'run the specific script', args => args
18
- .option('file', {
19
- type: 'string',
20
- default: 'zorb.yml',
21
- alias: 'f',
22
- })
23
- .option('with', {
24
- type: 'array',
25
- coerce: args => Object.fromEntries(args.map(a => a.split('='))),
26
- })
27
- .positional('script', {}))
28
- .command('use <package>', 'run the specific package', args => args
29
- .option('with', {
30
- type: 'array',
31
- coerce: args => Object.fromEntries(args.map(a => a.split('='))),
32
- })
33
- .positional('package', {}))
34
- .demandCommand(1);
35
-
36
- (async () => {
37
- try {
38
- const args = await parser.parseAsync();
39
- const log = (args.verbose > 0) ? createLogger(args.verbose) : undefined;
40
- log?.debug({ args });
41
-
42
- switch (args._.shift()) {
43
- case 'run':
44
- const file = await loadFile(args.cwd, args.file);
45
- const config = validateSchemaFile(file);
46
- log?.debug({ config });
47
- // @TODO Resolve variables within the config script
48
- await runWorkflow({ cwd: args.cwd, log }, config, args.script, args.with);
49
- break;
50
-
51
- case 'use':
52
- // @TODO Resolve variables within the config script
53
- await runWorkflow({
54
- cwd: args.cwd,
55
- log,
56
- }, {
57
- scripts: {
58
- '0': {
59
- steps: [
60
- { uses: args.package, with: args.with ? args.with : undefined },
61
- ],
62
- },
63
- },
64
- }, '0');
65
- break;
66
- }
67
-
68
- log?.debug('Finished');
69
- process.exit(0);
70
- } catch (err) {
71
- console.error(err);
72
- process.exit(1);
73
- }
74
- })();
package/index.js DELETED
@@ -1,11 +0,0 @@
1
- const { runWorkflow } = require('./lib/runner');
2
- const { validateSchemaFile } = require('./lib/schemas');
3
-
4
- module.exports = function zorb(input, { log }) {
5
- const config = validateSchemaFile(input);
6
-
7
- return Object.keys(config.scripts).reduce((fns, key) => ({
8
- ...fns,
9
- [key]: () => runWorkflow({ cwd: args.cwd, log }, config, args.script),
10
- }), {});
11
- };
package/lib/context.js DELETED
@@ -1,35 +0,0 @@
1
- /**
2
- * @param {Partial<import('../types').ZorbContext>} ctx
3
- * @param {Nullable<import('../types').ZorbContext['cwd']>} ctx.cwd
4
- * @param {Nullable<import('../types').ZorbContext['env']>} ctx.env
5
- * @param {Nullable<import('../types').ZorbContext['log']>} ctx.log
6
- * @returns {import('../types').ZorbContext}
7
- */
8
- function createContext({ cwd, env, log }) {
9
- return Object.create({}, {
10
- cwd: {
11
- enumerable: true,
12
- value: cwd ?? process.env.PWD,
13
- },
14
- env: {
15
- enumerable: true,
16
- value: { ...env },
17
- },
18
- log: {
19
- enumerable: true,
20
- get: () => log,
21
- },
22
- output: {
23
- enumerable: true,
24
- value: {},
25
- },
26
- // state: {
27
- // enumerable: true,
28
- // value: {},
29
- // },
30
- });
31
- }
32
-
33
- module.exports = {
34
- createContext,
35
- };
package/lib/files.js DELETED
@@ -1,50 +0,0 @@
1
- const assert = require('http-assert-plus');
2
- const fs = require('fs/promises');
3
- const path = require('path');
4
- const yaml = require('yaml');
5
-
6
- const { tryCatch } = require('./utils');
7
-
8
- /**
9
- * @param {string} dirname
10
- * @param {string} filename
11
- * @returns {Promise<Record<string, any>>}
12
- */
13
- async function loadFile(dirname, filename) {
14
- assert(typeof dirname === 'string', new TypeError('Expected dirname to be a string'));
15
- assert(typeof filename === 'string', new TypeError('Expected filename to be a string'));
16
-
17
- const contents = await fs.readFile(path.resolve(dirname, filename), 'utf8').catch(err => {
18
- let prefix = `Failed to read file at ${path.resolve(dirname, filename)}`;
19
- // @TODO Change prefix based on fs err code
20
- err.message = `${prefix}: ${err.message}`;
21
- throw err;
22
- });
23
-
24
- // let parsed;
25
-
26
- switch (path.extname(filename)) {
27
- case '.json': {
28
- return tryCatch(() => JSON.parse(contents), err => {
29
- // @TODO Change prefix based on JSON.parse err code
30
- err.message = `Failed to parse JSON file: ${err.message}`;
31
- throw err;
32
- });
33
- }
34
-
35
- case '.yaml': case '.yml': {
36
- return tryCatch(() => yaml.parse(contents), err => {
37
- // @TODO Change prefix based on yaml.parse err code
38
- err.message = `Failed to parse YAML file: ${err.message}`;
39
- throw err;
40
- });
41
- }
42
-
43
- default:
44
- throw new Error(`Unknown file extension: ${filename}`);
45
- }
46
- }
47
-
48
- module.exports = {
49
- loadFile,
50
- };
package/lib/logger.js DELETED
@@ -1,27 +0,0 @@
1
- function createLogLevel(prefix) {
2
- return function (first, ...rest) {
3
- if (typeof first === 'string') {
4
- console.log(prefix, (new Date()).toISOString(), first, ...rest);
5
- } else {
6
- console.log(prefix, (new Date()).toISOString(), first);
7
- if (rest.length) {
8
- console.log(prefix, (new Date()).toISOString(), first, ...rest);
9
- }
10
- }
11
- }
12
- }
13
-
14
- const noop = () => {};
15
-
16
- function createLogger(verbose) {
17
- return {
18
- error: createLogLevel('[ERROR]'),
19
- warn: createLogLevel('[WARN] '),
20
- info: verbose >= 1 ? createLogLevel('[INFO] ') : noop,
21
- debug: verbose > 1 ? createLogLevel('[DEBUG]') : noop,
22
- };
23
- }
24
-
25
- module.exports = {
26
- createLogger,
27
- };
package/lib/runner.js DELETED
@@ -1,50 +0,0 @@
1
- const assert = require('http-assert-plus');
2
-
3
- const { createContext } = require('./context');
4
-
5
- const { runShellStep } = require('../steps/run');
6
- const { runActionStep, loadActionSteps } = require('../steps/use');
7
-
8
- // Given a valid runner file
9
- // And some instruction
10
- // Complete the steps at hand
11
-
12
- async function runWorkflow({ cwd, log }, config, handle) {
13
- assert(config.scripts?.[handle], 'Script not found', {
14
- code: 'SCRIPT_NOT_FOUND',
15
- meta: { handle },
16
- });
17
-
18
- const { [handle]: script } = config.scripts;
19
-
20
- assert(Array.isArray(script.steps), 'No steps found for script', {
21
- code: 'SCRIPT_STEPS_NOT_FOUND',
22
- meta: { handle },
23
- });
24
-
25
- const ctx = createContext({
26
- cwd: typeof script.defaults?.run?.cwd === 'string'
27
- ? script.defaults.run.cwd
28
- : process.env.PWD,
29
- env: { ...config.env, ...script.defaults?.run?.env, ...script.env },
30
- log
31
- });
32
-
33
- loadActionSteps({ cwd, log }, script.steps
34
- .filter(step => typeof step.uses === 'string')
35
- .map(step => step.uses));
36
-
37
- for (const step of script.steps) {
38
- log?.info({ step }, '@zorb/run step');
39
-
40
- if (typeof step.uses === 'string') {
41
- await runActionStep(ctx, step, script);
42
- } else if (typeof step.run === 'string') {
43
- await runShellStep(ctx, step, script);
44
- }
45
- }
46
- }
47
-
48
- module.exports = {
49
- runWorkflow,
50
- };
package/lib/schemas.js DELETED
@@ -1,56 +0,0 @@
1
- const yup = require('yup');
2
-
3
- function dynamicObject(value) {
4
- return yup.lazy(map => yup.object((map ? Object.keys(map) : [])
5
- .reduce((newMap, key) => ({...newMap, [key]: value}), {})));
6
- }
7
-
8
- const runnerActionStep = module.exports.runnerActionStep = yup.object().noUnknown(true).shape({
9
- uses: yup.string().required(),
10
- with: yup.object(),
11
- });
12
-
13
- // const runnerCodeStep = module.exports.runnerCodeStep = yup.object().noUnknown(true).shape({
14
- // code: yup.string().required(),
15
- // with: yup.object(),
16
- // });
17
-
18
- const runnerShellStep = module.exports.runnerShellStep = yup.object().noUnknown(true).shape({
19
- run: yup.string().required(),
20
- cwd: yup.string(),
21
- env: dynamicObject(yup.string()),
22
- shell: yup.string(),
23
- });
24
-
25
- const runnerScriptSchema = module.exports.runnerScriptSchema = yup.object().noUnknown(true).shape({
26
- env: dynamicObject(yup.string()),
27
- defaults: yup.object(),
28
- steps: yup.array().required().min(1).of(yup.lazy(o => {
29
- if (o && typeof o.uses === 'string') {
30
- return runnerActionStep.clone();
31
- } else {
32
- return runnerShellStep.clone();
33
- }
34
- })),
35
- });
36
-
37
- const runnerFileSchema = module.exports.runnerFileSchema = yup.object().required().shape({
38
- version: yup.string().oneOf(['1']),
39
- env: dynamicObject(yup.string()),
40
- params: yup.object(),
41
- scripts: dynamicObject(runnerScriptSchema),
42
- });
43
-
44
- /**
45
- * @param {Record<string, any>} input
46
- * @returns {NonNullable<ReturnType<typeof runnerFileSchema.cast>>}
47
- */
48
- function validateSchemaFile(input) {
49
- return runnerFileSchema.validateSync(input, {
50
- abortEarly: false,
51
- });
52
- }
53
-
54
- module.exports = {
55
- validateSchemaFile,
56
- };
package/lib/utils.js DELETED
@@ -1,66 +0,0 @@
1
- const { assert } = require('@someimportantcompany/utils');
2
-
3
- /**
4
- * @param {string} str
5
- * @param {string|number} split
6
- * @param {boolean} first
7
- * @returns {[string, string | undefined]}
8
- */
9
- function splitOnIndex(str, split, first = true) {
10
- assert(typeof str === 'string', new TypeError('Expected first argument to be a string'));
11
- assert(['string', 'number'].includes(typeof split), new TypeError('Expected second argument to be a string or number'));
12
- assert(typeof first === 'boolean', new TypeError('Expected third argument to be a boolean'));
13
-
14
- if (typeof split === 'string') {
15
- split = first ? str.indexOf(split) : str.lastIndexOf(split);
16
- }
17
-
18
- return split >= 0 ? [str.slice(0, split), str.slice(split + 1)] : [str, undefined];
19
- }
20
-
21
- /**
22
- * @template T
23
- * @param {() => T} fn
24
- * @param {*} [err]
25
- * @returns {T}
26
-
27
- * @template T
28
- * @param {() => Promise<T>} fn
29
- * @param {*} [err]
30
- * @returns {Promise<T>}
31
-
32
- * @template T
33
- * @param {(() => T) | (() => Promise<T>)} fn
34
- * @param {*} [err]
35
- * @returns {T | Promise<T>}
36
- */
37
- function tryCatch(fn, err) {
38
- try {
39
- const r = fn();
40
- // If the function return a Promise-like result
41
- if (r instanceof Promise) {
42
- // Then wrap the promise to protect against errors
43
- return r.catch((e) => {
44
- if (typeof err === 'function') {
45
- return err(e);
46
- } else {
47
- throw e;
48
- }
49
- });
50
- } else {
51
- // Otherwise, return the raw value returned from the function
52
- return r;
53
- }
54
- } catch (e) {
55
- if (typeof err === 'function') {
56
- return err(e);
57
- } else {
58
- throw e;
59
- }
60
- }
61
- }
62
-
63
- module.exports = {
64
- splitOnIndex,
65
- tryCatch,
66
- };
package/steps/run.js DELETED
@@ -1,43 +0,0 @@
1
- const assert = require('http-assert-plus');
2
- const childProcess = require('child_process');
3
- const path = require('path');
4
-
5
- /**
6
- * @param {import('../types').ZorbContext} ctx
7
- * @param {Object} step
8
- * @param {string} step.run
9
- * @param {Object} script
10
- * @returns {number|undefined}
11
- */
12
- function runShellStep(ctx, step, script) {
13
- assert(typeof step.run === 'string', new TypeError('Expected $.run to be a string'));
14
-
15
- const cwd = typeof step.cwd === 'string' ? path.resolve(ctx.cwd, step.cwd) : ctx.cwd;
16
-
17
- const env = {
18
- ...script.env,
19
- ...script.defaults?.run?.env,
20
- ...step.env,
21
- };
22
-
23
- const shell = step.shell ?? script.defaults?.run?.shell ?? undefined;
24
-
25
- return new Promise((resolve, reject) => {
26
- ctx.log?.debug({ run: step.run, opts: { cwd, env, shell } });
27
-
28
- const child = childProcess.exec(step.run, { cwd, env, shell });
29
-
30
- child.stdout.setEncoding('utf8');
31
- child.stdout.on('data', data => process.stdout.write(data));
32
-
33
- child.stderr.setEncoding('utf8');
34
- child.stderr.on('data', data => process.stderr.write(data));
35
-
36
- child.on('close', code => resolve(code ?? undefined));
37
- child.on('err', err => reject(err));
38
- });
39
- }
40
-
41
- module.exports = {
42
- runShellStep,
43
- };
package/steps/use.js DELETED
@@ -1,38 +0,0 @@
1
- const path = require('path');
2
- const { assert } = require('@someimportantcompany/utils');
3
-
4
- const { tryCatch } = require('../lib/utils');
5
-
6
- const actions = new Map();
7
-
8
- function loadActionSteps(ctx, usesList) {
9
- for (const useName of usesList) {
10
- const functionName = path.extname(useName);
11
- const fileName = functionName ? useName.substring(0, useName.length - functionName.length) : useName;
12
-
13
- const requireName = tryCatch(() => require.resolve(fileName), () => null)
14
- ?? tryCatch(() => require.resolve(path.resolve(ctx.cwd, fileName)), () => null)
15
- ?? fileName;
16
-
17
- const mod = require(requireName);
18
- const handler = functionName ? mod[functionName] : mod;
19
- assert(typeof handler === 'function', new TypeError('Expected handler to be a function'));
20
- actions.set(useName, {
21
- handler,
22
- });
23
- }
24
-
25
- ctx.log?.debug({ name: '@zorb/core', use: Array.from(actions.keys()) }, 'Loaded actions');
26
- }
27
-
28
- async function runActionStep(ctx, step) {
29
- assert(typeof step.uses === 'string', new TypeError('Expected $.uses to be a string'));
30
- const { handler } = actions.get(step.uses) || {};
31
- assert(typeof handler === 'function', new Error('Expected use handler to be a function'));
32
- return handler(ctx, step.with ?? undefined);
33
- }
34
-
35
- module.exports = {
36
- loadActionSteps,
37
- runActionStep,
38
- };
package/types.d.ts DELETED
@@ -1,13 +0,0 @@
1
- export interface ZorbContext {
2
- cwd: string,
3
- env: Record<string, string | undefined>,
4
- log?: Record<'debug' | 'info' | 'warn' | 'error',
5
- ((msg: string) => void) |
6
- ((args: Record<string, any>, msg: string) => void)>,
7
- output: Record<string, any>,
8
- // state: Record<string, any>,
9
- }
10
-
11
- export interface ZorbAction {
12
- (ctx: ZorbContext, args: Record<string, any>): void | Promise<void>
13
- }
package/zorb.yml DELETED
@@ -1,16 +0,0 @@
1
- scripts:
2
- test:
3
- steps:
4
- - run: echo "Hello, world!"
5
- - uses: '@zorb/aws/s3-sync'
6
- with:
7
- bucket: mybucket
8
- folder: dist/
9
- - uses: './test/action/scenario1'
10
- with:
11
- bucket: mybucket
12
- folder: dist/
13
- a:
14
- b:
15
- c:
16
- d: true