From 539b7a6481ecbe6530b7cb3714ea7fe658423299 Mon Sep 17 00:00:00 2001
From: Luc Perkins <lucperkins@gmail.com>
Date: Fri, 26 Apr 2024 12:10:07 -0300
Subject: [PATCH] Remove Bash script and do more TS streamlining

---
 package.json         |  1 -
 pnpm-lock.yaml       |  5 ++---
 src/index.ts         | 34 ++++++++++++++++++----------------
 update-flake-lock.sh | 23 -----------------------
 4 files changed, 20 insertions(+), 43 deletions(-)
 delete mode 100755 update-flake-lock.sh

diff --git a/package.json b/package.json
index 2f9eb29..559d996 100644
--- a/package.json
+++ b/package.json
@@ -30,7 +30,6 @@
   },
   "devDependencies": {
     "@trivago/prettier-plugin-sort-imports": "^4.3.0",
-    "@types/node": "^20.12.7",
     "@typescript-eslint/eslint-plugin": "^7.7.0",
     "@vercel/ncc": "^0.38.1",
     "eslint": "^8.57.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 961b47a..baa924d 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -19,9 +19,6 @@ devDependencies:
   '@trivago/prettier-plugin-sort-imports':
     specifier: ^4.3.0
     version: 4.3.0(prettier@3.2.5)
-  '@types/node':
-    specifier: ^20.12.7
-    version: 20.12.7
   '@typescript-eslint/eslint-plugin':
     specifier: ^7.7.0
     version: 7.7.0(@typescript-eslint/parser@7.7.0)(eslint@8.57.0)(typescript@5.4.5)
@@ -897,6 +894,7 @@ packages:
     resolution: {integrity: sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==}
     dependencies:
       undici-types: 5.26.5
+    dev: false
 
   /@types/semver@7.5.8:
     resolution: {integrity: sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==}
@@ -3639,6 +3637,7 @@ packages:
 
   /undici-types@5.26.5:
     resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==}
+    dev: false
 
   /undici@5.28.4:
     resolution: {integrity: sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g==}
diff --git a/src/index.ts b/src/index.ts
index f199163..d74e483 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -6,8 +6,6 @@ const EVENT_EXECUTION_FAILURE = "execution_failure";
 
 class UpdateFlakeLockAction {
   idslib: IdsToolbox;
-  private nixOptions: string;
-  private targets: string[];
   private commitMessage: string;
   private pathToFlakeDir: string | null;
 
@@ -19,20 +17,23 @@ class UpdateFlakeLockAction {
     };
 
     this.idslib = new IdsToolbox(options);
-
-    this.nixOptions = inputs.getString("nix-options");
-    this.targets = inputs.getString("inputs").split(" ");
     this.commitMessage = inputs.getString("commit-msg");
     this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
   }
 
-  async update(): Promise<void> {
-    const nixOptions: string[] = this.nixOptions.split(",");
-    const inputFlags: string[] =
-      this.targets.length > 0
-        ? this.targets.map((input) => `--update-input ${input}`)
-        : [];
+  get flakeInputs(): string[] {
+    const targets: string[] = [];
+    for (const input of inputs.getString("inputs").split(",")) {
+      targets.concat(["--update-input", input]);
+    }
+    return targets;
+  }
 
+  get nixOptions(): string[] {
+    return inputs.getString("nix-options").split(",");
+  }
+
+  async update(): Promise<void> {
     if (this.pathToFlakeDir !== null) {
       const returnCode = await actionsExec.exec("cd", [this.pathToFlakeDir]);
       if (returnCode !== 0) {
@@ -46,12 +47,13 @@ class UpdateFlakeLockAction {
     }
 
     // Nix command of this form:
-    // nix ${nix options} flake lock ${input flags} --commit-lock-file --commit-lock-file-summary ${commit message}
-    // Example command:
-    // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lock-file-summary
-    const nixCommandArgs: string[] = nixOptions
+    // nix ${maybe nix options} flake lock ${maybe --update-input flags} --commit-lock-file --commit-lock-file-summary ${commit message}
+    // Example commands:
+    // nix --extra-substituters https://example.com flake lock --update-input nixpkgs --commit-lock-file --commit-lock-file-summary "updated flake.lock"
+    // nix flake lock --commit-lock-file --commit-lock-file-summary "updated flake.lock"
+    const nixCommandArgs: string[] = this.nixOptions
       .concat(["flake", "lock"])
-      .concat(inputFlags.length > 0 ? inputFlags : [])
+      .concat(this.flakeInputs)
       .concat([
         "--commit-lock-file",
         "--commit-lock-file-summary",
diff --git a/update-flake-lock.sh b/update-flake-lock.sh
deleted file mode 100755
index b63d9c3..0000000
--- a/update-flake-lock.sh
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-if [[ -n "$PATH_TO_FLAKE_DIR" ]]; then
-  cd "$PATH_TO_FLAKE_DIR"
-fi
-
-options=()
-if [[ -n "$NIX_OPTIONS" ]]; then
-    for option in $NIX_OPTIONS; do
-        options+=("${option}")
-    done
-fi
-
-if [[ -n "$TARGETS" ]]; then
-    inputs=()
-    for input in $TARGETS; do
-        inputs+=("--update-input" "$input")
-    done
-    nix "${options[@]}" flake lock "${inputs[@]}" --commit-lock-file --commit-lockfile-summary "$COMMIT_MSG"
-else
-    nix "${options[@]}" flake update --commit-lock-file --commit-lockfile-summary "$COMMIT_MSG"
-fi