This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
73 lines (62 loc) · 1.75 KB
/
helpers.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
const childProcess = require('child_process');
const path = require('path');
function $(command, options = {}) {
let output;
try {
output = childProcess.execSync(command, { encoding: 'utf-8', ...options })
} catch(error) {
if (error.status) {
exit(error.message);
} else {
throw error;
}
}
if (Buffer.isBuffer(output)) {
return output.toString().trim();;
} else if (typeof output === 'string') {
return output.trim();
} else {
return output;
}
}
function $$(command, options = {}) {
return $(command, { ...options, stdio: 'inherit' });
}
function getScriptName() {
const [/*node*/, file] = process.argv;
let name = path.basename(file, path.extname(file));
if (name.startsWith('git-')) {
name = name.substring(4);
}
return name;
}
function exit(message) {
console.error(`${getScriptName()}: ${message}`);
process.exit(1);
}
function graphql(query) {
const fullQuery = `query { repository(owner:\\"{owner}\\", name:\\"{repo}\\") { ${query} } }`;
const command = `hub api graphql -f query="${fullQuery}"`;
let result;
try {
result = JSON.parse($(command));
} catch(error) {
exit(`failed to execute query: ${error}`)
}
if (typeof result !== 'object') {
exit(`unexpected result ${result} from query`);
} else if (result.hasOwnProperty('errors')) {
exit(`query returned errors: ${JSON.stringify(result.errors)}`);
} else if (typeof result.data !== 'object' || typeof result.data.repository !== 'object') {
exit(`could not find repository data from query result`);
} else {
return result.data.repository;
}
}
function toArray(listString) {
if (!listString) {
return [];
}
return listString.split(/\r?\n/);
}
module.exports = { $, $$, exit, graphql, toArray };