| 1 | import fs from "fs"; |
| 2 | import { Command, Option } from "commander"; |
| 3 | import { merge, presets, PresetName } from "@fig/autocomplete-merge"; |
| 4 | |
| 5 | async function runProgram( |
| 6 | oldSpecPath: string, |
| 7 | newSpecPath: string, |
| 8 | options: { |
| 9 | ignoreProps: string[]; |
| 10 | ignoreCommandProps: string[]; |
| 11 | ignoreOptionProps: string[]; |
| 12 | ignoreArgProps: string[]; |
| 13 | newFile: string; |
| 14 | preset: PresetName; |
| 15 | } |
| 16 | ) { |
| 17 | const { |
| 18 | ignoreProps = [], |
| 19 | ignoreCommandProps = [], |
| 20 | ignoreOptionProps = [], |
| 21 | ignoreArgProps = [], |
| 22 | newFile: updatedSpecPath, |
| 23 | preset, |
| 24 | } = options; |
| 25 | |
| 26 | const output = await merge( |
| 27 | fs.readFileSync(oldSpecPath, { encoding: "utf8" }), |
| 28 | fs.readFileSync(newSpecPath, { encoding: "utf8" }), |
| 29 | { |
| 30 | preset, |
| 31 | ignore: { |
| 32 | commonProps: ignoreProps, |
| 33 | commandProps: ignoreCommandProps, |
| 34 | optionProps: ignoreOptionProps, |
| 35 | argProps: ignoreArgProps, |
| 36 | }, |
| 37 | } |
| 38 | ); |
| 39 | |
| 40 | fs.writeFileSync(updatedSpecPath || oldSpecPath, output); |
| 41 | } |
| 42 | |
| 43 | const program = new Command("merge") |
| 44 | .arguments("<oldspec> <newspec>") |
| 45 | .description("deep merge new spec into old spec") |
| 46 | .option("-n, --new-file <path>", "Create a new spec file instead of updating the old one") |
| 47 | .option("-i, --ignore-props <props>", "The props that should always be overridden.", (value) => |
| 48 | value.split(",") |
| 49 | ) |
| 50 | .option( |
| 51 | "--ignore-command-props <props>", |
| 52 | "The command props that should always be overridden.", |
| 53 | (value) => value.split(",") |
| 54 | ) |
| 55 | .option( |
| 56 | "--ignore-option-props <props>", |
| 57 | "The option props that should always be overridden.", |
| 58 | (value) => value.split(",") |
| 59 | ) |
| 60 | .option( |
| 61 | "--ignore-arg-props <props>", |
| 62 | "The arg props that should always be overridden.", |
| 63 | (value) => value.split(",") |
| 64 | ); |
| 65 | |
| 66 | const presetOption = new Option("-p, --preset <name>", "Use a preset").choices( |
| 67 | Object.keys(presets) |
| 68 | ); |
| 69 | program.addOption(presetOption); |
| 70 | program.action(runProgram); |
| 71 | |
| 72 | export default program; |