-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.js
42 lines (38 loc) · 1.22 KB
/
action.js
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
39
40
41
42
const fs = require("fs");
const readline = require("readline");
const readInterface = readline.createInterface({
input: fs.createReadStream(process.argv[2]),
output: process.stdout,
terminal: false,
});
const stripQuotes = (str) =>
str.startsWith('"') || str.startsWith("'") ? str.slice(1, -1) : str;
const replaceEnvVars = (str) => {
const value = str
.replaceAll(
/\$\{([a-zA-Z0-9_]+):\+:\$[a-zA-Z0-9_]+\}/g,
(_, key) => ((v) => v ? `:${v}` : "")(process.env[key]),
)
.replaceAll(/\$\{([a-zA-Z0-9_]+)\}/g, (_, key) => process.env[key] ?? "")
.replaceAll(/\$([a-zA-Z0-9_]+)/g, (_, key) => process.env[key] ?? "");
return value;
};
readInterface.on("line", (line) => {
const match = line.match(/^([^=]+)=(.*)$/);
if (match) {
const [_, key, value_] = match;
const value = stripQuotes(value_);
if (key === "PATH") {
value
.replaceAll("${PATH:+:$PATH}", "")
.replaceAll("$PATH", "")
.replaceAll("${PATH}", "")
.split(":").forEach((path) => {
fs.appendFileSync(process.env["GITHUB_PATH"], `${path}\n`);
});
} else {
let v = replaceEnvVars(value);
fs.appendFileSync(process.env["GITHUB_ENV"], `${key}=${v}\n`);
}
}
});