From 56ba67dfc00d8fdb57b47d848cdda5ca8c0ef9a2 Mon Sep 17 00:00:00 2001
From: gallexme <lostchaos3@gmail.com>
Date: Sun, 6 Aug 2023 04:32:52 +0200
Subject: [PATCH] fix: save store paths to file (#5)

closes #4
---
 src/stages/configure.ts |  5 ++---
 src/stages/push.ts      |  6 +++---
 src/utils.ts            | 25 ++++++-------------------
 3 files changed, 11 insertions(+), 25 deletions(-)

diff --git a/src/stages/configure.ts b/src/stages/configure.ts
index 4929748..e371287 100644
--- a/src/stages/configure.ts
+++ b/src/stages/configure.ts
@@ -1,6 +1,6 @@
 import * as core from "@actions/core";
 import { exec } from "@actions/exec";
-import { getStorePaths } from "../utils";
+import { saveStorePaths } from "../utils";
 
 export const configure = async () => {
 	core.startGroup("Configure attic");
@@ -14,8 +14,7 @@ export const configure = async () => {
 		await exec("attic", ["login", "--set-default", cache, endpoint, token]);
 
 		core.info("Collecting store paths before build");
-		const paths = await getStorePaths();
-		core.saveState("initial-paths", JSON.stringify(paths));
+		await saveStorePaths();
 	} catch (e) {
 		core.setFailed(`Action failed with error: ${e}`);
 	}
diff --git a/src/stages/push.ts b/src/stages/push.ts
index f911bb5..e4ac8d4 100644
--- a/src/stages/push.ts
+++ b/src/stages/push.ts
@@ -2,8 +2,7 @@ import * as core from "@actions/core";
 import { exec } from "@actions/exec";
 
 import splitArray from "just-split";
-import { getStorePaths } from "../utils";
-
+import { saveStorePaths, getStorePaths } from "../utils";
 export const push = async () => {
 	core.startGroup("Push to Attic");
 
@@ -15,7 +14,8 @@ export const push = async () => {
 			const cache = core.getInput("cache");
 
 			core.info("Pushing to cache");
-			const oldPaths = JSON.parse(core.getState("initial-paths")) as string[];
+			const oldPaths = await getStorePaths();
+			await saveStorePaths();
 			const newPaths = await getStorePaths();
 			const addedPaths = newPaths
 				.filter((p) => !oldPaths.includes(p))
diff --git a/src/utils.ts b/src/utils.ts
index a38183a..d5632ba 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -1,23 +1,10 @@
 import { exec } from "@actions/exec";
-import { Writable } from "node:stream";
 
-class StringStream extends Writable {
-	chunks: Buffer[] = [];
+import { readFile } from "node:fs/promises";
 
-	_write(chunk: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>, _enc: unknown, next: () => unknown) {
-		this.chunks.push(Buffer.from(chunk));
-		next();
-	}
-
-	string() {
-		return Buffer.concat(this.chunks).toString("utf-8");
-	}
-}
-
-export const getStorePaths = async () => {
-	const outStream = new StringStream();
-	await exec("nix", ["path-info", "--all"], { outStream });
-	const paths = outStream.string().split("\n").filter(Boolean);
-
-	return paths;
+export const saveStorePaths = async () => {
+	await exec("sh", ["-c", "nix  path-info --all --json > /tmp/store-paths"]);
+};
+export const getStorePaths = async () => {
+	return JSON.parse(await readFile("/tmp/store-paths", "utf8")).map((path) => path.path);
 };