nadesiko3 3.3.45 → 3.3.49

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 (329) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +143 -132
  3. package/batch/browsers.template.md +3 -3
  4. package/batch/build_browsers.nako3 +72 -72
  5. package/batch/build_command.nako3 +44 -44
  6. package/batch/build_nako_version.nako3 +42 -42
  7. package/batch/calc_hash.nako3 +29 -29
  8. package/batch/cmd_txt2json.nako3 +74 -74
  9. package/batch/command.txt +270 -338
  10. package/batch/command_nakopad.txt +9 -69
  11. package/batch/download-extlib.nako3 +43 -43
  12. package/batch/gen_command_nakopad.nako3 +57 -57
  13. package/batch/jsplugin2text.nako3 +285 -285
  14. package/batch/pickup_command.nako3 +110 -110
  15. package/batch/pickup_reserved_words.nako3 +11 -11
  16. package/batch/publish_version.nako3 +46 -46
  17. package/batch/show_agents.js +14 -14
  18. package/batch/turtle2js.nako3 +21 -21
  19. package/bin/cnako3 +10 -10
  20. package/core/.editorconfig +6 -0
  21. package/core/.eslintrc.cjs +33 -0
  22. package/core/.github/dependabot.yml +7 -0
  23. package/core/.github/workflows/nodejs.yml +37 -0
  24. package/core/.github/workflows/super-linter.yml +61 -0
  25. package/core/.github/workflows/textlint.yml +199 -0
  26. package/core/LICENSE +21 -0
  27. package/core/README.md +66 -0
  28. package/core/batch/build_nako_version.nako3 +42 -0
  29. package/core/command/snako.mjs +105 -0
  30. package/core/command/snako.mts +116 -0
  31. package/core/index.mjs +21 -0
  32. package/core/index.mts +21 -0
  33. package/core/package.json +47 -0
  34. package/core/sample/hello.nako3 +7 -0
  35. package/core/sample/hoge.mjs +4 -0
  36. package/core/sample/hoge.mts +6 -0
  37. package/core/src/nako3.mjs +858 -0
  38. package/core/src/nako3.mts +967 -0
  39. package/core/src/nako_colors.mjs +78 -0
  40. package/core/src/nako_colors.mts +86 -0
  41. package/core/src/nako_core_version.mjs +8 -0
  42. package/core/src/nako_core_version.mts +19 -0
  43. package/core/src/nako_csv.mjs +185 -0
  44. package/core/src/nako_csv.mts +188 -0
  45. package/core/src/nako_errors.mjs +173 -0
  46. package/core/src/nako_errors.mts +197 -0
  47. package/core/src/nako_from_dncl.mjs +255 -0
  48. package/core/src/nako_from_dncl.mts +250 -0
  49. package/core/src/nako_gen.mjs +1648 -0
  50. package/core/src/nako_gen.mts +1719 -0
  51. package/core/src/nako_gen_async.mjs +1659 -0
  52. package/core/src/nako_gen_async.mts +1732 -0
  53. package/core/src/nako_global.mjs +107 -0
  54. package/core/src/nako_global.mts +138 -0
  55. package/core/src/nako_indent.mjs +445 -0
  56. package/core/src/nako_indent.mts +492 -0
  57. package/core/src/nako_josi_list.mjs +38 -0
  58. package/core/src/nako_josi_list.mts +45 -0
  59. package/core/src/nako_lex_rules.mjs +253 -0
  60. package/core/src/nako_lex_rules.mts +260 -0
  61. package/core/src/nako_lexer.mjs +609 -0
  62. package/core/src/nako_lexer.mts +612 -0
  63. package/core/src/nako_logger.mjs +199 -0
  64. package/core/src/nako_logger.mts +232 -0
  65. package/core/src/nako_parser3.mjs +2439 -0
  66. package/core/src/nako_parser3.mts +2195 -0
  67. package/core/src/nako_parser_base.mjs +370 -0
  68. package/core/src/nako_parser_base.mts +370 -0
  69. package/core/src/nako_parser_const.mjs +37 -0
  70. package/core/src/nako_parser_const.mts +37 -0
  71. package/core/src/nako_prepare.mjs +304 -0
  72. package/core/src/nako_prepare.mts +315 -0
  73. package/core/src/nako_reserved_words.mjs +38 -0
  74. package/core/src/nako_reserved_words.mts +38 -0
  75. package/core/src/nako_source_mapping.mjs +207 -0
  76. package/core/src/nako_source_mapping.mts +262 -0
  77. package/core/src/nako_test.mjs +37 -0
  78. package/core/src/nako_types.mjs +25 -0
  79. package/core/src/nako_types.mts +151 -0
  80. package/core/src/plugin_csv.mjs +49 -0
  81. package/core/src/plugin_csv.mts +50 -0
  82. package/core/src/plugin_math.mjs +328 -0
  83. package/core/src/plugin_math.mts +326 -0
  84. package/core/src/plugin_promise.mjs +91 -0
  85. package/core/src/plugin_promise.mts +91 -0
  86. package/core/src/plugin_system.mjs +2832 -0
  87. package/core/src/plugin_system.mts +2690 -0
  88. package/core/src/plugin_test.mjs +34 -0
  89. package/core/src/plugin_test.mts +34 -0
  90. package/core/test/array_test.mjs +34 -0
  91. package/core/test/basic_test.mjs +344 -0
  92. package/core/test/calc_test.mjs +140 -0
  93. package/core/test/core_module_test.mjs +23 -0
  94. package/core/test/debug_test.mjs +16 -0
  95. package/core/test/dncl_test.mjs +94 -0
  96. package/core/test/error_message_test.mjs +210 -0
  97. package/core/test/error_test.mjs +16 -0
  98. package/core/test/flow_test.mjs +373 -0
  99. package/core/test/func_call.mjs +160 -0
  100. package/core/test/func_test.mjs +149 -0
  101. package/core/test/indent_test.mjs +364 -0
  102. package/core/test/lex_test.mjs +168 -0
  103. package/core/test/literal_test.mjs +73 -0
  104. package/core/test/nako_lexer_test.mjs +35 -0
  105. package/core/test/nako_logger_test.mjs +76 -0
  106. package/core/test/nako_logger_test.mts +78 -0
  107. package/core/test/plugin_csv_test.mjs +38 -0
  108. package/core/test/plugin_promise_test.mjs +18 -0
  109. package/core/test/plugin_system_test.mjs +630 -0
  110. package/core/test/prepare_test.mjs +96 -0
  111. package/core/test/re_test.mjs +22 -0
  112. package/core/test/side_effects_test.mjs +92 -0
  113. package/core/test/variable_scope_test.mjs +149 -0
  114. package/core/tsconfig.json +101 -0
  115. package/demo/ace_editor.html +89 -89
  116. package/demo/ace_editor_tabs.html +161 -161
  117. package/demo/basic.html +71 -71
  118. package/demo/browsers.html +9 -10
  119. package/demo/css/basic.css +3 -3
  120. package/demo/css/common.css +157 -157
  121. package/demo/css/editor.css +8 -8
  122. package/demo/css/flow.css +3 -3
  123. package/demo/css/index.css +3 -3
  124. package/demo/flow.html +98 -98
  125. package/demo/graph.html +53 -53
  126. package/demo/image/nakopad-icon256.png +0 -0
  127. package/demo/index.html +133 -133
  128. package/demo/js/common.js +17 -17
  129. package/demo/js/turtle3d_test.js +44 -44
  130. package/demo/js/turtle_test.js +45 -45
  131. package/demo/nako3/calc.nako3 +4 -4
  132. package/demo/runscript.html +47 -47
  133. package/demo/runscript2.html +33 -33
  134. package/demo/runscript3.html +35 -35
  135. package/demo/runscript4.html +33 -33
  136. package/demo/turtle.html +58 -58
  137. package/demo/turtle2.html +141 -141
  138. package/demo/turtle3.html +279 -279
  139. package/demo/turtle3d.html +58 -58
  140. package/demo/turtle3d2.html +107 -107
  141. package/demo/version.html +24 -24
  142. package/doc/SETUP.md +157 -157
  143. package/doc/about.md +17 -17
  144. package/doc/browsers.md +26 -26
  145. package/doc/docgen.md +21 -21
  146. package/doc/editor.md +44 -44
  147. package/doc/files.md +39 -39
  148. package/doc/plugins.md +234 -234
  149. package/doc/release.md +79 -79
  150. package/doc/textlint.md +43 -43
  151. package/doc/win32.md +57 -57
  152. package/package.json +195 -192
  153. package/release/_hash.txt +28 -28
  154. package/release/_script-tags.txt +14 -14
  155. package/release/command.json +1 -1
  156. package/release/command.json.js +1 -1
  157. package/release/command_cnako3.json +1 -1
  158. package/release/command_list.json +1 -1
  159. package/release/editor.js +1 -1
  160. package/release/nako_gen_async.js +1 -1
  161. package/release/plugin_csv.js +1 -1
  162. package/release/stats.json +1 -1
  163. package/release/version.js +1 -1
  164. package/release/wnako3.js +1 -1
  165. package/release/wnako3webworker.js +1 -1
  166. package/src/browsers.txt +11 -12
  167. package/src/browsers_agents.json +2 -2
  168. package/src/browsers_agents.mjs +1 -1
  169. package/src/cnako3.mjs +17 -10
  170. package/src/cnako3.mts +18 -12
  171. package/src/cnako3mod.mjs +707 -687
  172. package/src/cnako3mod.mts +712 -696
  173. package/src/commander_ja.mjs +164 -164
  174. package/src/commander_ja.mts +161 -161
  175. package/src/enako3.mjs +68 -69
  176. package/src/era.mjs +22 -22
  177. package/src/image_turtle-elephant.mjs +2 -5
  178. package/src/image_turtle-panda.mjs +2 -5
  179. package/src/image_turtle64.mjs +2 -5
  180. package/src/index.mjs +9 -9
  181. package/src/index.mts +10 -11
  182. package/src/nako3editorfix.sfd +106 -106
  183. package/src/nako_version.mjs +8 -8
  184. package/src/nako_version.mts +2 -2
  185. package/src/plugin_browser.mjs +213 -212
  186. package/src/plugin_browser.mts +206 -205
  187. package/src/plugin_browser_ajax.mjs +399 -399
  188. package/src/plugin_browser_audio.mjs +109 -109
  189. package/src/plugin_browser_canvas.mjs +449 -449
  190. package/src/plugin_browser_chart.mjs +294 -294
  191. package/src/plugin_browser_color.mjs +49 -49
  192. package/src/plugin_browser_crypto.mjs +26 -26
  193. package/src/plugin_browser_dialog.mjs +53 -53
  194. package/src/plugin_browser_dom_basic.mjs +336 -336
  195. package/src/plugin_browser_dom_event.mjs +193 -193
  196. package/src/plugin_browser_dom_parts.mjs +396 -396
  197. package/src/plugin_browser_geolocation.mjs +51 -51
  198. package/src/plugin_browser_hotkey.mjs +25 -25
  199. package/src/plugin_browser_html.mjs +59 -59
  200. package/src/plugin_browser_in_worker.mjs +45 -45
  201. package/src/plugin_browser_location.mjs +21 -21
  202. package/src/plugin_browser_speech.mjs +111 -111
  203. package/src/plugin_browser_storage.mjs +121 -121
  204. package/src/plugin_browser_system.mjs +31 -31
  205. package/src/plugin_browser_websocket.mjs +73 -73
  206. package/src/plugin_caniuse.mjs +29 -29
  207. package/src/plugin_datetime.mjs +394 -394
  208. package/src/plugin_httpserver.mjs +277 -0
  209. package/src/plugin_httpserver.mts +286 -0
  210. package/src/plugin_kansuji.mjs +224 -224
  211. package/src/plugin_keigo.mjs +55 -55
  212. package/src/plugin_markup.mjs +32 -32
  213. package/src/plugin_node.mjs +1047 -1047
  214. package/src/plugin_node.mts +980 -980
  215. package/src/plugin_turtle.mjs +647 -647
  216. package/src/plugin_webworker.mjs +334 -334
  217. package/src/plugin_weykturtle3d.mjs +1214 -1214
  218. package/src/plugin_worker.mjs +95 -95
  219. package/src/repl.nako3 +63 -63
  220. package/src/wnako3.mjs +12 -12
  221. package/src/wnako3.mts +11 -11
  222. package/src/wnako3_editor.css +215 -215
  223. package/src/wnako3_editor.mjs +1542 -1542
  224. package/src/wnako3_editor.mts +1657 -1656
  225. package/src/wnako3mod.mjs +213 -213
  226. package/src/wnako3mod.mts +214 -214
  227. package/src/wnako3webworker.mjs +69 -68
  228. package/test/ace_editor/karma.config.js +94 -94
  229. package/test/ace_editor/test/.babelrc.json +3 -3
  230. package/test/ace_editor/test/ace_editor_test.js +178 -178
  231. package/test/ace_editor/test/html/custom_context.html +139 -139
  232. package/test/async/async_basic_test.mjs +122 -122
  233. package/test/browser/karma.config.js +221 -221
  234. package/test/browser/test/.babelrc.json +3 -3
  235. package/test/browser/test/compare_util.js +50 -50
  236. package/test/browser/test/html/div_basic.html +2 -2
  237. package/test/browser/test/html/event_dom_form.html +4 -4
  238. package/test/browser/test/html/event_dom_scrolldiv.html +5 -5
  239. package/test/browser/test/import_plugin_checker.js +24 -24
  240. package/test/browser/test/plugin_browser_test.js +51 -51
  241. package/test/browser/test/plugin_browser_test_ajax.js +123 -123
  242. package/test/browser/test/plugin_browser_test_color.js +18 -18
  243. package/test/browser/test/plugin_browser_test_dialog.js +72 -72
  244. package/test/browser/test/plugin_browser_test_dom_event.js +598 -598
  245. package/test/browser/test/plugin_browser_test_dom_parts.js +125 -125
  246. package/test/browser/test/plugin_browser_test_system.js +9 -9
  247. package/test/browser/test/plugin_turtle_test.js +817 -817
  248. package/test/browser/test/plugin_webworker_test.js +86 -86
  249. package/test/browser/test/require_test.js +68 -68
  250. package/test/bundled/karma.config.base.js +117 -117
  251. package/test/bundled/karma.config.js +86 -86
  252. package/test/bundled/test/.babelrc.json +3 -3
  253. package/test/bundled/test/bundled_test.js +69 -69
  254. package/test/bundled/test/html/custom_context.html +65 -65
  255. package/test/bundled/test/html/custom_debug.html +66 -66
  256. package/test/bundled/test4b.cmd +52 -52
  257. package/test/bundled/test_base/.babelrc.json +3 -3
  258. package/test/bundled/test_base/_checktool_test.js +25 -25
  259. package/test/bundled/test_base/basic_ajax_test.js +56 -56
  260. package/test/bundled/test_base/basic_async_test.js +18 -18
  261. package/test/bundled/test_base/basic_test.js +153 -153
  262. package/test/bundled/test_base/calc_test.js +132 -132
  263. package/test/bundled/test_base/css/browsers_box.css +114 -114
  264. package/test/bundled/test_base/html/custom_context.html +69 -69
  265. package/test/bundled/test_base/html/custom_debug.html +71 -71
  266. package/test/bundled/test_base/js/browsers_box.js +72 -72
  267. package/test/bundled/test_base/plugin_csv_test.js +37 -37
  268. package/test/bundled/test_base/plugin_datetime_test.js +115 -115
  269. package/test/bundled/test_base/plugin_kansuji_test.js +49 -49
  270. package/test/bundled/test_base/plugin_system_test.js +410 -410
  271. package/test/bundled/test_base/plugin_webworker_test.js +53 -53
  272. package/test/bundled/test_base/test_utils.js +191 -191
  273. package/test/common/plugin_browser_test.mjs +22 -24
  274. package/test/common/plugin_browser_ut_audio_test.mjs +108 -108
  275. package/test/common/plugin_browser_ut_color_test.mjs +21 -21
  276. package/test/common/plugin_browser_ut_dialog_test.mjs +100 -100
  277. package/test/common/plugin_browser_ut_html_test.mjs +13 -13
  278. package/test/common/plugin_browser_ut_system_test.mjs +10 -10
  279. package/test/common/plugin_markup_test.mjs +23 -23
  280. package/test/jsconfig.json +19 -19
  281. package/test/karma.config.js +91 -91
  282. package/test/node/async_test.mjs +96 -82
  283. package/test/node/commander_ja_test.mjs +89 -89
  284. package/test/node/error_message_test.mjs +243 -253
  285. package/test/node/kai_test.nako3 +6 -6
  286. package/test/node/node_test.mjs +60 -60
  287. package/test/node/plugin_broken.js.txt +3 -3
  288. package/test/node/plugin_browser_ut_ajax_test.mjs.todo +357 -357
  289. package/test/node/plugin_browser_ut_location_test.mjs +42 -42
  290. package/test/node/plugin_markup_test.mjs +47 -46
  291. package/test/node/plugin_math_test.mjs +45 -44
  292. package/test/node/plugin_node_test.mjs +98 -97
  293. package/test/node/plugin_test.mjs +44 -43
  294. package/test/node/relative_import_test_1.nako3 +1 -1
  295. package/test/node/relative_import_test_2.nako3 +2 -2
  296. package/test/node/require_nako3_test.mjs +67 -66
  297. package/test/node/requiretest.nako3 +4 -4
  298. package/test/node/requiretest_indirect.nako3 +1 -1
  299. package/test/node/requiretest_name.nako3 +5 -5
  300. package/test/node/runtime_error.nako3 +2 -2
  301. package/test/node/scope1.nako3 +10 -10
  302. package/test/node/scope2.nako3 +12 -12
  303. package/test/node/side_effects_test.mjs +39 -119
  304. package/test/node/sjis.txt +5 -5
  305. package/test/node/syntax_error.nako3 +1 -1
  306. package/test/node/wnako3_editor_test.mjs +384 -384
  307. package/tools/README.md +12 -7
  308. package/tools/check_new_version.nako3 +25 -25
  309. package/tools/nako3edit/html/daisyui/LICENSE +22 -22
  310. package/tools/nako3edit/html/daisyui/version_2.14.1 +1 -1
  311. package/tools/nako3edit/html/edit.html +170 -170
  312. package/tools/nako3edit/html/edit_plugin.js +6 -6
  313. package/tools/nako3edit/html/files.html +125 -125
  314. package/tools/nako3edit/html/nako3edit.css +65 -65
  315. package/tools/nako3edit/index.mjs +248 -244
  316. package/tools/nako3server/index.html +10 -0
  317. package/tools/nako3server/index.mjs +116 -116
  318. package/tools/nako3server/index.nako3 +34 -0
  319. package/release/nako_gen_async.js.LICENSE.txt +0 -35
  320. package/release/plugin_caniuse.js.LICENSE.txt +0 -11
  321. package/release/plugin_csv.js.LICENSE.txt +0 -15
  322. package/release/plugin_datetime.js.LICENSE.txt +0 -15
  323. package/release/plugin_kansuji.js.LICENSE.txt +0 -3
  324. package/release/plugin_markup.js.LICENSE.txt +0 -11
  325. package/release/plugin_turtle.js.LICENSE.txt +0 -15
  326. package/release/plugin_webworker.js.LICENSE.txt +0 -3
  327. package/release/plugin_weykturtle3d.js.LICENSE.txt +0 -3
  328. package/release/wnako3webworker.js.LICENSE.txt +0 -131
  329. package/tools/nako3edit/a.sqlite3 +0 -0
@@ -1,1047 +1,1047 @@
1
- /**
2
- * file: plugin_node.mjs
3
- * node.js のためのプラグイン
4
- */
5
- import fs from 'fs';
6
- import fse from 'fs-extra';
7
- import fetch from 'node-fetch';
8
- import { exec, execSync } from 'child_process';
9
- import path from 'path';
10
- import iconv from 'iconv-lite';
11
- import opener from 'opener';
12
- import assert from 'assert';
13
- // 「標準入力取得時」「尋」で利用
14
- import readline from 'readline';
15
- // ハッシュ関数で利用
16
- import crypto from 'crypto';
17
- import os from 'os';
18
- export default {
19
- '初期化': {
20
- type: 'func',
21
- josi: [],
22
- pure: true,
23
- fn: function (sys) {
24
- sys.__getBinPath = (tool) => {
25
- let fpath = tool;
26
- if (process.platform === 'win32') {
27
- if (!fileExists(tool)) {
28
- const nodeDir = path.dirname(process.argv[0]);
29
- const root = path.resolve(path.join(nodeDir, '..'));
30
- fpath = path.join(root, 'bin', tool + '.exe');
31
- if (fileExists(fpath)) {
32
- return `"${fpath}"`;
33
- }
34
- return tool;
35
- }
36
- }
37
- return fpath;
38
- };
39
- sys.__getBokanPath = () => {
40
- let nakofile;
41
- const cmd = path.basename(process.argv[1]);
42
- if (cmd.indexOf('cnako3') < 0) {
43
- nakofile = process.argv[1];
44
- }
45
- else {
46
- nakofile = process.argv[2];
47
- }
48
- return path.dirname(path.resolve(nakofile));
49
- };
50
- sys.__v0['コマンドライン'] = process.argv;
51
- sys.__v0['ナデシコランタイムパス'] = process.argv[0];
52
- sys.__v0['ナデシコランタイム'] = path.basename(process.argv[0]);
53
- sys.__v0['母艦パス'] = sys.__getBokanPath();
54
- sys.__v0['AJAX:ONERROR'] = null;
55
- }
56
- },
57
- // @ファイル入出力
58
- '開': {
59
- type: 'func',
60
- josi: [['を', 'から']],
61
- pure: true,
62
- fn: function (s) {
63
- return fs.readFileSync(s, 'utf-8');
64
- }
65
- },
66
- '読': {
67
- type: 'func',
68
- josi: [['を', 'から']],
69
- pure: false,
70
- fn: function (s, sys) {
71
- return sys.__exec('開', [s]);
72
- }
73
- },
74
- 'バイナリ読': {
75
- type: 'func',
76
- josi: [['を', 'から']],
77
- pure: true,
78
- fn: function (s, sys) {
79
- return fs.readFileSync(s);
80
- }
81
- },
82
- '保存': {
83
- type: 'func',
84
- josi: [['を'], ['へ', 'に']],
85
- pure: true,
86
- fn: function (s, f) {
87
- // Buffer?
88
- if (typeof s === 'string') {
89
- fs.writeFileSync(f, s, 'utf-8');
90
- }
91
- else {
92
- fs.writeFileSync(f, s);
93
- }
94
- },
95
- return_none: true
96
- },
97
- 'SJISファイル読': {
98
- type: 'func',
99
- josi: [['を', 'から']],
100
- pure: true,
101
- fn: function (s, sys) {
102
- // iconv.skipDecodeWarning = true
103
- const buf = fs.readFileSync(s);
104
- const text = iconv.decode(Buffer.from(buf), 'sjis');
105
- return text;
106
- }
107
- },
108
- 'SJISファイル保存': {
109
- type: 'func',
110
- josi: [['を'], ['へ', 'に']],
111
- pure: true,
112
- fn: function (s, f, sys) {
113
- // iconv.skipDecodeWarning = true
114
- const buf = iconv.encode(s, 'Shift_JIS');
115
- fs.writeFileSync(f, buf);
116
- },
117
- return_none: true
118
- },
119
- '起動待機': {
120
- type: 'func',
121
- josi: [['を']],
122
- pure: true,
123
- fn: function (s) {
124
- const r = execSync(s);
125
- return r.toString();
126
- }
127
- },
128
- '起動': {
129
- type: 'func',
130
- josi: [['を']],
131
- pure: true,
132
- fn: function (s) {
133
- exec(s, (err, stdout, stderr) => {
134
- if (err) {
135
- console.error(stderr);
136
- }
137
- else if (stdout) {
138
- console.log(stdout);
139
- }
140
- });
141
- }
142
- },
143
- '起動時': {
144
- type: 'func',
145
- josi: [['で'], ['を']],
146
- pure: true,
147
- fn: function (callback, s, sys) {
148
- exec(s, (err, stdout, stderr) => {
149
- if (err) {
150
- throw new Error(stderr);
151
- }
152
- else {
153
- callback(stdout);
154
- }
155
- });
156
- }
157
- },
158
- 'ブラウザ起動': {
159
- type: 'func',
160
- josi: [['を']],
161
- pure: true,
162
- fn: function (url) {
163
- opener(url);
164
- }
165
- },
166
- 'ファイル列挙': {
167
- type: 'func',
168
- josi: [['の', 'を', 'で']],
169
- pure: true,
170
- fn: function (s) {
171
- if (s.indexOf('*') >= 0) { // ワイルドカードがある場合
172
- const searchPath = path.dirname(s);
173
- const mask1 = path.basename(s)
174
- .replace(/\./g, '\\.')
175
- .replace(/\*/g, '.*');
176
- const mask2 = (mask1.indexOf(';') < 0)
177
- ? mask1 + '$'
178
- : '(' + mask1.replace(/;/g, '|') + ')$';
179
- const maskRE = new RegExp(mask2, 'i');
180
- const list = fs.readdirSync(searchPath);
181
- return list.filter((n) => maskRE.test(n));
182
- }
183
- else {
184
- return fs.readdirSync(s);
185
- }
186
- }
187
- },
188
- '全ファイル列挙': {
189
- type: 'func',
190
- josi: [['の', 'を', 'で']],
191
- pure: true,
192
- fn: function (s) {
193
- /** @type {string[]} */
194
- const result = [];
195
- // ワイルドカードの有無を確認
196
- let mask = '.*';
197
- let basepath = s;
198
- if (s.indexOf('*') >= 0) {
199
- basepath = path.dirname(s);
200
- const mask1 = path.basename(s)
201
- .replace(/\./g, '\\.')
202
- .replace(/\*/g, '.*');
203
- mask = (mask1.indexOf(';') < 0)
204
- ? mask1 + '$'
205
- : '(' + mask1.replace(/;/g, '|') + ')$';
206
- }
207
- basepath = path.resolve(basepath);
208
- const maskRE = new RegExp(mask, 'i');
209
- // 再帰関数を定義
210
- const enumR = (base) => {
211
- const list = fs.readdirSync(base);
212
- for (const f of list) {
213
- if (f === '.' || f === '..') {
214
- continue;
215
- }
216
- const fullpath = path.join(base, f);
217
- let st = null;
218
- try {
219
- st = fs.statSync(fullpath);
220
- }
221
- catch (e) {
222
- st = null;
223
- }
224
- if (st == null) {
225
- continue;
226
- }
227
- if (st.isDirectory()) {
228
- enumR(fullpath);
229
- continue;
230
- }
231
- if (maskRE.test(f)) {
232
- result.push(fullpath);
233
- }
234
- }
235
- };
236
- // 検索実行
237
- enumR(basepath);
238
- return result;
239
- }
240
- },
241
- '存在': {
242
- type: 'func',
243
- josi: [['が', 'の']],
244
- pure: true,
245
- fn: function (path) {
246
- return fileExists(path);
247
- }
248
- },
249
- 'フォルダ存在': {
250
- type: 'func',
251
- josi: [['が', 'の']],
252
- pure: true,
253
- fn: function (path) {
254
- return isDir(path);
255
- }
256
- },
257
- 'フォルダ作成': {
258
- type: 'func',
259
- josi: [['の', 'を', 'に', 'へ']],
260
- pure: true,
261
- fn: function (path) {
262
- return fse.mkdirpSync(path);
263
- }
264
- },
265
- 'ファイルコピー': {
266
- type: 'func',
267
- josi: [['から', 'を'], ['に', 'へ']],
268
- pure: true,
269
- fn: function (a, b, sys) {
270
- return fse.copySync(a, b);
271
- }
272
- },
273
- 'ファイルコピー時': {
274
- type: 'func',
275
- josi: [['で'], ['から', 'を'], ['に', 'へ']],
276
- pure: true,
277
- fn: function (callback, a, b, sys) {
278
- return fse.copy(a, b, (err) => {
279
- if (err) {
280
- throw new Error('ファイルコピー時:' + err);
281
- }
282
- callback();
283
- });
284
- },
285
- return_none: false
286
- },
287
- 'ファイル移動': {
288
- type: 'func',
289
- josi: [['から', 'を'], ['に', 'へ']],
290
- pure: true,
291
- fn: function (a, b, sys) {
292
- return fse.moveSync(a, b);
293
- }
294
- },
295
- 'ファイル移動時': {
296
- type: 'func',
297
- josi: [['で'], ['から', 'を'], ['に', 'へ']],
298
- pure: true,
299
- fn: function (callback, a, b, sys) {
300
- fse.move(a, b, (err) => {
301
- if (err) {
302
- throw new Error('ファイル移動時:' + err);
303
- }
304
- callback();
305
- });
306
- },
307
- return_none: false
308
- },
309
- 'ファイル削除': {
310
- type: 'func',
311
- josi: [['の', 'を']],
312
- pure: true,
313
- fn: function (path, sys) {
314
- return fse.removeSync(path);
315
- }
316
- },
317
- 'ファイル削除時': {
318
- type: 'func',
319
- josi: [['で'], ['の', 'を']],
320
- pure: true,
321
- fn: function (callback, path, sys) {
322
- return fse.remove(path, (err) => {
323
- if (err) {
324
- throw new Error('ファイル削除時:' + err);
325
- }
326
- callback();
327
- });
328
- },
329
- return_none: false
330
- },
331
- 'ファイル情報取得': {
332
- type: 'func',
333
- josi: [['の', 'から']],
334
- pure: true,
335
- fn: function (path, sys) {
336
- return fs.statSync(path);
337
- }
338
- },
339
- 'ファイルサイズ取得': {
340
- type: 'func',
341
- josi: [['の', 'から']],
342
- pure: true,
343
- fn: function (path, sys) {
344
- const st = fs.statSync(path);
345
- if (!st) {
346
- return -1;
347
- }
348
- return st.size;
349
- }
350
- },
351
- // @パス操作
352
- 'ファイル名抽出': {
353
- type: 'func',
354
- josi: [['から', 'の']],
355
- pure: true,
356
- fn: function (s) {
357
- return path.basename(s);
358
- }
359
- },
360
- 'パス抽出': {
361
- type: 'func',
362
- josi: [['から', 'の']],
363
- pure: true,
364
- fn: function (s) {
365
- return path.dirname(s);
366
- }
367
- },
368
- '絶対パス変換': {
369
- type: 'func',
370
- josi: [['を', 'の']],
371
- pure: true,
372
- fn: function (a) {
373
- return path.resolve(a);
374
- }
375
- },
376
- '相対パス展開': {
377
- type: 'func',
378
- josi: [['を'], ['で']],
379
- pure: true,
380
- fn: function (a, b) {
381
- return path.resolve(path.join(a, b));
382
- }
383
- },
384
- // @フォルダ取得
385
- 'カレントディレクトリ取得': {
386
- type: 'func',
387
- josi: [],
388
- pure: true,
389
- fn: function () {
390
- const cwd = process.cwd();
391
- return path.resolve(cwd);
392
- }
393
- },
394
- 'カレントディレクトリ変更': {
395
- type: 'func',
396
- josi: [['に', 'へ']],
397
- pure: true,
398
- fn: function (dir) {
399
- process.chdir(dir);
400
- },
401
- return_none: true
402
- },
403
- '作業フォルダ取得': {
404
- type: 'func',
405
- josi: [],
406
- pure: true,
407
- fn: function () {
408
- const cwd = process.cwd();
409
- return path.resolve(cwd);
410
- }
411
- },
412
- '作業フォルダ変更': {
413
- type: 'func',
414
- josi: [['に', 'へ']],
415
- pure: true,
416
- fn: function (dir) {
417
- process.chdir(dir);
418
- },
419
- return_none: true
420
- },
421
- 'ホームディレクトリ取得': {
422
- type: 'func',
423
- josi: [],
424
- pure: true,
425
- fn: function () {
426
- return process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'];
427
- }
428
- },
429
- 'デスクトップ': {
430
- type: 'func',
431
- josi: [],
432
- pure: false,
433
- fn: function (sys) {
434
- const home = sys.__exec('ホームディレクトリ取得', [sys]);
435
- return path.join(home, 'Desktop');
436
- }
437
- },
438
- 'マイドキュメント': {
439
- type: 'func',
440
- josi: [],
441
- pure: false,
442
- fn: function (sys) {
443
- const home = sys.__exec('ホームディレクトリ取得', [sys]);
444
- return path.join(home, 'Documents');
445
- }
446
- },
447
- '母艦パス': { type: 'const', value: '' },
448
- '母艦パス取得': {
449
- type: 'func',
450
- josi: [],
451
- pure: true,
452
- fn: function (sys) {
453
- return sys.__getBokanPath();
454
- }
455
- },
456
- // @環境変数
457
- '環境変数取得': {
458
- type: 'func',
459
- josi: [['の']],
460
- pure: true,
461
- fn: function (s) {
462
- return process.env[s];
463
- }
464
- },
465
- '環境変数一覧取得': {
466
- type: 'func',
467
- josi: [],
468
- pure: true,
469
- fn: function () {
470
- return process.env;
471
- }
472
- },
473
- // @圧縮・解凍
474
- '圧縮解凍ツールパス': { type: 'const', value: '7z' },
475
- '圧縮解凍ツールパス変更': {
476
- type: 'func',
477
- josi: [['に', 'へ']],
478
- pure: true,
479
- fn: function (v, sys) {
480
- sys.__v0['圧縮解凍ツールパス'] = v;
481
- },
482
- return_none: true
483
- },
484
- '解凍': {
485
- type: 'func',
486
- josi: [['を', 'から'], ['に', 'へ']],
487
- pure: true,
488
- fn: function (a, b, sys) {
489
- const tpath = sys.__getBinPath(sys.__v0['圧縮解凍ツールパス']);
490
- const cmd = `${tpath} x "${a}" -o"${b}" -y`;
491
- execSync(cmd);
492
- return true;
493
- }
494
- },
495
- '解凍時': {
496
- type: 'func',
497
- josi: [['で'], ['を', 'から'], ['に', 'へ']],
498
- pure: true,
499
- fn: function (callback, a, b, sys) {
500
- const tpath = sys.__getBinPath(sys.__v0['圧縮解凍ツールパス']);
501
- const cmd = `${tpath} x "${a}" -o"${b}" -y`;
502
- exec(cmd, (err, stdout, stderr) => {
503
- if (err) {
504
- throw new Error('[エラー]『解凍時』' + err);
505
- }
506
- callback(stdout);
507
- });
508
- },
509
- return_none: false
510
- },
511
- '圧縮': {
512
- type: 'func',
513
- josi: [['を', 'から'], ['に', 'へ']],
514
- pure: true,
515
- fn: function (a, b, sys) {
516
- const tpath = sys.__getBinPath(sys.__v0['圧縮解凍ツールパス']);
517
- const cmd = `${tpath} a -r "${b}" "${a}" -y`;
518
- execSync(cmd);
519
- return true;
520
- }
521
- },
522
- '圧縮時': {
523
- type: 'func',
524
- josi: [['で'], ['を', 'から'], ['に', 'へ']],
525
- pure: true,
526
- fn: function (callback, a, b, sys) {
527
- const tpath = sys.__getBinPath(sys.__v0['圧縮解凍ツールパス']);
528
- const cmd = `${tpath} a -r "${b}" "${a}" -y`;
529
- exec(cmd, (err, stdout, stderr) => {
530
- if (err) {
531
- throw new Error('[エラー]『圧縮時』' + err);
532
- }
533
- callback(stdout);
534
- });
535
- },
536
- return_none: true
537
- },
538
- // @Nodeプロセス
539
- '終': {
540
- type: 'func',
541
- josi: [],
542
- pure: true,
543
- fn: function () {
544
- process.exit();
545
- },
546
- return_none: true
547
- },
548
- '強制終了時': {
549
- type: 'func',
550
- josi: [['を']],
551
- pure: true,
552
- fn: function (func, sys) {
553
- if (typeof (func) === 'string') {
554
- func = sys.__findFunc(func, '強制終了時');
555
- }
556
- process.on('SIGINT', (signal) => {
557
- const flag = func(sys);
558
- if (flag) {
559
- process.exit();
560
- }
561
- });
562
- },
563
- return_none: true
564
- },
565
- '終了': {
566
- type: 'func',
567
- josi: [],
568
- pure: false,
569
- fn: function (sys) {
570
- sys.__exec('終', []);
571
- },
572
- return_none: true
573
- },
574
- 'OS取得': {
575
- type: 'func',
576
- josi: [],
577
- pure: true,
578
- fn: function (sys) {
579
- return process.platform;
580
- }
581
- },
582
- 'OSアーキテクチャ取得': {
583
- type: 'func',
584
- josi: [],
585
- pure: true,
586
- fn: function (sys) {
587
- return process.arch;
588
- }
589
- },
590
- // @コマンドラインと標準入出力
591
- 'コマンドライン': { type: 'const', value: '' },
592
- 'ナデシコランタイム': { type: 'const', value: '' },
593
- 'ナデシコランタイムパス': { type: 'const', value: '' },
594
- '標準入力取得時': {
595
- type: 'func',
596
- josi: [['を']],
597
- pure: true,
598
- fn: function (callback) {
599
- const reader = readline.createInterface({
600
- input: process.stdin,
601
- output: process.stdout
602
- });
603
- reader.on('line', function (line) {
604
- callback(line);
605
- });
606
- }
607
- },
608
- '尋': {
609
- type: 'func',
610
- josi: [['と', 'を']],
611
- pure: true,
612
- asyncFn: true,
613
- fn: function (msg) {
614
- return new Promise((resolve, reject) => {
615
- const rl = readline.createInterface(process.stdin, process.stdout);
616
- if (!rl) {
617
- reject(new Error('『尋』命令で標準入力が取得できません'));
618
- return;
619
- }
620
- rl.question(msg, (buf) => {
621
- rl.close();
622
- if (buf && buf.match(/^[0-9.]+$/)) {
623
- buf = parseFloat(buf);
624
- }
625
- resolve(buf);
626
- });
627
- });
628
- }
629
- },
630
- // @テスト
631
- 'ASSERT等': {
632
- type: 'func',
633
- josi: [['と'], ['が']],
634
- pure: true,
635
- fn: function (a, b, sys) {
636
- assert.strictEqual(a, b);
637
- }
638
- },
639
- // @ネットワーク
640
- '自分IPアドレス取得': {
641
- type: 'func',
642
- josi: [],
643
- pure: true,
644
- fn: function (sys) {
645
- const nif = os.networkInterfaces();
646
- if (!nif) {
647
- throw new Error('『自分IPアドレス取得』でネットワークのインターフェイスが種畜できません。');
648
- }
649
- /**
650
- * @type {string[]}
651
- */
652
- const result = [];
653
- for (const dev in nif) {
654
- const n = nif[dev];
655
- if (!n) {
656
- continue;
657
- }
658
- n.forEach((detail) => {
659
- if (detail.family === 'IPv4') {
660
- result.push(detail.address);
661
- }
662
- });
663
- }
664
- return result;
665
- }
666
- },
667
- '自分IPV6アドレス取得': {
668
- type: 'func',
669
- josi: [],
670
- pure: true,
671
- fn: function (sys) {
672
- const nif = os.networkInterfaces();
673
- if (!nif) {
674
- throw new Error('『自分IPアドレス取得』でネットワークのインターフェイスが種畜できません。');
675
- }
676
- const result = [];
677
- for (const dev in nif) {
678
- const n = nif[dev];
679
- if (!n) {
680
- continue;
681
- }
682
- n.forEach((detail) => {
683
- if (detail.family === 'IPv6') {
684
- result.push(detail.address);
685
- }
686
- });
687
- }
688
- return result;
689
- }
690
- },
691
- // @Ajax
692
- 'AJAX送信時': {
693
- type: 'func',
694
- josi: [['の'], ['まで', 'へ', 'に']],
695
- pure: true,
696
- fn: function (callback, url, sys) {
697
- let options = sys.__v0['AJAXオプション'];
698
- if (options === '') {
699
- options = { method: 'GET' };
700
- }
701
- fetch(url, options).then((res) => {
702
- return res.text();
703
- }).then((text) => {
704
- sys.__v0['対象'] = text;
705
- callback(text);
706
- }).catch((err) => {
707
- console.log('[fetch.error]', err);
708
- throw err;
709
- });
710
- },
711
- return_none: true
712
- },
713
- 'AJAX受信時': {
714
- type: 'func',
715
- josi: [['で'], ['から', 'を']],
716
- pure: true,
717
- fn: function (callback, url, sys) {
718
- sys.__exec('AJAX送信時', [callback, url, sys]);
719
- },
720
- return_none: true
721
- },
722
- 'GET送信時': {
723
- type: 'func',
724
- josi: [['の'], ['まで', 'へ', 'に']],
725
- pure: false,
726
- fn: function (callback, url, sys) {
727
- sys.__exec('AJAX送信時', [callback, url, sys]);
728
- },
729
- return_none: true
730
- },
731
- 'POST送信時': {
732
- type: 'func',
733
- josi: [['の'], ['まで', 'へ', 'に'], ['を']],
734
- pure: true,
735
- fn: function (callback, url, params, sys) {
736
- const flist = [];
737
- for (const key in params) {
738
- const v = params[key];
739
- const kv = encodeURIComponent(key) + '=' + encodeURIComponent(v);
740
- flist.push(kv);
741
- }
742
- const bodyData = flist.join('&');
743
- const options = {
744
- method: 'POST',
745
- headers: {
746
- 'Content-Type': 'application/x-www-form-urlencoded'
747
- },
748
- body: bodyData
749
- };
750
- fetch(url, options).then((res) => {
751
- return res.text();
752
- }).then((text) => {
753
- sys.__v0['対象'] = text;
754
- callback(text);
755
- }).catch((err) => {
756
- sys.__v0['AJAX:ONERROR'](err);
757
- });
758
- }
759
- },
760
- 'POSTフォーム送信時': {
761
- type: 'func',
762
- josi: [['の'], ['まで', 'へ', 'に'], ['を']],
763
- pure: true,
764
- fn: function (callback, url, params, sys) {
765
- const fd = new FormData();
766
- for (const key in params) {
767
- fd.set(key, params[key]);
768
- }
769
- const options = {
770
- method: 'POST',
771
- headers: {
772
- 'Content-Type': 'multipart/form-data'
773
- },
774
- body: fd
775
- };
776
- fetch(url, options).then((res) => {
777
- return res.text();
778
- }).then((text) => {
779
- sys.__v0['対象'] = text;
780
- callback(text);
781
- }).catch((err) => {
782
- sys.__v0['AJAX:ONERROR'](err);
783
- });
784
- }
785
- },
786
- 'AJAX失敗時': {
787
- type: 'func',
788
- josi: [['の']],
789
- pure: true,
790
- fn: function (callback, sys) {
791
- sys.__v0['AJAX:ONERROR'] = callback;
792
- }
793
- },
794
- 'AJAXオプション': { type: 'const', value: '' },
795
- 'AJAXオプション設定': {
796
- type: 'func',
797
- josi: [['に', 'へ', 'と']],
798
- pure: true,
799
- fn: function (option, sys) {
800
- sys.__v0['AJAXオプション'] = option;
801
- },
802
- return_none: true
803
- },
804
- 'AJAX保障送信': {
805
- type: 'func',
806
- josi: [['まで', 'へ', 'に']],
807
- pure: true,
808
- fn: function (url, sys) {
809
- let options = sys.__v0['AJAXオプション'];
810
- if (options === '') {
811
- options = { method: 'GET' };
812
- }
813
- return fetch(url, options);
814
- },
815
- return_none: false
816
- },
817
- 'HTTP保障取得': {
818
- type: 'func',
819
- josi: [['の', 'から', 'を']],
820
- pure: true,
821
- fn: function (url, sys) {
822
- return sys.__exec('AJAX保障送信', [url, sys]);
823
- },
824
- return_none: false
825
- },
826
- 'GET保障送信': {
827
- type: 'func',
828
- josi: [['まで', 'へ', 'に']],
829
- pure: true,
830
- fn: function (url, sys) {
831
- return sys.__exec('AJAX保障送信', [url, sys]);
832
- },
833
- return_none: false
834
- },
835
- 'POST保障送信': {
836
- type: 'func',
837
- josi: [['まで', 'へ', 'に'], ['を']],
838
- pure: true,
839
- fn: function (url, params, sys) {
840
- const flist = [];
841
- for (const key in params) {
842
- const v = params[key];
843
- const kv = encodeURIComponent(key) + '=' + encodeURIComponent(v);
844
- flist.push(kv);
845
- }
846
- const bodyData = flist.join('&');
847
- const options = {
848
- method: 'POST',
849
- headers: {
850
- 'Content-Type': 'application/x-www-form-urlencoded'
851
- },
852
- body: bodyData
853
- };
854
- return fetch(url, options);
855
- },
856
- return_none: false
857
- },
858
- 'POSTフォーム保障送信': {
859
- type: 'func',
860
- josi: [['まで', 'へ', 'に'], ['を']],
861
- pure: true,
862
- fn: function (url, params, sys) {
863
- const fd = new FormData();
864
- for (const key in params) {
865
- fd.set(key, params[key]);
866
- }
867
- const options = {
868
- method: 'POST',
869
- body: fd
870
- };
871
- return fetch(url, options);
872
- },
873
- return_none: false
874
- },
875
- 'AJAX内容取得': {
876
- type: 'func',
877
- josi: [['から'], ['で']],
878
- pure: true,
879
- fn: function (res, type, sys) {
880
- type = type.toString().toUpperCase();
881
- if (type === 'TEXT' || type === 'テキスト') {
882
- return res.text();
883
- }
884
- else if (type === 'JSON') {
885
- return res.json();
886
- }
887
- else if (type === 'BLOB') {
888
- return res.blob();
889
- }
890
- else if (type === 'ARRAY' || type === '配列') {
891
- return res.arrayBuffer();
892
- }
893
- else if (type === 'BODY' || type === '本体') {
894
- return res.body;
895
- }
896
- return res.body();
897
- },
898
- return_none: false
899
- },
900
- 'AJAX受信': {
901
- type: 'func',
902
- josi: [['から', 'を']],
903
- pure: true,
904
- fn: function (url, sys) {
905
- if (sys.__genMode !== '非同期モード') {
906
- throw new Error('『AJAX受信』を使うには、プログラムの冒頭で「!非同期モード」と宣言してください。');
907
- }
908
- const sysenv = sys.setAsync(sys);
909
- let options = sys.__v0['AJAXオプション'];
910
- if (options === '') {
911
- options = { method: 'GET' };
912
- }
913
- // fetch 実行
914
- fetch(url, options).then((res) => {
915
- if (res.ok) { // 成功したとき
916
- return res.text();
917
- }
918
- else { // 失敗したとき
919
- throw new Error('status=' + res.status);
920
- }
921
- }).then((text) => {
922
- sys.__v0['対象'] = text;
923
- sys.compAsync(sys, sysenv);
924
- }).catch((err) => {
925
- console.error('[AJAX受信のエラー]', err);
926
- sys.__errorAsync(err, sys);
927
- });
928
- },
929
- return_none: true
930
- },
931
- // @新AJAX
932
- 'AJAXテキスト取得': {
933
- type: 'func',
934
- josi: [['から']],
935
- pure: true,
936
- asyncFn: true,
937
- fn: async function (url, sys) {
938
- let options = sys.__v0['AJAXオプション'];
939
- if (options === '') {
940
- options = { method: 'GET' };
941
- }
942
- const res = await fetch(url, options);
943
- const txt = await res.text();
944
- return txt;
945
- },
946
- return_none: false
947
- },
948
- 'AJAX_JSON取得': {
949
- type: 'func',
950
- josi: [['から']],
951
- pure: true,
952
- asyncFn: true,
953
- fn: async function (url, sys) {
954
- let options = sys.__v0['AJAXオプション'];
955
- if (options === '') {
956
- options = { method: 'GET' };
957
- }
958
- const res = await fetch(url, options);
959
- const txt = await res.json();
960
- return txt;
961
- },
962
- return_none: false
963
- },
964
- // @文字コード
965
- '文字コード変換サポート判定': {
966
- type: 'func',
967
- josi: [['の', 'を']],
968
- pure: true,
969
- fn: function (code, sys) {
970
- return iconv.encodingExists(code);
971
- }
972
- },
973
- 'SJIS変換': {
974
- type: 'func',
975
- josi: [['に', 'へ', 'を']],
976
- pure: true,
977
- fn: function (str, sys) {
978
- // iconv.skipDecodeWarning = true
979
- return iconv.encode(str, 'Shift_JIS');
980
- }
981
- },
982
- 'SJIS取得': {
983
- type: 'func',
984
- josi: [['から', 'を', 'で']],
985
- pure: true,
986
- fn: function (buf, sys) {
987
- // iconv.skipDecodeWarning = true
988
- return iconv.decode(Buffer.from(buf), 'sjis');
989
- }
990
- },
991
- 'エンコーディング変換': {
992
- type: 'func',
993
- josi: [['を'], ['へ', 'で']],
994
- pure: true,
995
- fn: function (s, code, sys) {
996
- // iconv.skipDecodeWarning = true
997
- return iconv.encode(s, code);
998
- }
999
- },
1000
- 'エンコーディング取得': {
1001
- type: 'func',
1002
- josi: [['を'], ['から', 'で']],
1003
- pure: true,
1004
- fn: function (buf, code, sys) {
1005
- // iconv.skipDecodeWarning = true
1006
- return iconv.decode(Buffer.from(buf), code);
1007
- }
1008
- },
1009
- // @ハッシュ関数
1010
- 'ハッシュ関数一覧取得': {
1011
- type: 'func',
1012
- josi: [],
1013
- pure: true,
1014
- fn: function (sys) {
1015
- return crypto.getHashes();
1016
- }
1017
- },
1018
- 'ハッシュ値計算': {
1019
- type: 'func',
1020
- josi: [['を'], ['の'], ['で']],
1021
- pure: true,
1022
- fn: function (s, alg, enc, sys) {
1023
- const hashsum = crypto.createHash(alg);
1024
- hashsum.update(s);
1025
- return hashsum.digest(enc);
1026
- }
1027
- }
1028
- };
1029
- // ローカル関数
1030
- function fileExists(f) {
1031
- try {
1032
- fs.statSync(f);
1033
- return true;
1034
- }
1035
- catch (err) {
1036
- return false;
1037
- }
1038
- }
1039
- function isDir(f) {
1040
- try {
1041
- const st = fs.statSync(f);
1042
- return st.isDirectory();
1043
- }
1044
- catch (err) {
1045
- return false;
1046
- }
1047
- }
1
+ /**
2
+ * file: plugin_node.mjs
3
+ * node.js のためのプラグイン
4
+ */
5
+ import fs from 'fs';
6
+ import fse from 'fs-extra';
7
+ import fetch from 'node-fetch';
8
+ import { exec, execSync } from 'child_process';
9
+ import path from 'path';
10
+ import iconv from 'iconv-lite';
11
+ import opener from 'opener';
12
+ import assert from 'assert';
13
+ // 「標準入力取得時」「尋」で利用
14
+ import readline from 'readline';
15
+ // ハッシュ関数で利用
16
+ import crypto from 'crypto';
17
+ import os from 'os';
18
+ export default {
19
+ '初期化': {
20
+ type: 'func',
21
+ josi: [],
22
+ pure: true,
23
+ fn: function (sys) {
24
+ sys.__getBinPath = (tool) => {
25
+ let fpath = tool;
26
+ if (process.platform === 'win32') {
27
+ if (!fileExists(tool)) {
28
+ const nodeDir = path.dirname(process.argv[0]);
29
+ const root = path.resolve(path.join(nodeDir, '..'));
30
+ fpath = path.join(root, 'bin', tool + '.exe');
31
+ if (fileExists(fpath)) {
32
+ return `"${fpath}"`;
33
+ }
34
+ return tool;
35
+ }
36
+ }
37
+ return fpath;
38
+ };
39
+ sys.__getBokanPath = () => {
40
+ let nakofile;
41
+ const cmd = path.basename(process.argv[1]);
42
+ if (cmd.indexOf('cnako3') < 0) {
43
+ nakofile = process.argv[1];
44
+ }
45
+ else {
46
+ nakofile = process.argv[2];
47
+ }
48
+ return path.dirname(path.resolve(nakofile));
49
+ };
50
+ sys.__v0['コマンドライン'] = process.argv;
51
+ sys.__v0['ナデシコランタイムパス'] = process.argv[0];
52
+ sys.__v0['ナデシコランタイム'] = path.basename(process.argv[0]);
53
+ sys.__v0['母艦パス'] = sys.__getBokanPath();
54
+ sys.__v0['AJAX:ONERROR'] = null;
55
+ }
56
+ },
57
+ // @ファイル入出力
58
+ '開': {
59
+ type: 'func',
60
+ josi: [['を', 'から']],
61
+ pure: true,
62
+ fn: function (s) {
63
+ return fs.readFileSync(s, 'utf-8');
64
+ }
65
+ },
66
+ '読': {
67
+ type: 'func',
68
+ josi: [['を', 'から']],
69
+ pure: false,
70
+ fn: function (s, sys) {
71
+ return sys.__exec('開', [s]);
72
+ }
73
+ },
74
+ 'バイナリ読': {
75
+ type: 'func',
76
+ josi: [['を', 'から']],
77
+ pure: true,
78
+ fn: function (s, sys) {
79
+ return fs.readFileSync(s);
80
+ }
81
+ },
82
+ '保存': {
83
+ type: 'func',
84
+ josi: [['を'], ['へ', 'に']],
85
+ pure: true,
86
+ fn: function (s, f) {
87
+ // Buffer?
88
+ if (typeof s === 'string') {
89
+ fs.writeFileSync(f, s, 'utf-8');
90
+ }
91
+ else {
92
+ fs.writeFileSync(f, s);
93
+ }
94
+ },
95
+ return_none: true
96
+ },
97
+ 'SJISファイル読': {
98
+ type: 'func',
99
+ josi: [['を', 'から']],
100
+ pure: true,
101
+ fn: function (s, sys) {
102
+ // iconv.skipDecodeWarning = true
103
+ const buf = fs.readFileSync(s);
104
+ const text = iconv.decode(Buffer.from(buf), 'sjis');
105
+ return text;
106
+ }
107
+ },
108
+ 'SJISファイル保存': {
109
+ type: 'func',
110
+ josi: [['を'], ['へ', 'に']],
111
+ pure: true,
112
+ fn: function (s, f, sys) {
113
+ // iconv.skipDecodeWarning = true
114
+ const buf = iconv.encode(s, 'Shift_JIS');
115
+ fs.writeFileSync(f, buf);
116
+ },
117
+ return_none: true
118
+ },
119
+ '起動待機': {
120
+ type: 'func',
121
+ josi: [['を']],
122
+ pure: true,
123
+ fn: function (s) {
124
+ const r = execSync(s);
125
+ return r.toString();
126
+ }
127
+ },
128
+ '起動': {
129
+ type: 'func',
130
+ josi: [['を']],
131
+ pure: true,
132
+ fn: function (s) {
133
+ exec(s, (err, stdout, stderr) => {
134
+ if (err) {
135
+ console.error(stderr);
136
+ }
137
+ else if (stdout) {
138
+ console.log(stdout);
139
+ }
140
+ });
141
+ }
142
+ },
143
+ '起動時': {
144
+ type: 'func',
145
+ josi: [['で'], ['を']],
146
+ pure: true,
147
+ fn: function (callback, s, sys) {
148
+ exec(s, (err, stdout, stderr) => {
149
+ if (err) {
150
+ throw new Error(stderr);
151
+ }
152
+ else {
153
+ callback(stdout);
154
+ }
155
+ });
156
+ }
157
+ },
158
+ 'ブラウザ起動': {
159
+ type: 'func',
160
+ josi: [['を']],
161
+ pure: true,
162
+ fn: function (url) {
163
+ opener(url);
164
+ }
165
+ },
166
+ 'ファイル列挙': {
167
+ type: 'func',
168
+ josi: [['の', 'を', 'で']],
169
+ pure: true,
170
+ fn: function (s) {
171
+ if (s.indexOf('*') >= 0) { // ワイルドカードがある場合
172
+ const searchPath = path.dirname(s);
173
+ const mask1 = path.basename(s)
174
+ .replace(/\./g, '\\.')
175
+ .replace(/\*/g, '.*');
176
+ const mask2 = (mask1.indexOf(';') < 0)
177
+ ? mask1 + '$'
178
+ : '(' + mask1.replace(/;/g, '|') + ')$';
179
+ const maskRE = new RegExp(mask2, 'i');
180
+ const list = fs.readdirSync(searchPath);
181
+ return list.filter((n) => maskRE.test(n));
182
+ }
183
+ else {
184
+ return fs.readdirSync(s);
185
+ }
186
+ }
187
+ },
188
+ '全ファイル列挙': {
189
+ type: 'func',
190
+ josi: [['の', 'を', 'で']],
191
+ pure: true,
192
+ fn: function (s) {
193
+ /** @type {string[]} */
194
+ const result = [];
195
+ // ワイルドカードの有無を確認
196
+ let mask = '.*';
197
+ let basepath = s;
198
+ if (s.indexOf('*') >= 0) {
199
+ basepath = path.dirname(s);
200
+ const mask1 = path.basename(s)
201
+ .replace(/\./g, '\\.')
202
+ .replace(/\*/g, '.*');
203
+ mask = (mask1.indexOf(';') < 0)
204
+ ? mask1 + '$'
205
+ : '(' + mask1.replace(/;/g, '|') + ')$';
206
+ }
207
+ basepath = path.resolve(basepath);
208
+ const maskRE = new RegExp(mask, 'i');
209
+ // 再帰関数を定義
210
+ const enumR = (base) => {
211
+ const list = fs.readdirSync(base);
212
+ for (const f of list) {
213
+ if (f === '.' || f === '..') {
214
+ continue;
215
+ }
216
+ const fullpath = path.join(base, f);
217
+ let st = null;
218
+ try {
219
+ st = fs.statSync(fullpath);
220
+ }
221
+ catch (e) {
222
+ st = null;
223
+ }
224
+ if (st == null) {
225
+ continue;
226
+ }
227
+ if (st.isDirectory()) {
228
+ enumR(fullpath);
229
+ continue;
230
+ }
231
+ if (maskRE.test(f)) {
232
+ result.push(fullpath);
233
+ }
234
+ }
235
+ };
236
+ // 検索実行
237
+ enumR(basepath);
238
+ return result;
239
+ }
240
+ },
241
+ '存在': {
242
+ type: 'func',
243
+ josi: [['が', 'の']],
244
+ pure: true,
245
+ fn: function (path) {
246
+ return fileExists(path);
247
+ }
248
+ },
249
+ 'フォルダ存在': {
250
+ type: 'func',
251
+ josi: [['が', 'の']],
252
+ pure: true,
253
+ fn: function (path) {
254
+ return isDir(path);
255
+ }
256
+ },
257
+ 'フォルダ作成': {
258
+ type: 'func',
259
+ josi: [['の', 'を', 'に', 'へ']],
260
+ pure: true,
261
+ fn: function (path) {
262
+ return fse.mkdirpSync(path);
263
+ }
264
+ },
265
+ 'ファイルコピー': {
266
+ type: 'func',
267
+ josi: [['から', 'を'], ['に', 'へ']],
268
+ pure: true,
269
+ fn: function (a, b, sys) {
270
+ return fse.copySync(a, b);
271
+ }
272
+ },
273
+ 'ファイルコピー時': {
274
+ type: 'func',
275
+ josi: [['で'], ['から', 'を'], ['に', 'へ']],
276
+ pure: true,
277
+ fn: function (callback, a, b, sys) {
278
+ return fse.copy(a, b, (err) => {
279
+ if (err) {
280
+ throw new Error('ファイルコピー時:' + err);
281
+ }
282
+ callback();
283
+ });
284
+ },
285
+ return_none: false
286
+ },
287
+ 'ファイル移動': {
288
+ type: 'func',
289
+ josi: [['から', 'を'], ['に', 'へ']],
290
+ pure: true,
291
+ fn: function (a, b, sys) {
292
+ return fse.moveSync(a, b);
293
+ }
294
+ },
295
+ 'ファイル移動時': {
296
+ type: 'func',
297
+ josi: [['で'], ['から', 'を'], ['に', 'へ']],
298
+ pure: true,
299
+ fn: function (callback, a, b, sys) {
300
+ fse.move(a, b, (err) => {
301
+ if (err) {
302
+ throw new Error('ファイル移動時:' + err);
303
+ }
304
+ callback();
305
+ });
306
+ },
307
+ return_none: false
308
+ },
309
+ 'ファイル削除': {
310
+ type: 'func',
311
+ josi: [['の', 'を']],
312
+ pure: true,
313
+ fn: function (path, sys) {
314
+ return fse.removeSync(path);
315
+ }
316
+ },
317
+ 'ファイル削除時': {
318
+ type: 'func',
319
+ josi: [['で'], ['の', 'を']],
320
+ pure: true,
321
+ fn: function (callback, path, sys) {
322
+ return fse.remove(path, (err) => {
323
+ if (err) {
324
+ throw new Error('ファイル削除時:' + err);
325
+ }
326
+ callback();
327
+ });
328
+ },
329
+ return_none: false
330
+ },
331
+ 'ファイル情報取得': {
332
+ type: 'func',
333
+ josi: [['の', 'から']],
334
+ pure: true,
335
+ fn: function (path, sys) {
336
+ return fs.statSync(path);
337
+ }
338
+ },
339
+ 'ファイルサイズ取得': {
340
+ type: 'func',
341
+ josi: [['の', 'から']],
342
+ pure: true,
343
+ fn: function (path, sys) {
344
+ const st = fs.statSync(path);
345
+ if (!st) {
346
+ return -1;
347
+ }
348
+ return st.size;
349
+ }
350
+ },
351
+ // @パス操作
352
+ 'ファイル名抽出': {
353
+ type: 'func',
354
+ josi: [['から', 'の']],
355
+ pure: true,
356
+ fn: function (s) {
357
+ return path.basename(s);
358
+ }
359
+ },
360
+ 'パス抽出': {
361
+ type: 'func',
362
+ josi: [['から', 'の']],
363
+ pure: true,
364
+ fn: function (s) {
365
+ return path.dirname(s);
366
+ }
367
+ },
368
+ '絶対パス変換': {
369
+ type: 'func',
370
+ josi: [['を', 'の']],
371
+ pure: true,
372
+ fn: function (a) {
373
+ return path.resolve(a);
374
+ }
375
+ },
376
+ '相対パス展開': {
377
+ type: 'func',
378
+ josi: [['を'], ['で']],
379
+ pure: true,
380
+ fn: function (a, b) {
381
+ return path.resolve(path.join(a, b));
382
+ }
383
+ },
384
+ // @フォルダ取得
385
+ 'カレントディレクトリ取得': {
386
+ type: 'func',
387
+ josi: [],
388
+ pure: true,
389
+ fn: function () {
390
+ const cwd = process.cwd();
391
+ return path.resolve(cwd);
392
+ }
393
+ },
394
+ 'カレントディレクトリ変更': {
395
+ type: 'func',
396
+ josi: [['に', 'へ']],
397
+ pure: true,
398
+ fn: function (dir) {
399
+ process.chdir(dir);
400
+ },
401
+ return_none: true
402
+ },
403
+ '作業フォルダ取得': {
404
+ type: 'func',
405
+ josi: [],
406
+ pure: true,
407
+ fn: function () {
408
+ const cwd = process.cwd();
409
+ return path.resolve(cwd);
410
+ }
411
+ },
412
+ '作業フォルダ変更': {
413
+ type: 'func',
414
+ josi: [['に', 'へ']],
415
+ pure: true,
416
+ fn: function (dir) {
417
+ process.chdir(dir);
418
+ },
419
+ return_none: true
420
+ },
421
+ 'ホームディレクトリ取得': {
422
+ type: 'func',
423
+ josi: [],
424
+ pure: true,
425
+ fn: function () {
426
+ return process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'];
427
+ }
428
+ },
429
+ 'デスクトップ': {
430
+ type: 'func',
431
+ josi: [],
432
+ pure: false,
433
+ fn: function (sys) {
434
+ const home = sys.__exec('ホームディレクトリ取得', [sys]);
435
+ return path.join(home, 'Desktop');
436
+ }
437
+ },
438
+ 'マイドキュメント': {
439
+ type: 'func',
440
+ josi: [],
441
+ pure: false,
442
+ fn: function (sys) {
443
+ const home = sys.__exec('ホームディレクトリ取得', [sys]);
444
+ return path.join(home, 'Documents');
445
+ }
446
+ },
447
+ '母艦パス': { type: 'const', value: '' },
448
+ '母艦パス取得': {
449
+ type: 'func',
450
+ josi: [],
451
+ pure: true,
452
+ fn: function (sys) {
453
+ return sys.__getBokanPath();
454
+ }
455
+ },
456
+ // @環境変数
457
+ '環境変数取得': {
458
+ type: 'func',
459
+ josi: [['の']],
460
+ pure: true,
461
+ fn: function (s) {
462
+ return process.env[s];
463
+ }
464
+ },
465
+ '環境変数一覧取得': {
466
+ type: 'func',
467
+ josi: [],
468
+ pure: true,
469
+ fn: function () {
470
+ return process.env;
471
+ }
472
+ },
473
+ // @圧縮・解凍
474
+ '圧縮解凍ツールパス': { type: 'const', value: '7z' },
475
+ '圧縮解凍ツールパス変更': {
476
+ type: 'func',
477
+ josi: [['に', 'へ']],
478
+ pure: true,
479
+ fn: function (v, sys) {
480
+ sys.__v0['圧縮解凍ツールパス'] = v;
481
+ },
482
+ return_none: true
483
+ },
484
+ '解凍': {
485
+ type: 'func',
486
+ josi: [['を', 'から'], ['に', 'へ']],
487
+ pure: true,
488
+ fn: function (a, b, sys) {
489
+ const tpath = sys.__getBinPath(sys.__v0['圧縮解凍ツールパス']);
490
+ const cmd = `${tpath} x "${a}" -o"${b}" -y`;
491
+ execSync(cmd);
492
+ return true;
493
+ }
494
+ },
495
+ '解凍時': {
496
+ type: 'func',
497
+ josi: [['で'], ['を', 'から'], ['に', 'へ']],
498
+ pure: true,
499
+ fn: function (callback, a, b, sys) {
500
+ const tpath = sys.__getBinPath(sys.__v0['圧縮解凍ツールパス']);
501
+ const cmd = `${tpath} x "${a}" -o"${b}" -y`;
502
+ exec(cmd, (err, stdout, stderr) => {
503
+ if (err) {
504
+ throw new Error('[エラー]『解凍時』' + err);
505
+ }
506
+ callback(stdout);
507
+ });
508
+ },
509
+ return_none: false
510
+ },
511
+ '圧縮': {
512
+ type: 'func',
513
+ josi: [['を', 'から'], ['に', 'へ']],
514
+ pure: true,
515
+ fn: function (a, b, sys) {
516
+ const tpath = sys.__getBinPath(sys.__v0['圧縮解凍ツールパス']);
517
+ const cmd = `${tpath} a -r "${b}" "${a}" -y`;
518
+ execSync(cmd);
519
+ return true;
520
+ }
521
+ },
522
+ '圧縮時': {
523
+ type: 'func',
524
+ josi: [['で'], ['を', 'から'], ['に', 'へ']],
525
+ pure: true,
526
+ fn: function (callback, a, b, sys) {
527
+ const tpath = sys.__getBinPath(sys.__v0['圧縮解凍ツールパス']);
528
+ const cmd = `${tpath} a -r "${b}" "${a}" -y`;
529
+ exec(cmd, (err, stdout, stderr) => {
530
+ if (err) {
531
+ throw new Error('[エラー]『圧縮時』' + err);
532
+ }
533
+ callback(stdout);
534
+ });
535
+ },
536
+ return_none: true
537
+ },
538
+ // @Nodeプロセス
539
+ '終': {
540
+ type: 'func',
541
+ josi: [],
542
+ pure: true,
543
+ fn: function () {
544
+ process.exit();
545
+ },
546
+ return_none: true
547
+ },
548
+ '強制終了時': {
549
+ type: 'func',
550
+ josi: [['を']],
551
+ pure: true,
552
+ fn: function (func, sys) {
553
+ if (typeof (func) === 'string') {
554
+ func = sys.__findFunc(func, '強制終了時');
555
+ }
556
+ process.on('SIGINT', (signal) => {
557
+ const flag = func(sys);
558
+ if (flag) {
559
+ process.exit();
560
+ }
561
+ });
562
+ },
563
+ return_none: true
564
+ },
565
+ '終了': {
566
+ type: 'func',
567
+ josi: [],
568
+ pure: false,
569
+ fn: function (sys) {
570
+ sys.__exec('終', []);
571
+ },
572
+ return_none: true
573
+ },
574
+ 'OS取得': {
575
+ type: 'func',
576
+ josi: [],
577
+ pure: true,
578
+ fn: function (sys) {
579
+ return process.platform;
580
+ }
581
+ },
582
+ 'OSアーキテクチャ取得': {
583
+ type: 'func',
584
+ josi: [],
585
+ pure: true,
586
+ fn: function (sys) {
587
+ return process.arch;
588
+ }
589
+ },
590
+ // @コマンドラインと標準入出力
591
+ 'コマンドライン': { type: 'const', value: '' },
592
+ 'ナデシコランタイム': { type: 'const', value: '' },
593
+ 'ナデシコランタイムパス': { type: 'const', value: '' },
594
+ '標準入力取得時': {
595
+ type: 'func',
596
+ josi: [['を']],
597
+ pure: true,
598
+ fn: function (callback) {
599
+ const reader = readline.createInterface({
600
+ input: process.stdin,
601
+ output: process.stdout
602
+ });
603
+ reader.on('line', function (line) {
604
+ callback(line);
605
+ });
606
+ }
607
+ },
608
+ '尋': {
609
+ type: 'func',
610
+ josi: [['と', 'を']],
611
+ pure: true,
612
+ asyncFn: true,
613
+ fn: function (msg) {
614
+ return new Promise((resolve, reject) => {
615
+ const rl = readline.createInterface(process.stdin, process.stdout);
616
+ if (!rl) {
617
+ reject(new Error('『尋』命令で標準入力が取得できません'));
618
+ return;
619
+ }
620
+ rl.question(msg, (buf) => {
621
+ rl.close();
622
+ if (buf && buf.match(/^[0-9.]+$/)) {
623
+ buf = parseFloat(buf);
624
+ }
625
+ resolve(buf);
626
+ });
627
+ });
628
+ }
629
+ },
630
+ // @テスト
631
+ 'ASSERT等': {
632
+ type: 'func',
633
+ josi: [['と'], ['が']],
634
+ pure: true,
635
+ fn: function (a, b, sys) {
636
+ assert.strictEqual(a, b);
637
+ }
638
+ },
639
+ // @ネットワーク
640
+ '自分IPアドレス取得': {
641
+ type: 'func',
642
+ josi: [],
643
+ pure: true,
644
+ fn: function (sys) {
645
+ const nif = os.networkInterfaces();
646
+ if (!nif) {
647
+ throw new Error('『自分IPアドレス取得』でネットワークのインターフェイスが種畜できません。');
648
+ }
649
+ /**
650
+ * @type {string[]}
651
+ */
652
+ const result = [];
653
+ for (const dev in nif) {
654
+ const n = nif[dev];
655
+ if (!n) {
656
+ continue;
657
+ }
658
+ n.forEach((detail) => {
659
+ if (detail.family === 'IPv4') {
660
+ result.push(detail.address);
661
+ }
662
+ });
663
+ }
664
+ return result;
665
+ }
666
+ },
667
+ '自分IPV6アドレス取得': {
668
+ type: 'func',
669
+ josi: [],
670
+ pure: true,
671
+ fn: function (sys) {
672
+ const nif = os.networkInterfaces();
673
+ if (!nif) {
674
+ throw new Error('『自分IPアドレス取得』でネットワークのインターフェイスが種畜できません。');
675
+ }
676
+ const result = [];
677
+ for (const dev in nif) {
678
+ const n = nif[dev];
679
+ if (!n) {
680
+ continue;
681
+ }
682
+ n.forEach((detail) => {
683
+ if (detail.family === 'IPv6') {
684
+ result.push(detail.address);
685
+ }
686
+ });
687
+ }
688
+ return result;
689
+ }
690
+ },
691
+ // @Ajax
692
+ 'AJAX送信時': {
693
+ type: 'func',
694
+ josi: [['の'], ['まで', 'へ', 'に']],
695
+ pure: true,
696
+ fn: function (callback, url, sys) {
697
+ let options = sys.__v0['AJAXオプション'];
698
+ if (options === '') {
699
+ options = { method: 'GET' };
700
+ }
701
+ fetch(url, options).then((res) => {
702
+ return res.text();
703
+ }).then((text) => {
704
+ sys.__v0['対象'] = text;
705
+ callback(text);
706
+ }).catch((err) => {
707
+ console.log('[fetch.error]', err);
708
+ throw err;
709
+ });
710
+ },
711
+ return_none: true
712
+ },
713
+ 'AJAX受信時': {
714
+ type: 'func',
715
+ josi: [['で'], ['から', 'を']],
716
+ pure: true,
717
+ fn: function (callback, url, sys) {
718
+ sys.__exec('AJAX送信時', [callback, url, sys]);
719
+ },
720
+ return_none: true
721
+ },
722
+ 'GET送信時': {
723
+ type: 'func',
724
+ josi: [['の'], ['まで', 'へ', 'に']],
725
+ pure: false,
726
+ fn: function (callback, url, sys) {
727
+ sys.__exec('AJAX送信時', [callback, url, sys]);
728
+ },
729
+ return_none: true
730
+ },
731
+ 'POST送信時': {
732
+ type: 'func',
733
+ josi: [['の'], ['まで', 'へ', 'に'], ['を']],
734
+ pure: true,
735
+ fn: function (callback, url, params, sys) {
736
+ const flist = [];
737
+ for (const key in params) {
738
+ const v = params[key];
739
+ const kv = encodeURIComponent(key) + '=' + encodeURIComponent(v);
740
+ flist.push(kv);
741
+ }
742
+ const bodyData = flist.join('&');
743
+ const options = {
744
+ method: 'POST',
745
+ headers: {
746
+ 'Content-Type': 'application/x-www-form-urlencoded'
747
+ },
748
+ body: bodyData
749
+ };
750
+ fetch(url, options).then((res) => {
751
+ return res.text();
752
+ }).then((text) => {
753
+ sys.__v0['対象'] = text;
754
+ callback(text);
755
+ }).catch((err) => {
756
+ sys.__v0['AJAX:ONERROR'](err);
757
+ });
758
+ }
759
+ },
760
+ 'POSTフォーム送信時': {
761
+ type: 'func',
762
+ josi: [['の'], ['まで', 'へ', 'に'], ['を']],
763
+ pure: true,
764
+ fn: function (callback, url, params, sys) {
765
+ const fd = new FormData();
766
+ for (const key in params) {
767
+ fd.set(key, params[key]);
768
+ }
769
+ const options = {
770
+ method: 'POST',
771
+ headers: {
772
+ 'Content-Type': 'multipart/form-data'
773
+ },
774
+ body: fd
775
+ };
776
+ fetch(url, options).then((res) => {
777
+ return res.text();
778
+ }).then((text) => {
779
+ sys.__v0['対象'] = text;
780
+ callback(text);
781
+ }).catch((err) => {
782
+ sys.__v0['AJAX:ONERROR'](err);
783
+ });
784
+ }
785
+ },
786
+ 'AJAX失敗時': {
787
+ type: 'func',
788
+ josi: [['の']],
789
+ pure: true,
790
+ fn: function (callback, sys) {
791
+ sys.__v0['AJAX:ONERROR'] = callback;
792
+ }
793
+ },
794
+ 'AJAXオプション': { type: 'const', value: '' },
795
+ 'AJAXオプション設定': {
796
+ type: 'func',
797
+ josi: [['に', 'へ', 'と']],
798
+ pure: true,
799
+ fn: function (option, sys) {
800
+ sys.__v0['AJAXオプション'] = option;
801
+ },
802
+ return_none: true
803
+ },
804
+ 'AJAX保障送信': {
805
+ type: 'func',
806
+ josi: [['まで', 'へ', 'に']],
807
+ pure: true,
808
+ fn: function (url, sys) {
809
+ let options = sys.__v0['AJAXオプション'];
810
+ if (options === '') {
811
+ options = { method: 'GET' };
812
+ }
813
+ return fetch(url, options);
814
+ },
815
+ return_none: false
816
+ },
817
+ 'HTTP保障取得': {
818
+ type: 'func',
819
+ josi: [['の', 'から', 'を']],
820
+ pure: true,
821
+ fn: function (url, sys) {
822
+ return sys.__exec('AJAX保障送信', [url, sys]);
823
+ },
824
+ return_none: false
825
+ },
826
+ 'GET保障送信': {
827
+ type: 'func',
828
+ josi: [['まで', 'へ', 'に']],
829
+ pure: true,
830
+ fn: function (url, sys) {
831
+ return sys.__exec('AJAX保障送信', [url, sys]);
832
+ },
833
+ return_none: false
834
+ },
835
+ 'POST保障送信': {
836
+ type: 'func',
837
+ josi: [['まで', 'へ', 'に'], ['を']],
838
+ pure: true,
839
+ fn: function (url, params, sys) {
840
+ const flist = [];
841
+ for (const key in params) {
842
+ const v = params[key];
843
+ const kv = encodeURIComponent(key) + '=' + encodeURIComponent(v);
844
+ flist.push(kv);
845
+ }
846
+ const bodyData = flist.join('&');
847
+ const options = {
848
+ method: 'POST',
849
+ headers: {
850
+ 'Content-Type': 'application/x-www-form-urlencoded'
851
+ },
852
+ body: bodyData
853
+ };
854
+ return fetch(url, options);
855
+ },
856
+ return_none: false
857
+ },
858
+ 'POSTフォーム保障送信': {
859
+ type: 'func',
860
+ josi: [['まで', 'へ', 'に'], ['を']],
861
+ pure: true,
862
+ fn: function (url, params, sys) {
863
+ const fd = new FormData();
864
+ for (const key in params) {
865
+ fd.set(key, params[key]);
866
+ }
867
+ const options = {
868
+ method: 'POST',
869
+ body: fd
870
+ };
871
+ return fetch(url, options);
872
+ },
873
+ return_none: false
874
+ },
875
+ 'AJAX内容取得': {
876
+ type: 'func',
877
+ josi: [['から'], ['で']],
878
+ pure: true,
879
+ fn: function (res, type, sys) {
880
+ type = type.toString().toUpperCase();
881
+ if (type === 'TEXT' || type === 'テキスト') {
882
+ return res.text();
883
+ }
884
+ else if (type === 'JSON') {
885
+ return res.json();
886
+ }
887
+ else if (type === 'BLOB') {
888
+ return res.blob();
889
+ }
890
+ else if (type === 'ARRAY' || type === '配列') {
891
+ return res.arrayBuffer();
892
+ }
893
+ else if (type === 'BODY' || type === '本体') {
894
+ return res.body;
895
+ }
896
+ return res.body();
897
+ },
898
+ return_none: false
899
+ },
900
+ 'AJAX受信': {
901
+ type: 'func',
902
+ josi: [['から', 'を']],
903
+ pure: true,
904
+ fn: function (url, sys) {
905
+ if (sys.__genMode !== '非同期モード') {
906
+ throw new Error('『AJAX受信』を使うには、プログラムの冒頭で「!非同期モード」と宣言してください。');
907
+ }
908
+ const sysenv = sys.setAsync(sys);
909
+ let options = sys.__v0['AJAXオプション'];
910
+ if (options === '') {
911
+ options = { method: 'GET' };
912
+ }
913
+ // fetch 実行
914
+ fetch(url, options).then((res) => {
915
+ if (res.ok) { // 成功したとき
916
+ return res.text();
917
+ }
918
+ else { // 失敗したとき
919
+ throw new Error('status=' + res.status);
920
+ }
921
+ }).then((text) => {
922
+ sys.__v0['対象'] = text;
923
+ sys.compAsync(sys, sysenv);
924
+ }).catch((err) => {
925
+ console.error('[AJAX受信のエラー]', err);
926
+ sys.__errorAsync(err, sys);
927
+ });
928
+ },
929
+ return_none: true
930
+ },
931
+ // @新AJAX
932
+ 'AJAXテキスト取得': {
933
+ type: 'func',
934
+ josi: [['から']],
935
+ pure: true,
936
+ asyncFn: true,
937
+ fn: async function (url, sys) {
938
+ let options = sys.__v0['AJAXオプション'];
939
+ if (options === '') {
940
+ options = { method: 'GET' };
941
+ }
942
+ const res = await fetch(url, options);
943
+ const txt = await res.text();
944
+ return txt;
945
+ },
946
+ return_none: false
947
+ },
948
+ 'AJAX_JSON取得': {
949
+ type: 'func',
950
+ josi: [['から']],
951
+ pure: true,
952
+ asyncFn: true,
953
+ fn: async function (url, sys) {
954
+ let options = sys.__v0['AJAXオプション'];
955
+ if (options === '') {
956
+ options = { method: 'GET' };
957
+ }
958
+ const res = await fetch(url, options);
959
+ const txt = await res.json();
960
+ return txt;
961
+ },
962
+ return_none: false
963
+ },
964
+ // @文字コード
965
+ '文字コード変換サポート判定': {
966
+ type: 'func',
967
+ josi: [['の', 'を']],
968
+ pure: true,
969
+ fn: function (code, sys) {
970
+ return iconv.encodingExists(code);
971
+ }
972
+ },
973
+ 'SJIS変換': {
974
+ type: 'func',
975
+ josi: [['に', 'へ', 'を']],
976
+ pure: true,
977
+ fn: function (str, sys) {
978
+ // iconv.skipDecodeWarning = true
979
+ return iconv.encode(str, 'Shift_JIS');
980
+ }
981
+ },
982
+ 'SJIS取得': {
983
+ type: 'func',
984
+ josi: [['から', 'を', 'で']],
985
+ pure: true,
986
+ fn: function (buf, sys) {
987
+ // iconv.skipDecodeWarning = true
988
+ return iconv.decode(Buffer.from(buf), 'sjis');
989
+ }
990
+ },
991
+ 'エンコーディング変換': {
992
+ type: 'func',
993
+ josi: [['を'], ['へ', 'で']],
994
+ pure: true,
995
+ fn: function (s, code, sys) {
996
+ // iconv.skipDecodeWarning = true
997
+ return iconv.encode(s, code);
998
+ }
999
+ },
1000
+ 'エンコーディング取得': {
1001
+ type: 'func',
1002
+ josi: [['を'], ['から', 'で']],
1003
+ pure: true,
1004
+ fn: function (buf, code, sys) {
1005
+ // iconv.skipDecodeWarning = true
1006
+ return iconv.decode(Buffer.from(buf), code);
1007
+ }
1008
+ },
1009
+ // @ハッシュ関数
1010
+ 'ハッシュ関数一覧取得': {
1011
+ type: 'func',
1012
+ josi: [],
1013
+ pure: true,
1014
+ fn: function (sys) {
1015
+ return crypto.getHashes();
1016
+ }
1017
+ },
1018
+ 'ハッシュ値計算': {
1019
+ type: 'func',
1020
+ josi: [['を'], ['の'], ['で']],
1021
+ pure: true,
1022
+ fn: function (s, alg, enc, sys) {
1023
+ const hashsum = crypto.createHash(alg);
1024
+ hashsum.update(s);
1025
+ return hashsum.digest(enc);
1026
+ }
1027
+ }
1028
+ };
1029
+ // ローカル関数
1030
+ function fileExists(f) {
1031
+ try {
1032
+ fs.statSync(f);
1033
+ return true;
1034
+ }
1035
+ catch (err) {
1036
+ return false;
1037
+ }
1038
+ }
1039
+ function isDir(f) {
1040
+ try {
1041
+ const st = fs.statSync(f);
1042
+ return st.isDirectory();
1043
+ }
1044
+ catch (err) {
1045
+ return false;
1046
+ }
1047
+ }