Skip to content

Commit 694a3b8

Browse files
committed
feat: generalize bin/check-package-locks.js to detect local packages
Check local packages instead of hard-coded `@loopback/*` to make sure `package-lock.json` does not have entries for local packages.
1 parent 4ee1de7 commit 694a3b8

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

bin/check-package-locks.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
'use strict';
1212

1313
const path = require('path');
14-
const fs = require('fs');
15-
const promisify = require('util').promisify;
16-
17-
const readFile = promisify(fs.readFile);
18-
const exists = promisify(fs.exists);
14+
const fs = require('fs-extra');
1915

2016
const Project = require('@lerna/project');
2117

2218
async function checkPackageLocks() {
2319
const project = new Project(process.cwd());
2420
const packages = await project.getPackages();
21+
const packageNames = packages.map(p => p.name);
2522
const rootPath = project.rootPath;
2623
const lockFiles = packages.map(p =>
2724
path.relative(rootPath, path.join(p.location, 'package-lock.json')),
@@ -46,6 +43,22 @@ async function checkPackageLocks() {
4643
console.error('\nRun the following command to fix the problems:');
4744
console.error('\n $ npm run update-package-locks\n');
4845
return false;
46+
47+
async function checkLockFile(lockFile) {
48+
const file = path.resolve(project.rootPath, lockFile);
49+
const found = await fs.exists(file);
50+
if (!found) return [];
51+
let data = {};
52+
try {
53+
data = require(file);
54+
} catch (err) {
55+
return [err.message];
56+
}
57+
// Find dependency module names
58+
const deps = Object.keys(data.dependencies || {});
59+
// Local packages should not be included
60+
return Object.keys(deps).filter(dep => packageNames.includes(dep));
61+
}
4962
}
5063

5164
if (require.main === module) {
@@ -57,12 +70,3 @@ if (require.main === module) {
5770
},
5871
);
5972
}
60-
61-
async function checkLockFile(lockFile) {
62-
const found = await exists(lockFile);
63-
if (!found) return [];
64-
const data = JSON.parse(await readFile(lockFile, 'utf-8'));
65-
return Object.keys(data.dependencies || []).filter(dep =>
66-
dep.startsWith('@loopback/'),
67-
);
68-
}

0 commit comments

Comments
 (0)