-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (47 loc) · 1.55 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const core = require('@actions/core');
const fs = require('fs');
const run = () => {
try {
const target = core.getInput('target');
const encoding = core.getInput('encoding');
const beginMarker = core.getInput('begin-marker', { required: true });
const endMarker = core.getInput('end-marker', { required: true });
const input = core.getInput('input', { required: true });
const inputType = core.getInput('input-type');
let insertion = input;
if (inputType == 'file') {
const buffer = fs.readFileSync(input);
insertion = buffer.toString();
}
const content = fs.readFileSync(target, encoding);
const start = content.indexOf(beginMarker);
const end = content.lastIndexOf(endMarker);
if (start < 0 && end < 0) {
throw new Error(`begin and end marker not found in ${target}`);
}
if (start < 0) {
throw new Error(`begin marker not found in ${target}`);
}
if (end < 0) {
throw new Error(`end marker not found in ${target}`);
}
if (end < start) {
throw new Error(`end marker is before the begin marker in ${target}`);
}
const before = content.substring(0, start);
const after = content.substring(end + endMarker.length);
const generated = before.concat(
beginMarker,
'\n',
insertion,
'\n',
endMarker,
after,
);
fs.writeFileSync(target, generated), { encoding: encoding, mode: 0o644 };
core.setOutput('changed', content !== generated);
} catch (error) {
core.setFailed(error.message);
}
};
run();