-
Notifications
You must be signed in to change notification settings - Fork 6
/
cylc-action-utils.js
76 lines (65 loc) · 2.37 KB
/
cylc-action-utils.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
/* THIS FILE IS PART OF THE CYLC SUITE ENGINE.
Copyright (C) NIWA & British Crown (Met Office) & Contributors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
// Helpers for NodeJS steps in GitHub Actions
const {env} = process;
const {execSync} = require('child_process');
const execSyncOpts = {stdio: 'pipe', encoding: 'utf8'};
const {writeFileSync} = require('fs');
exports.curlOpts = '--silent --fail --show-error';
/** Escape single quotes in string */
exports.escSQ = (str) => {
return str.replace(/'/g, "'")
}
/**
* Node's execSync() but with improved logging
*
* @param {string} cmd - The command to execute.
* @param {{ quiet: boolean, verbose: boolean }} options
* @return {string} - stdout
*/
exports.execSync = (cmd, options = {}) => {
let stdout;
try {
stdout = execSync(cmd, execSyncOpts);
} catch (err) {
console.log(`::error:: ${err.stderr ? err.stderr : 'Error executing command'}`);
throw err.message;
}
if (!options.quiet) {
if (!options.verbose) {
console.log(`::group::exec ${cmd.slice(0, 60)}...`);
}
console.log(`[command]${cmd}`);
try {
console.log(JSON.stringify(JSON.parse(stdout), null, 2));
} catch {
console.log(stdout);
}
if (!options.verbose) {
console.log('::endgroup::');
}
}
return stdout;
};
/** Set an environment variable for later steps in a workflow to use */
exports.setEnv = (name, value) => {
writeFileSync(env.GITHUB_ENV, `${name}=${value}\n`, {flag: 'a'})
};
/** Set a step's output */
exports.setOutput = (name, value) => {
writeFileSync(env.GITHUB_OUTPUT, `${name}=${value}\n`, {flag: 'a'})
};
/** Add a path to $PATH */
exports.addPath = (path) => {
writeFileSync(env.GITHUB_PATH, `${path}\n`, {flag: 'a'})
};