Skip to content

Commit c8e6cdf

Browse files
authored
Merge pull request #3957 from illume/hlp-create-ci
headlamp-plugin: Fix create command to use package-lock.json and npm ci
2 parents 99b57fc + fe7369c commit c8e6cdf

File tree

5 files changed

+18153
-27
lines changed

5 files changed

+18153
-27
lines changed

plugins/headlamp-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ v0.6.0-alpha.1
167167
To set the "alpha" tag when publishing do the following.
168168

169169
```bash
170-
npm run build && npm pack
170+
npm run build && npm run copy-package-lock && npm pack
171171
npm publish kinvolk-headlamp-plugin-0.6.0-alpha.0.tgz --tag alpha
172172
```
173173

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ const vitePromise = import('vite');
4242
* Creates a new plugin folder.
4343
*
4444
* Copies the files within template, and modifies a couple.
45-
* Then runs npm install inside of the folder.
45+
* Then runs "npm ci" inside of the folder.
4646
*
4747
* @param {string} name - name of package and output folder.
4848
* @param {boolean} link - if we link @kinvolk/headlamp-plugin for testing
49+
* @param {boolean} noInstall - if we skip installing with "npm ci"
4950
* @returns {0 | 1 | 2 | 3} Exit code, where 0 is success, 1, 2, and 3 are failures.
5051
*/
51-
function create(name, link) {
52+
function create(name, link, noInstall) {
5253
const dstFolder = name;
5354
const templateFolder = path.resolve(__dirname, '..', 'template');
5455
const indexPath = path.join(dstFolder, 'src', 'index.tsx');
5556
const packagePath = path.join(dstFolder, 'package.json');
57+
const packageLockPath = path.join(dstFolder, 'package-lock.json');
5658
const readmePath = path.join(dstFolder, 'README.md');
5759

5860
if (fs.existsSync(name)) {
@@ -89,6 +91,7 @@ function create(name, link) {
8991
}
9092

9193
replaceFileVariables(packagePath);
94+
replaceFileVariables(packageLockPath);
9295
replaceFileVariables(indexPath);
9396
replaceFileVariables(readmePath);
9497

@@ -100,28 +103,29 @@ function create(name, link) {
100103
});
101104
}
102105

103-
console.log('Installing dependencies...');
104-
// Run npm install.
105-
try {
106-
child_process.execSync('npm install', {
107-
stdio: 'inherit',
108-
cwd: dstFolder,
109-
encoding: 'utf8',
110-
});
111-
} catch (e) {
112-
console.error(
113-
`Problem running npm install inside of "${dstFolder}" abs: "${resolve(dstFolder)}"`
114-
);
115-
return 3;
116-
}
106+
if (noInstall) {
107+
console.log('Skipping dependency installation...');
108+
} else {
109+
console.log('Installing dependencies...');
110+
try {
111+
child_process.execSync('npm ci', {
112+
stdio: 'inherit',
113+
cwd: dstFolder,
114+
encoding: 'utf8',
115+
});
116+
} catch (e) {
117+
console.error(`Problem running npm ci inside of "${dstFolder}" abs: "${resolve(dstFolder)}"`);
118+
return 3;
119+
}
117120

118-
// This can be used to make testing locally easier.
119-
if (link) {
120-
// Seems to require linking again with npm 7+
121-
console.log('Linking @kinvolk/headlamp-plugin');
122-
child_process.spawnSync('npm', ['link', '@kinvolk/headlamp-plugin'], {
123-
cwd: dstFolder,
124-
});
121+
// This can be used to make testing locally easier.
122+
if (link) {
123+
// Seems to require linking again with npm 7+
124+
console.log('Linking @kinvolk/headlamp-plugin');
125+
child_process.spawnSync('npm', ['link', '@kinvolk/headlamp-plugin'], {
126+
cwd: dstFolder,
127+
});
128+
}
125129
}
126130

127131
console.log(`"${dstFolder}" created.`);
@@ -1642,11 +1646,15 @@ yargs(process.argv.slice(2))
16421646
describe:
16431647
'For development of headlamp-plugin itself, so it uses npm link @kinvolk/headlamp-plugin.',
16441648
type: 'boolean',
1649+
})
1650+
.option('noinstall', {
1651+
describe: 'Skip installing dependencies with npm ci',
1652+
type: 'boolean',
16451653
});
16461654
},
16471655
argv => {
16481656
// @ts-ignore
1649-
process.exitCode = create(argv.name, argv.link);
1657+
process.exitCode = create(argv.name, argv.link, argv.noinstall);
16501658
}
16511659
)
16521660
.command(

plugins/headlamp-plugin/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"scripts": {
99
"prepack": "(node -e \"if (! require('fs').existsSync('./lib/components')) {process.exit(1)} \" || (echo 'lib dir is empty. Remember to run `npm run build` before packing' && exit 1))",
1010
"build": "npx shx rm -rf lib types lib/assets lib/components lib/helpers lib/i18n lib/lib lib/plugin lib/redux lib/resources && npx shx cp -r ../../frontend/src/assets ../../frontend/src/components ../../frontend/src/helpers ../../frontend/src/stateless ../../frontend/src/i18n ../../frontend/src/lib ../../frontend/src/plugin ../../frontend/src/redux ../../frontend/src/resources src/ && tsc --build ./tsconfig.json && npx shx cp -r src/additional.d.ts lib/ && npx shx cp -r ../../frontend/src/assets lib/ && npx shx cp -r ../../frontend/src/resources lib/ && npx shx cp -r ../../frontend/src/i18n/locales lib/i18n && node scripts/copy-static-assets.js",
11-
"update-dependencies": "node dependencies-sync.js update && npm install",
12-
"check-dependencies": "node dependencies-sync.js check"
11+
"update-dependencies": "node dependencies-sync.js update && npm install && node scripts/copy-package-lock.js",
12+
"check-dependencies": "node dependencies-sync.js check",
13+
"copy-package-lock": "node scripts/copy-package-lock.js"
1314
},
1415
"prettier": "@headlamp-k8s/eslint-config/prettier-config",
1516
"dependencies": {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2025 The Kubernetes Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const fs = require('fs');
18+
const path = require('path');
19+
const child_process = require('child_process');
20+
21+
/**
22+
* Copies the package-lock.json file to the template folder and modifies its contents.
23+
*/
24+
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+
);
30+
31+
// Make a tmp mypkgtmp with bin/headlamp-plugin.js create mypkgtmp
32+
// If mypkgtmp exists remove it first
33+
const packageName = 'mypkgtmp';
34+
if (fs.existsSync(packageName)) {
35+
fs.rmSync(packageName, { recursive: true, force: true });
36+
}
37+
child_process.spawnSync('node', ['bin/headlamp-plugin.js', 'create', packageName, '--noinstall'], {
38+
stdio: 'inherit',
39+
});
40+
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'], {
44+
cwd: packageName,
45+
stdio: 'inherit',
46+
});
47+
48+
// Remove the node_modules inside packageName, and run npm install again.
49+
// This is necessary to ensure that the package-lock.json file is stabalized.
50+
console.log('copy_package_lock: Removing node_modules and reinstalling to stabalize packages...');
51+
fs.rmSync(path.join(packageName, 'node_modules'), { recursive: true, force: true });
52+
child_process.spawnSync('npm', ['install'], {
53+
cwd: packageName,
54+
stdio: 'inherit',
55+
});
56+
57+
// Remove the node_modules and run "npm ci" to confirm it's ok.
58+
console.log('copy_package_lock: Removing node_modules and running "npm ci" to confirm it is ok...');
59+
fs.rmSync(path.join(packageName, 'node_modules'), { recursive: true, force: true });
60+
child_process.spawnSync('npm', ['ci'], {
61+
cwd: packageName,
62+
stdio: 'inherit',
63+
});
64+
65+
// copy mypkgtmp/package-lock.json into template/package-lock.json
66+
fs.copyFileSync(`${packageName}/package-lock.json`, 'template/package-lock.json');
67+
68+
// Clean up temporary package
69+
console.log('copy_package_lock: Cleaning up temporary package...');
70+
fs.rmSync(packageName, { recursive: true, force: true });
71+
72+
// replace in file template/package-lock.json packageName with $${name}
73+
// just do a search / replace in the file
74+
let packageLockContent = fs.readFileSync('template/package-lock.json', 'utf8');
75+
// Use a replacer function so the replacement string is inserted literally as $${name}
76+
packageLockContent = packageLockContent.replace(new RegExp(packageName, 'g'), () => '$${name}');
77+
fs.writeFileSync('template/package-lock.json', packageLockContent);
78+
console.log('copy_package_lock: Updated template/package-lock.json');
79+
}
80+
81+
copyPackageLock();

0 commit comments

Comments
 (0)