Philipp Schrader | 272e07b | 2024-03-29 17:26:02 -0700 | [diff] [blame^] | 1 | // This script acts as an "npm" binary for the foxglove-extension binary. We |
| 2 | // don't actually care to do any npm things here. For some reason |
| 3 | // foxglove-extension defers to npm to execute the various build stages. So |
| 4 | // all this script does is execute those various build stages. The stages are |
| 5 | // defined in the package.json file. |
| 6 | |
| 7 | const fs = require('fs'); |
| 8 | const { execSync } = require('child_process'); |
| 9 | const path = require('path'); |
| 10 | |
| 11 | // Read the package.json file. |
| 12 | function readPackageJson() { |
| 13 | try { |
| 14 | const packageJson = fs.readFileSync('package.json', 'utf8'); |
| 15 | return JSON.parse(packageJson); |
| 16 | } catch (error) { |
| 17 | console.error('Error reading package.json:', error); |
| 18 | process.exit(1); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Execute the named script specified in package.json. |
| 23 | function executeScript(scriptName) { |
| 24 | const packageJson = readPackageJson(); |
| 25 | const scripts = packageJson.scripts || {}; |
| 26 | |
| 27 | if (!scripts[scriptName]) { |
| 28 | console.error(`Script '${scriptName}' not found in package.json`); |
| 29 | process.exit(1); |
| 30 | } |
| 31 | |
| 32 | // We cannot execute the `foxglove-extension` binary as-is (at least not |
| 33 | // without setting up a custom PATH). So we instead point at the |
| 34 | // Bazel-generated wrapper script for that binary. |
| 35 | const scriptParts = scripts[scriptName].split(' '); |
| 36 | const bin = scriptParts[0]; |
| 37 | if (bin !== 'foxglove-extension') { |
| 38 | console.error(`Cannot support commands other than 'foxglove-extension'. Got: ${bin}`); |
| 39 | process.exit(1); |
| 40 | } |
| 41 | scriptParts[0] = path.join(__dirname, 'foxglove_extension.sh'); |
| 42 | |
| 43 | // Execute the `foxglove-extension` command specified in the script. |
| 44 | try { |
| 45 | console.log(`Executing script '${scriptName}'...`); |
| 46 | execSync(scriptParts.join(' '), { stdio: 'inherit' }); |
| 47 | } catch (error) { |
| 48 | console.error(`Error executing script '${scriptName}':`, error); |
| 49 | process.exit(1); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | function main() { |
| 54 | // Validate the input arguments. |
| 55 | if (process.argv.length !== 4) { |
| 56 | console.error('Usage: node foxglove_extension_wrapper_npm.js <scriptName>'); |
| 57 | process.exit(1); |
| 58 | } |
| 59 | if (process.argv[2] !== "run") { |
| 60 | console.error(`Cannot support commands other than 'run'. Got: ${process.argv[2]}`); |
| 61 | process.exit(1); |
| 62 | } |
| 63 | |
| 64 | // Run the script specified in the package.json file. |
| 65 | const scriptName = process.argv[3]; |
| 66 | executeScript(scriptName); |
| 67 | } |
| 68 | |
| 69 | main(); |