|
| 1 | +import * as cp from 'node:child_process' |
| 2 | +import * as fs from 'node:fs/promises' |
| 3 | +import * as path from 'node:path' |
| 4 | + |
| 5 | +const main = async () => { |
| 6 | + // It's best practice to include a shrinkwrap when shipping a CLI. npm has a bug that makes it |
| 7 | + // not ignore development dependencies in an installed package's shrinkwrap, though: |
| 8 | + // |
| 9 | + // https://github.com/npm/cli/issues/4323 |
| 10 | + // |
| 11 | + // Leaving development dependencies makes the CLI installation significantly larger and increases |
| 12 | + // the risk of platform-specific dependency installation issues. |
| 13 | + // eslint-disable-next-line no-restricted-properties |
| 14 | + const packageJSONPath = path.join(process.cwd(), 'package.json') |
| 15 | + const rawPackageJSON = await fs.readFile(packageJSONPath, 'utf8') |
| 16 | + |
| 17 | + try { |
| 18 | + // Remove dev dependencies from the package.json... |
| 19 | + const packageJSON = JSON.parse(rawPackageJSON) |
| 20 | + Reflect.deleteProperty(packageJSON, 'devDependencies') |
| 21 | + await fs.writeFile(packageJSONPath, JSON.stringify(packageJSON, null, 2)) |
| 22 | + |
| 23 | + // Prune out dev dependencies (this updates the `package-lock.json` lockfile) |
| 24 | + cp.spawnSync('npm', ['prune'], { stdio: 'inherit' }) |
| 25 | + |
| 26 | + // Convert `package-lock.json` lockfile to `npm-shrinkwrap.json` |
| 27 | + cp.spawnSync('npm', ['shrinkwrap'], { stdio: 'inherit' }) |
| 28 | + } finally { |
| 29 | + // Restore the original `package.json`. (This makes no functional difference in a publishing |
| 30 | + // environment, it's purely to minimize how destructive this script is.) |
| 31 | + await fs.writeFile(packageJSONPath, rawPackageJSON) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +await main() |
1 commit comments
github-actions[bot] commentedon Apr 1, 2025
📊 Benchmark results