-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
76 lines (68 loc) · 2.76 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*-----------------------------------------------------------------------------------------------------------\
| _____ _ _____ _ _ _ _____ _____ _____ __ _______ _____ _____ _____ |
| |_ _| (_) / ___| | | (_) / __ \| _ |/ __ \/ | / / __ \| _ |/ __ \|____ | |
| | | __ _ _ \ `--.| |_ _ _ __| |_ ___ `' / /'| |/' |`' / /'`| | / /`' / /'| |/' |`' / /' / / |
| | |/ _` | | `--. \ __| | | |/ _` | |/ _ \ / / | /| | / / | | / / / / | /| | / / \ \ |
| | | (_| | | /\__/ / |_| |_| | (_| | | (_) | ./ /___\ |_/ /./ /____| |_/ / ./ /___\ |_/ /./ /___.___/ / |
| \_/\__,_|_| \____/ \__|\__,_|\__,_|_|\___/ \_____/ \___/ \_____/\___/_/ \_____/ \___/ \_____/\____/ |
\-----------------------------------------------------------------------------------------------------------*/
const core = require('@actions/core');
const github = require('@actions/github');
const yaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
try {
const type = core.getInput('type');
const files = core.getInput('files');
const replace = core.getInput('replace');
const replaceFor = core.getInput('replaceFor');
switch(type){
case 'single':
single(files, replace, replaceFor);
break;
case 'multiple':
multiple(files, replace, replaceFor);
break;
default:
single(files, replace, replaceFor);
}
} catch (error) {
core.setFailed(error.message);
}
function single(files, replace, replaceFor){
if(typeof files == "object"){
multiple(files, replace, replaceFor);
}
else{
editor(files, replace, replaceFor);
}
}
function multiple(files, replace, replaceFor){
if(typeof files == "string"){
single(files, replace, replaceFor);
}
else{
files.forEach(file => {
single(file, replace, replaceFor);
});
}
}
function editor(file, strPARAM, find, replace){
file = path.join(process.env.RUNNER_WORKSPACE, file);
console.log(file, process.env.RUNNER_WORKSPACE);
console.log(process.env);
try {
const doc = yaml.load(fs.readFileSync(file, 'utf8'));
doc['files'][0]['url'] = replaceALL(strPARAM, find, replace);
doc['path'] = replaceALL(strPARAM, find, replace);
var newFile = yaml.dump(doc);
fs.writeFileSync(file, newFile, 'utf8');
} catch (e) {
console.log(e);
}
}
function replaceALL(str, find, replace) {
var escapedFind=find.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
return str.replace(new RegExp(escapedFind, 'g'), replace);
}