fix: use StringStream

This commit is contained in:
Ryan Cao 2023-07-19 10:02:52 +08:00
parent 5dc7b671af
commit e0114a9f15
No known key found for this signature in database
4 changed files with 23 additions and 16 deletions

View file

@ -19,3 +19,4 @@ runs:
using: "node16"
main: "dist/index.js"
post: "dist/post.js"
post-if: "success()"

4
dist/index.js vendored

File diff suppressed because one or more lines are too long

4
dist/post.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1,21 +1,27 @@
import { exec } from "@actions/exec";
import { Writable } from "node:stream";
const streamToString = (stream: Writable): Promise<string> => {
const chunks: Buffer[] = [];
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
stream.on("error", (err) => reject(err));
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
};
class StringStream extends Writable {
chunks: Buffer[] = [];
_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 Writable();
const outStream = new StringStream();
await exec("nix", ["path-info", "--all"], { outStream });
const paths = await streamToString(outStream)
.then((res) => res.split("\n"))
.then((paths) => paths.filter(Boolean));
const paths = outStream.string().split("\n").filter(Boolean);
return paths;
};