-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (134 loc) · 3.96 KB
/
Copy pathindex.js
File metadata and controls
153 lines (134 loc) · 3.96 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
153
const core = require("@actions/core");
const fs = require("fs");
function parseSetFile(filePath) {
const entries = [];
const content = fs.readFileSync(filePath, "utf8");
for (const rawLine of content.split("\n")) {
const line = rawLine.trim();
if (!line || line.startsWith("#")) continue;
const idx = line.indexOf("=");
if (idx === -1) continue;
entries.push({ key: line.slice(0, idx), value: line.slice(idx + 1) });
}
return entries;
}
function parseUnsetFile(filePath) {
const entries = [];
const content = fs.readFileSync(filePath, "utf8");
for (const rawLine of content.split("\n")) {
const line = rawLine.trim();
if (!line || line.startsWith("#")) continue;
entries.push(line);
}
return entries;
}
function applySet(configData, entries) {
let modified = 0;
let added = 0;
let skipped = 0;
const lines = configData.split("\n");
for (const { key, value } of entries) {
const setPrefix = `${key}=`;
const unsetMarker = `# ${key} is not set`;
let found = false;
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith(setPrefix)) {
if (lines[i] === `${key}=${value}`) {
core.info(`${key}=${value} already set, skipping`);
skipped++;
} else {
lines[i] = `${key}=${value}`;
modified++;
}
found = true;
break;
}
if (lines[i] === unsetMarker) {
lines[i] = `${key}=${value}`;
found = true;
modified++;
break;
}
}
if (!found) {
lines.push(`${key}=${value}`);
added++;
}
}
return { configData: lines.join("\n"), modified, added, skipped };
}
function applyUnset(configData, entries) {
let unsetCount = 0;
const notFound = [];
const lines = configData.split("\n");
for (const key of entries) {
const setPrefix = `${key}=`;
const unsetMarker = `# ${key} is not set`;
let found = false;
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith(setPrefix) || lines[i] === unsetMarker) {
lines[i] = `# ${key} is not set`;
found = true;
unsetCount++;
break;
}
}
if (!found) {
notFound.push(`${key} not found in config, skipping unset`);
}
}
return { configData: lines.join("\n"), unsetCount, notFoundCount: notFound.length, notFound };
}
function main() {
const configPath = core.getInput("config", { required: true });
const setPaths = core.getMultilineInput("set", { required: true });
const unsetPaths = core.getMultilineInput("unset").filter(p => p.length > 0);
const outputPath = core.getInput("output") || configPath;
if (!fs.existsSync(configPath)) {
core.setFailed(`config file not found: ${configPath}`);
return;
}
for (const p of setPaths) {
if (!fs.existsSync(p)) {
core.setFailed(`set file not found: ${p}`);
return;
}
}
for (const p of unsetPaths) {
if (!fs.existsSync(p)) {
core.setFailed(`unset file not found: ${p}`);
return;
}
}
let configData = fs.readFileSync(configPath, "utf8");
const setEntries = [];
for (const p of setPaths) {
setEntries.push(...parseSetFile(p));
}
const setResult = applySet(configData, setEntries);
configData = setResult.configData;
let unsetCount = 0;
let notFoundCount = 0;
if (unsetPaths.length > 0) {
const unsetEntries = [];
for (const p of unsetPaths) {
unsetEntries.push(...parseUnsetFile(p));
}
const unsetResult = applyUnset(configData, unsetEntries);
configData = unsetResult.configData;
unsetCount = unsetResult.unsetCount;
notFoundCount = unsetResult.notFoundCount;
for (const msg of unsetResult.notFound) {
core.info(msg);
}
}
fs.writeFileSync(outputPath, configData);
core.info(
`Set: ${setResult.modified}, Skipped: ${setResult.skipped}, Added: ${setResult.added}, Unset: ${unsetCount}, Not found: ${notFoundCount}`
);
}
try {
main();
} catch (error) {
core.setFailed(error.message);
}