|
| 1 | +"use strict"; |
| 2 | +// This file should be committed to your repository! It wraps Nx and ensures |
| 3 | +// that your local installation matches nx.json. |
| 4 | +// See: https://nx.dev/recipes/installation/install-non-javascript for more info. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 10 | +const fs = require('fs'); |
| 11 | +const path = require('path'); |
| 12 | +const cp = require('child_process'); |
| 13 | +const installationPath = path.join(__dirname, 'installation', 'package.json'); |
| 14 | +function matchesCurrentNxInstall(currentInstallation, nxJsonInstallation) { |
| 15 | + if (!currentInstallation.devDependencies || |
| 16 | + !Object.keys(currentInstallation.devDependencies).length) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + try { |
| 20 | + if (currentInstallation.devDependencies['nx'] !== |
| 21 | + nxJsonInstallation.version || |
| 22 | + require(path.join(path.dirname(installationPath), 'node_modules', 'nx', 'package.json')).version !== nxJsonInstallation.version) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + for (const [plugin, desiredVersion] of Object.entries(nxJsonInstallation.plugins || {})) { |
| 26 | + if (currentInstallation.devDependencies[plugin] !== desiredVersion) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + } |
| 30 | + return true; |
| 31 | + } |
| 32 | + catch { |
| 33 | + return false; |
| 34 | + } |
| 35 | +} |
| 36 | +function ensureDir(p) { |
| 37 | + if (!fs.existsSync(p)) { |
| 38 | + fs.mkdirSync(p, { recursive: true }); |
| 39 | + } |
| 40 | +} |
| 41 | +function getCurrentInstallation() { |
| 42 | + try { |
| 43 | + return require(installationPath); |
| 44 | + } |
| 45 | + catch { |
| 46 | + return { |
| 47 | + name: 'nx-installation', |
| 48 | + version: '0.0.0', |
| 49 | + devDependencies: {}, |
| 50 | + }; |
| 51 | + } |
| 52 | +} |
| 53 | +function performInstallation(currentInstallation, nxJson) { |
| 54 | + fs.writeFileSync(installationPath, JSON.stringify({ |
| 55 | + name: 'nx-installation', |
| 56 | + devDependencies: { |
| 57 | + nx: nxJson.installation.version, |
| 58 | + ...nxJson.installation.plugins, |
| 59 | + }, |
| 60 | + })); |
| 61 | + try { |
| 62 | + cp.execSync('npm i', { |
| 63 | + cwd: path.dirname(installationPath), |
| 64 | + stdio: 'inherit', |
| 65 | + }); |
| 66 | + } |
| 67 | + catch (e) { |
| 68 | + // revert possible changes to the current installation |
| 69 | + fs.writeFileSync(installationPath, JSON.stringify(currentInstallation)); |
| 70 | + // rethrow |
| 71 | + throw e; |
| 72 | + } |
| 73 | +} |
| 74 | +function ensureUpToDateInstallation() { |
| 75 | + const nxJsonPath = path.join(__dirname, '..', 'nx.json'); |
| 76 | + let nxJson; |
| 77 | + try { |
| 78 | + nxJson = require(nxJsonPath); |
| 79 | + if (!nxJson.installation) { |
| 80 | + console.error('[NX]: The "installation" entry in the "nx.json" file is required when running the nx wrapper. See https://nx.dev/recipes/installation/install-non-javascript'); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + } |
| 84 | + catch { |
| 85 | + console.error('[NX]: The "nx.json" file is required when running the nx wrapper. See https://nx.dev/recipes/installation/install-non-javascript'); |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | + try { |
| 89 | + ensureDir(path.join(__dirname, 'installation')); |
| 90 | + const currentInstallation = getCurrentInstallation(); |
| 91 | + if (!matchesCurrentNxInstall(currentInstallation, nxJson.installation)) { |
| 92 | + performInstallation(currentInstallation, nxJson); |
| 93 | + } |
| 94 | + } |
| 95 | + catch (e) { |
| 96 | + const messageLines = [ |
| 97 | + '[NX]: Nx wrapper failed to synchronize installation.', |
| 98 | + ]; |
| 99 | + if (e instanceof Error) { |
| 100 | + messageLines.push(''); |
| 101 | + messageLines.push(e.message); |
| 102 | + messageLines.push(e.stack); |
| 103 | + } |
| 104 | + else { |
| 105 | + messageLines.push(e.toString()); |
| 106 | + } |
| 107 | + console.error(messageLines.join('\n')); |
| 108 | + process.exit(1); |
| 109 | + } |
| 110 | +} |
| 111 | +if (!process.env.NX_WRAPPER_SKIP_INSTALL) { |
| 112 | + ensureUpToDateInstallation(); |
| 113 | +} |
| 114 | + |
| 115 | +require('./installation/node_modules/nx/bin/nx'); |
0 commit comments