Skip to content

Commit

Permalink
chore: madrun: use @putout/printer
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 1, 2023
1 parent 50a508d commit 449c7df
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 58 deletions.
1 change: 0 additions & 1 deletion .madrun.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ export default {
'postpublish': () => 'npm i -g',
'hello': noop,
};

1 change: 1 addition & 0 deletions .putout.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"printer": "putout",
"match": {
"{lib,bin,test}": {
"madrun": "off"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ export default {
'lint:env': () => ['putout .', {
CI: 1,
}],
'env:lint': () => [env, 'putout .'],
'env:lint': () => [
env,
'putout .',
],
'lint:no-env': async () => await cutEnv('lint:env'),
};
```
Expand Down
4 changes: 1 addition & 3 deletions bin/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
writeFile,
access,
} from 'fs/promises';

import tryToCatch from 'try-to-catch';
import montag from 'montag';

Expand All @@ -21,7 +20,7 @@ export const createMadrun = async (cwd, info) => {
if (!name) {
const {scripts = {}} = info;

const madrun = montag `
const madrun = montag`
'use strict';
const {
Expand Down Expand Up @@ -82,4 +81,3 @@ async function findMadrun(cwd) {

return '';
}

20 changes: 5 additions & 15 deletions bin/init.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ import {
join,
} from 'path';
import {fileURLToPath} from 'url';

import {
test,
stub,
} from 'supertape';

import montag from 'montag';
import tryToCatch from 'try-to-catch';
import {createMockImport} from 'mock-import';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const {
mockImport,
reImport,
Expand Down Expand Up @@ -72,10 +71,7 @@ test('madrun: init: createMadrun: writeFile', async (t) => {
};
`;

const expected = [
'/hello/.madrun.js',
code,
];
const expected = ['/hello/.madrun.js', code];

stopAll();

Expand Down Expand Up @@ -109,10 +105,7 @@ test('madrun: init: createMadrun: writeFile: no scripts', async (t) => {
module.exports = {};
`;

const expected = [
'/hello/.madrun.js',
code,
];
const expected = ['/hello/.madrun.js', code];

stopAll();

Expand All @@ -137,6 +130,7 @@ test('madrun: init: patchPackage: import error: writeFile', async (t) => {
});

const {patchPackage} = await reImport('./init.mjs');

await patchPackage(madrunFile, {
hello: 'world',
});
Expand All @@ -151,12 +145,8 @@ test('madrun: init: patchPackage: import error: writeFile', async (t) => {

stopAll();

const expected = [
'./package.json',
content,
];
const expected = ['./package.json', content];

t.calledWith(writeFile, expected);
t.end();
});

1 change: 0 additions & 1 deletion bin/madrun-completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ const read = fs.createReadStream(filename);
const write = process.stdout;

read.pipe(write);

21 changes: 14 additions & 7 deletions bin/madrun.mjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#!/usr/bin/env node

import {createRequire} from 'module';
import {
dirname,
basename,
} from 'path';

import findUp from 'find-up';
import tryToCatch from 'try-to-catch';
import yargsParser from 'yargs-parser';

import {series} from '../lib/madrun.js';
import check from '../lib/check.js';
import {choose} from '../lib/choose.mjs';

const require = createRequire(import.meta.url);

const {exit} = process;

const {
MADRUN_PWD,
MADRUN_NAME,
} = process.env;

const cwd = process.cwd();

const args = yargsParser(process.argv.slice(2), {
Expand Down Expand Up @@ -86,7 +85,9 @@ const [dir, scripts] = await getScript();
const problems = check(scripts);

if (problems) {
const result = await putoutMadrun(dir, {fix});
const result = await putoutMadrun(dir, {
fix,
});

if (fix) {
exit();
Expand Down Expand Up @@ -119,7 +120,10 @@ if (e) {
exit(1);
}

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

function getOutput({cmd, cwd}) {
Expand Down Expand Up @@ -176,17 +180,20 @@ async function getScript() {
async function putoutMadrun(dir, {fix}) {
const name = `${dir}/.madrun.js`;
const {runPutout} = await import('../lib/fix.mjs');

const {
readFile,
writeFile,
} = await import('fs/promises');

const data = await readFile(name, 'utf8');
const {places, code} = await runPutout(data);
const {
places,
code,
} = await runPutout(data);

if (fix)
await writeFile(name, code);

return places;
}

1 change: 0 additions & 1 deletion lib/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ function getNames(scripts) {

return result;
}

1 change: 1 addition & 0 deletions lib/choose.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const choose = async (scripts) => {
});

const answer = await run(prompt);

return answer || [];
};

Expand Down
7 changes: 5 additions & 2 deletions lib/fix.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import putout, {
initReport,
} from 'putout';

import dumpFormatter from '@putout/formatter-dump';

const report = initReport();

export const runPutout = async (data, options) => {
const {code, places} = putout(data, {
const {
code,
places,
} = putout(data, {
printer: 'putout',
...options,
plugins: [
'madrun',
Expand Down
4 changes: 2 additions & 2 deletions lib/fix.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {runPutout} from './fix.mjs';

test('madrun: fix', async (t) => {
const result = await runPutout('hello');

const expected = {
code: 'hello',
code: 'hello;\n',
places: '',
};

t.deepEqual(result, expected);
t.end();
});

1 change: 0 additions & 1 deletion lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ module.exports.help = () => {

return result.join('\n');
};

30 changes: 17 additions & 13 deletions lib/madrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const findUp = require('find-up');
const wildcard = require('./wildcard');
const check = require('./check');
const supported = require('../supported');

const maybeArray = (a) => isArray(a) ? a : [a];
const isStr = (a) => typeof a === 'string';
const {isArray} = Array;

Expand All @@ -22,12 +22,11 @@ async function getScripts() {
}

module.exports.cutEnv = async (names, opts = '', env, scripts) => {
if (isStr(names))
names = [names];

names = maybeArray(names);
scripts = scripts || await getScripts();

const isParallel = false;

const commands = await run(names, opts, scripts, {
env,
cutEnv: true,
Expand All @@ -53,12 +52,11 @@ module.exports.run = async (name, opts = '', env, scripts) => {
module.exports.series = series;

async function series(names, opts = '', env, scripts) {
if (isStr(names))
names = [names];

names = maybeArray(names);
scripts = scripts || await getScripts();

const isParallel = false;

const commands = await run(names, opts, scripts, {
env,
isParallel,
Expand All @@ -71,12 +69,14 @@ async function series(names, opts = '', env, scripts) {

module.exports.parallel = async (names, opts = '', env, scripts) => {
scripts = scripts || await getScripts();

if (isStr(names))
names = [names];
names = maybeArray(names);

const isParallel = true;
const commands = await run(names, opts, scripts, {env, isParallel});

const commands = await run(names, opts, scripts, {
env,
isParallel,
});

return joinByType(commands, {
isParallel,
Expand Down Expand Up @@ -140,8 +140,13 @@ const parse = async (name, {scripts, opts, env, isParallel, cutEnv}) => {

const fn = jessy(key, scripts);

const {line, lineEnv} = await runScriptFn(fn);
const {
line,
lineEnv,
} = await runScriptFn(fn);

const cmd = addOpts(line, opts);

const cmdEnv = addEnv(cmd, {
...cutEnv ? {} : lineEnv,
...env,
Expand Down Expand Up @@ -195,4 +200,3 @@ async function addPrePost(cmd, key, scripts) {

return cmd;
}

1 change: 0 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ module.exports = (runners) => {

return null;
};

5 changes: 2 additions & 3 deletions lib/wildcard.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

module.exports = (str) => {
const wildcard = '^' + str/* search from start of line */
const wildcard = '^' + str
.replace('.', '\\.')
.replace('*', '.*')
.replace('?', '.?') + '$'; /* search to end of line */
.replace('?', '.?') + '$';

return RegExp(wildcard);
};

1 change: 0 additions & 1 deletion test/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ test('madrun: check', (t) => {
t.equal(result, expected);
t.end();
});

1 change: 0 additions & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ test('madrun: cli: kill', (t) => {
t.match(result, expected);
t.end();
});

3 changes: 2 additions & 1 deletion test/fix.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ const {

test('madrun: fix', async (t) => {
const fixture = await import('./fixture/fix.json');

const options = {
fixCount: 1,
};

mockImport('@putout/formatter-dump', jsonFormatter);

const {runPutout} = await reImport('../lib/fix.mjs');

const result = runPutout(`
module.exports = {
'hello': 'world'
Expand All @@ -28,4 +30,3 @@ test('madrun: fix', async (t) => {
t.deepEqual(result, fixture);
t.end();
});

Loading

0 comments on commit 449c7df

Please sign in to comment.