-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (131 loc) · 4.08 KB
/
index.js
File metadata and controls
152 lines (131 loc) · 4.08 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import core from "@actions/core";
import fs from "fs";
import fsasync from "fs/promises";
import path from "path";
import { set } from "lodash";
import { globSync } from "glob";
// determine whether the provided path points to a file or a directory that exists
const checkPath = (path) => {
try {
const info = fs.statSync(path);
switch (true) {
case info.isDirectory():
return "directory";
case info.isFile():
return "file";
default:
return "unknown";
}
} catch (error) {
return "unknown";
}
};
const doReplace = (file, prefix, omitSuffix) => {
switch (path.extname(file)) {
case ".json":
doReplaceJson(file);
break;
default:
doReplaceTxt(file, prefix, omitSuffix);
}
};
let paths = [];
// get an array containing all the json paths in the specified object
const getPaths = (object, previousPath) => {
for (const key in object) {
const currentPath = previousPath ? `${previousPath}.${key}` : key;
if (Array.isArray(object[key])) {
paths.push(currentPath);
getPaths(object[key], currentPath);
} else if (typeof object[key] === "object") {
if (!Array.isArray(object)) {
paths.push(currentPath);
}
getPaths(object[key], currentPath);
} else {
paths.push(currentPath);
}
}
};
// replace the values of the json key/value pair with matching
// values from the env variables
// env variable case must match the json key case
const doReplaceJson = async (file) => {
const fileContents = await fsasync.readFile(file, "utf-8");
const json = JSON.parse(fileContents);
paths = [];
getPaths(json);
core.info(`zjson ${json}`);
core.info(`zpath ${path}`);
paths.forEach((path) => {
// does the variable exist in the env?
const env = process.env[path] || "not found";
core.info(`zenv ${env}`);
if (env !== "not found") {
core.info(`Replacing placeholder for ${path}`);
set(json, path, env);
}
});
await fsasync.writeFile(file, JSON.stringify(json));
core.info("File saved");
};
// replace the placeholders with matching values from the env variables
// env variables must be all uppercase, placeholder case is unimportant
const doReplaceTxt = async (file, prefix, omitSuffix) => {
const pattern = `${prefix}{.+}${prefix}`;
const checkPattern = `${prefix}{.+)}${prefix}`;
if (omitSuffix === true) {
pattern = `${prefix}{\\w+}`;
checkPattern = `${prefix}{(\\w+)}`;
core.info("omitting suffix");
}
const regExp = new RegExp(pattern, "gi");
const check = new RegExp(checkPattern, "i");
const fileContents = await fsasync.readFile(file, "utf-8");
const result = fileContents.replace(regExp, (placeholder) => {
// we've found a placeholder, now extract the variable name
const varName = placeholder.match(check)[1];
// does the variable exist in the env?
const env = process.env[varName.toUpperCase()];
if (typeof env === "undefined") {
core.warning(`Value missing for ${varName}`);
return placeholder;
} else {
core.info(`Replacing placeholder ${placeholder}`);
}
// we're all good, return the value to use as the replacement
return env;
});
await fsasync.writeFile(file, result);
core.info("File saved");
};
// main part of code
try {
const pathList = core.getInput("files");
const prefix = core.getInput("prefix");
const omitSuffix = core.getInput("omit-suffix");
let paths;
if (pathList.includes("*")) {
paths = globSync(pathList, { ignore: "node_modules/**" });
} else {
paths = pathList.split("\n");
}
paths.forEach(async (path) => {
if (path.length > 0) {
const pathType = checkPath(path);
switch (pathType) {
case "file":
doReplace(path, prefix, omitSuffix);
break;
case "directory":
const files = await fsasync.readdir(path);
files.forEach((file) => doReplace(file, prefix, omitSuffix));
break;
default:
throw new Error(`invalid or missing path/file: ${path}`);
}
}
});
} catch (error) {
core.setFailed(error.message);
}