-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
80 lines (75 loc) · 2.24 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const core = require('@actions/core');
const io = require('@actions/io');
const tc = require('@actions/tool-cache');
const ex = require('@actions/exec');
//async function ensureBinDir() {
// bindir = `${process.env.HOME}/.local/bin`
// if (!fs.existsSync(bindir)) {
// await io.mkdirP(bindir);
// core.addPath(bindir);
// }
// if(!fs.statSync(bindir).isDirectory()) {
// throw {"message": `${bindir} exists and is not directory`};
// }
// return bindir;
//}
//
//async function setup_jq(bindir) {
// var jqPath = await io.which('jq', false);
// if (! jqPath) {
// const bindl = await tc.find("jq", "1.6")
// const bindl = await tc.downloadTool("https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64");
// const st = fs.statSync(bindl);
// fs.chmodSync(bindl, st.mode | fs.constants.S_IXUSR | fs.constants.S_IXGRP | fs.constants.S_IXOTH);
// await tc.cacheFile(bindl, 'jq', 'jq', '1.6');
// jqPath = path.join([bindir, 'jq']);
// await io.cp(bindl, jqPath);
// if(!await io.which('jq', false)) {
// core.addPath(bindir);
// }
// }
//}
function MissingBinary(program) {
this.message = `${program} not installed on path`;
}
async function findBinary() {
let binpath = await io.which('jq', false);
if (!binpath) {
binpath = await tc.find('jq', '*');
if(!binpath) {
throw new MissingBinary('jq');
}
}
return binpath;
}
async function run() {
try {
var output = '';
const jq = await findBinary();
const input = core.getInput("input", {required: true});
const script = core.getInput("script", {required: true});
const flags = [];
if (core.getInput("compact") === 'true') {
flags.push('c');
}
if (core.getInput("raw-output") === 'true') {
flags.push('r');
}
const args = flags.length > 0 ? [`-${flags.join('')}`, script] : [script];
await ex.exec(jq, args, {
listeners: {
stdout: (data) => {
output += data.toString();
}
},
input: Buffer.from(input, 'utf8')
});
if (core.getInput("remove-trailing-newline", {required: false}) === "true") {
output = output.replace(/\r?\n$/, "");
}
core.setOutput('output', output);
} catch (error) {
core.setFailed(error.message);
}
}
run();