node-automator 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/assets/alert.png +0 -0
- package/assets/error.png +0 -0
- package/assets/success.png +0 -0
- package/commands/abort.js +11 -0
- package/commands/alert.js +19 -0
- package/commands/assemble.js +56 -0
- package/commands/assert.js +18 -0
- package/commands/base.js +363 -0
- package/commands/break.js +11 -0
- package/commands/change_filter.js +34 -0
- package/commands/cheerio.js +15 -0
- package/commands/clear_screen.js +14 -0
- package/commands/commit.js +31 -0
- package/commands/compare.js +23 -0
- package/commands/confirm.js +16 -0
- package/commands/continue.js +11 -0
- package/commands/copy.js +14 -0
- package/commands/cursor_up.js +12 -0
- package/commands/date.js +25 -0
- package/commands/debugger.js +11 -0
- package/commands/dingtalk.js +110 -0
- package/commands/disassemble.js +36 -0
- package/commands/divination.js +15 -0
- package/commands/download.js +12 -0
- package/commands/edit.js +83 -0
- package/commands/edit_excel.js +57 -0
- package/commands/env_optional.js +57 -0
- package/commands/eval.js +27 -0
- package/commands/exec_from_file.js +31 -0
- package/commands/export_diff_head.js +31 -0
- package/commands/file_info.js +41 -0
- package/commands/file_size.js +12 -0
- package/commands/files.js +14 -0
- package/commands/first_exist.js +18 -0
- package/commands/flatten.js +18 -0
- package/commands/ftp.js +161 -0
- package/commands/git_commit.js +31 -0
- package/commands/git_export_diff_head.js +13 -0
- package/commands/git_is_dirty.js +13 -0
- package/commands/hash.js +21 -0
- package/commands/http_request.js +12 -0
- package/commands/image_crop.js +19 -0
- package/commands/input.js +37 -0
- package/commands/interval.js +60 -0
- package/commands/is_dirty.js +31 -0
- package/commands/jwt_decode.js +22 -0
- package/commands/list2dict.js +32 -0
- package/commands/log.js +72 -0
- package/commands/loop.js +115 -0
- package/commands/loop_forever.js +66 -0
- package/commands/merge_files.js +39 -0
- package/commands/mgr.js +476 -0
- package/commands/modify.js +44 -0
- package/commands/move.js +14 -0
- package/commands/move_local.js +14 -0
- package/commands/multi_select.js +16 -0
- package/commands/mysql_connect.js +29 -0
- package/commands/mysql_end.js +13 -0
- package/commands/mysql_query.js +19 -0
- package/commands/notify.js +13 -0
- package/commands/open.js +30 -0
- package/commands/parse.js +18 -0
- package/commands/pause.js +15 -0
- package/commands/print.js +47 -0
- package/commands/read_cache.js +12 -0
- package/commands/read_cfg.js +13 -0
- package/commands/read_clipboard.js +19 -0
- package/commands/read_excel.js +17 -0
- package/commands/read_only_excel.js +40 -0
- package/commands/read_plain.js +14 -0
- package/commands/remove.js +14 -0
- package/commands/retrieve.js +17 -0
- package/commands/reveal.js +33 -0
- package/commands/revision_info.js +31 -0
- package/commands/schedule.js +80 -0
- package/commands/select.js +17 -0
- package/commands/send_email.js +25 -0
- package/commands/share_data.js +745 -0
- package/commands/shell.js +13 -0
- package/commands/sleep.js +17 -0
- package/commands/store.js +13 -0
- package/commands/store_optional.js +33 -0
- package/commands/stream.js +16 -0
- package/commands/stringify.js +24 -0
- package/commands/svn_add.js +26 -0
- package/commands/svn_commit.js +49 -0
- package/commands/svn_export_diff_head.js +13 -0
- package/commands/svn_is_dirty.js +13 -0
- package/commands/svn_update.js +27 -0
- package/commands/switch.js +18 -0
- package/commands/temp.js +12 -0
- package/commands/tencent_cos.js +323 -0
- package/commands/test.js +14 -0
- package/commands/title.js +18 -0
- package/commands/tmpdir.js +21 -0
- package/commands/translate.js +58 -0
- package/commands/traversal.js +38 -0
- package/commands/truncate.js +31 -0
- package/commands/truncate_all.js +62 -0
- package/commands/ts_sort.js +18 -0
- package/commands/unzip.js +50 -0
- package/commands/updating_revision.js +28 -0
- package/commands/valid_required.js +31 -0
- package/commands/void.js +10 -0
- package/commands/watch_mail.js +94 -0
- package/commands/web_socket.js +233 -0
- package/commands/which.js +60 -0
- package/commands/write_cache.js +16 -0
- package/commands/write_cfg.js +18 -0
- package/commands/write_clipboard.js +23 -0
- package/commands/write_excel.js +17 -0
- package/commands/write_plain.js +18 -0
- package/commands/zip.js +78 -0
- package/commands/zip_data.js +64 -0
- package/examples/print_env.yml +3 -0
- package/index.js +109 -0
- package/package.json +63 -0
- package/scratch/.gitkeep +0 -0
- package/utils/alert_tool.js +133 -0
- package/utils/base64_tool.js +13 -0
- package/utils/cache/local_cache.js +79 -0
- package/utils/cache/raw_cache.js +47 -0
- package/utils/cache/simple_local_cache.js +33 -0
- package/utils/cache/simple_sqlite_cache.js +43 -0
- package/utils/cache/simple_svn_cache.js +49 -0
- package/utils/cache/svn_cache.js +40 -0
- package/utils/cache_tool.js +53 -0
- package/utils/display_tool.js +85 -0
- package/utils/divination_util.js +555 -0
- package/utils/file_tool.js +529 -0
- package/utils/hash_tool.js +8 -0
- package/utils/interaction_tool.js +228 -0
- package/utils/log_tool.js +190 -0
- package/utils/parse_directive.js +194 -0
- package/utils/parse_tool.js +236 -0
- package/utils/request_tool.js +156 -0
- package/utils/scm_tool.js +108 -0
- package/utils/selection_tool.js +593 -0
- package/utils/shell_tool.js +162 -0
- package/utils/svn_diff/diff_file.js +44 -0
- package/utils/svn_diff/diff_parser.js +55 -0
- package/utils/transform_tool.js +520 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
const markdown_table = require('markdown-table');
|
|
2
|
+
const xml2js = require('xml-js');
|
|
3
|
+
const xmldom = require('xmldom');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
json_compacted,
|
|
7
|
+
} = require("./transform_tool");
|
|
8
|
+
var json_stringify = require('json-stable-stringify');
|
|
9
|
+
const parse_markdown_table = require("parse-markdown-table");
|
|
10
|
+
|
|
11
|
+
const resolveCode = function(doc, node) {
|
|
12
|
+
return eval(`(${node.strValue})`);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const yamlOptions = {
|
|
16
|
+
customTags: [
|
|
17
|
+
{ tag: "!fn", class: Function, resolve: resolveCode },
|
|
18
|
+
{ tag: "!re", class: RegExp, resolve: resolveCode },
|
|
19
|
+
]
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
async function parse(content, type, options) {
|
|
23
|
+
switch (type && type.toLowerCase()) {
|
|
24
|
+
case "markdown_table_array": {
|
|
25
|
+
let result = [];
|
|
26
|
+
const table = await parse_markdown_table.createMarkdownArrayTable(content);
|
|
27
|
+
result.push(table.headers);
|
|
28
|
+
for await (const row of table.rows) {
|
|
29
|
+
if (!row[0].replace(/-/g, "").trim()) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
result.push(row);
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
case "markdown_table":
|
|
37
|
+
case "markdown_table_object": {
|
|
38
|
+
let result = [];
|
|
39
|
+
const table = await parse_markdown_table.createMarkdownObjectTable(content);
|
|
40
|
+
for await (const row of table) {
|
|
41
|
+
let values = Object.values(row);
|
|
42
|
+
if (!values[0].replace(/-/g, "").trim()) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
result.push(row);
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
default: {
|
|
50
|
+
return parse_sync(content, type, options);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parse_sync(content, type, options) {
|
|
56
|
+
switch (type && type.toLowerCase()) {
|
|
57
|
+
case "yml":
|
|
58
|
+
case "yaml": {
|
|
59
|
+
let opts = Object.assign({ }, yamlOptions, { merge: true });
|
|
60
|
+
return require("yaml").parse(content, opts);
|
|
61
|
+
}
|
|
62
|
+
case "json": {
|
|
63
|
+
if (options?.strip_comments) {
|
|
64
|
+
content = require("strip-json-comments")(content);
|
|
65
|
+
}
|
|
66
|
+
return JSON.parse(content);
|
|
67
|
+
}
|
|
68
|
+
case "xml": {
|
|
69
|
+
return new xmldom.DOMParser().parseFromString(content);
|
|
70
|
+
}
|
|
71
|
+
case "xml2json": {
|
|
72
|
+
return xml2js.xml2json(content);
|
|
73
|
+
}
|
|
74
|
+
case "xml2js": {
|
|
75
|
+
return xml2js.xml2js(content);
|
|
76
|
+
}
|
|
77
|
+
case "json2xml": {
|
|
78
|
+
return xml2js.json2xml(content);
|
|
79
|
+
}
|
|
80
|
+
case "js2xml": {
|
|
81
|
+
return xml2js.js2xml(content);
|
|
82
|
+
}
|
|
83
|
+
case "lua_assignment": {
|
|
84
|
+
let ast = require('luaparse').parse(content, {
|
|
85
|
+
locations: true,
|
|
86
|
+
comments: true
|
|
87
|
+
});
|
|
88
|
+
let ret = {};
|
|
89
|
+
for (let i = 0, len = ast.body.length; i < len; ++i) {
|
|
90
|
+
let statement = ast.body[i];
|
|
91
|
+
if (statement.type == "AssignmentStatement") {
|
|
92
|
+
ret[statement.variables[0].name] = eval(statement.init[0].raw);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return ret;
|
|
96
|
+
}
|
|
97
|
+
case "lua": {
|
|
98
|
+
let ast = require('luaparse').parse(content, {
|
|
99
|
+
locations: true,
|
|
100
|
+
comments: true
|
|
101
|
+
});
|
|
102
|
+
let all = ast.body.concat(ast.comments).sort((s1, s2) => {
|
|
103
|
+
return s1.loc.start.line - s2.loc.start.line
|
|
104
|
+
|| s1.loc.start.column - s2.loc.start.column;
|
|
105
|
+
});
|
|
106
|
+
return all;
|
|
107
|
+
}
|
|
108
|
+
case "svn_diff": {
|
|
109
|
+
let ret = require("./svn_diff/diff_parser").parse(content);
|
|
110
|
+
return ret;
|
|
111
|
+
}
|
|
112
|
+
case "sequence": {
|
|
113
|
+
return content.split(/[^0-9\-]+/).map(v => {
|
|
114
|
+
let arr = v.split("-");
|
|
115
|
+
if (arr.length == 2) {
|
|
116
|
+
let ret = [];
|
|
117
|
+
for (let i = +arr[0]; i <= +arr[1]; ++i) {
|
|
118
|
+
ret.push("" + i);
|
|
119
|
+
}
|
|
120
|
+
return ret;
|
|
121
|
+
}
|
|
122
|
+
return v;
|
|
123
|
+
}).flat().filter(v => v.trim()).map(v => +v).sort((a, b) => a - b);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
async function stringify(data, type, options) {
|
|
130
|
+
switch (type && type.toLowerCase()) {
|
|
131
|
+
default: {
|
|
132
|
+
return stringify_sync(data, type, options);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function stringify_sync(data, type, options) {
|
|
138
|
+
switch (type && type.toLowerCase()) {
|
|
139
|
+
case "yml":
|
|
140
|
+
case "yaml": {
|
|
141
|
+
let opts = Object.assign({ }, yamlOptions, options);
|
|
142
|
+
return require("yaml").stringify(data, opts);
|
|
143
|
+
}
|
|
144
|
+
case "yml_compacted":
|
|
145
|
+
case "yaml_compacted": {
|
|
146
|
+
return require("yamljs").stringify(data, options.inline == undefined ? 1 : options.inline, options.space == undefined ? 2 : options.space);
|
|
147
|
+
}
|
|
148
|
+
case "json": {
|
|
149
|
+
options = Object.assign({ space: 4 }, {}, options);
|
|
150
|
+
let defaultSortKeys = [
|
|
151
|
+
|
|
152
|
+
];
|
|
153
|
+
defaultSortKeys = Object.assign(defaultSortKeys, options.sort_keys);
|
|
154
|
+
return json_stringify(data, {
|
|
155
|
+
space: options.indent != null ? options.indent : 4,
|
|
156
|
+
cmp: (a, b) => {
|
|
157
|
+
let idxa = defaultSortKeys.indexOf(a.key);
|
|
158
|
+
let idxb = defaultSortKeys.indexOf(b.key);
|
|
159
|
+
if (idxa != idxb) {
|
|
160
|
+
if (idxa == -1) {
|
|
161
|
+
return 1;
|
|
162
|
+
} else {
|
|
163
|
+
return -1;
|
|
164
|
+
}
|
|
165
|
+
return idxa - idxb;
|
|
166
|
+
}
|
|
167
|
+
let keya = parseInt(a.key);
|
|
168
|
+
let keyb = parseInt(b.key);
|
|
169
|
+
return keya - keyb;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
case "json_compacted": {
|
|
174
|
+
options = Object.assign({ space: 4 }, options);
|
|
175
|
+
return json_compacted(data, options && options.space, options && options.sort_keys);
|
|
176
|
+
}
|
|
177
|
+
case "json_simple": {
|
|
178
|
+
options = Object.assign({ space: 4 }, options);
|
|
179
|
+
return JSON.stringify(data, null, options.space);
|
|
180
|
+
}
|
|
181
|
+
case "markdown_table":{
|
|
182
|
+
let records = [];
|
|
183
|
+
let headers;
|
|
184
|
+
for (const k in data) {
|
|
185
|
+
if (Object.hasOwnProperty.call(data, k)) {
|
|
186
|
+
const ele = data[k];
|
|
187
|
+
if (!headers) {
|
|
188
|
+
headers = options && options.headers || Object.keys(ele);
|
|
189
|
+
records.push(headers);
|
|
190
|
+
}
|
|
191
|
+
records.push(headers.map(header => ele[header]));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return markdown_table(records, options);
|
|
195
|
+
}
|
|
196
|
+
case "string": {
|
|
197
|
+
return data && data.toString();
|
|
198
|
+
}
|
|
199
|
+
case "sequence": {
|
|
200
|
+
let str = "";
|
|
201
|
+
let delimiter = ",", joint = "-";
|
|
202
|
+
let in_sequence = false;
|
|
203
|
+
let prev = null;
|
|
204
|
+
data.sort((a, b) => a - b).forEach((curr, curr_index, arr) => {
|
|
205
|
+
if (prev == null) {
|
|
206
|
+
str += curr;
|
|
207
|
+
} else if (prev != curr - 1) {
|
|
208
|
+
if (in_sequence) {
|
|
209
|
+
str += prev;
|
|
210
|
+
in_sequence = false;
|
|
211
|
+
}
|
|
212
|
+
str += delimiter + curr;
|
|
213
|
+
} else if (!in_sequence) {
|
|
214
|
+
str += joint;
|
|
215
|
+
in_sequence = true;
|
|
216
|
+
}
|
|
217
|
+
if (curr_index == arr.length - 1 && in_sequence) {
|
|
218
|
+
str += curr;
|
|
219
|
+
}
|
|
220
|
+
prev = curr;
|
|
221
|
+
});
|
|
222
|
+
return str;
|
|
223
|
+
}
|
|
224
|
+
default: {
|
|
225
|
+
// 默认返回自身
|
|
226
|
+
return data;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
module.exports = {
|
|
232
|
+
parse,
|
|
233
|
+
parse_sync,
|
|
234
|
+
stringify,
|
|
235
|
+
stringify_sync,
|
|
236
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
const { get_full_path, remove } = require("./file_tool");
|
|
2
|
+
const transform_tool = require("./transform_tool");
|
|
3
|
+
const { warn, info, log, setLastError } = require("./log_tool");
|
|
4
|
+
const { progress } = require("./display_tool");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const { eval_code, shareData } = require("../commands/share_data");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const { hash } = require('./hash_tool');
|
|
9
|
+
|
|
10
|
+
async function doRequest(data) {
|
|
11
|
+
const request = require("request");
|
|
12
|
+
const request_progress = require("request-progress");
|
|
13
|
+
|
|
14
|
+
let url = data.src || data?.target?.url;
|
|
15
|
+
let urlObj = new URL(url);
|
|
16
|
+
/** 是否启用 http 缓存 */
|
|
17
|
+
const enable_http_cache = data.enable_cache || false;
|
|
18
|
+
/** @type {http.RequestOptions} */
|
|
19
|
+
let options = {
|
|
20
|
+
method: "GET",
|
|
21
|
+
rejectUnauthorized: false,
|
|
22
|
+
timeout: 10 * 60 * 1000, // 超时(ms)
|
|
23
|
+
};
|
|
24
|
+
let maxRetryTimes = 0;
|
|
25
|
+
let leftRetryTimes = maxRetryTimes;
|
|
26
|
+
options = Object.assign(options, data.options);
|
|
27
|
+
if (!options.proxy) {
|
|
28
|
+
options.proxy = shareData.ENV.NODE_AUTOMATOR_PROXY;
|
|
29
|
+
}
|
|
30
|
+
/** @type {string} */
|
|
31
|
+
let http_cache;
|
|
32
|
+
let cache_name = `[${options.method}]` + hash(url) + "." + (path.basename(urlObj.pathname) || "index.html");
|
|
33
|
+
let cache_path = path.resolve(shareData.AUTOMATOR_APPDATA, ".raw_cache", cache_name);
|
|
34
|
+
if (enable_http_cache && fs.existsSync(cache_path)) {
|
|
35
|
+
http_cache = fs.readFileSync(cache_path);
|
|
36
|
+
}
|
|
37
|
+
let result;
|
|
38
|
+
let useCache = enable_http_cache && http_cache;
|
|
39
|
+
let quiet = data.quiet;
|
|
40
|
+
!quiet && info(useCache ? "[缓存]" : `[${options.method}] ${url} `);
|
|
41
|
+
let hasProgress = false;
|
|
42
|
+
if (useCache) {
|
|
43
|
+
result = http_cache;
|
|
44
|
+
if (data.dst) {
|
|
45
|
+
let file = fs.createWriteStream(get_full_path(data.dst, "FILE"));
|
|
46
|
+
file.write(result);
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
result = await new Promise(resolve => {
|
|
50
|
+
let sizeTotal = 0;
|
|
51
|
+
let fileDst = data.dst || shareData.AUTOMATOR_SCRATCH + "/request_tool/" + cache_name;
|
|
52
|
+
let file = fs.createWriteStream(get_full_path(fileDst, "FILE"));
|
|
53
|
+
let cacheDir = path.dirname(cache_path);
|
|
54
|
+
if (!fs.existsSync(cacheDir)) {
|
|
55
|
+
fs.mkdirSync(cacheDir, {
|
|
56
|
+
recursive: true
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
let cachedFile;
|
|
60
|
+
if (enable_http_cache) {
|
|
61
|
+
cachedFile = fs.createWriteStream(cache_path);
|
|
62
|
+
}
|
|
63
|
+
function send_request() {
|
|
64
|
+
let req = request(url, options, (err, res, result) => {
|
|
65
|
+
if (err) {
|
|
66
|
+
// 检查重试
|
|
67
|
+
if (leftRetryTimes > 0) {
|
|
68
|
+
--leftRetryTimes;
|
|
69
|
+
!quiet && warn(`[重试] ${url} ${err}`);
|
|
70
|
+
send_request();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
setLastError(`${url} ${err}`);
|
|
74
|
+
resolve();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
sizeTotal = res.client.bytesRead;
|
|
78
|
+
!quiet && progress(sizeTotal, sizeTotal, { desc: "完成", format_bytes: true, color: "green" });
|
|
79
|
+
// 仅在响应码合法时,写入缓存
|
|
80
|
+
if (enable_http_cache) {
|
|
81
|
+
if (res.statusCode > 0 && res.statusCode < 400) {
|
|
82
|
+
|
|
83
|
+
} else {
|
|
84
|
+
if (fs.existsSync(cache_path)) {
|
|
85
|
+
fs.unlinkSync(cache_path);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (!data.ignore_status_code) {
|
|
90
|
+
if (res.statusCode >= 400) {
|
|
91
|
+
setLastError(`${options.method} ${url} ${res.statusCode} (${res.statusMessage}) "${typeof res.body == "string" ? res.body : JSON.stringify(res.body)}" `);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// 返回校验
|
|
95
|
+
if (data.response_validator) {
|
|
96
|
+
let [ok, errmsg] = eval_code(data.response_validator)(res);
|
|
97
|
+
if (!ok) {
|
|
98
|
+
setLastError(errmsg || "校验失败");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
switch (data.response_type) {
|
|
102
|
+
case "HEADERS": {
|
|
103
|
+
resolve(res.headers);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "BODY": {
|
|
107
|
+
resolve(res.body);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case "STATUS_CODE": {
|
|
111
|
+
resolve(res.statusCode);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "RAW": {
|
|
115
|
+
resolve(res);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case "DEFAULT":
|
|
119
|
+
default: {
|
|
120
|
+
if (options.method.toLowerCase() == "head") {
|
|
121
|
+
resolve(res.headers);
|
|
122
|
+
} else {
|
|
123
|
+
resolve(result);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
req.pipe(file);
|
|
129
|
+
if (enable_http_cache) {
|
|
130
|
+
req.pipe(cachedFile);
|
|
131
|
+
}
|
|
132
|
+
request_progress(
|
|
133
|
+
req.on('progress', function (state) {
|
|
134
|
+
sizeTotal = state.size.total;
|
|
135
|
+
hasProgress = true;
|
|
136
|
+
!quiet && progress(state.size.transferred, state.size.total, {
|
|
137
|
+
desc: `请求中...(elapsed: ${transform_tool.formatTimeInSec(state.time.elapsed)}, speed: ${transform_tool.formatByteSize(state.speed)}/s, ETA: ${transform_tool.formatTimeInSec(state.time.remaining)})`,
|
|
138
|
+
format_bytes: true,
|
|
139
|
+
color: "cyan"
|
|
140
|
+
}, true);
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
send_request();
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
if (hasProgress) {
|
|
149
|
+
log("");
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
module.exports = {
|
|
155
|
+
doRequest,
|
|
156
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
const { option } = require("yargs");
|
|
2
|
+
const { vital, log, warn } = require("./log_tool");
|
|
3
|
+
const { exec_shell } = require("./shell_tool");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const { copy, get_fst_file, get_full_path } = require("./file_tool");
|
|
7
|
+
|
|
8
|
+
async function svn_is_dirty(cwd) {
|
|
9
|
+
return await exec_shell({
|
|
10
|
+
cmd: "svn status . --quiet",
|
|
11
|
+
cwd: cwd,
|
|
12
|
+
capture_stdout: true,
|
|
13
|
+
}, true);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function git_is_dirty(cwd) {
|
|
17
|
+
let result = await exec_shell({
|
|
18
|
+
cmd: "git status .",
|
|
19
|
+
cwd: cwd,
|
|
20
|
+
capture_stdout: true,
|
|
21
|
+
}, true);
|
|
22
|
+
return result && result.indexOf("nothing to commit, working tree clean") == -1;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function git_commit(cwd, msg) {
|
|
26
|
+
await exec_shell({
|
|
27
|
+
cmd: "git add .",
|
|
28
|
+
cwd: cwd
|
|
29
|
+
}, true);
|
|
30
|
+
await exec_shell({
|
|
31
|
+
cmd: "git commit -m".split(" ").concat(msg),
|
|
32
|
+
cwd: cwd,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function git_push(cwd, options) {
|
|
37
|
+
let remote = options.remote || (await exec_shell({cmd: "git remote", capture_stdout: true})).trim();
|
|
38
|
+
let branch = options.branch || (await exec_shell({cmd: "git symbolic-ref --short -q HEAD", capture_stdout: true})).trim();
|
|
39
|
+
if (!remote || !branch) {
|
|
40
|
+
warn(`在 ${cwd} 中未找到 remote 和 branch, 无法 执行 git push`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
let cmd = "git push";
|
|
44
|
+
if (options.remote && options.branch) {
|
|
45
|
+
cmd = `git push --set-upstream ${options.remote} ${options.branch}`;
|
|
46
|
+
}
|
|
47
|
+
await exec_shell({
|
|
48
|
+
cmd: cmd,
|
|
49
|
+
cwd: cwd,
|
|
50
|
+
shell: true,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function git_export_diff_head(cwd, dst, base, options) {
|
|
55
|
+
let diff = await exec_shell({
|
|
56
|
+
// cmd: "git diff HEAD^ HEAD --stat",
|
|
57
|
+
// cmd: "git diff --name-only HEAD~1",
|
|
58
|
+
cmd: "git diff --name-only HEAD^",
|
|
59
|
+
cwd: cwd,
|
|
60
|
+
capture_stdout: true,
|
|
61
|
+
}, true);
|
|
62
|
+
let git_root = await exec_shell({
|
|
63
|
+
cmd: "git rev-parse --show-toplevel",
|
|
64
|
+
cwd: cwd,
|
|
65
|
+
capture_stdout: true,
|
|
66
|
+
}, true);
|
|
67
|
+
git_root = git_root.trim();
|
|
68
|
+
if (!git_root) {
|
|
69
|
+
throw ('git根目录未找到');
|
|
70
|
+
}
|
|
71
|
+
/** @type {string[]} */
|
|
72
|
+
let lines = diff.split("\n");
|
|
73
|
+
/** @type {string[]} */
|
|
74
|
+
let files = [];
|
|
75
|
+
for (let line of lines) {
|
|
76
|
+
let raw_file = line.trim();
|
|
77
|
+
if (!raw_file)
|
|
78
|
+
break;
|
|
79
|
+
let file = path.join(git_root, raw_file);
|
|
80
|
+
if (fs.existsSync(file)) {
|
|
81
|
+
files.push(file);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
await copy(files, dst, base, options);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function svn_export_diff_head(cwd, dst, base, options) {
|
|
88
|
+
cwd = get_full_path(cwd);
|
|
89
|
+
dst = get_full_path(dst);
|
|
90
|
+
let diff = await exec_shell({
|
|
91
|
+
cmd: 'svn diff -r PREV:HEAD --summarize',
|
|
92
|
+
cwd: cwd,
|
|
93
|
+
capture_stdout: true,
|
|
94
|
+
}, true);
|
|
95
|
+
let lines = diff.split('\n');
|
|
96
|
+
let files = lines.filter(v => v).map(v => path.join(cwd, /\w\s+(\S+)/.exec(v)[1]));
|
|
97
|
+
await copy(files, dst, base, options);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
module.exports = {
|
|
102
|
+
svn_is_dirty,
|
|
103
|
+
git_is_dirty,
|
|
104
|
+
git_commit,
|
|
105
|
+
git_push,
|
|
106
|
+
git_export_diff_head,
|
|
107
|
+
svn_export_diff_head,
|
|
108
|
+
};
|