Skip to content

Commit 5d06fa4

Browse files
committed
headlamp-plugin: Fix template/package-lock.json handling
There were some bugs in this, and now it generates a valid template package-lock.json. Except there is a circular dependency on the integrity field. So we make sure we get the integrity field from the registry and update the package-lock.json before npm ci is run.
1 parent 4a7d602 commit 5d06fa4

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

plugins/headlamp-plugin/bin/headlamp-plugin.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@ function create(name, link, noInstall) {
107107
console.log('Skipping dependency installation...');
108108
} else {
109109
console.log('Installing dependencies...');
110+
111+
// In the package-lock.json we need to update the integrity field of the
112+
// @kinvolk/headlamp-plugin package to match the integrity field on npmjs
113+
// registry for this version.
114+
// This is because when creating the template package-lock.json there is
115+
// a circular dependency where we need the integrity field inside it...
116+
// which changes the sha512 integrity field.
117+
// So we need to download this field from the registry.
118+
const npmJsPkgResponse = child_process.execFileSync(
119+
'npm',
120+
['view', `@kinvolk/headlamp-plugin@${headlampPluginPkg.version}`, 'dist', '--json'],
121+
{ encoding: 'utf8' }
122+
);
123+
const npmJsPkg = JSON.parse(npmJsPkgResponse);
124+
const npmJsIntegrity = npmJsPkg.integrity;
125+
126+
// Now replace integrity in the package-lock.json to match npmjs registry integrity.
127+
// "node_modules/@kinvolk/headlamp-plugin": {
128+
// "version": "0.13.0-alpha.13",
129+
// "resolved": "https://registry.npmjs.org/@kinvolk/headlamp-plugin/-/headlamp-plugin-0.13.0-alpha.13.tgz",
130+
// "integrity": "sha512-iDIo0afLpITCRNxV7j8whPDFNPLPzeCXIOah0+q1VtCcRrwiCerl5LCT+vnufU98C/HgEIeK6aLMIdqp53Mv7Q==",
131+
let packageLockContent = fs.readFileSync(packageLockPath, 'utf8');
132+
const integrityPattern = new RegExp(
133+
`("node_modules/@kinvolk/headlamp-plugin": \\{[\\s\\S]*?"integrity": ")[^"]+(")`,
134+
'g'
135+
);
136+
packageLockContent = packageLockContent.replace(integrityPattern, `$1${npmJsIntegrity}$2`);
137+
fs.writeFileSync(packageLockPath, packageLockContent);
138+
110139
try {
111140
child_process.execSync('npm ci', {
112141
stdio: 'inherit',

plugins/headlamp-plugin/scripts/copy-package-lock.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
2424
function 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

Comments
 (0)