Skip to content

Commit

Permalink
feature(madrun) asyncify
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Nov 26, 2020
1 parent 5dd1ece commit c9d586b
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 70 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"rules": {
"node/no-unsupported-features/es-syntax": "off"
},
"extends": [
"plugin:node/recommended",
"plugin:putout/recommended"
Expand Down
11 changes: 5 additions & 6 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:
1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

6 changes: 3 additions & 3 deletions .madrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down
20 changes: 10 additions & 10 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Commit
---------------
## Commit

Format of the commit message: **type(scope) subject**

**Type**:

- feature(scope) subject
- fix(scope) subject
- docs(scope) subject
Expand All @@ -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 <subject> 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)
16 changes: 8 additions & 8 deletions bin/madrun.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -88,7 +88,7 @@ if (init)
process.exit();

if (problems) {
execute(`echo '${problems}'`);
await execute(`echo '${problems}'`);
process.exit(1);
}

Expand All @@ -98,15 +98,15 @@ 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);
process.exit(1);
}

console.log(getOutput({cmd, cwd}));
execute(cmd);
await execute(cmd);

function getOutput({cmd, cwd}) {
if (MADRUN_PWD)
Expand All @@ -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'],
Expand Down Expand Up @@ -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,
Expand Down
52 changes: 38 additions & 14 deletions lib/madrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) => {
Expand All @@ -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);
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Loading

0 comments on commit c9d586b

Please sign in to comment.