From c9d586bfeaecb121ef6336ff87b1bfae6fea4ab5 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 27 Nov 2020 01:07:50 +0200 Subject: [PATCH] feature(madrun) asyncify --- .eslintrc.json | 3 ++ .github/ISSUE_TEMPLATE.md | 11 +++---- .github/PULL_REQUEST_TEMPLATE.md | 1 - .madrun.js | 6 ++-- CODE_OF_CONDUCT.md | 20 ++++++------ CONTRIBUTING.md | 9 ++++-- bin/madrun.mjs | 16 +++++----- lib/madrun.js | 52 +++++++++++++++++++++++--------- package.json | 1 + test/madrun.js | 50 +++++++++++++++--------------- 10 files changed, 99 insertions(+), 70 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 06b4cbb..6e80ddc 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,4 +1,7 @@ { + "rules": { + "node/no-unsupported-features/es-syntax": "off" + }, "extends": [ "plugin:node/recommended", "plugin:putout/recommended" diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 5e97b78..09e1c6d 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -3,9 +3,8 @@ Thank you for reporting an issue. Please fill in the template below. If unsure about something, just do as best as you're able. --> -* **Version** (`redrun -v`): -* **Node Version** `node -v`: -* **OS** (`uname -a` on Linux): -* **Browser name/version**: -* **Used Command Line Parameters**: - +- **Version** (`redrun -v`): +- **Node Version** `node -v`: +- **OS** (`uname -a` on Linux): +- **Browser name/version**: +- **Used Command Line Parameters**: diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 98b1f1a..96b7e2e 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -6,4 +6,3 @@ about something, just do as best as you're able. - [ ] commit message named according to [Contributing Guide](https://github.com/coderaiser/cloudcmd/blob/master/CONTRIBUTING.md "Contributting Guide") - [ ] `npm run codestyle` is OK - [ ] `npm test` is OK - diff --git a/.madrun.js b/.madrun.js index b0c0834..ac79e7d 100644 --- a/.madrun.js +++ b/.madrun.js @@ -6,14 +6,14 @@ module.exports = { 'lint': () => 'putout .', 'fix:lint': () => run('lint', '--fix'), 'test': () => `tape 'test/**/*.js' 'lib/**/*.spec.js'`, - 'watch:test': () => run('watcher', run('test')), + 'watch:test': async () => await run('watcher', await run('test')), 'watch:tape': () => 'nodemon -w test -w lib --exec tape', - 'watch:coverage:base': () => run('watcher', `nyc ${run('test')}`), + 'watch:coverage:base': async () => run('watcher', `nyc ${await run('test')}`), 'watch:coverage:tape': () => run('watcher', 'nyc tape'), 'watch:coverage': () => run('watch:coverage:base'), 'watch:lint': () => run('watcher', run('lint')), 'watcher': () => 'nodemon -w test -w lib --exec', - 'coverage': () => `nyc ${run('test')}`, + 'coverage': async () => `nyc ${await run('test')}`, 'report': () => 'nyc report --reporter=text-lcov | coveralls', 'postpublish': () => 'npm i -g', }; diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 7e77a3a..0e2522d 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -8,19 +8,19 @@ In the interest of fostering an open and welcoming environment, we as contributo Examples of behavior that contributes to creating a positive environment include: -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members Examples of unacceptable behavior by participants include: -* The use of sexualized language or imagery and unwelcome sexual attention or advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a professional setting +- The use of sexualized language or imagery and unwelcome sexual attention or advances +- Trolling, insulting/derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or electronic address, without explicit permission +- Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c7c9b91..7438bf1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,8 +1,9 @@ -Commit ---------------- +## Commit + Format of the commit message: **type(scope) subject** **Type**: + - feature(scope) subject - fix(scope) subject - docs(scope) subject @@ -15,13 +16,15 @@ Scope could be anything specifying place of the commit change. For example util, console, view, edit, style etc... **Subject text**: + - use imperative, present tense: “change” not “changed” nor “changes” - don't capitalize first letter - no dot (.) at the end -**Message body**: + **Message body**: - just as in use imperative, present tense: “change” not “changed” nor “changes” - includes motivation for the change and contrasts with previous behavior **Examples**: + - [fix(style) .name{width}: 37% -> 35%](https://github.com/coderaiser/cloudcmd/commit/94b0642e3990c17b3a0ee3efeb75f343e1e7c050) - [fix(console) dispatch: focus -> mouseup](https://github.com/coderaiser/cloudcmd/commit/f41ec5058d1411e86a881f8e8077e0572e0409ec) diff --git a/bin/madrun.mjs b/bin/madrun.mjs index c45e2d3..0167896 100755 --- a/bin/madrun.mjs +++ b/bin/madrun.mjs @@ -5,7 +5,7 @@ import {dirname, basename} from 'path'; import findUp from 'find-up'; -import tryCatch from 'try-catch'; +import tryToCatch from 'try-to-catch'; import yargsParser from 'yargs-parser'; import {series} from '../lib/madrun.js'; @@ -88,7 +88,7 @@ if (init) process.exit(); if (problems) { - execute(`echo '${problems}'`); + await execute(`echo '${problems}'`); process.exit(1); } @@ -98,7 +98,7 @@ if (!names.length) { } const env = {}; -const [e, cmd] = tryCatch(series, names, options, env, script); +const [e, cmd] = await tryToCatch(series, names, options, env, script); if (e) { console.error(e.message); @@ -106,7 +106,7 @@ if (e) { } console.log(getOutput({cmd, cwd})); -execute(cmd); +await execute(cmd); function getOutput({cmd, cwd}) { if (MADRUN_PWD) @@ -118,9 +118,9 @@ function getOutput({cmd, cwd}) { return `> ${cmd}`; } -function execute(cmd) { - const {execSync} = import('child_process'); - const tryCatch = import('try-catch'); +async function execute(cmd) { + const {execSync} = await import('child_process'); + const tryCatch = (await import('try-catch')).default; const [e] = tryCatch(execSync, cmd, { stdio: [0, 1, 2, 'pipe'], @@ -161,7 +161,7 @@ async function getScript() { async function putoutMadrun(dir, {fix}) { const name = `${dir}/.madrun.js`; - const {runPutout} = import('../lib/fix.js'); + const {runPutout} = await import('../lib/fix.js'); const { readFile, writeFile, diff --git a/lib/madrun.js b/lib/madrun.js index 8600dc5..24d4c8d 100644 --- a/lib/madrun.js +++ b/lib/madrun.js @@ -9,47 +9,57 @@ const check = require('./check'); const isStr = (a) => typeof a === 'string'; -function getScripts() { +async function getScripts() { const path = findUp.sync([ '.madrun.js', '.madrun.cjs', + '.madrun.mjs', ]); if (!path) throw Error('.madrun.js is missing!'); - return require(path); + const madrun = await import(path); + + return madrun.default; } module.exports.predefined = require('./predefined'); -module.exports.run = (name, opts = '', env, scripts = getScripts()) => { +module.exports.run = async (name, opts = '', env, scripts) => { + scripts = scripts || await getScripts(); + const problems = check(scripts); if (problems) return problems; - return series(name, opts, env, scripts); + return await series(name, opts, env, scripts); }; module.exports.series = series; -function series(names, opts = '', env, scripts = getScripts()) { +async function series(names, opts = '', env, scripts) { names = isStr(names) ? [names] : names; - const isParallel = false; + scripts = scripts || await getScripts(); - const commands = run(names, opts, scripts, {env, isParallel: false}); + const isParallel = false; + const commands = await run(names, opts, scripts, { + env, + isParallel, + }); return joinByType(commands, { isParallel, }); } -module.exports.parallel = (names, opts = '', env, scripts = getScripts()) => { +module.exports.parallel = async (names, opts = '', env, scripts) => { + scripts = scripts || await getScripts(); names = isStr(names) ? [names] : names; const isParallel = true; - const commands = run(names, opts, scripts, {env, isParallel}); + const commands = await run(names, opts, scripts, {env, isParallel}); return joinByType(commands, { isParallel, @@ -58,18 +68,32 @@ module.exports.parallel = (names, opts = '', env, scripts = getScripts()) => { function joinByType(commands, {isParallel}) { const type = isParallel ? ' & ' : ' && '; - return commands.join(type); } -function run(names, opts, scripts, {env, isParallel}) { +async function run(names, opts, scripts, {env, isParallel}) { + /* const cmd = names .map(parse(scripts, opts, { env, isParallel, })); + */ - return cmd; + const cmds = []; + + for (const name of names) { + const cmd = await parse(name, { + scripts, + opts, + env, + isParallel, + }); + + cmds.push(cmd); + } + + return cmds; } const addOpts = (name, opts) => { @@ -95,7 +119,7 @@ const addEnv = (cmd, env) => { return `${envLine} ${cmd}`; }; -const parse = (scripts, opts, {env, isParallel}) => (name) => { +const parse = async (name, {scripts, opts, env, isParallel}) => { const result = []; const all = keys(scripts); @@ -105,7 +129,7 @@ const parse = (scripts, opts, {env, isParallel}) => (name) => { const fn = jessy(key, scripts); - const cmd = addOpts(fn(), opts); + const cmd = addOpts(await fn(), opts); const cmdEnv = addEnv(cmd, env); const processed = addPrePost(cmdEnv, key, scripts); diff --git a/package.json b/package.json index 88ca9c2..b72de10 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "parent-dirs": "^1.0.0", "putout": "^11.0.4", "try-catch": "^3.0.0", + "try-to-catch": "^3.0.0", "yargs-parser": "^20.0.0" }, "devDependencies": { diff --git a/test/madrun.js b/test/madrun.js index 1bc33a1..31181ff 100644 --- a/test/madrun.js +++ b/test/madrun.js @@ -1,7 +1,7 @@ 'use strict'; const test = require('supertape'); -const tryCatch = require('try-catch'); +const tryToCatch = require('try-to-catch'); const stub = require('@cloudcmd/stub'); const mockRequire = require('mock-require'); @@ -12,120 +12,120 @@ const { parallel, } = require('..'); -test('madrun: run', (t) => { +test('madrun: run', async (t) => { const lint = 'eslint lib'; const env = {}; const scripts = { lint: () => lint, }; - const result = run('lint', '', env, scripts); + const result = await run('lint', '', env, scripts); t.equal(result, lint, 'should equal'); t.end(); }); -test('madrun: run: series', (t) => { +test('madrun: run: series', async (t) => { const lint = 'eslint lib'; const env = {}; const scripts = { lint: () => lint, }; - const result = run(['lint'], '', env, scripts); + const result = await run(['lint'], '', env, scripts); t.equal(result, lint, 'should equal'); t.end(); }); -test('madrun: run: not found', (t) => { +test('madrun: run: not found', async (t) => { const scripts = {}; const env = {}; - const [e] = tryCatch(run, 'test', '', env, scripts); + const [e] = await tryToCatch(run, 'test', '', env, scripts); const expected = 'one of scripts not found: test'; t.equal(e.message, expected, 'should equal'); t.end(); }); -test('madrun: run: problem script', (t) => { +test('madrun: run: problem script', async (t) => { const env = {}; const scripts = { hello: 'world', }; - const [, data] = tryCatch(run, 'test', '', env, scripts); + const [, data] = await tryToCatch(run, 'test', '', env, scripts); const expected = `echo 'fix scripts first: "hello"'`; t.equal(data, expected, 'should equal'); t.end(); }); -test('madrun: run: not found: no scripts provided', (t) => { - const [e] = tryCatch(run, 'abc', ''); +test('madrun: run: not found: no scripts provided', async (t) => { + const [e] = await tryToCatch(run, 'abc', ''); const expected = 'one of scripts not found: abc'; t.equal(e.message, expected, 'should equal'); t.end(); }); -test('madrun: run: not found: deep', (t) => { +test('madrun: run: not found: deep', async (t) => { const env = {}; const scripts = { lint: () => run('test', '', env, scripts), }; - const [e] = tryCatch(run, 'lint', '', env, scripts); + const [e] = await tryToCatch(run, 'lint', '', env, scripts); const expected = 'one of scripts not found: test'; t.equal(e.message, expected, 'should equal'); t.end(); }); -test('madrun: series: opts', (t) => { +test('madrun: series: opts', async (t) => { const env = {}; const scripts = { 'lint:lib': () => 'eslint lib', 'lint:bin': () => 'eslint bin', }; - const result = run(['lint:lib', 'lint:bin'], '--fix', env, scripts); + const result = await run(['lint:lib', 'lint:bin'], '--fix', env, scripts); const expected = 'eslint lib --fix && eslint bin --fix'; t.equal(result, expected, 'should equal'); t.end(); }); -test('madrun: series: one arg', (t) => { +test('madrun: series: one arg', async (t) => { const env = {}; const scripts = { 'lint:lib': () => 'eslint lib', 'lint:bin': () => 'eslint bin', }; - const result = run('lint:*', '', env, scripts); + const result = await run('lint:*', '', env, scripts); const expected = 'eslint lib && eslint bin'; t.equal(result, expected, 'should equal'); t.end(); }); -test('madrun: parallel', (t) => { +test('madrun: parallel', async (t) => { const env = {}; const scripts = { 'lint:lib': () => 'eslint lib', 'lint:bin': () => 'eslint bin', }; - const result = parallel('lint:*', '', env, scripts); + const result = await parallel('lint:*', '', env, scripts); const expected = 'eslint lib & eslint bin'; t.equal(result, expected, 'should equal'); t.end(); }); -test('madrun: parallel: env', (t) => { +test('madrun: parallel: env', async (t) => { const scripts = { 'lint:lib': () => 'eslint lib', 'lint:bin': () => 'eslint bin', @@ -135,14 +135,14 @@ test('madrun: parallel: env', (t) => { NODE_ENV: 'development', }; - const result = parallel('lint:*', '', env, scripts); + const result = await parallel('lint:*', '', env, scripts); const expected = 'NODE_ENV=development eslint lib & NODE_ENV=development eslint bin'; t.equal(result, expected, 'should equal'); t.end(); }); -test('madrun: pre, post', (t) => { +test('madrun: pre, post', async (t) => { const env = {}; const scripts = { prelint: () => 'echo pre', @@ -150,19 +150,19 @@ test('madrun: pre, post', (t) => { postlint: () => 'echo post', }; - const result = run('lint', '', env, scripts); + const result = await run('lint', '', env, scripts); const expected = 'echo pre && eslint lib && echo post'; t.equal(result, expected, 'should equal'); t.end(); }); -test('madrun: run: .madrun.js not found', (t) => { +test('madrun: run: .madrun.js not found', async (t) => { mockRequire('find-up', { sync: stub(), }); const {run} = reRequire('..'); - const [e] = tryCatch(run); + const [e] = await tryToCatch(run); stopAll();