@@ -22,11 +22,30 @@ const child_process = require('child_process');
2222 * Copies the package-lock.json file to the template folder and modifies its contents.
2323 */
2424function copyPackageLock ( ) {
25- console . log ( 'copy_package_lock: Copying package-lock.json to template folder...' ) ;
26- fs . copyFileSync (
27- 'package-lock.json' ,
28- path . join ( 'template' , 'package-lock.json' )
29- ) ;
25+ // Remove all kinvolk-headlamp-plugin*.tgz
26+ const oldTgzFiles = fs . readdirSync ( '.' ) . filter ( file => file . startsWith ( 'kinvolk-headlamp-plugin' ) && file . endsWith ( '.tgz' ) ) ;
27+ oldTgzFiles . forEach ( file => {
28+ console . log ( `copy_package_lock: Removing old package file ${ file } ` ) ;
29+ fs . rmSync ( file , { force : true } )
30+ } ) ;
31+
32+ // Build a .tgz package. "npm run build && npm pack"
33+ console . log ( 'copy_package_lock: Building kinvolk-headlamp-plugin package...' ) ;
34+ child_process . spawnSync ( 'npm' , [ 'run' , 'build' ] , {
35+ stdio : 'inherit' ,
36+ } ) ;
37+ child_process . spawnSync ( 'npm' , [ 'pack' ] , {
38+ stdio : 'inherit' ,
39+ } ) ;
40+
41+ // Get filename of new kinvolk-headlamp-plugin*.tgz
42+ const tgzFiles = fs . readdirSync ( '.' ) . filter ( file => file . startsWith ( 'kinvolk-headlamp-plugin' ) && file . endsWith ( '.tgz' ) ) ;
43+ if ( tgzFiles . length === 0 ) {
44+ console . error ( 'copy_package_lock: Error: No kinvolk-headlamp-plugin*.tgz file found after npm pack' ) ;
45+ process . exit ( 1 ) ;
46+ }
47+ const tgzFile = tgzFiles [ 0 ] ;
48+ console . log ( `copy_package_lock: Found package file ${ tgzFile } ` ) ;
3049
3150 // Make a tmp mypkgtmp with bin/headlamp-plugin.js create mypkgtmp
3251 // If mypkgtmp exists remove it first
@@ -38,9 +57,9 @@ function copyPackageLock() {
3857 stdio : 'inherit' ,
3958 } ) ;
4059
41- // Go into the folder and run "npm install"
42- console . log ( ' copy_package_lock: Installing dependencies in temporary folder to make sure everything is up to date ...' ) ;
43- child_process . spawnSync ( 'npm' , [ 'install' ] , {
60+ // npm i the .tgz into mypkgtmp
61+ console . log ( ` copy_package_lock: Installing package ${ tgzFile } into temporary folder...` ) ;
62+ child_process . spawnSync ( 'npm' , [ 'install' , path . join ( '..' , tgzFile ) ] , {
4463 cwd : packageName ,
4564 stdio : 'inherit' ,
4665 } ) ;
@@ -74,6 +93,29 @@ function copyPackageLock() {
7493 let packageLockContent = fs . readFileSync ( 'template/package-lock.json' , 'utf8' ) ;
7594 // Use a replacer function so the replacement string is inserted literally as $${name}
7695 packageLockContent = packageLockContent . replace ( new RegExp ( packageName , 'g' ) , ( ) => '$${name}' ) ;
96+
97+ // replace in template/package-lock.json "@kinvolk/headlamp-plugin": "file:../kinvolk-headlamp-plugin-<version>.tgz"
98+ // with the version field of from ./package.json with a ^ in front
99+ const mainPackageJson = JSON . parse ( fs . readFileSync ( 'package.json' , 'utf8' ) ) ;
100+ const pluginVersion = mainPackageJson . version ;
101+ const tgzPattern = new RegExp ( `"@kinvolk/headlamp-plugin": "file:\\.\\./${ tgzFile . replace ( / \. / g, '\\.' ) } "` , 'g' ) ;
102+ const replacementString = `"@kinvolk/headlamp-plugin": "^${ pluginVersion } "` ;
103+ packageLockContent = packageLockContent . replace ( tgzPattern , replacementString ) ;
104+
105+ // Also replace the resolved fields for @kinvolk /headlamp-plugin in
106+ // template/package-lock.json to match the version from main package.json
107+ //
108+ // Example of the change:
109+ // "packages": {
110+ // ...
111+ // "node_modules/@kinvolk/headlamp-plugin": {
112+ // ...
113+ // - "resolved": "file:../kinvolk-headlamp-plugin-0.13.0-alpha.13.tgz",
114+ // + "resolved": "https://registry.npmjs.org/@kinvolk/headlamp-plugin/-/headlamp-plugin-0.13.0-alpha.13.tgz",
115+ const resolvedPattern = new RegExp ( `"resolved": "file:\\.\\./${ tgzFile . replace ( / \. / g, '\\.' ) } "` , 'g' ) ;
116+ const resolvedReplacement = `"resolved": "https://registry.npmjs.org/@kinvolk/headlamp-plugin/-/headlamp-plugin-${ pluginVersion } .tgz"` ;
117+ packageLockContent = packageLockContent . replace ( resolvedPattern , resolvedReplacement ) ;
118+
77119 fs . writeFileSync ( 'template/package-lock.json' , packageLockContent ) ;
78120 console . log ( 'copy_package_lock: Updated template/package-lock.json' ) ;
79121}
0 commit comments