|
| 1 | +#!/usr/bin/env node |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const process = require('process'); |
| 5 | + |
| 6 | +const [inputPath, outputPath] = process.argv.slice(2); |
| 7 | + |
| 8 | +class ExitError extends Error { |
| 9 | + code; |
| 10 | + constructor(code, message) { |
| 11 | + super(`Error: ${message}`); |
| 12 | + this.name = ExitError.name; |
| 13 | + this.code = code; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +try { |
| 18 | + if (!inputPath) throw new ExitError(1, 'Missing package.json input path'); |
| 19 | + const inputPkgPath = |
| 20 | + path.basename(inputPath) === 'package.json' ? inputPath : path.join(inputPath, 'package.json'); |
| 21 | + const pkgSource = (() => { |
| 22 | + try { |
| 23 | + return fs.readFileSync(inputPkgPath, 'utf-8'); |
| 24 | + } catch (error) { |
| 25 | + throw new ExitError(1, `Failed to read package.json: ${inputPkgPath} (${error.code})\n`); |
| 26 | + } |
| 27 | + })(); |
| 28 | + const pkg = (() => { |
| 29 | + try { |
| 30 | + return JSON.parse(pkgSource); |
| 31 | + } catch { |
| 32 | + throw new ExitError(1, `Invalid package.json contents: ${inputPkgPath}\n`); |
| 33 | + } |
| 34 | + })(); |
| 35 | + const { |
| 36 | + name, |
| 37 | + version, |
| 38 | + license, |
| 39 | + description, |
| 40 | + author, |
| 41 | + repository, |
| 42 | + keywords, |
| 43 | + dependencies, |
| 44 | + pkg: overrides, |
| 45 | + } = pkg; |
| 46 | + const json = `${JSON.stringify( |
| 47 | + { |
| 48 | + name, |
| 49 | + version, |
| 50 | + license, |
| 51 | + description, |
| 52 | + author, |
| 53 | + repository, |
| 54 | + keywords, |
| 55 | + ...overrides, |
| 56 | + dependencies: dependencies |
| 57 | + ? // This assumes all workspace packages share the same version number |
| 58 | + fixWorkspaceDependencyVersions(dependencies, version) |
| 59 | + : undefined, |
| 60 | + }, |
| 61 | + null, |
| 62 | + 2, |
| 63 | + )}\n`; |
| 64 | + if (outputPath) { |
| 65 | + try { |
| 66 | + fs.writeFileSync(outputPath, json); |
| 67 | + exit(0); |
| 68 | + } catch (error) { |
| 69 | + throw new ExitError(1, `Failed to write output package.json: ${outputPath} (${error.code})`); |
| 70 | + } |
| 71 | + } else { |
| 72 | + process.stdout.write(json, (error) => { |
| 73 | + if (error) { |
| 74 | + exit(1, `Failed to write output JSON: (${error.code})`); |
| 75 | + } else { |
| 76 | + exit(0); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | +} catch (error) { |
| 81 | + if (error instanceof ExitError) { |
| 82 | + exit(error.code, error.message); |
| 83 | + } else { |
| 84 | + throw error; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function fixWorkspaceDependencyVersions(dependencies, workspaceVersion) { |
| 89 | + return Object.fromEntries( |
| 90 | + Object.entries(dependencies).map((dependency) => { |
| 91 | + const [name, version] = dependency; |
| 92 | + if (version === 'workspace:*') return [name, workspaceVersion]; |
| 93 | + return dependency; |
| 94 | + }), |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +function exit(code, message) { |
| 99 | + if (typeof message === 'string') process.stderr.write(`${message}\n`); |
| 100 | + process.exit(code); |
| 101 | +} |
0 commit comments