Skip to content

Commit

Permalink
feat: generate package lock file (#63)
Browse files Browse the repository at this point in the history
* feat: generate package lock file

* fix: fix test

* fix: use child_process generate package-lock.json

* fix: add package-lock option

* fix: use execSync

* fix: del env in test

* fix: use execa handle npm install

---------

Co-authored-by: shixia.ly <[email protected]>
  • Loading branch information
akitaSummer and shixia.ly authored Feb 1, 2024
1 parent 9f84d95 commit 29e0f82
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
### Independent Client
```bash
$ npm i @cnpmjs/rapid --registry=https://registry.npmmirror.com
$ npm i --package-lock-only --registry=https://registry.npmmirror.com
$ rapid install
```

Expand Down
1 change: 1 addition & 0 deletions integration/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ describe('test/index.test.js', () => {
.end(done);
});
});

});
7 changes: 7 additions & 0 deletions packages/cli/bin/rapid.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ const argv = yargs
describe: 'Dependency types to omit from the installation tree on disk',
type: 'array',
default: [],
})
.option('package-lock', {
describe: 'Whether to generate package-lock.json file',
type: 'boolean',
default: true,
});
},
handler: async argv => {
const ignoreScripts = argv['ignore-scripts'];
const mode = argv.by || NpmFsMode.NPM;
const productionMode = argv.production || argv.omit.includes('dev') || process.env.NODE_ENV === 'production';
const noPackageLock = !argv['package-lock'];

const cwd = process.cwd();
const pkgRes = await util.readPkgJSON();
Expand All @@ -57,6 +63,7 @@ const argv = yargs
nydusMode: NYDUS_TYPE.FUSE,
ignoreScripts,
productionMode,
noPackageLock,
});

Alert.success('🚀 Success', [
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const { MirrorConfig } = require('binary-mirror-config');
// 有依赖树(package-lock.json)走 npm / npminstall 极速安装
exports.install = async options => {
options.env = util.getEnv(options.env, options.args);

if (!options.noPackageLock) {
await util.generatePackageLock(options.cwd);
}

const { packageLock } = options.packageLock || (await util.readPackageLock(options.cwd));

const currentMountInfo = await util.listMountInfo();
Expand Down
33 changes: 33 additions & 0 deletions packages/cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,39 @@ exports.readPkgJSON = async function readPkgJSON(cwd) {
return { pkg, pkgPath };
};

exports.generatePackageLock = async cwd => {
let isExist = true;
try {
const lockPath = path.join(cwd || exports.findLocalPrefix(), './package-lock.json');
await await fs.stat(lockPath);
} catch {
isExist = false;
}
try {
if (!isExist) {
console.log('npm install --force --package-lock-only --ignore-scripts is running');

const childProcess = execa('npm', [ 'install', '--force', '--package-lock-only', '--ignore-scripts' ], {
cwd,
stdio: 'inherit',
});

await new Promise((resolve, reject) => {
childProcess.then(resolve).catch(reject);

process.on('exit', () => {
childProcess.kill();
});
});
}
} catch (e) {
Alert.error('Error', [
'generate package-lock.json error.',
'Run `npm i --package-lock-only` to generate it.',
]);
}
};

exports.readPackageLock = async function readPackageLock(cwd) {
try {
const lockPath = path.join(cwd || exports.findLocalPrefix(), './package-lock.json');
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/test/fixtures/not-exist-lock-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "test",
"version": "1.0.0",
"dependencies": {
"lodash.has": "4.5.2"
}
}
37 changes: 37 additions & 0 deletions packages/cli/test/package_lock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs/promises');
const mm = require('mm');
const PackageLock = require('../lib/package_lock').PackageLock;
const { install } = require('../lib');
const httpclient = require('../lib/httpclient');
const nydusd = require('../lib/nydusd');
const downloadDependency = require('../lib/download_dependency');

const fixture = path.join(__dirname, 'fixtures/lockfile');

Expand Down Expand Up @@ -43,4 +49,35 @@ describe('test/package_lock.test.js', () => {
assert.strictEqual(packageLock.isWorkspacesPkg('xxx/lodash.has'), false);
assert.strictEqual(packageLock.isDepsPkg('node_modules/lodash.has'), true);
});

describe('not exist lock file', async () => {
let fixture;
beforeEach(async () => {
mm(process, 'cwd', () => fixture);
mm(nydusd, 'startNydusFs', async () => { });
mm(downloadDependency, 'download', async () => {
return {
depsTree: [ 1 ],
};
});

});
afterEach(async () => {
await fs.rm(path.join(fixture, 'node_modules'), { recursive: true, force: true });
await fs.rm(path.join(fixture, 'package-lock.json'), { force: true });
mm.restore();
});

it('should run all project installation scripts', async () => {
fixture = path.join(__dirname, './fixtures/not-exist-lock-file');
const pkg = require(path.join(fixture, 'package.json'));
await install({
httpclient,
pkg,
cwd: fixture,
console: global.console,
});
await fs.stat(path.join(fixture, 'package-lock.json'));
});
});
});

0 comments on commit 29e0f82

Please sign in to comment.