Skip to content

Commit 9129f1a

Browse files
committed
feature(madrun) init: convert to ESM
1 parent 85f924e commit 9129f1a

File tree

12 files changed

+66
-44
lines changed

12 files changed

+66
-44
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ npm-debug.log
77
coverage
88

99
yarn-error.log
10-
.putoutcache

.madrun.mjs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import {run, cutEnv} from './lib/madrun.js';
1+
import {
2+
run,
3+
cutEnv,
4+
} from './lib/madrun.js';
5+
6+
const NODE_OPTIONS = `'--loader mock-import --no-warnings'`;
27

38
const env = {
49
SUPERTAPE_PROGRESS_BAR_MIN: 20,
10+
NODE_OPTIONS,
511
};
612

713
export default {
814
'lint': () => 'putout .',
915
'fresh:lint': () => run('lint', '--fresh'),
1016
'lint:fresh': () => run('lint', '--fresh'),
1117
'fix:lint': () => run('lint', '--fix'),
12-
'test': () => [`tape 'test/**/*.js' '{lib,bin}/**/*.spec.{js,mjs}'`, env],
18+
'test': () => [env, `tape 'test/**/*.js' '{lib,bin}/**/*.spec.{js,mjs}'`],
1319
'watch:test': async () => await run('watcher', await run('test')),
1420
'watch:tape': () => 'nodemon -w test -w lib --exec tape',
1521
'watch:lint': async () => await run('watcher', await run('lint')),
1622
'watcher': () => 'nodemon -w test -w lib -w bin --exec',
17-
'coverage': async () => [`nyc ${await cutEnv('test')}`, env],
18-
'report': () => 'nyc report --reporter=text-lcov | coveralls',
23+
'coverage': async () => [`c8 ${await cutEnv('test')}`, env],
24+
'report': () => 'c8 report --reporter=text-lcov | coveralls',
1925
'postpublish': () => 'npm i -g',
2026
'hello': () => {},
2127
};

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.*
2-
*.spec.js
2+
*.spec.*
33

44
test
55
fixture

.nycrc.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
"all": true,
77
"exclude": [
88
".*",
9-
"**/*.spec.js",
10-
"test"
9+
"bin/madrun.mjs",
10+
"**/*.spec.*",
11+
"test",
12+
"**/fixture"
1113
],
1214
"branches": 100,
1315
"lines": 100,

bin/init.js renamed to bin/init.mjs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
'use strict';
2-
3-
const {join} = require('path');
4-
const {
1+
import {join} from 'path';
2+
import {
53
writeFile,
64
access,
7-
} = require('fs/promises');
5+
} from 'fs/promises';
86

9-
const tryToCatch = require('try-to-catch');
10-
const montag = require('montag');
11-
const supported = require('../supported.json');
7+
import tryToCatch from 'try-to-catch';
8+
import montag from 'montag';
9+
import {createSimport} from 'simport';
1210

1311
const {stringify} = JSON;
1412
const {keys} = Object;
1513

16-
module.exports.createMadrun = async (cwd, info) => {
14+
const simport = createSimport(import.meta.url);
15+
const supported = await simport('../supported.json');
16+
17+
export const createMadrun = async (cwd, info) => {
1718
let name = await findMadrun(cwd);
1819

1920
if (!name) {
@@ -38,8 +39,8 @@ module.exports.createMadrun = async (cwd, info) => {
3839
return name;
3940
};
4041

41-
module.exports.patchPackage = async (name, info) => {
42-
const {default: content} = await import(name);
42+
export const patchPackage = async (name, info) => {
43+
const {default: content} = await simport(name);
4344

4445
const updatedScripts = updatePackage(content);
4546
const prepared = preparePackage(info, updatedScripts);

bin/init.spec.js renamed to bin/init.spec.mjs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
'use strict';
1+
import {join} from 'path';
22

3-
const {join} = require('path');
4-
5-
const {
3+
import {
64
test,
75
stub,
8-
} = require('supertape');
6+
} from 'supertape';
97

10-
const montag = require('montag');
11-
const mockRequire = require('mock-require');
12-
const tryToCatch = require('try-to-catch');
8+
import montag from 'montag';
9+
import tryToCatch from 'try-to-catch';
10+
import {createCommons} from 'simport';
11+
import {createMockImport} from 'mock-import';
12+
13+
const {__dirname} = createCommons(import.meta.url);
14+
const {
15+
mockImport,
16+
reImport,
17+
stopAll,
18+
} = createMockImport(import.meta.url);
1319

14-
const {reRequire} = mockRequire;
1520
const {stringify} = JSON;
1621

1722
test('madrun: init: createMadrun: found', async (t) => {
1823
const access = stub();
1924

20-
mockRequire('fs/promises', {
25+
mockImport('fs/promises', {
2126
access,
2227
});
2328

24-
const {createMadrun} = await reRequire('./init');
29+
const {createMadrun} = await reImport('./init.mjs');
2530
const cwd = '/hello';
2631
const name = await createMadrun(cwd);
2732

@@ -33,12 +38,12 @@ test('madrun: init: createMadrun: writeFile', async (t) => {
3338
const access = stub().throws(Error('xxx'));
3439
const writeFile = stub();
3540

36-
mockRequire('fs/promises', {
41+
mockImport('fs/promises', {
3742
access,
3843
writeFile,
3944
});
4045

41-
const {createMadrun} = await reRequire('./init');
46+
const {createMadrun} = await reImport('./init.mjs');
4247
const cwd = '/hello';
4348

4449
await createMadrun(cwd, {
@@ -72,12 +77,12 @@ test('madrun: init: createMadrun: writeFile: no scripts', async (t) => {
7277
const access = stub().throws(Error('xxx'));
7378
const writeFile = stub();
7479

75-
mockRequire('fs/promises', {
80+
mockImport('fs/promises', {
7681
access,
7782
writeFile,
7883
});
7984

80-
const {createMadrun} = await reRequire('./init');
85+
const {createMadrun} = await reImport('./init.mjs');
8186
const cwd = '/hello';
8287

8388
await createMadrun(cwd, {});
@@ -104,7 +109,7 @@ test('madrun: init: createMadrun: writeFile: no scripts', async (t) => {
104109
});
105110

106111
test('madrun: init: patchPackage: import error', async (t) => {
107-
const {patchPackage} = await reRequire('./init');
112+
const {patchPackage} = await reImport('./init.mjs');
108113
const [error] = await tryToCatch(patchPackage, 'xxx');
109114

110115
t.ok(error.message.includes(`Cannot find package 'xxx'`));
@@ -115,11 +120,11 @@ test('madrun: init: patchPackage: import error', async (t) => {
115120
const writeFile = stub();
116121
const madrunFile = join(__dirname, 'fixture', 'madrun.mjs');
117122

118-
mockRequire('fs/promises', {
123+
mockImport('fs/promises', {
119124
writeFile,
120125
});
121126

122-
const {patchPackage} = await reRequire('./init');
127+
const {patchPackage} = await reImport('./init.mjs');
123128
await patchPackage(madrunFile, {
124129
hello: 'world',
125130
});
@@ -131,6 +136,8 @@ test('madrun: init: patchPackage: import error', async (t) => {
131136
},
132137
}, null, 2) + '\n';
133138

139+
stopAll();
140+
134141
const expected = [
135142
'./package.json',
136143
content,

bin/madrun.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env node
22

3-
import {dirname, basename} from 'path';
3+
import {
4+
dirname,
5+
basename,
6+
} from 'path';
47

58
import findUp from 'find-up';
69
import tryToCatch from 'try-to-catch';

help.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"--init ": "init scripts file madrun.js",
3-
"--fix ": "fix scripts file madrun.js",
2+
"--init ": "init scripts file",
3+
"--fix ": "fix scripts file",
44
"-h, --help ": "display this help and exit",
55
"-v, --version ": "output version information and exit"
66
}

lib/help.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ const help = require('../help');
55
const {entries} = Object;
66

77
module.exports.help = () => {
8-
const result = [];
8+
const result = [
9+
'Usage: madrun [options] [script]',
10+
'Options:',
11+
];
912

1013
for (const [name, description] of entries(help)) {
11-
result.push(`${name} ${description}`);
14+
result.push(` ${name} ${description}`);
1215
}
1316

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"eslint": "^7.3.1",
7777
"eslint-plugin-node": "^11.0.0",
7878
"eslint-plugin-putout": "^7.0.0",
79+
"mock-import": "^1.0.5",
7980
"mock-require": "^3.0.3",
8081
"nodemon": "^2.0.0",
8182
"nyc": "^15.0.0",

0 commit comments

Comments
 (0)