Skip to content

Commit 05320ac

Browse files
LitoMoresindresorhus
andauthoredApr 27, 2021
Require Node.js 12 and move to ESM (#181)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 5400b66 commit 05320ac

14 files changed

+254
-261
lines changed
 

‎.github/workflows/main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
1616
steps:
1717
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
18+
- uses: actions/setup-node@v2
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install

‎estest/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import {createRequire} from 'module';
2-
3-
const meow = createRequire(import.meta.url)('../index.js');
1+
import meow from '../index.js';
42

53
meow(`
64
Usage

‎index.d.ts

+212-215
Large diffs are not rendered by default.

‎index.js

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
'use strict';
2-
const path = require('path');
3-
const buildParserOptions = require('minimist-options');
4-
const parseArguments = require('yargs-parser');
5-
const camelCaseKeys = require('camelcase-keys');
6-
const decamelize = require('decamelize');
7-
const decamelizeKeys = require('decamelize-keys');
8-
const trimNewlines = require('trim-newlines');
9-
const redent = require('redent');
10-
const readPkgUp = require('read-pkg-up');
11-
const hardRejection = require('hard-rejection');
12-
const normalizePackageData = require('normalize-package-data');
13-
14-
// Prevent caching of this module so module.parent is always accurate
15-
delete require.cache[__filename];
16-
const parentDir = path.dirname(module.parent && module.parent.filename ? module.parent.filename : '.');
1+
import buildParserOptions from 'minimist-options';
2+
import parseArguments from 'yargs-parser';
3+
import camelCaseKeys from 'camelcase-keys';
4+
import decamelize from 'decamelize';
5+
import decamelizeKeys from 'decamelize-keys';
6+
import trimNewlines from 'trim-newlines';
7+
import redent from 'redent';
8+
import {readPackageUpSync} from 'read-pkg-up';
9+
import hardRejection from 'hard-rejection';
10+
import normalizePackageData from 'normalize-package-data';
1711

1812
const isFlagMissing = (flagName, definedFlags, receivedFlags, input) => {
1913
const flag = definedFlags[flagName];
@@ -109,8 +103,7 @@ const meow = (helpText, options) => {
109103
helpText = '';
110104
}
111105

112-
const foundPkg = readPkgUp.sync({
113-
cwd: parentDir,
106+
const foundPkg = readPackageUpSync({
114107
normalize: false
115108
});
116109

@@ -231,4 +224,4 @@ const meow = (helpText, options) => {
231224
};
232225
};
233226

234-
module.exports = meow;
227+
export default meow;

‎index.test-d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {expectAssignable, expectType} from 'tsd';
22
import {PackageJson} from 'type-fest';
3-
import meow = require('.');
4-
import {Result} from '.';
3+
import meow, {Result} from './index.js';
54

65
expectType<Result<never>>(meow('Help text'));
76
expectType<Result<never>>(meow('Help text', {hardRejection: false}));

‎package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": ">=12"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd"
@@ -47,18 +49,19 @@
4749
"hard-rejection": "^2.1.0",
4850
"minimist-options": "4.1.0",
4951
"normalize-package-data": "^3.0.0",
50-
"read-pkg-up": "^7.0.1",
52+
"read-pkg-up": "^8.0.0",
5153
"redent": "^3.0.0",
5254
"trim-newlines": "^3.0.0",
5355
"type-fest": "^0.18.0",
5456
"yargs-parser": "^20.2.3"
5557
},
5658
"devDependencies": {
57-
"ava": "^2.4.0",
59+
"ava": "^3.15.0",
5860
"execa": "^4.1.0",
5961
"indent-string": "^4.0.0",
62+
"read-pkg": "^6.0.0",
6063
"tsd": "^0.13.1",
61-
"xo": "^0.34.1"
64+
"xo": "^0.39.1"
6265
},
6366
"xo": {
6467
"rules": {

‎test/allow-unkonwn-flags.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import path from 'path';
1+
import path from 'node:path';
2+
import {fileURLToPath} from 'node:url';
23
import test from 'ava';
34
import execa from 'execa';
45

6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
57
const fixtureAllowUnknownFlags = path.join(__dirname, 'fixtures', 'fixture-allow-unknown-flags.js');
68

79
test('spawn CLI and test specifying unknown flags', async t => {

‎test/fixtures/fixture-allow-unknown-flags.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
2-
'use strict';
3-
const meow = require('../..');
2+
import meow from '../../index.js';
43

54
const cli = meow({
65
description: 'Custom description',

‎test/fixtures/fixture-required-function.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
2-
'use strict';
3-
const meow = require('../..');
2+
import meow from '../../index.js';
43

54
const cli = meow({
65
description: 'Custom description',

‎test/fixtures/fixture-required-multiple.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
2-
'use strict';
3-
const meow = require('../..');
2+
import meow from '../../index.js';
43

54
const cli = meow({
65
description: 'Custom description',

‎test/fixtures/fixture-required.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
'use strict';
3-
const meow = require('../..');
3+
import meow from '../../index.js';
44

55
const cli = meow({
66
description: 'Custom description',

‎test/fixtures/fixture.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
2-
'use strict';
3-
const meow = require('../..');
2+
import meow from '../../index.js';
43

54
const cli = meow({
65
description: 'Custom description',
@@ -18,9 +17,9 @@ const cli = meow({
1817
});
1918

2019
if (cli.flags.camelCaseOption === 'foo') {
21-
Object.keys(cli.flags).forEach(x => {
20+
for (const x of Object.keys(cli.flags)) {
2221
console.log(x);
23-
});
22+
}
2423
} else {
2524
console.log(cli.flags.camelCaseOption);
2625
}

‎test/is-required-flag.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import path from 'node:path';
2+
import {fileURLToPath} from 'node:url';
13
import test from 'ava';
24
import execa from 'execa';
3-
const path = require('path');
45

6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
57
const fixtureRequiredPath = path.join(__dirname, 'fixtures', 'fixture-required.js');
68
const fixtureRequiredFunctionPath = path.join(__dirname, 'fixtures', 'fixture-required-function.js');
79
const fixtureRequiredMultiplePath = path.join(__dirname, 'fixtures', 'fixture-required-multiple.js');

‎test/test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import path from 'node:path';
2+
import {fileURLToPath} from 'node:url';
13
import test from 'ava';
24
import indentString from 'indent-string';
35
import execa from 'execa';
4-
import path from 'path';
5-
import pkg from '../package.json';
6-
import meow from '..';
6+
import {readPackageAsync} from 'read-pkg';
7+
import meow from '../index.js';
78

9+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
810
const fixturePath = path.join(__dirname, 'fixtures', 'fixture.js');
911
const NODE_MAJOR_VERSION = process.versions.node.split('.')[0];
1012

@@ -40,6 +42,7 @@ test('support help shortcut', t => {
4042
});
4143

4244
test('spawn cli and show version', async t => {
45+
const pkg = await readPackageAsync();
4346
const {stdout} = await execa(fixturePath, ['--version']);
4447
t.is(stdout, pkg.version);
4548
});

0 commit comments

Comments
 (0)
Please sign in to comment.