Skip to content
This repository was archived by the owner on Jul 31, 2024. It is now read-only.

Commit 84af24d

Browse files
committed
[Refactor] move entrypoint to ESM
1 parent 7ba7bec commit 84af24d

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

bin.js renamed to bin.mjs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
#!/usr/bin/env node
22

3-
'use strict';
3+
import npx from 'libnpx';
4+
import getLockfile from 'npm-lockfile/getLockfile';
5+
import finder from 'find-package-json';
6+
import semver from 'semver';
7+
import colors from 'colors/safe.js';
48

5-
const npx = require('libnpx');
6-
const getLockfile = require('npm-lockfile/getLockfile');
7-
const finder = require('find-package-json');
8-
const semver = require('semver');
9-
const colors = require('colors/safe');
9+
import path from 'path';
10+
import { existsSync } from 'fs';
11+
import { copyFile, writeFile } from 'fs/promises';
12+
import { execSync } from 'child_process';
1013

11-
const path = require('path');
12-
const { existsSync } = require('fs');
13-
const { copyFile, writeFile } = require('fs').promises;
14-
const { execSync } = require('child_process');
15-
16-
const getProjectTempDir = require('./getProjectTempDir');
14+
import getProjectTempDir from './getProjectTempDir.js';
1715

1816
const { filename: pkg } = finder(process.cwd()).next();
1917
const pkgDir = path.dirname(pkg);
@@ -50,13 +48,14 @@ if (npmIsGood && (hasPkgLock || hasShrink || isFix)) {
5048
Promise.all([
5149
getLockfile(pkg),
5250
getProjectTempDir({ npmNeeded }),
53-
]).then(([lockfile, tmpDir]) => {
51+
]).then(async ([lockfile, tmpDir]) => {
5452
const lockfilePath = path.join(tmpDir, 'package-lock.json');
5553
const writtenLockfile = writeFile(lockfilePath, lockfile, encoding);
5654
const writtenPkg = copyFile(pkg, path.join(tmpDir, 'package.json'));
5755
const auditLevel = execSync(`npm config get audit-level --no-workspaces --prefix="${process.cwd()}"`, encoding).trim();
5856
const writtenRC = auditLevel && auditLevel !== 'undefined' ? writeFile(path.join(tmpDir, '.npmrc'), `audit-level=${auditLevel}`, encoding) : null;
59-
return Promise.all([writtenLockfile, writtenPkg, writtenRC]).then(() => tmpDir);
57+
await Promise.all([writtenLockfile, writtenPkg, writtenRC]);
58+
return tmpDir;
6059
}).then((tmpDir) => {
6160
process.chdir(tmpDir);
6261
process.env.PATH = `${path.join(tmpDir, '../node_modules/.bin')}:${process.env.PATH}`;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "aud",
33
"version": "2.0.5",
44
"description": "Use `npx aud` instead of `npm audit`, whether you have a lockfile or not!",
5-
"bin": "./bin.js",
5+
"bin": "./bin.mjs",
66
"exports": {
77
"./package.json": "./package.json"
88
},
@@ -14,7 +14,7 @@
1414
"lint": "eslint --ext=js,mjs .",
1515
"tests-only": "nyc tape 'test/**/*.js'",
1616
"test": "npm run tests-only",
17-
"posttest": "./bin.js --production",
17+
"posttest": "./bin.mjs --production",
1818
"version": "auto-changelog && git add CHANGELOG.md",
1919
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
2020
},

test/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ function hideWarnings(lines) {
1111
return lines.filter((x) => !(/^npm WARN|^\(node:\d+\) ExperimentalWarning/).test(x));
1212
}
1313

14+
const binPath = require('../package.json').bin;
15+
1416
test('fix option', (t) => {
1517
t.plan(6);
1618
process.chdir(path.join(__dirname, '..'));
17-
exec('./bin.js fix', { encoding: 'utf-8' }, (error, stdout, stderr) => {
19+
exec(`${binPath} fix`, { encoding: 'utf-8' }, (error, stdout, stderr) => {
1820
process.chdir(cwd);
1921

2022
t.ok(error, 'errors');

0 commit comments

Comments
 (0)