Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions plugins/headlamp-plugin/bin/headlamp-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,67 @@ function create(name, link, noInstall) {
if (noInstall) {
console.log('Skipping dependency installation...');
} else {
console.log('Installing dependencies...');
console.log('Installing...');

// In the package-lock.json we try to update the integrity field of the
// @kinvolk/headlamp-plugin package to match the integrity field on npmjs
// registry for this version. If that fails (e.g. version not published yet),
// we fall back to running `npm install` instead of `npm ci` and skip the update.
let useNpmCi = false;
try {
child_process.execSync('npm ci', {
stdio: 'inherit',
cwd: dstFolder,
encoding: 'utf8',
});
const npmJsPkgResponse = child_process.execFileSync(
'npm',
['view', `@kinvolk/headlamp-plugin@${headlampPluginPkg.version}`, 'dist', '--json'],
{ encoding: 'utf8' }
);
const npmJsPkg = JSON.parse(npmJsPkgResponse);
const npmJsIntegrity = npmJsPkg.integrity;

// Now replace integrity in the package-lock.json to match npmjs registry integrity.
// "node_modules/@kinvolk/headlamp-plugin": {
// "version": "0.13.0-alpha.13",
// "resolved": "...",
// "integrity": "sha512-..."
const packageLockContent = fs.readFileSync(packageLockPath, 'utf8');
const integrityPattern = new RegExp(
`("node_modules/@kinvolk/headlamp-plugin": \\{[\\s\\S]*?"integrity": ")[^"]+(")`,
'g'
);
const packageLockContentNew = packageLockContent.replace(
integrityPattern,
`$1${npmJsIntegrity}$2`
);
if (packageLockContent !== packageLockContentNew) {
fs.writeFileSync(packageLockPath, packageLockContentNew);
}

useNpmCi = true;
} catch (e) {
console.error(`Problem running npm ci inside of "${dstFolder}" abs: "${resolve(dstFolder)}"`);
// no warning, we just fall back to npm install
}

try {
if (useNpmCi) {
console.log('Running npm ci...');
child_process.execSync('npm ci', {
stdio: 'inherit',
cwd: dstFolder,
encoding: 'utf8',
});
} else {
console.log('Running npm install...');
child_process.execSync('npm install', {
stdio: 'inherit',
cwd: dstFolder,
encoding: 'utf8',
});
}
} catch (e) {
console.error(
`Problem running ${
useNpmCi ? 'npm ci' : 'npm install'
} inside of "${dstFolder}" abs: "${resolve(dstFolder)}"`
);
return 3;
}

Expand Down
58 changes: 50 additions & 8 deletions plugins/headlamp-plugin/scripts/copy-package-lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,30 @@ const child_process = require('child_process');
* Copies the package-lock.json file to the template folder and modifies its contents.
*/
function copyPackageLock() {
console.log('copy_package_lock: Copying package-lock.json to template folder...');
fs.copyFileSync(
'package-lock.json',
path.join('template', 'package-lock.json')
);
// Remove all kinvolk-headlamp-plugin*.tgz
const oldTgzFiles = fs.readdirSync('.').filter(file => file.startsWith('kinvolk-headlamp-plugin') && file.endsWith('.tgz'));
oldTgzFiles.forEach(file => {
console.log(`copy_package_lock: Removing old package file ${file}`);
fs.rmSync(file, { force: true })
});

// Build a .tgz package. "npm run build && npm pack"
console.log('copy_package_lock: Building kinvolk-headlamp-plugin package...');
child_process.spawnSync('npm', ['run', 'build'], {
stdio: 'inherit',
});
child_process.spawnSync('npm', ['pack'], {
stdio: 'inherit',
});

// Get filename of new kinvolk-headlamp-plugin*.tgz
const tgzFiles = fs.readdirSync('.').filter(file => file.startsWith('kinvolk-headlamp-plugin') && file.endsWith('.tgz'));
if (tgzFiles.length === 0) {
console.error('copy_package_lock: Error: No kinvolk-headlamp-plugin*.tgz file found after npm pack');
process.exit(1);
}
const tgzFile = tgzFiles[0];
console.log(`copy_package_lock: Found package file ${tgzFile}`);

// Make a tmp mypkgtmp with bin/headlamp-plugin.js create mypkgtmp
// If mypkgtmp exists remove it first
Expand All @@ -38,9 +57,9 @@ function copyPackageLock() {
stdio: 'inherit',
});

// Go into the folder and run "npm install"
console.log('copy_package_lock: Installing dependencies in temporary folder to make sure everything is up to date...');
child_process.spawnSync('npm', ['install'], {
// npm i the .tgz into mypkgtmp
console.log(`copy_package_lock: Installing package ${tgzFile} into temporary folder...`);
child_process.spawnSync('npm', ['install', path.join('..', tgzFile)], {
cwd: packageName,
stdio: 'inherit',
});
Expand Down Expand Up @@ -74,6 +93,29 @@ function copyPackageLock() {
let packageLockContent = fs.readFileSync('template/package-lock.json', 'utf8');
// Use a replacer function so the replacement string is inserted literally as $${name}
packageLockContent = packageLockContent.replace(new RegExp(packageName, 'g'), () => '$${name}');

// replace in template/package-lock.json "@kinvolk/headlamp-plugin": "file:../kinvolk-headlamp-plugin-<version>.tgz"
// with the version field of from ./package.json with a ^ in front
const mainPackageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const pluginVersion = mainPackageJson.version;
const tgzPattern = new RegExp(`"@kinvolk/headlamp-plugin": "file:\\.\\./${tgzFile.replace(/\./g, '\\.')}"`, 'g');
const replacementString = `"@kinvolk/headlamp-plugin": "^${pluginVersion}"`;
packageLockContent = packageLockContent.replace(tgzPattern, replacementString);

// Also replace the resolved fields for @kinvolk/headlamp-plugin in
// template/package-lock.json to match the version from main package.json
//
// Example of the change:
// "packages": {
// ...
// "node_modules/@kinvolk/headlamp-plugin": {
// ...
// - "resolved": "file:../kinvolk-headlamp-plugin-0.13.0-alpha.13.tgz",
// + "resolved": "https://registry.npmjs.org/@kinvolk/headlamp-plugin/-/headlamp-plugin-0.13.0-alpha.13.tgz",
const resolvedPattern = new RegExp(`"resolved": "file:\\.\\./${tgzFile.replace(/\./g, '\\.')}"`, 'g');
const resolvedReplacement = `"resolved": "https://registry.npmjs.org/@kinvolk/headlamp-plugin/-/headlamp-plugin-${pluginVersion}.tgz"`;
packageLockContent = packageLockContent.replace(resolvedPattern, resolvedReplacement);

fs.writeFileSync('template/package-lock.json', packageLockContent);
console.log('copy_package_lock: Updated template/package-lock.json');
}
Expand Down
Loading