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

Commit

Permalink
[Refactor] move entrypoint to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 31, 2024
1 parent 7ba7bec commit 84af24d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
27 changes: 13 additions & 14 deletions bin.js → bin.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/usr/bin/env node

'use strict';
import npx from 'libnpx';
import getLockfile from 'npm-lockfile/getLockfile';
import finder from 'find-package-json';
import semver from 'semver';
import colors from 'colors/safe.js';

const npx = require('libnpx');
const getLockfile = require('npm-lockfile/getLockfile');
const finder = require('find-package-json');
const semver = require('semver');
const colors = require('colors/safe');
import path from 'path';
import { existsSync } from 'fs';
import { copyFile, writeFile } from 'fs/promises';
import { execSync } from 'child_process';

const path = require('path');
const { existsSync } = require('fs');
const { copyFile, writeFile } = require('fs').promises;
const { execSync } = require('child_process');

const getProjectTempDir = require('./getProjectTempDir');
import getProjectTempDir from './getProjectTempDir.js';

const { filename: pkg } = finder(process.cwd()).next();
const pkgDir = path.dirname(pkg);
Expand Down Expand Up @@ -50,13 +48,14 @@ if (npmIsGood && (hasPkgLock || hasShrink || isFix)) {
Promise.all([
getLockfile(pkg),
getProjectTempDir({ npmNeeded }),
]).then(([lockfile, tmpDir]) => {
]).then(async ([lockfile, tmpDir]) => {
const lockfilePath = path.join(tmpDir, 'package-lock.json');
const writtenLockfile = writeFile(lockfilePath, lockfile, encoding);
const writtenPkg = copyFile(pkg, path.join(tmpDir, 'package.json'));
const auditLevel = execSync(`npm config get audit-level --no-workspaces --prefix="${process.cwd()}"`, encoding).trim();
const writtenRC = auditLevel && auditLevel !== 'undefined' ? writeFile(path.join(tmpDir, '.npmrc'), `audit-level=${auditLevel}`, encoding) : null;
return Promise.all([writtenLockfile, writtenPkg, writtenRC]).then(() => tmpDir);
await Promise.all([writtenLockfile, writtenPkg, writtenRC]);
return tmpDir;
}).then((tmpDir) => {
process.chdir(tmpDir);
process.env.PATH = `${path.join(tmpDir, '../node_modules/.bin')}:${process.env.PATH}`;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "aud",
"version": "2.0.5",
"description": "Use `npx aud` instead of `npm audit`, whether you have a lockfile or not!",
"bin": "./bin.js",
"bin": "./bin.mjs",
"exports": {
"./package.json": "./package.json"
},
Expand All @@ -14,7 +14,7 @@
"lint": "eslint --ext=js,mjs .",
"tests-only": "nyc tape 'test/**/*.js'",
"test": "npm run tests-only",
"posttest": "./bin.js --production",
"posttest": "./bin.mjs --production",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
},
Expand Down
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ function hideWarnings(lines) {
return lines.filter((x) => !(/^npm WARN|^\(node:\d+\) ExperimentalWarning/).test(x));
}

const binPath = require('../package.json').bin;

test('fix option', (t) => {
t.plan(6);
process.chdir(path.join(__dirname, '..'));
exec('./bin.js fix', { encoding: 'utf-8' }, (error, stdout, stderr) => {
exec(`${binPath} fix`, { encoding: 'utf-8' }, (error, stdout, stderr) => {
process.chdir(cwd);

t.ok(error, 'errors');
Expand Down

0 comments on commit 84af24d

Please sign in to comment.