|
| 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