1111'use strict' ;
1212
1313const 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
2016const Project = require ( '@lerna/project' ) ;
2117
2218async 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
5164if ( 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