update-flake-lock/dist/index.js.map
2024-06-04 09:19:35 -07:00

1 line
No EOL
11 KiB
Text

{"version":3,"sources":["../src/nix.ts","../src/template.ts","../src/index.ts"],"sourcesContent":["// Build the Nix args out of inputs from the Actions environment\nexport function makeNixCommandArgs(\n nixOptions: string[],\n flakeInputs: string[],\n commitMessage: string,\n): string[] {\n const flakeInputFlags = flakeInputs.flatMap((input) => [\n \"--update-input\",\n input,\n ]);\n\n const updateLockMechanism = flakeInputFlags.length === 0 ? \"update\" : \"lock\";\n\n return nixOptions\n .concat([\"flake\", updateLockMechanism])\n .concat(flakeInputFlags)\n .concat([\"--commit-lock-file\", \"--commit-lockfile-summary\", commitMessage]);\n}\n","import Handlebars from \"handlebars\";\n\nexport function renderPullRequestBody(\n template: string,\n dirs: string[],\n): string {\n const commaSeparated = dirs.join(\", \");\n const spaceSeparated = dirs.join(\" \");\n const dirsList = dirs.map((d: string) => `* ${d}`).join(\"\\n\");\n\n const tpl = Handlebars.compile(template);\n\n return tpl({\n // eslint-disable-next-line camelcase\n comma_separated_dirs: commaSeparated,\n // eslint-disable-next-line camelcase\n space_separated_dirs: spaceSeparated,\n // eslint-disable-next-line camelcase\n updated_dirs_list: dirsList,\n });\n}\n\nexport function renderCommitMessage(\n template: string,\n flakeDotLockDir: string,\n flakeDotLock: string,\n): string {\n return render(template, {\n // eslint-disable-next-line camelcase\n flake_dot_lock_dir: flakeDotLockDir,\n // eslint-disable-next-line camelcase\n flake_dot_lock: flakeDotLock,\n });\n}\n\nfunction render(template: string, inputs: Record<string, string>): string {\n const tpl = Handlebars.compile(template);\n return tpl(inputs);\n}\n","import { makeNixCommandArgs } from \"./nix.js\";\nimport { renderCommitMessage, renderPullRequestBody } from \"./template.js\";\nimport * as actionsCore from \"@actions/core\";\nimport * as actionsExec from \"@actions/exec\";\nimport { DetSysAction, inputs } from \"detsys-ts\";\nimport * as fs from \"fs\";\n\nconst DEFAULT_FLAKE_DIR = \".\";\n\nconst PR_BODY_OUTPUT_KEY = \"pr-body\";\n\nconst EVENT_EXECUTION_FAILURE = \"execution_failure\";\n\nclass UpdateFlakeLockAction extends DetSysAction {\n private commitMessage: string;\n private commitMessageTemplate: string;\n private prBody: string;\n private prBodyTemplate: string;\n private nixOptions: string[];\n private flakeInputs: string[];\n private pathToFlakeDir: string | null;\n private flakeDirsInput: string[] | null;\n private flakeDirs: string[];\n\n constructor() {\n super({\n name: \"update-flake-lock\",\n fetchStyle: \"universal\",\n requireNix: \"fail\",\n });\n\n this.commitMessage = inputs.getString(\"commit-msg\");\n this.commitMessageTemplate = inputs.getString(\"commit-msg-template\");\n this.prBody = inputs.getString(\"pr-body\");\n this.prBodyTemplate = inputs.getString(\"pr-body-template\");\n this.flakeInputs = inputs.getArrayOfStrings(\"inputs\", \"space\");\n this.nixOptions = inputs.getArrayOfStrings(\"nix-options\", \"space\");\n this.pathToFlakeDir = inputs.getStringOrNull(\"path-to-flake-dir\");\n this.flakeDirsInput = inputs.getArrayOfStringsOrNull(\"flake-dirs\", \"space\");\n\n this.validateInputs();\n\n if (this.flakeDirsInput !== null && this.flakeDirsInput.length > 0) {\n this.flakeDirs = this.flakeDirsInput;\n } else {\n this.flakeDirs = [this.pathToFlakeDir ?? DEFAULT_FLAKE_DIR];\n }\n }\n\n async main(): Promise<void> {\n for (const directory of this.flakeDirs) {\n await this.updateFlakeInDirectory(directory);\n }\n\n const prBody =\n this.prBody !== \"\"\n ? this.prBody\n : renderPullRequestBody(this.prBodyTemplate, this.flakeDirs);\n\n actionsCore.setOutput(PR_BODY_OUTPUT_KEY, prBody);\n }\n\n // No post phase\n async post(): Promise<void> {}\n\n private async updateFlakeInDirectory(flakeDir: string): Promise<void> {\n this.ensureDirectoryExists(flakeDir);\n this.ensureDirectoryIsFlake(flakeDir);\n\n actionsCore.debug(`Running flake lock update in directory \\`${flakeDir}\\``);\n\n const flakeDotLock = `${flakeDir}/flake.lock`;\n const commitMessage =\n this.commitMessage !== \"\"\n ? this.commitMessage\n : renderCommitMessage(\n this.commitMessageTemplate,\n flakeDir,\n flakeDotLock,\n );\n\n // Nix command of this form:\n // nix ${maybe nix options} flake ${\"update\" or \"lock\"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}\n // Example commands:\n // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n // nix flake update --commit-lock-file --commit-lockfile-summary \"updated flake.lock\"\n const nixCommandArgs: string[] = makeNixCommandArgs(\n this.nixOptions,\n this.flakeInputs,\n commitMessage,\n );\n\n actionsCore.debug(\n JSON.stringify({\n directory: flakeDir,\n options: this.nixOptions,\n inputs: this.flakeInputs,\n message: this.commitMessage,\n args: nixCommandArgs,\n }),\n );\n\n const execOptions: actionsExec.ExecOptions = {\n cwd: flakeDir,\n };\n\n const exitCode = await actionsExec.exec(\"nix\", nixCommandArgs, execOptions);\n\n if (exitCode !== 0) {\n this.recordEvent(EVENT_EXECUTION_FAILURE, {\n exitCode,\n });\n actionsCore.setFailed(\n `non-zero exit code of ${exitCode} detected while updating directory \\`${flakeDir}\\``,\n );\n } else {\n actionsCore.info(\n `flake.lock file in \\`${flakeDir}\\` was successfully updated`,\n );\n }\n }\n\n private validateInputs(): void {\n // Ensure that either `path-to-flake-dir` or `flake-dirs` is set to a meaningful value but not both\n if (\n this.flakeDirsInput !== null &&\n this.flakeDirsInput.length > 0 &&\n this.pathToFlakeDir !== null &&\n this.pathToFlakeDir !== \"\"\n ) {\n throw new Error(\n \"Both `path-to-flake-dir` and `flake-dirs` are set, whereas only one can be\",\n );\n }\n\n // Ensure that `flake-dirs` isn't an empty array if set\n if (this.flakeDirsInput !== null && this.flakeDirsInput.length === 0) {\n throw new Error(\n \"The `flake-dirs` input is set to an empty array; it must contain at least one directory\",\n );\n }\n\n // Ensure that both `flake-dirs` and `inputs` aren't set at the same time\n if (\n this.flakeDirsInput !== null &&\n this.flakeDirsInput.length > 0 &&\n this.flakeInputs.length > 0\n ) {\n throw new Error(\n `You've set both \\`flake-dirs\\` and \\`inputs\\` but you can only set one`,\n );\n }\n }\n\n private ensureDirectoryExists(flakeDir: string): void {\n actionsCore.debug(`Checking that flake directory \\`${flakeDir}\\` exists`);\n\n // Ensure the directory exists\n fs.access(flakeDir, fs.constants.F_OK, (err) => {\n if (err !== null) {\n throw new Error(`Directory \\`${flakeDir}\\` doesn't exist`);\n } else {\n actionsCore.debug(`Flake directory \\`${flakeDir}\\` exists`);\n }\n });\n }\n\n private ensureDirectoryIsFlake(flakeDir: string): void {\n const flakeDotNix = `${flakeDir}/flake.nix`;\n if (!fs.existsSync(flakeDotNix)) {\n throw new Error(\n `Directory \\`${flakeDir}\\` is not a valid flake as it doesn't contain a \\`flake.nix\\``,\n );\n } else {\n actionsCore.debug(`Directory \\`${flakeDir}\\` is a valid flake`);\n }\n }\n}\n\nfunction main(): void {\n new UpdateFlakeLockAction().execute();\n}\n\nmain();\n"],"mappings":";AACO,SAAS,mBACd,YACA,aACA,eACU;AACV,QAAM,kBAAkB,YAAY,QAAQ,CAAC,UAAU;AAAA,IACrD;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,sBAAsB,gBAAgB,WAAW,IAAI,WAAW;AAEtE,SAAO,WACJ,OAAO,CAAC,SAAS,mBAAmB,CAAC,EACrC,OAAO,eAAe,EACtB,OAAO,CAAC,sBAAsB,6BAA6B,aAAa,CAAC;AAC9E;;;ACjBA,OAAO,gBAAgB;AAEhB,SAAS,sBACd,UACA,MACQ;AACR,QAAM,iBAAiB,KAAK,KAAK,IAAI;AACrC,QAAM,iBAAiB,KAAK,KAAK,GAAG;AACpC,QAAM,WAAW,KAAK,IAAI,CAAC,MAAc,KAAK,CAAC,EAAE,EAAE,KAAK,IAAI;AAE5D,QAAM,MAAM,WAAW,QAAQ,QAAQ;AAEvC,SAAO,IAAI;AAAA;AAAA,IAET,sBAAsB;AAAA;AAAA,IAEtB,sBAAsB;AAAA;AAAA,IAEtB,mBAAmB;AAAA,EACrB,CAAC;AACH;AAEO,S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