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.
Files changed (143) hide show
  1. package/LICENSE +15 -0
  2. package/assets/alert.png +0 -0
  3. package/assets/error.png +0 -0
  4. package/assets/success.png +0 -0
  5. package/commands/abort.js +11 -0
  6. package/commands/alert.js +19 -0
  7. package/commands/assemble.js +56 -0
  8. package/commands/assert.js +18 -0
  9. package/commands/base.js +363 -0
  10. package/commands/break.js +11 -0
  11. package/commands/change_filter.js +34 -0
  12. package/commands/cheerio.js +15 -0
  13. package/commands/clear_screen.js +14 -0
  14. package/commands/commit.js +31 -0
  15. package/commands/compare.js +23 -0
  16. package/commands/confirm.js +16 -0
  17. package/commands/continue.js +11 -0
  18. package/commands/copy.js +14 -0
  19. package/commands/cursor_up.js +12 -0
  20. package/commands/date.js +25 -0
  21. package/commands/debugger.js +11 -0
  22. package/commands/dingtalk.js +110 -0
  23. package/commands/disassemble.js +36 -0
  24. package/commands/divination.js +15 -0
  25. package/commands/download.js +12 -0
  26. package/commands/edit.js +83 -0
  27. package/commands/edit_excel.js +57 -0
  28. package/commands/env_optional.js +57 -0
  29. package/commands/eval.js +27 -0
  30. package/commands/exec_from_file.js +31 -0
  31. package/commands/export_diff_head.js +31 -0
  32. package/commands/file_info.js +41 -0
  33. package/commands/file_size.js +12 -0
  34. package/commands/files.js +14 -0
  35. package/commands/first_exist.js +18 -0
  36. package/commands/flatten.js +18 -0
  37. package/commands/ftp.js +161 -0
  38. package/commands/git_commit.js +31 -0
  39. package/commands/git_export_diff_head.js +13 -0
  40. package/commands/git_is_dirty.js +13 -0
  41. package/commands/hash.js +21 -0
  42. package/commands/http_request.js +12 -0
  43. package/commands/image_crop.js +19 -0
  44. package/commands/input.js +37 -0
  45. package/commands/interval.js +60 -0
  46. package/commands/is_dirty.js +31 -0
  47. package/commands/jwt_decode.js +22 -0
  48. package/commands/list2dict.js +32 -0
  49. package/commands/log.js +72 -0
  50. package/commands/loop.js +115 -0
  51. package/commands/loop_forever.js +66 -0
  52. package/commands/merge_files.js +39 -0
  53. package/commands/mgr.js +476 -0
  54. package/commands/modify.js +44 -0
  55. package/commands/move.js +14 -0
  56. package/commands/move_local.js +14 -0
  57. package/commands/multi_select.js +16 -0
  58. package/commands/mysql_connect.js +29 -0
  59. package/commands/mysql_end.js +13 -0
  60. package/commands/mysql_query.js +19 -0
  61. package/commands/notify.js +13 -0
  62. package/commands/open.js +30 -0
  63. package/commands/parse.js +18 -0
  64. package/commands/pause.js +15 -0
  65. package/commands/print.js +47 -0
  66. package/commands/read_cache.js +12 -0
  67. package/commands/read_cfg.js +13 -0
  68. package/commands/read_clipboard.js +19 -0
  69. package/commands/read_excel.js +17 -0
  70. package/commands/read_only_excel.js +40 -0
  71. package/commands/read_plain.js +14 -0
  72. package/commands/remove.js +14 -0
  73. package/commands/retrieve.js +17 -0
  74. package/commands/reveal.js +33 -0
  75. package/commands/revision_info.js +31 -0
  76. package/commands/schedule.js +80 -0
  77. package/commands/select.js +17 -0
  78. package/commands/send_email.js +25 -0
  79. package/commands/share_data.js +745 -0
  80. package/commands/shell.js +13 -0
  81. package/commands/sleep.js +17 -0
  82. package/commands/store.js +13 -0
  83. package/commands/store_optional.js +33 -0
  84. package/commands/stream.js +16 -0
  85. package/commands/stringify.js +24 -0
  86. package/commands/svn_add.js +26 -0
  87. package/commands/svn_commit.js +49 -0
  88. package/commands/svn_export_diff_head.js +13 -0
  89. package/commands/svn_is_dirty.js +13 -0
  90. package/commands/svn_update.js +27 -0
  91. package/commands/switch.js +18 -0
  92. package/commands/temp.js +12 -0
  93. package/commands/tencent_cos.js +323 -0
  94. package/commands/test.js +14 -0
  95. package/commands/title.js +18 -0
  96. package/commands/tmpdir.js +21 -0
  97. package/commands/translate.js +58 -0
  98. package/commands/traversal.js +38 -0
  99. package/commands/truncate.js +31 -0
  100. package/commands/truncate_all.js +62 -0
  101. package/commands/ts_sort.js +18 -0
  102. package/commands/unzip.js +50 -0
  103. package/commands/updating_revision.js +28 -0
  104. package/commands/valid_required.js +31 -0
  105. package/commands/void.js +10 -0
  106. package/commands/watch_mail.js +94 -0
  107. package/commands/web_socket.js +233 -0
  108. package/commands/which.js +60 -0
  109. package/commands/write_cache.js +16 -0
  110. package/commands/write_cfg.js +18 -0
  111. package/commands/write_clipboard.js +23 -0
  112. package/commands/write_excel.js +17 -0
  113. package/commands/write_plain.js +18 -0
  114. package/commands/zip.js +78 -0
  115. package/commands/zip_data.js +64 -0
  116. package/examples/print_env.yml +3 -0
  117. package/index.js +109 -0
  118. package/package.json +63 -0
  119. package/scratch/.gitkeep +0 -0
  120. package/utils/alert_tool.js +133 -0
  121. package/utils/base64_tool.js +13 -0
  122. package/utils/cache/local_cache.js +79 -0
  123. package/utils/cache/raw_cache.js +47 -0
  124. package/utils/cache/simple_local_cache.js +33 -0
  125. package/utils/cache/simple_sqlite_cache.js +43 -0
  126. package/utils/cache/simple_svn_cache.js +49 -0
  127. package/utils/cache/svn_cache.js +40 -0
  128. package/utils/cache_tool.js +53 -0
  129. package/utils/display_tool.js +85 -0
  130. package/utils/divination_util.js +555 -0
  131. package/utils/file_tool.js +529 -0
  132. package/utils/hash_tool.js +8 -0
  133. package/utils/interaction_tool.js +228 -0
  134. package/utils/log_tool.js +190 -0
  135. package/utils/parse_directive.js +194 -0
  136. package/utils/parse_tool.js +236 -0
  137. package/utils/request_tool.js +156 -0
  138. package/utils/scm_tool.js +108 -0
  139. package/utils/selection_tool.js +593 -0
  140. package/utils/shell_tool.js +162 -0
  141. package/utils/svn_diff/diff_file.js +44 -0
  142. package/utils/svn_diff/diff_parser.js +55 -0
  143. package/utils/transform_tool.js +520 -0
@@ -0,0 +1,60 @@
1
+ const { BaseCommand } = require("./base");
2
+ const { exec_shell } = require('../utils/shell_tool');
3
+ const { get_fst_file } = require("../utils/file_tool");
4
+ const path = require("path");
5
+ const fs = require("fs");
6
+ const { warn } = require("../utils/log_tool");
7
+
8
+ class WhichCommand extends BaseCommand {
9
+ async execute() {
10
+ let cmd;
11
+ switch (process.platform) {
12
+ case "win32": {
13
+ cmd = "where";
14
+ break;
15
+ }
16
+ default: {
17
+ cmd = "which";
18
+ break;
19
+ }
20
+ }
21
+
22
+ let file;
23
+ let stdout = await exec_shell({
24
+ cmd: `${cmd} ${this.selfData.target}`,
25
+ capture_stdout: true,
26
+ ignore_code: true,
27
+ });
28
+ if (stdout) {
29
+ /** @type {string[]} */
30
+ let files = stdout.split(/[\r\n]{1,2}/);
31
+ files = files.filter(v => v && v.trim());
32
+ // windows系统下,优先选择有 .cmd 或者 .exe
33
+ if (process.platform === "win32") {
34
+ let preferExts = ['.exe', '.cmd'];
35
+ files.sort((a, b) => {
36
+ let exta = path.extname(a);
37
+ let extb = path.extname(b);
38
+ if (preferExts.includes(exta)) {
39
+ return -1;
40
+ } else if (preferExts.includes(extb)) {
41
+ return 1;
42
+ }
43
+ return 0;
44
+ });
45
+ }
46
+ file = get_fst_file(files);
47
+ if (file) {
48
+ file = fs.realpathSync(file);
49
+ }
50
+ }
51
+ if (!file) {
52
+ warn(`未找到 ${this.selfData.target}`);
53
+ }
54
+ return file;
55
+ }
56
+ }
57
+
58
+ module.exports = {
59
+ WhichCommand,
60
+ };
@@ -0,0 +1,16 @@
1
+ const { getCache, setCache } = require("../utils/cache_tool");
2
+ const { BaseCommand } = require("./base");
3
+
4
+ class WriteCacheCommand extends BaseCommand {
5
+ async execute() {
6
+ await setCache(this.selfData.cache_key, this.content, this.selfData.cache_type, this.selfData.cache_param);
7
+ }
8
+
9
+ getRequireContent() {
10
+ return true;
11
+ }
12
+ }
13
+
14
+ module.exports = {
15
+ WriteCacheCommand,
16
+ };
@@ -0,0 +1,18 @@
1
+ const { write_with_type } = require("../utils/file_tool");
2
+ const { BaseCommand } = require("./base");
3
+
4
+ class WriteCfgCommand extends BaseCommand {
5
+ async execute() {
6
+ let data = this.selfData;
7
+ let content = this.content;
8
+ await write_with_type(content, data.dst, data.type, data.options);
9
+ }
10
+
11
+ getRequireContent() {
12
+ return true;
13
+ }
14
+ }
15
+
16
+ module.exports = {
17
+ WriteCfgCommand,
18
+ };
@@ -0,0 +1,23 @@
1
+ const clipboardy = require('clipboardy');
2
+ const { warn } = require('../utils/log_tool');
3
+ const { BaseCommand } = require('./base');
4
+
5
+ class WriteClipboard extends BaseCommand {
6
+ async execute() {
7
+ let data = this.selfData;
8
+ let content = typeof this.content === "string" ? this.content : JSON.stringify(this.content);
9
+ try {
10
+ clipboardy.writeSync(content);
11
+ } catch (error) {
12
+ warn('写入剪切板失败');
13
+ }
14
+ }
15
+
16
+ getRequireContent() {
17
+ return true;
18
+ }
19
+ }
20
+
21
+ module.exports = {
22
+ WriteClipboard,
23
+ };
@@ -0,0 +1,17 @@
1
+ const { get_fst_file, get_full_path } = require("../utils/file_tool");
2
+ const { BaseCommand } = require("./base");
3
+
4
+ class WriteExcelCommand extends BaseCommand {
5
+ async execute() {
6
+ let dst = get_full_path(this.selfData.dst);
7
+ /**
8
+ * @type {Excel.Workbook}
9
+ */
10
+ let workbook = this.selfData.content;
11
+ await workbook.xlsx.writeFile(dst);
12
+ }
13
+ }
14
+
15
+ module.exports = {
16
+ WriteExcelCommand,
17
+ };
@@ -0,0 +1,18 @@
1
+ const { write_plain } = require("../utils/file_tool");
2
+ const { BaseCommand } = require("./base");
3
+
4
+ class WritePlainCommand extends BaseCommand {
5
+ async execute() {
6
+ let data = this.selfData;
7
+ let content = this.content;
8
+ write_plain(data.dst, content, data.options);
9
+ }
10
+
11
+ getRequireContent() {
12
+ return true;
13
+ }
14
+ }
15
+
16
+ module.exports = {
17
+ WritePlainCommand,
18
+ };
@@ -0,0 +1,78 @@
1
+ const { BaseCommand } = require("./base");
2
+ const path = require("path");
3
+ const { get_file_list, get_fst_file, get_full_path, processRename } = require("../utils/file_tool");
4
+ const fs = require("fs");
5
+ const { progress } = require("../utils/display_tool");
6
+ const { Alignment, alignStr } = require("../utils/transform_tool");
7
+ const { clearLine } = require("../utils/interaction_tool");
8
+ const { info } = require("../utils/log_tool");
9
+
10
+ class ZipCommand extends BaseCommand {
11
+ async execute() {
12
+ const AdmZip = require("adm-zip");
13
+ let data = this.selfData;
14
+ let files = get_file_list(data.src, undefined, true);
15
+ let base = data.base;
16
+ if (base) {
17
+ base = get_fst_file(base);
18
+ }
19
+ // creating archives
20
+ var zip = new AdmZip();
21
+
22
+ // add file directly
23
+ // var content = "inner content of the file";
24
+ // zip.addFile("test.txt", Buffer.alloc(content.length, content), "entry comment goes here");
25
+ // get everything as a buffer
26
+ // var willSendthis = zip.toBuffer();
27
+ // add local file
28
+ for (let index = 0; index < files.length; index++) {
29
+ let file = files[index];
30
+ let zipPath = base ? path.relative(base, path.dirname(file)) : "";
31
+ if (data.base_range) {
32
+ let zipPathParts = zipPath.split("\\").slice(...data.base_range);
33
+ zipPath = path.join(...zipPathParts);
34
+ }
35
+ let entryName = path.join(zipPath, path.basename(file));
36
+ entryName = processRename(entryName, data.options && data.options.rename);
37
+ let show_src = entryName;
38
+ progress(index, files.length, {
39
+ desc: `Add ${show_src}`,
40
+ depth: 0,
41
+ align: true,
42
+ });
43
+ // zip.addLocalFile(file, zipPath);
44
+ zip.addFile(entryName, fs.readFileSync(file));
45
+ }
46
+ clearLine();
47
+ progress(files.length, files.length, {
48
+ desc: `压缩中...`,
49
+ depth: 0,
50
+ align: true,
51
+ });
52
+ // or write everything to disk
53
+ if (data.dst) {
54
+ let dst = get_full_path(data.dst);
55
+ await new Promise(resolve => {
56
+ zip.writeZip(dst, (err) => {
57
+ if (err) {
58
+ throw (err);
59
+ }
60
+ resolve();
61
+ });
62
+ });
63
+ } else {
64
+ let buffer = zip.toBuffer();
65
+ return buffer;
66
+ }
67
+ progress(files.length, files.length, {
68
+ desc: `压缩完成!`,
69
+ depth: 0,
70
+ color: 'green',
71
+ });
72
+ info("");
73
+ }
74
+ }
75
+
76
+ module.exports = {
77
+ ZipCommand,
78
+ };
@@ -0,0 +1,64 @@
1
+ const { BaseCommand } = require("./base");
2
+ const path = require("path");
3
+ const { get_file_list, get_fst_file, get_full_path, processRename } = require("../utils/file_tool");
4
+ const fs = require("fs");
5
+ const { progress } = require("../utils/display_tool");
6
+ const { Alignment, alignStr } = require("../utils/transform_tool");
7
+ const { clearLine } = require("../utils/interaction_tool");
8
+ const { info } = require("../utils/log_tool");
9
+
10
+ class ZipDataCommand extends BaseCommand {
11
+ async execute() {
12
+ const AdmZip = require("adm-zip");
13
+ let data = this.selfData;
14
+ /** @type { {path: string, content: string | Buffer}[] } */
15
+ let entries = this.selfData.entries;
16
+ // creating archives
17
+ var zip = new AdmZip();
18
+
19
+ for (let index = 0; index < entries.length; index++) {
20
+ let entry = entries[index];
21
+ let entryName = entry.path;
22
+ entryName = processRename(entryName, data.options && data.options.rename);
23
+ let show_src = entryName;
24
+ progress(index, entries.length, {
25
+ desc: `Add ${show_src}`,
26
+ depth: 0,
27
+ align: true,
28
+ });
29
+ // zip.addLocalFile(file, zipPath);
30
+ zip.addFile(entryName, entry.content);
31
+ }
32
+ clearLine();
33
+ progress(entries.length, entries.length, {
34
+ desc: `压缩中...`,
35
+ depth: 0,
36
+ align: true,
37
+ });
38
+ // or write everything to disk
39
+ if (data.dst) {
40
+ let dst = get_full_path(data.dst);
41
+ await new Promise(resolve => {
42
+ zip.writeZip(dst, (err) => {
43
+ if (err) {
44
+ throw (err);
45
+ }
46
+ resolve();
47
+ });
48
+ });
49
+ } else {
50
+ let buffer = zip.toBuffer();
51
+ return buffer;
52
+ }
53
+ progress(entries.length, entries.length, {
54
+ desc: `压缩完成!`,
55
+ depth: 0,
56
+ color: 'green',
57
+ });
58
+ info("");
59
+ }
60
+ }
61
+
62
+ module.exports = {
63
+ ZipDataCommand,
64
+ };
@@ -0,0 +1,3 @@
1
+ - type: print
2
+ data:
3
+ key: ENV.USERNAME
package/index.js ADDED
@@ -0,0 +1,109 @@
1
+ // 控制台文本颜色
2
+ const path = require("path");
3
+ require("colors");
4
+ const {
5
+ argv,
6
+ } = require('yargs');
7
+ require('dotenv').config({
8
+ path: argv.dotenv || path.resolve(process.cwd(), '.env'),
9
+ override: true,
10
+ });
11
+
12
+ // 禁用其他打印
13
+ console.log = () => {};
14
+ console.warn = () => {};
15
+ console.info = () => {};
16
+ console.error = () => {};
17
+
18
+ const mgr = require("./commands/mgr");
19
+ const {
20
+ getLastError,
21
+ info,
22
+ setLogLevel,
23
+ LogLevel,
24
+ success,
25
+ error,
26
+ getLastErrorCode,
27
+ error2,
28
+ } = require("./utils/log_tool");
29
+ const {
30
+ showAlert,
31
+ showNotify
32
+ } = require("./utils/alert_tool");
33
+ const {
34
+ parse
35
+ } = require("./utils/parse_tool");
36
+ const {
37
+ shareData
38
+ } = require("./commands/share_data");
39
+ const { decode } = require('./utils/base64_tool');
40
+
41
+ process.on('exit', (code) => {
42
+ if (!code) {
43
+ success(`>>> 任务结束 <<<`);
44
+ } else {
45
+ error(`>>> 任务异常结束 #${code} <<<`);
46
+ }
47
+ });
48
+
49
+ setLogLevel(argv.LOG_LEVEL || LogLevel.INFO);
50
+ mgr.init();
51
+
52
+ /** @return {Promise<string>} */
53
+ async function getCfgs() {
54
+ let cfgs = argv._[0];
55
+ if (cfgs) return cfgs;
56
+ return new Promise((resolve) => {
57
+ process.stdin.on("data", data => {
58
+ // 标准输入模式,启用全自动
59
+ shareData.FULL_AUTOMATIC = 1;
60
+ resolve(data.toString().trim());
61
+ });
62
+ });
63
+ }
64
+ async function MainProcess() {
65
+ let lastError = null;
66
+ const cfgs = await getCfgs();
67
+ try {
68
+ if (cfgs.indexOf(".") != -1) {
69
+ await mgr.exec([{
70
+ type: "exec_from_file",
71
+ data: {
72
+ src: cfgs
73
+ }
74
+ }], 0, false);
75
+ } else {
76
+ await mgr.exec(await parse(decode(cfgs), 'yaml'), 0);
77
+ }
78
+ lastError = getLastError();
79
+ lastError = lastError && lastError.message || lastError;
80
+ } catch (err) {
81
+ lastError = err.message || err;
82
+ }
83
+ let code = lastError ? (getLastErrorCode() || 1) : 0;
84
+ let extname = path.extname(argv.$0);
85
+ let hasError = code != 0;
86
+ info("");
87
+ if (hasError) {
88
+ error2(lastError);
89
+ }
90
+ if (shareData.VERBOSE) {
91
+ // 非代码启动,需要暂停
92
+ if (extname != ".js") {
93
+ let title = `${shareData.TITLE || "任务即将结束"}`;
94
+ await showAlert(hasError ? "error" : "info", hasError ? `✖ 失败, ${lastError}` : "✔ 成功!", title);
95
+ } else {
96
+ await showNotify({
97
+ title: `${shareData.TITLE || cfgs}`,
98
+ message: hasError ? lastError : `任务即将结束`,
99
+ icon: shareData.AUTOMATOR_ROOT + (hasError ? "/assets/error.png" : "/assets/success.png"),
100
+ });
101
+ }
102
+ }
103
+ // 手动调用 exit, 会导致异步的定时器事件失效
104
+ // process.exit(code);
105
+ process.exitCode = code;
106
+ }
107
+
108
+
109
+ MainProcess();
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "node-automator",
3
+ "version": "1.0.0",
4
+ "description": "Execute automation with yaml configuration(compatible with json)",
5
+ "main": "index.js",
6
+ "repository": {
7
+ "url": "https://gitee.com/TsubasaYeung/node-automator.git",
8
+ "type": "git"
9
+ },
10
+ "scripts": {
11
+ "test": "node . ./examples/print_env.yml"
12
+ },
13
+ "keywords": [
14
+ "node-automator"
15
+ ],
16
+ "author": "TsubasaYeung",
17
+ "license": "ISC",
18
+ "dependencies": {
19
+ "adm-zip": "^0.5.1",
20
+ "await-event-emitter": "^2.0.2",
21
+ "change-case": "^4.1.2",
22
+ "chardet": "^1.3.0",
23
+ "cheerio": "^1.0.0-rc.12",
24
+ "clipboardy": "^2.3.0",
25
+ "colors": "^1.4.0",
26
+ "cos-nodejs-sdk-v5": "^2.11.6",
27
+ "dotenv": "^16.1.4",
28
+ "emailjs": "^4.0.2",
29
+ "exceljs": "^4.3.0",
30
+ "ftp": "^0.3.10",
31
+ "glob": "^7.1.6",
32
+ "html-to-text": "^8.2.0",
33
+ "iconv-lite": "^0.6.2",
34
+ "image-size": "^1.0.0",
35
+ "jimp": "^0.22.10",
36
+ "json-stable-stringify": "^1.0.1",
37
+ "jwt-js": "^0.5.0",
38
+ "keypress": "^0.2.1",
39
+ "log4js": "^6.3.0",
40
+ "luaparse": "^0.3.1",
41
+ "mail-watcher": "^0.6.1",
42
+ "markdown-table": "^2.0.0",
43
+ "mysql": "^2.18.1",
44
+ "node-localstorage": "^2.1.6",
45
+ "node-notifier": "^9.0.1",
46
+ "open": "^7.3.1",
47
+ "parse-markdown-table": "^0.0.4",
48
+ "persist-path": "^1.0.2",
49
+ "request": "^2.88.2",
50
+ "request-progress": "^3.0.0",
51
+ "spreadsheet-column": "^1.1.1",
52
+ "strip-json-comments": "^3.1.1",
53
+ "temp": "^0.9.4",
54
+ "typescript-plus": "^3.1.5",
55
+ "websocket": "^1.0.34",
56
+ "xlsx": "^0.17.0",
57
+ "xml-js": "^1.6.11",
58
+ "xmldom": "^0.4.0",
59
+ "yaml": "^1.10.0",
60
+ "yamljs": "^0.3.0",
61
+ "yargs": "^16.2.0"
62
+ }
63
+ }
File without changes
@@ -0,0 +1,133 @@
1
+ const {
2
+ shareData
3
+ } = require('../commands/share_data');
4
+ const {
5
+ copy,
6
+ get_fst_file,
7
+ write_plain
8
+ } = require('./file_tool');
9
+ const {
10
+ warn
11
+ } = require('./log_tool');
12
+ const {
13
+ exec_shell
14
+ } = require('./shell_tool');
15
+ const notifier = require('node-notifier');
16
+
17
+ /**
18
+ *
19
+ * @param { "error" | "info" | "warning" | ""} type
20
+ * @param {string} message
21
+ * @param {string} title
22
+ * @returns {boolean} 是否点击OK
23
+ */
24
+ async function showAlert(type = "info", message, title = "提示") {
25
+ let os_name = process.platform;
26
+ let str = message;
27
+ let cmd = [];
28
+ let cwd = shareData.AUTOMATOR_ROOT;
29
+ if (os_name == 'linux') {
30
+ str = str.replace(/[<>]/g, '');
31
+ cmd.push('zenity');
32
+ cmd.push('--' + type);
33
+ cmd.push('--text') && cmd.push(str);
34
+ cmd.push('--title') && cmd.push(title);
35
+ if (str.length > 30) cmd.push('--width') && cmd.push('300');
36
+ // return codes in macOS are exactly as in Linux
37
+ // 0 for 'OK', 1 for 'Cancel'
38
+ } else if (os_name == 'darwin') {
39
+
40
+ switch (type) { // Set dialog icon
41
+ case 'error':
42
+ type = 0;
43
+ break;
44
+ case 'info':
45
+ type = 1;
46
+ break;
47
+ case 'warning':
48
+ type = 2;
49
+ break;
50
+ default:
51
+ type = '';
52
+ }
53
+
54
+ str = str.replace(/"/g, "'"); // double quotes to single quotes
55
+ cmd.push('osascript') && cmd.push('-e');
56
+ var script = 'tell app \"System Events\" to display dialog ';
57
+ script += '\"' + str + '\" with title \"' + title + '\" buttons \"OK\"';
58
+ script += ' with icon ' + type;
59
+ cmd.push(script);
60
+
61
+ } else { // windows
62
+
63
+ // return codes for Windows (default value minus 1)
64
+ // defaults in https://www.tutorialspoint.com/vbscript/vbscript_dialog_boxes.htm
65
+
66
+ // 0 - vbOK
67
+ // 1 - vbCancel
68
+ // 2 - vbAbort
69
+ // 3 - vbRetry
70
+ // 4 - vbIgnore
71
+ // 5 - vbYes
72
+ // 6 - vbNo
73
+
74
+ switch (type) { // Set MsgBox icon
75
+ case 'error':
76
+ type = 16;
77
+ break;
78
+ case 'info':
79
+ type = 64;
80
+ break;
81
+ case 'warning':
82
+ type = 48;
83
+ break;
84
+ default:
85
+ type = 0;
86
+ }
87
+
88
+ str = str.replace(/"/g, "'"); // double quotes to single quotes
89
+
90
+ // msgbox.vbs script from http://stackoverflow.com/questions/774175
91
+ cmd.push('cscript');
92
+
93
+ let vbs_file = `${shareData.AUTOMATOR_ROOT}/scripts/msgbox.vbs`;
94
+ let exist = get_fst_file(vbs_file);
95
+ if (!exist) {
96
+ // 本地不存在脚本需要生成
97
+ write_plain(vbs_file, `Set objArgs = WScript.Arguments
98
+ messageText = objArgs(0)
99
+ messageType = objArgs(1)
100
+ messageTitle = objArgs(2)
101
+ retValue = MsgBox(messageText, messageType, messageTitle)
102
+ WScript.Quit (retValue - 1)`);
103
+ }
104
+ cmd.push(vbs_file);
105
+ cmd.push(str) && cmd.push(type) && cmd.push(title);
106
+
107
+ }
108
+ let ret = await exec_shell({
109
+ cmd: cmd,
110
+ cwd: cwd,
111
+ capture_stdout: true,
112
+ });
113
+ return ret;
114
+ }
115
+
116
+ async function showNotify(data) {
117
+ let options = Object.assign({
118
+ title: "",
119
+ message: "<空>",
120
+ // actions: ["OK"],
121
+ }, data);
122
+ let ret = await new Promise(resolve => {
123
+ notifier.notify(options, (err, resp, meta) => {
124
+ resolve(meta.button);
125
+ });
126
+ });
127
+ return ret;
128
+ }
129
+
130
+ module.exports = {
131
+ showAlert,
132
+ showNotify,
133
+ };
@@ -0,0 +1,13 @@
1
+ function decode(input) {
2
+ return decodeURIComponent(escape(atob(input)))
3
+ }
4
+
5
+ function encode(input) {
6
+ return btoa(unescape(encodeURIComponent(input)))
7
+ }
8
+
9
+
10
+ module.exports = {
11
+ decode,
12
+ encode,
13
+ };