-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
198 lines (180 loc) · 5.79 KB
/
helper.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const ora = require("ora");
const { spawn } = require("child_process");
const chalk = require("chalk");
const { promisify } = require("util");
const download = promisify(require("download-git-repo"));
const axios = require("axios");
const difftool = require("diff");
const path = require("path");
require("dotenv").config({ path: __dirname + "/.env.migration" });
/**
* Get path to yarn module
*/
module.exports.yarn = path.normalize(__dirname + "/node_modules/yarn");
/**
* Write log to console with magenta color.
* @param {string} content the log content
*/
module.exports.log = (content) => console.log(chalk.magentaBright(content));
/**
* Download a repository with a elegant terminal spinner.
* @param {string} repo the repository path in gitHub
* @param {string} desc the destination of downloaded repository
*/
module.exports.clone = async (repo, desc) => {
const process = ora(`Download ${repo}`);
console.log(desc);
process.start();
try {
await download(repo, desc);
} catch (error) {
console.log(error);
process.fail();
}
process.succeed();
};
/**
* Execute Shell command and pipe output to console
* @param {...} args the command and its configuration, e.g. work directory
*/
module.exports.spawn_console = async (...args) => {
return new Promise((resolve) => {
let [command,arg,options] = args;
options = {...options, shell: process.platform == 'win32'};
const proc = spawn(command, arg, options);
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on("close", () => {
resolve();
});
});
};
/**
* Execute Shell command, separate subprocess if necessary
* @param {boolean} unref the flag for separating the subprocess from the main process
* @param {...} args the command and its configuration, e.g. environment variables
*/
module.exports.spawn_log = async (unref, ...args) => {
return new Promise((resolve) => {
let [command,arg,options] = args;
options = {...options, shell: process.platform == 'win32'};
const proc = spawn(command, arg, options);
if (unref) {
proc.unref();
}
proc.on("close", () => {
resolve();
});
});
};
/**
* Execute Shell commands and save the output as JS string
* @param {string} cmd1 the first command
* @param {[string]} arg1 the arguments for the first command
* @param {string} cmd2 the second command
* @param {[string]} arg1 the arguments for the second command
* @param {string} cmd3 the third command
* @param {[string]} arg1 the arguments for the third command
* @returns {string} result after the execution of commands
*/
module.exports.spawn_string = async (cmd1, arg1, cmd2, arg2, cmd3, arg3) => {
return new Promise((resolve, reject) => {
const proc1 = spawn(cmd1, arg1);
const proc2 = spawn(cmd2, arg2);
const proc3 = spawn(cmd3, arg3);
let output = "";
proc1.stdout.on("data", (data) => proc2.stdin.write(data));
proc1.stderr.on("data", (data) => console.error(`${cmd1} stderr: ${data}`));
proc1.on("close", (code) => {
if (code !== 0) {
console.log(`${cmd1} process exited with code ${code}`);
}
proc2.stdin.end();
});
proc2.stdout.on("data", (data) => proc3.stdin.write(data));
proc2.stderr.on("data", (data) => console.error(`${cmd2} stderr: ${data}`));
proc2.on("close", (code) => {
if (code !== 0) {
console.log(`${cmd1} process exited with code ${code}`);
}
proc3.stdin.end();
});
proc3.stdout.on("data", (data) => (output += data.toString()));
proc3.on("close", () => resolve(output));
proc3.on("error", (err) => reject(err));
});
};
/**
* Compare the actual result with expected result
* @param {string} actual actual result
* @param {string} expected expected result
* @returns {string} comparison report
*/
module.exports.diffByLine = async (actual, expected) => {
var diff = difftool.diffTrimmedLines(actual, expected);
//var diff = difftool.diffWords(actual, expected);
var report = [];
var lastRemoved = false;
var lastValue = "";
diff.forEach((item, i) => {
if (lastRemoved && item.added) {
if (lastValue.replace(/\s/g, "") != item.value.replace(/\s/g, "")) {
//skip whitespace-only differences
report.push("Actual: " + lastValue["red"]);
report.push("Expected: " + item.value["grey"]);
}
} else if (!item.added && !item.removed) {
report.push(item.value["green"]);
}
lastRemoved = item.removed;
lastValue = item.value;
});
return report.join("");
};
/**
* Execute query or mutation and return the response
* @param {string} query query or mutation
* @returns {string} response from server
*/
module.exports.axios_post = async (query) => {
try {
const OAUTH2_TOKEN_URI = process.env.OAUTH2_TOKEN_URI;
const username = process.env.USER_NAME;
const password = process.env.PASSWORD;
const url = process.env.REMOTE_URL;
const id = process.env.CLIENT_ID;
let res;
if (OAUTH2_TOKEN_URI && username && password && url && id) {
res = await axios({
method: "post",
url: OAUTH2_TOKEN_URI,
data: `username=${username}&password=${password}&grant_type=password&client_id=${id}`,
headers: {
"content-type": "application/x-www-form-urlencoded;charset=utf-8",
},
});
}
let headers = {
"Content-Type": "application/json",
Accept: "application/graphql",
};
if (res && res.data) {
const token = res.data.access_token;
headers["authorization"] = "Bearer " + token;
}
const response = await axios.post(
url,
{ query: query },
{
headers: headers,
}
);
return response;
} catch (error) {
if (error.response && error.response.data) {
throw error.response.data;
} else {
throw new Error(error);
}
}
};