-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsave-result
executable file
·38 lines (30 loc) · 983 Bytes
/
save-result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env node
const path = require("path");
const {
promises: { writeFile },
} = require("fs");
const readStdIn = async () => {
const chunks = [];
for await (const chunk of process.stdin) {
chunks.push(chunk);
}
return Buffer.concat(chunks).toString("utf8");
};
const stripOrigin = (url) => url.replace(new URL(url).origin, "");
const snapshotFileName = (url, name) => {
const pathSlug = stripOrigin(url)
.replace(/^\//, "") // "/path" -> "path"
.replace(/\/$/, "") // "path/" -> "path"
.replaceAll(/[^a-zA-Z0-9]+/g, "-");
return `${name ? name : pathSlug}.json`;
};
(async () => {
const [, , snapshotsPath = "", url, name] = process.argv;
const psiResult = {
name: name ? name : undefined,
...JSON.parse(await readStdIn()),
};
const fileName = snapshotFileName(url, name);
const filePath = path.resolve(__dirname, snapshotsPath, fileName);
await writeFile(filePath, JSON.stringify(psiResult, null, 2), "utf-8");
})();