nadesiko3 3.3.49 → 3.3.50

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 (342) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +143 -143
  3. package/batch/.DS_Store +0 -0
  4. package/batch/browsers.template.md +3 -3
  5. package/batch/build_browsers.nako3 +72 -72
  6. package/batch/build_command.nako3 +44 -44
  7. package/batch/build_nako_version.nako3 +42 -42
  8. package/batch/calc_hash.nako3 +29 -29
  9. package/batch/cmd_txt2json.nako3 +74 -74
  10. package/batch/command.txt +79 -0
  11. package/batch/command_nakopad.txt +69 -0
  12. package/batch/download-extlib.nako3 +43 -43
  13. package/batch/gen_command_nakopad.nako3 +57 -57
  14. package/batch/jsplugin2text.nako3 +285 -285
  15. package/batch/pickup_command.nako3 +110 -110
  16. package/batch/pickup_reserved_words.nako3 +11 -11
  17. package/batch/publish_version.nako3 +46 -46
  18. package/batch/show_agents.js +14 -14
  19. package/batch/turtle2js.nako3 +21 -21
  20. package/bin/cnako3 +10 -10
  21. package/core/LICENSE +21 -21
  22. package/core/README.md +66 -66
  23. package/core/batch/build_nako_version.nako3 +42 -42
  24. package/core/command/snako.mjs +105 -105
  25. package/core/command/snako.mts +116 -116
  26. package/core/index.mjs +21 -21
  27. package/core/package.json +47 -47
  28. package/core/sample/hello.nako3 +7 -7
  29. package/core/sample/hoge.mjs +4 -4
  30. package/core/sample/hoge.mts +6 -6
  31. package/core/src/nako3.mjs +864 -858
  32. package/core/src/nako3.mts +976 -967
  33. package/core/src/nako_colors.mjs +78 -78
  34. package/core/src/nako_colors.mts +86 -86
  35. package/core/src/nako_core_version.mjs +8 -8
  36. package/core/src/nako_core_version.mts +19 -19
  37. package/core/src/nako_csv.mjs +185 -185
  38. package/core/src/nako_csv.mts +188 -188
  39. package/core/src/nako_errors.mjs +173 -173
  40. package/core/src/nako_errors.mts +197 -197
  41. package/core/src/nako_from_dncl.mjs +255 -255
  42. package/core/src/nako_from_dncl.mts +250 -250
  43. package/core/src/nako_gen.mjs +1647 -1648
  44. package/core/src/nako_gen.mts +1718 -1719
  45. package/core/src/nako_gen_async.mjs +1659 -1659
  46. package/core/src/nako_gen_async.mts +1732 -1732
  47. package/core/src/nako_global.mjs +107 -107
  48. package/core/src/nako_global.mts +138 -138
  49. package/core/src/nako_indent.mjs +445 -445
  50. package/core/src/nako_indent.mts +492 -492
  51. package/core/src/nako_josi_list.mjs +38 -38
  52. package/core/src/nako_josi_list.mts +45 -45
  53. package/core/src/nako_lex_rules.mjs +253 -253
  54. package/core/src/nako_lex_rules.mts +260 -260
  55. package/core/src/nako_lexer.mjs +609 -609
  56. package/core/src/nako_lexer.mts +612 -612
  57. package/core/src/nako_logger.mjs +199 -199
  58. package/core/src/nako_logger.mts +232 -232
  59. package/core/src/nako_parser3.mjs +2439 -2439
  60. package/core/src/nako_parser3.mts +2195 -2195
  61. package/core/src/nako_parser_base.mjs +370 -370
  62. package/core/src/nako_parser_base.mts +370 -370
  63. package/core/src/nako_parser_const.mjs +37 -37
  64. package/core/src/nako_parser_const.mts +37 -37
  65. package/core/src/nako_prepare.mjs +304 -304
  66. package/core/src/nako_prepare.mts +315 -315
  67. package/core/src/nako_reserved_words.mjs +38 -38
  68. package/core/src/nako_reserved_words.mts +38 -38
  69. package/core/src/nako_source_mapping.mjs +207 -207
  70. package/core/src/nako_source_mapping.mts +262 -262
  71. package/core/src/nako_test.mjs +37 -37
  72. package/core/src/nako_types.mjs +25 -25
  73. package/core/src/nako_types.mts +151 -151
  74. package/core/src/plugin_csv.mjs +49 -49
  75. package/core/src/plugin_csv.mts +50 -50
  76. package/core/src/plugin_math.mjs +328 -328
  77. package/core/src/plugin_math.mts +326 -326
  78. package/core/src/plugin_promise.mjs +91 -91
  79. package/core/src/plugin_promise.mts +91 -91
  80. package/core/src/plugin_system.mjs +2832 -2832
  81. package/core/src/plugin_system.mts +2690 -2690
  82. package/core/src/plugin_test.mjs +34 -34
  83. package/core/src/plugin_test.mts +34 -34
  84. package/demo/.DS_Store +0 -0
  85. package/demo/ace_editor.html +89 -89
  86. package/demo/ace_editor_tabs.html +161 -161
  87. package/demo/basic.html +71 -71
  88. package/demo/browsers.html +10 -9
  89. package/demo/css/basic.css +3 -3
  90. package/demo/css/common.css +157 -157
  91. package/demo/css/editor.css +8 -8
  92. package/demo/css/flow.css +3 -3
  93. package/demo/css/index.css +3 -3
  94. package/demo/extlib/.DS_Store +0 -0
  95. package/demo/flow.html +98 -98
  96. package/demo/graph.html +53 -53
  97. package/demo/image/nakopad-icon256.png +0 -0
  98. package/demo/index.html +133 -133
  99. package/demo/js/common.js +17 -17
  100. package/demo/js/turtle3d_test.js +44 -44
  101. package/demo/js/turtle_test.js +45 -45
  102. package/demo/nako3/calc.nako3 +4 -4
  103. package/demo/runscript.html +47 -47
  104. package/demo/runscript2.html +33 -33
  105. package/demo/runscript3.html +35 -35
  106. package/demo/runscript4.html +33 -33
  107. package/demo/turtle.html +58 -58
  108. package/demo/turtle2.html +141 -141
  109. package/demo/turtle3.html +279 -279
  110. package/demo/turtle3d.html +58 -58
  111. package/demo/turtle3d2.html +107 -107
  112. package/demo/version.html +24 -24
  113. package/doc/SETUP.md +157 -157
  114. package/doc/about.md +17 -17
  115. package/doc/browsers.md +26 -26
  116. package/doc/docgen.md +21 -21
  117. package/doc/editor.md +44 -44
  118. package/doc/files.md +39 -39
  119. package/doc/plugins.md +234 -234
  120. package/doc/release.md +79 -79
  121. package/doc/textlint.md +43 -43
  122. package/doc/win32.md +57 -57
  123. package/package.json +195 -195
  124. package/release/_hash.txt +28 -28
  125. package/release/_script-tags.txt +14 -14
  126. package/release/command.json +1 -1
  127. package/release/command.json.js +1 -1
  128. package/release/command_cnako3.json +1 -1
  129. package/release/command_list.json +1 -1
  130. package/release/editor.js +1 -1
  131. package/release/nako_gen_async.js +1 -1
  132. package/release/nako_gen_async.js.LICENSE.txt +35 -0
  133. package/release/plugin_caniuse.js.LICENSE.txt +11 -0
  134. package/release/plugin_csv.js +1 -1
  135. package/release/plugin_csv.js.LICENSE.txt +15 -0
  136. package/release/plugin_datetime.js.LICENSE.txt +15 -0
  137. package/release/plugin_kansuji.js.LICENSE.txt +3 -0
  138. package/release/plugin_markup.js.LICENSE.txt +11 -0
  139. package/release/plugin_turtle.js.LICENSE.txt +15 -0
  140. package/release/plugin_webworker.js.LICENSE.txt +3 -0
  141. package/release/plugin_weykturtle3d.js.LICENSE.txt +3 -0
  142. package/release/stats.json +1 -1
  143. package/release/version.js +1 -1
  144. package/release/wnako3.js +1 -1
  145. package/release/wnako3webworker.js +1 -1
  146. package/release/wnako3webworker.js.LICENSE.txt +131 -0
  147. package/src/.DS_Store +0 -0
  148. package/src/browsers.txt +12 -11
  149. package/src/browsers_agents.json +2 -2
  150. package/src/browsers_agents.mjs +1 -1
  151. package/src/cnako3.mjs +17 -17
  152. package/src/cnako3.mts +18 -18
  153. package/src/cnako3mod.mjs +707 -707
  154. package/src/cnako3mod.mts +712 -712
  155. package/src/commander_ja.mjs +164 -164
  156. package/src/commander_ja.mts +161 -161
  157. package/src/enako3.mjs +68 -68
  158. package/src/era.mjs +22 -22
  159. package/src/image_turtle-elephant.mjs +2 -2
  160. package/src/image_turtle-panda.mjs +2 -2
  161. package/src/image_turtle64.mjs +2 -2
  162. package/src/index.mjs +9 -9
  163. package/src/index.mts +10 -10
  164. package/src/nako3editorfix.sfd +106 -106
  165. package/src/nako_version.mjs +8 -8
  166. package/src/nako_version.mts +2 -2
  167. package/src/plugin_browser.mjs +213 -213
  168. package/src/plugin_browser.mts +206 -206
  169. package/src/plugin_browser_ajax.mjs +399 -399
  170. package/src/plugin_browser_audio.mjs +109 -109
  171. package/src/plugin_browser_canvas.mjs +449 -449
  172. package/src/plugin_browser_chart.mjs +294 -294
  173. package/src/plugin_browser_color.mjs +49 -49
  174. package/src/plugin_browser_crypto.mjs +26 -26
  175. package/src/plugin_browser_dialog.mjs +53 -53
  176. package/src/plugin_browser_dom_basic.mjs +336 -336
  177. package/src/plugin_browser_dom_event.mjs +193 -193
  178. package/src/plugin_browser_dom_parts.mjs +396 -396
  179. package/src/plugin_browser_geolocation.mjs +51 -51
  180. package/src/plugin_browser_hotkey.mjs +25 -25
  181. package/src/plugin_browser_html.mjs +59 -59
  182. package/src/plugin_browser_in_worker.mjs +45 -45
  183. package/src/plugin_browser_location.mjs +21 -21
  184. package/src/plugin_browser_speech.mjs +111 -111
  185. package/src/plugin_browser_storage.mjs +121 -121
  186. package/src/plugin_browser_system.mjs +31 -31
  187. package/src/plugin_browser_websocket.mjs +73 -73
  188. package/src/plugin_caniuse.mjs +29 -29
  189. package/src/plugin_datetime.mjs +394 -394
  190. package/src/plugin_httpserver.mjs +277 -277
  191. package/src/plugin_httpserver.mts +286 -286
  192. package/src/plugin_kansuji.mjs +224 -224
  193. package/src/plugin_keigo.mjs +55 -55
  194. package/src/plugin_markup.mjs +32 -32
  195. package/src/plugin_node.mjs +1047 -1047
  196. package/src/plugin_node.mts +980 -980
  197. package/src/plugin_turtle.mjs +647 -647
  198. package/src/plugin_webworker.mjs +334 -334
  199. package/src/plugin_weykturtle3d.mjs +1214 -1214
  200. package/src/plugin_worker.mjs +95 -95
  201. package/src/repl.nako3 +63 -63
  202. package/src/wnako3.mjs +12 -12
  203. package/src/wnako3.mts +11 -11
  204. package/src/wnako3_editor.css +215 -215
  205. package/src/wnako3_editor.mjs +1542 -1542
  206. package/src/wnako3_editor.mts +1658 -1657
  207. package/src/wnako3mod.mjs +213 -213
  208. package/src/wnako3mod.mts +214 -214
  209. package/src/wnako3webworker.mjs +69 -69
  210. package/test/.DS_Store +0 -0
  211. package/test/ace_editor/karma.config.js +94 -94
  212. package/test/ace_editor/test/.babelrc.json +3 -3
  213. package/test/ace_editor/test/ace_editor_test.js +178 -178
  214. package/test/ace_editor/test/html/custom_context.html +139 -139
  215. package/test/async/async_basic_test.mjs +122 -122
  216. package/test/browser/.DS_Store +0 -0
  217. package/test/browser/karma.config.js +221 -221
  218. package/test/browser/test/.babelrc.json +3 -3
  219. package/test/browser/test/compare_util.js +50 -50
  220. package/test/browser/test/html/div_basic.html +2 -2
  221. package/test/browser/test/html/event_dom_form.html +4 -4
  222. package/test/browser/test/html/event_dom_scrolldiv.html +5 -5
  223. package/test/browser/test/import_plugin_checker.js +24 -24
  224. package/test/browser/test/plugin_browser_test.js +51 -51
  225. package/test/browser/test/plugin_browser_test_ajax.js +123 -123
  226. package/test/browser/test/plugin_browser_test_color.js +18 -18
  227. package/test/browser/test/plugin_browser_test_dialog.js +72 -72
  228. package/test/browser/test/plugin_browser_test_dom_event.js +598 -598
  229. package/test/browser/test/plugin_browser_test_dom_parts.js +125 -125
  230. package/test/browser/test/plugin_browser_test_system.js +9 -9
  231. package/test/browser/test/plugin_turtle_test.js +817 -817
  232. package/test/browser/test/plugin_webworker_test.js +86 -86
  233. package/test/browser/test/require_test.js +68 -68
  234. package/test/bundled/.DS_Store +0 -0
  235. package/test/bundled/karma.config.base.js +117 -117
  236. package/test/bundled/karma.config.js +86 -86
  237. package/test/bundled/test/.babelrc.json +3 -3
  238. package/test/bundled/test/bundled_test.js +69 -69
  239. package/test/bundled/test/html/custom_context.html +65 -65
  240. package/test/bundled/test/html/custom_debug.html +66 -66
  241. package/test/bundled/test4b.cmd +52 -52
  242. package/test/bundled/test_base/.DS_Store +0 -0
  243. package/test/bundled/test_base/.babelrc.json +3 -3
  244. package/test/bundled/test_base/_checktool_test.js +25 -25
  245. package/test/bundled/test_base/basic_ajax_test.js +56 -56
  246. package/test/bundled/test_base/basic_async_test.js +18 -18
  247. package/test/bundled/test_base/basic_test.js +153 -153
  248. package/test/bundled/test_base/calc_test.js +132 -132
  249. package/test/bundled/test_base/css/browsers_box.css +114 -114
  250. package/test/bundled/test_base/html/custom_context.html +69 -69
  251. package/test/bundled/test_base/html/custom_debug.html +71 -71
  252. package/test/bundled/test_base/js/browsers_box.js +72 -72
  253. package/test/bundled/test_base/plugin_csv_test.js +37 -37
  254. package/test/bundled/test_base/plugin_datetime_test.js +115 -115
  255. package/test/bundled/test_base/plugin_kansuji_test.js +49 -49
  256. package/test/bundled/test_base/plugin_system_test.js +410 -410
  257. package/test/bundled/test_base/plugin_webworker_test.js +53 -53
  258. package/test/bundled/test_base/test_utils.js +191 -191
  259. package/test/common/.DS_Store +0 -0
  260. package/test/common/plugin_browser_test.mjs +22 -22
  261. package/test/common/plugin_browser_ut_audio_test.mjs +108 -108
  262. package/test/common/plugin_browser_ut_color_test.mjs +21 -21
  263. package/test/common/plugin_browser_ut_dialog_test.mjs +100 -100
  264. package/test/common/plugin_browser_ut_html_test.mjs +13 -13
  265. package/test/common/plugin_browser_ut_system_test.mjs +10 -10
  266. package/test/common/plugin_markup_test.mjs +23 -23
  267. package/test/jsconfig.json +19 -19
  268. package/test/karma.config.js +91 -91
  269. package/test/node/.DS_Store +0 -0
  270. package/test/node/async_test.mjs +96 -96
  271. package/test/node/commander_ja_test.mjs +89 -89
  272. package/test/node/error_message_test.mjs +243 -243
  273. package/test/node/kai_test.nako3 +6 -6
  274. package/test/node/node_test.mjs +60 -60
  275. package/test/node/plugin_broken.js.txt +3 -3
  276. package/test/node/plugin_browser_ut_ajax_test.mjs.todo +357 -357
  277. package/test/node/plugin_browser_ut_location_test.mjs +42 -42
  278. package/test/node/plugin_markup_test.mjs +47 -47
  279. package/test/node/plugin_math_test.mjs +45 -45
  280. package/test/node/plugin_node_test.mjs +98 -98
  281. package/test/node/plugin_test.mjs +44 -44
  282. package/test/node/relative_import_test_1.nako3 +1 -1
  283. package/test/node/relative_import_test_2.nako3 +2 -2
  284. package/test/node/require_nako3_test.mjs +67 -67
  285. package/test/node/requiretest.nako3 +4 -4
  286. package/test/node/requiretest_indirect.nako3 +1 -1
  287. package/test/node/requiretest_name.nako3 +5 -5
  288. package/test/node/runtime_error.nako3 +2 -2
  289. package/test/node/scope1.nako3 +10 -10
  290. package/test/node/scope2.nako3 +12 -12
  291. package/test/node/side_effects_test.mjs +39 -39
  292. package/test/node/sjis.txt +5 -5
  293. package/test/node/syntax_error.nako3 +1 -1
  294. package/test/node/wnako3_editor_test.mjs +384 -384
  295. package/tools/.DS_Store +0 -0
  296. package/tools/README.md +12 -12
  297. package/tools/check_new_version.nako3 +25 -25
  298. package/tools/nako3edit/.DS_Store +0 -0
  299. package/tools/nako3edit/a.sqlite3 +0 -0
  300. package/tools/nako3edit/html/.DS_Store +0 -0
  301. package/tools/nako3edit/html/daisyui/LICENSE +22 -22
  302. package/tools/nako3edit/html/daisyui/version_2.14.1 +1 -1
  303. package/tools/nako3edit/html/edit.html +170 -170
  304. package/tools/nako3edit/html/edit_plugin.js +6 -6
  305. package/tools/nako3edit/html/files.html +125 -125
  306. package/tools/nako3edit/html/nako3edit.css +65 -65
  307. package/tools/nako3edit/index.mjs +248 -248
  308. package/tools/nako3server/index.html +10 -10
  309. package/tools/nako3server/index.mjs +116 -116
  310. package/tools/nako3server/index.nako3 +34 -34
  311. package/core/.editorconfig +0 -6
  312. package/core/.eslintrc.cjs +0 -33
  313. package/core/.github/dependabot.yml +0 -7
  314. package/core/.github/workflows/nodejs.yml +0 -37
  315. package/core/.github/workflows/super-linter.yml +0 -61
  316. package/core/.github/workflows/textlint.yml +0 -199
  317. package/core/index.mts +0 -21
  318. package/core/test/array_test.mjs +0 -34
  319. package/core/test/basic_test.mjs +0 -344
  320. package/core/test/calc_test.mjs +0 -140
  321. package/core/test/core_module_test.mjs +0 -23
  322. package/core/test/debug_test.mjs +0 -16
  323. package/core/test/dncl_test.mjs +0 -94
  324. package/core/test/error_message_test.mjs +0 -210
  325. package/core/test/error_test.mjs +0 -16
  326. package/core/test/flow_test.mjs +0 -373
  327. package/core/test/func_call.mjs +0 -160
  328. package/core/test/func_test.mjs +0 -149
  329. package/core/test/indent_test.mjs +0 -364
  330. package/core/test/lex_test.mjs +0 -168
  331. package/core/test/literal_test.mjs +0 -73
  332. package/core/test/nako_lexer_test.mjs +0 -35
  333. package/core/test/nako_logger_test.mjs +0 -76
  334. package/core/test/nako_logger_test.mts +0 -78
  335. package/core/test/plugin_csv_test.mjs +0 -38
  336. package/core/test/plugin_promise_test.mjs +0 -18
  337. package/core/test/plugin_system_test.mjs +0 -630
  338. package/core/test/prepare_test.mjs +0 -96
  339. package/core/test/re_test.mjs +0 -22
  340. package/core/test/side_effects_test.mjs +0 -92
  341. package/core/test/variable_scope_test.mjs +0 -149
  342. package/core/tsconfig.json +0 -101
package/src/cnako3mod.mjs CHANGED
@@ -1,707 +1,707 @@
1
- /**
2
- * コマンドライン版のなでしこ3をモジュールとして定義
3
- * 実際には cnako3.mjs から読み込まれる
4
- */
5
- import fs from 'fs';
6
- import fse from 'fs-extra';
7
- import { exec } from 'child_process';
8
- import path from 'path';
9
- import { NakoCompiler } from '../core/src/nako3.mjs';
10
- import { NakoImportError } from '../core/src/nako_errors.mjs';
11
- import { CompilerOptions } from '../core/src/nako_types.mjs';
12
- import nakoVersion from './nako_version.mjs';
13
- import PluginNode from './plugin_node.mjs';
14
- import app from './commander_ja.mjs';
15
- import fetch from 'node-fetch';
16
- import { NakoGenOptions } from '../core/src/nako_gen.mjs';
17
- // __dirname のために
18
- import url from 'url';
19
- const __filename = url.fileURLToPath(import.meta.url);
20
- const __dirname = path.dirname(__filename);
21
- /** CNako3 */
22
- export class CNako3 extends NakoCompiler {
23
- constructor(opts = { nostd: false }) {
24
- super({ useBasicPlugin: !opts.nostd });
25
- this.debug = false;
26
- this.filename = 'main.nako3';
27
- this.version = nakoVersion.version;
28
- if (!opts.nostd) {
29
- this.addPluginFile('PluginNode', path.join(__dirname, 'plugin_node.mjs'), PluginNode);
30
- }
31
- // 必要な定数を設定
32
- this.addListener('beforeRun', (g) => {
33
- g.__varslist[0]['ナデシコ種類'] = 'cnako3';
34
- g.__varslist[0]['ナデシコバージョン'] = this.version;
35
- });
36
- }
37
- // CNAKO3で使えるコマンドを登録する
38
- registerCommands() {
39
- // コマンド引数がないならば、ヘルプを表示(-hはcommandarにデフォルト用意されている)
40
- if (process.argv.length <= 2) {
41
- process.argv.push('-h');
42
- }
43
- const verInfo = `v${nakoVersion.version}`;
44
- // commanderを使って引数を解析する
45
- app
46
- .title('日本語プログラミング言語「なでしこ」' + verInfo)
47
- .version(verInfo, '-v, --version')
48
- .usage('[オプション] 入力ファイル.nako3')
49
- .option('-h, --help', 'コマンドの使い方を表示')
50
- .option('-w, --warn', '警告を表示する')
51
- .option('-d, --debug', 'デバッグモードの指定')
52
- .option('-D, --trace', '詳細デバッグモードの指定')
53
- .option('-c, --compile', 'コンパイルモードの指定')
54
- .option('-t, --test', 'コンパイルモードの指定 (テスト用コードを出力)')
55
- .option('-r, --run', 'コンパイルモードでも実行する')
56
- .option('-e, --eval [src]', '直接プログラムを実行するワンライナーモード')
57
- .option('-o, --output', '出力ファイル名の指定')
58
- .option('-s, --silent', 'サイレントモードの指定')
59
- .option('-l, --repl', '対話シェル(REPL)の実行')
60
- .option('-b, --browsers', '対応機器/Webブラウザを表示する')
61
- .option('-m, --man [command]', 'マニュアルを表示する')
62
- .option('-p, --speed', 'スピード優先モードの指定')
63
- .option('-A, --ast', 'パースした結果をASTで出力する')
64
- // .option('-h, --help', '使い方を表示する')
65
- // .option('-v, --version', 'バージョンを表示する')
66
- .parse(process.argv);
67
- return app;
68
- }
69
- /** コマンドライン引数を解析 */
70
- checkArguments() {
71
- const app = this.registerCommands();
72
- let logLevel = 'error';
73
- if (app.trace) {
74
- logLevel = 'trace';
75
- }
76
- else if (app.debug) {
77
- logLevel = 'debug';
78
- }
79
- else if (app.warn) {
80
- logLevel = 'warn';
81
- }
82
- this.getLogger().addListener(logLevel, ({ level, nodeConsole }) => {
83
- console.log(nodeConsole);
84
- });
85
- const args = {
86
- compile: app.compile || false,
87
- run: app.run || false,
88
- source: app.eval || '',
89
- man: app.man || '',
90
- one_liner: app.eval || false,
91
- debug: this.debug || false,
92
- trace: app.trace,
93
- warn: app.warn,
94
- repl: app.repl || false,
95
- test: app.test || false,
96
- browsers: app.browsers || false,
97
- speed: app.speed || false,
98
- ast: app.ast || false
99
- };
100
- args.mainfile = app.args[0];
101
- args.output = app.output;
102
- // todo: ESModule 対応の '.mjs' のコードを吐くように修正 #1217
103
- const ext = '.mjs';
104
- if (/\.(nako|nako3|txt|bak)$/.test(args.mainfile)) {
105
- if (!args.output) {
106
- if (args.test) {
107
- args.output = args.mainfile.replace(/\.(nako|nako3)$/, '.spec' + ext);
108
- }
109
- else {
110
- args.output = args.mainfile.replace(/\.(nako|nako3)$/, ext);
111
- }
112
- }
113
- }
114
- else {
115
- if (!args.output) {
116
- if (args.test) {
117
- args.output = args.mainfile + '.spec' + ext;
118
- }
119
- else {
120
- args.output = args.mainfile + ext;
121
- }
122
- }
123
- args.mainfile += '.nako3';
124
- }
125
- return args;
126
- }
127
- // 実行する
128
- async execCommand() {
129
- // コマンドを解析
130
- const opt = this.checkArguments();
131
- // 使い方の表示か?
132
- if (opt.man) {
133
- this.cnakoMan(opt.man);
134
- return;
135
- }
136
- // 対応ブラウザを表示する
137
- if (opt.browsers) {
138
- this.cnakoBrowsers();
139
- return;
140
- }
141
- // REPLを実行する
142
- if (opt.repl) {
143
- this.cnakoRepl(opt);
144
- return;
145
- }
146
- // ワンライナーで実行する
147
- if (opt.one_liner) {
148
- this.cnakoOneLiner(opt);
149
- return;
150
- }
151
- // メインプログラムを読み込む
152
- this.filename = opt.mainfile;
153
- const src = fs.readFileSync(opt.mainfile, 'utf-8');
154
- if (opt.compile) {
155
- await this.nakoCompile(opt, src, false);
156
- return;
157
- }
158
- // ASTを出力する
159
- if (opt.ast) {
160
- try {
161
- await this.loadDependencies(src, opt.mainfile, '');
162
- }
163
- catch (err) {
164
- if (this.numFailures > 0) {
165
- this.logger.error(err);
166
- process.exit(1);
167
- }
168
- }
169
- this.outputAST(opt, src);
170
- return;
171
- }
172
- // テストを実行する
173
- if (opt.test) {
174
- try {
175
- await this.loadDependencies(src, opt.mainfile, '');
176
- this.test(src, opt.mainfile);
177
- return;
178
- }
179
- catch (e) {
180
- if (this.numFailures > 0) {
181
- this.logger.error(e);
182
- process.exit(1);
183
- }
184
- }
185
- }
186
- // ファイルを読んで実行する
187
- try {
188
- // コンパイルと実行を行うメソッド
189
- const g = await this.runAsync(src, opt.mainfile);
190
- return g;
191
- }
192
- catch (e) {
193
- this.logger.error(e);
194
- // 文法エラーなどがあった場合
195
- if (opt.debug || opt.trace) {
196
- throw e;
197
- }
198
- }
199
- }
200
- /**
201
- * コンパイルモードの場合
202
- * @param opt
203
- * @param {string} src
204
- * @param {boolean} isTest
205
- */
206
- async nakoCompile(opt, src, isTest) {
207
- // 依存ライブラリなどを読み込む
208
- await this.loadDependencies(src, this.filename, '');
209
- // JSにコンパイル
210
- const genOpt = new NakoGenOptions(isTest, ['plugin_node.mjs'], 'self.__varslist[0][\'ナデシコ種類\']=\'cnako3\';');
211
- const jscode = this.compileStandalone(src, this.filename, genOpt);
212
- console.log(opt.output);
213
- fs.writeFileSync(opt.output, jscode, 'utf-8');
214
- // 実行に必要なファイルをコピー
215
- const nakoRuntime = __dirname;
216
- const outRuntime = path.join(path.dirname(opt.output), 'nako3runtime');
217
- if (!fs.existsSync(outRuntime)) {
218
- fs.mkdirSync(outRuntime);
219
- }
220
- // from ./src
221
- for (const mod of ['nako_version.mjs', 'plugin_node.mjs']) {
222
- fs.copyFileSync(path.join(nakoRuntime, mod), path.join(outRuntime, mod));
223
- }
224
- // from nadesiko3core/src
225
- const srcDir = path.join(__dirname, '..', 'node_modules', 'nadesiko3core', 'src');
226
- const baseFiles = ['nako_errors.mjs', 'nako_core_version.mjs',
227
- 'plugin_system.mjs', 'plugin_math.mjs', 'plugin_promise.mjs', 'plugin_test.mjs', 'plugin_csv.mjs', 'nako_csv.mjs'];
228
- for (const mod of baseFiles) {
229
- fs.copyFileSync(path.join(srcDir, mod), path.join(outRuntime, mod));
230
- }
231
- // or 以下のコピーだと依存ファイルがコピーされない package.jsonを見てコピーする必要がある
232
- const orgModule = path.join(__dirname, '..', 'node_modules');
233
- const dirNodeModules = path.join(path.dirname(opt.output), 'node_modules');
234
- const modlist = ['fs-extra', 'iconv-lite', 'opener', 'node-fetch'];
235
- const copied = {};
236
- // 再帰的に必要なモジュールをコピーする
237
- const copyModule = (mod) => {
238
- if (copied[mod]) {
239
- return;
240
- }
241
- copied[mod] = true;
242
- // ライブラリ自身をコピー
243
- fse.copySync(path.join(orgModule, mod), path.join(dirNodeModules, mod));
244
- // 依存ライブラリをコピー
245
- const packageFile = path.join(orgModule, mod, 'package.json');
246
- const jsonStr = fs.readFileSync(packageFile, 'utf-8');
247
- const jsonData = JSON.parse(jsonStr);
248
- // サブモジュールをコピー
249
- for (const smod in jsonData.dependencies) {
250
- copyModule(smod);
251
- }
252
- };
253
- for (const mod of modlist) {
254
- copyModule(mod);
255
- }
256
- if (opt.run) {
257
- exec(`node ${opt.output}`, function (err, stdout, stderr) {
258
- if (err) {
259
- console.log('[ERROR]', stderr);
260
- }
261
- console.log(stdout);
262
- });
263
- }
264
- }
265
- // ワンライナーの場合
266
- async cnakoOneLiner(opt) {
267
- const org = opt.source;
268
- try {
269
- if (opt.source.indexOf('表示') < 0) {
270
- opt.source = '' + opt.source + 'を表示。';
271
- }
272
- await this.runAsync(opt.source, 'main.nako3');
273
- }
274
- catch (e) {
275
- // エラーになったら元のワンライナーで再挑戦
276
- try {
277
- if (opt.source !== org) {
278
- await this.runAsync(org, 'main.nako3');
279
- }
280
- else {
281
- throw e;
282
- }
283
- }
284
- catch (e) {
285
- if (this.debug) {
286
- throw e;
287
- }
288
- else {
289
- console.error(e.message);
290
- }
291
- }
292
- }
293
- }
294
- /**
295
- * ASTを出力
296
- * @param opt
297
- * @param {string} src
298
- */
299
- outputAST(opt, src) {
300
- const ast = this.parse(src, opt.mainfile);
301
- const makeIndent = (level) => {
302
- let s = '';
303
- for (let i = 0; i < level; i++) {
304
- s += ' ';
305
- }
306
- return s;
307
- };
308
- const trim = (s) => { return s.replace(/(^\s+|\s+$)/g, ''); };
309
- /**
310
- * AST文字列に変換して返す
311
- * @param {*} ast
312
- * @param {number} level
313
- * @return {string}
314
- */
315
- const outAST = (ast, level) => {
316
- if (typeof (ast) === 'string') {
317
- return makeIndent(level) + '"' + ast + '"';
318
- }
319
- if (typeof (ast) === 'number') {
320
- return makeIndent(level) + ast;
321
- }
322
- if (ast instanceof Array) {
323
- const s = makeIndent(level) + '[\n';
324
- const sa = [];
325
- ast.forEach((a) => {
326
- sa.push(outAST(a, level + 1));
327
- });
328
- return s + sa.join(',\n') + '\n' + makeIndent(level) + ']';
329
- }
330
- if (ast instanceof Object) {
331
- const s = makeIndent(level) + '{\n';
332
- const sa = [];
333
- for (const key in ast) {
334
- const sv = trim(outAST(ast[key], level + 1));
335
- const so = makeIndent(level + 1) + '"' + key + '": ' + sv;
336
- sa.push(so);
337
- }
338
- return s + sa.join(',\n') + '\n' + makeIndent(level) + '}';
339
- }
340
- return makeIndent(level) + ast;
341
- };
342
- console.log(outAST(ast, 0));
343
- }
344
- // REPL(対話実行環境)の場合
345
- async cnakoRepl(_opt) {
346
- const fname = path.join(__dirname, 'repl.nako3');
347
- const src = fs.readFileSync(fname, 'utf-8');
348
- await this.runAsync(src, 'main.nako3');
349
- }
350
- // マニュアルを表示する
351
- cnakoMan(command) {
352
- try {
353
- const pathCommands = path.join(__dirname, '../release/command_cnako3.json');
354
- const commands = JSON.parse(fs.readFileSync(pathCommands, 'utf-8'));
355
- const data = commands[command];
356
- for (const key in data) {
357
- console.log(`${key}: ${data[key]}`);
358
- }
359
- }
360
- catch (e) {
361
- if (e.code === 'MODULE_NOT_FOUND') {
362
- console.log('コマンド一覧がないため、マニュアルを表示できません。以下のコマンドでコマンド一覧を生成してください。\n$ npm run build');
363
- }
364
- else {
365
- throw e;
366
- }
367
- }
368
- }
369
- // 対応機器/Webブラウザを表示する
370
- cnakoBrowsers() {
371
- const fileMD = path.resolve(__dirname, '../doc', 'browsers.md');
372
- console.log(fs.readFileSync(fileMD, 'utf-8'));
373
- }
374
- // (js|nako3) loader
375
- getLoaderTools() {
376
- /** @type {string[]} */
377
- const log = [];
378
- const tools = {
379
- resolvePath: (name, token, fromFile) => {
380
- // 最初に拡張子があるかどうかをチェック
381
- // JSプラグインか?
382
- if (/\.(js|mjs)(\.txt)?$/.test(name)) {
383
- const jspath = CNako3.findJSPluginFile(name, fromFile, __dirname, log);
384
- if (jspath === '') {
385
- throw new NakoImportError(`JSプラグイン『${name}』が見つかりません。以下のパスを検索しました。\n${log.join('\n')}`, token.file, token.line);
386
- }
387
- return { filePath: jspath, type: 'js' };
388
- }
389
- // なでしこプラグインか?
390
- if (/\.(nako3|nako)(\.txt)?$/.test(name)) {
391
- // ファイルかHTTPか
392
- if (name.startsWith('http://') || name.startsWith('https://')) {
393
- return { filePath: name, type: 'nako3' };
394
- }
395
- if (path.isAbsolute(name)) {
396
- return { filePath: path.resolve(name), type: 'nako3' };
397
- }
398
- else {
399
- // filename が undefined のとき token.file が undefined になる。
400
- if (token.file === undefined) {
401
- throw new Error('ファイル名を指定してください。');
402
- }
403
- const dir = path.dirname(fromFile);
404
- return { filePath: path.resolve(path.join(dir, name)), type: 'nako3' };
405
- }
406
- }
407
- // 拡張子がない、あるいは、(.js|.mjs|.nako3|.nako)以外はJSモジュールと見なす
408
- const jspath2 = CNako3.findJSPluginFile(name, fromFile, __dirname, log);
409
- if (jspath2 === '') {
410
- throw new NakoImportError(`JSプラグイン『${name}』が見つかりません。以下のパスを検索しました。\n${log.join('\n')}`, token.file, token.line);
411
- }
412
- return { filePath: jspath2, type: 'js' };
413
- },
414
- readNako3: (name, token) => {
415
- const loader = { task: null };
416
- // ファイルかHTTPか
417
- if (name.startsWith('http://') || name.startsWith('https://')) {
418
- // Webのファイルを非同期で読み込む
419
- loader.task = (async () => {
420
- const res = await fetch(name);
421
- if (!res.ok) {
422
- throw new NakoImportError(`『${name}』からのダウンロードに失敗しました: ${res.status} ${res.statusText}`, token.file, token.line);
423
- }
424
- return await res.text();
425
- })();
426
- }
427
- else {
428
- // ファイルを非同期で読み込む
429
- // ファイルチェックだけ先に実行
430
- if (!fs.existsSync(name)) {
431
- throw new NakoImportError(`ファイル ${name} が存在しません。`, token.file, token.line);
432
- }
433
- loader.task = (new Promise((resolve, reject) => {
434
- fs.readFile(name, { encoding: 'utf-8' }, (err, data) => {
435
- if (err) {
436
- return reject(err);
437
- }
438
- resolve(data);
439
- });
440
- }));
441
- }
442
- // 非同期で読み込む
443
- return loader;
444
- },
445
- readJs: (filePath, token) => {
446
- const loader = { task: null };
447
- if (process.platform === 'win32') {
448
- if (filePath.substring(1, 3) === ':\\') {
449
- filePath = 'file://' + filePath;
450
- }
451
- }
452
- // URLからの読み取り
453
- // ファイルかHTTPか
454
- if (filePath.startsWith('http://') || filePath.startsWith('https://')) {
455
- // 動的 import が http 未対応のため、一度、Webのファイルを非同期で読み込んで/tmpに保存してから動的importを行う
456
- loader.task = (new Promise((resolve, reject) => {
457
- // 一時フォルダを得る
458
- const osTmpDir = (process.platform === 'win32') ? process.env.TEMP : '/tmp';
459
- const osTmpDir2 = (osTmpDir) || path.join('./tmp');
460
- const tmpDir = path.join(osTmpDir2, 'com.nadesi.v3.cnako');
461
- const tmpFile = path.join(tmpDir, filePath.replace(/[^a-zA-Z0-9_.]/g, '_'));
462
- if (!fs.existsSync(tmpDir)) {
463
- fs.mkdirSync(tmpDir, { recursive: true });
464
- }
465
- // WEBからダウンロード
466
- fetch(filePath)
467
- .then((res) => {
468
- return res.text();
469
- })
470
- .then((txt) => {
471
- // 一時ファイルに保存
472
- try {
473
- fs.writeFileSync(tmpFile, txt, 'utf-8');
474
- }
475
- catch (err) {
476
- const err2 = new NakoImportError(`URL『${filePath}』からダウンロードしたJSファイルがキャッシュに書き込めません。${err}`, token.file, token.line);
477
- reject(err2);
478
- }
479
- })
480
- .then(() => {
481
- // 一時ファイルから読み込む
482
- import(tmpFile).then((mod) => {
483
- // プラグインは export default で宣言
484
- const obj = Object.assign({}, mod);
485
- resolve(() => {
486
- return obj.default;
487
- });
488
- }).catch((err) => {
489
- const err2 = new NakoImportError(`URL『${filePath}』からダウンロードしたはずのJSファイル読み込めません。${err}`, token.file, token.line);
490
- reject(err2);
491
- });
492
- })
493
- .catch((err) => {
494
- const err2 = new NakoImportError(`URL『${filePath}』からJSファイルが読み込めません。${err}`, token.file, token.line);
495
- reject(err2);
496
- });
497
- }));
498
- return loader;
499
- }
500
- loader.task = (new Promise((resolve, reject) => {
501
- import(filePath).then((mod) => {
502
- // プラグインは export default で宣言
503
- const obj = Object.assign({}, mod);
504
- resolve(() => { return obj.default; });
505
- }).catch((err) => {
506
- const err2 = new NakoImportError(`ファイル『${filePath}』が読み込めません。${err}`, token.file, token.line);
507
- reject(err2);
508
- });
509
- }));
510
- return loader;
511
- }
512
- };
513
- return tools;
514
- }
515
- /**
516
- * @param {string} code
517
- * @param {string} filename
518
- * @param {string} preCode
519
- * @returns {Promise<void>}
520
- */
521
- async loadDependencies(code, filename, preCode) {
522
- const tools = this.getLoaderTools();
523
- await super._loadDependencies(code, filename, preCode, tools);
524
- }
525
- /**
526
- * @param code
527
- * @param fname
528
- * @param [preCode]
529
- */
530
- async runAsync(code, fname, options = new CompilerOptions()) {
531
- // 取り込む文の処理
532
- try {
533
- await this.loadDependencies(code, fname, options.preCode);
534
- }
535
- catch (err) {
536
- // 読み込みエラーは報告のみして続けて実行してみる
537
- this.getLogger().error(err);
538
- }
539
- // 実行
540
- return super.runAsync(code, fname, options);
541
- }
542
- /**
543
- * プラグインファイルの検索を行う
544
- * @param {string} pname プラグインの名前
545
- * @param {string} filename 取り込み元ファイル名
546
- * @param {string} srcDir このファイルが存在するディレクトリ
547
- * @param {string[]} [log]
548
- * @return {string} フルパス、失敗した時は、''を返す
549
- */
550
- static findJSPluginFile(pname, filename, srcDir, log = []) {
551
- log.length = 0;
552
- const cachePath = {};
553
- /** キャッシュ付きでファイルがあるか検索 */
554
- const exists = (f) => {
555
- // 同じパスを何度も検索することがないように
556
- if (cachePath[f]) {
557
- return cachePath[f];
558
- }
559
- try {
560
- // ファイルがないと例外が出る
561
- const stat = fs.statSync(f);
562
- const b = !!(stat && stat.isFile());
563
- cachePath[f] = b;
564
- return b;
565
- }
566
- catch (err) {
567
- return false;
568
- }
569
- };
570
- /** 普通にファイルをチェック
571
- * @param {string} pathTest
572
- * @param {string} desc
573
- * @returns {boolean}
574
- */
575
- const fCheck = (pathTest, desc) => {
576
- // 素直に指定されたパスをチェック
577
- const bExists = exists(pathTest);
578
- log.push(`- (${desc}) ${pathTest}, ${bExists}`);
579
- return bExists;
580
- };
581
- /** 通常 + package.json のパスを調べる
582
- * @param {string} pathTest
583
- * @param {string} desc
584
- * @returns {string}
585
- */
586
- const fCheckEx = (pathTest, desc) => {
587
- // 直接JSファイルが指定された?
588
- if (/\.(js|mjs)$/.test(pathTest)) {
589
- if (fCheck(pathTest, desc)) {
590
- return pathTest;
591
- }
592
- }
593
- // 指定パスのpackage.jsonを調べる
594
- const json = path.join(pathTest, 'package.json');
595
- if (fCheck(json, desc + '/package.json')) {
596
- // package.jsonを見つけたので、メインファイルを調べて取り込む (CommonJSモジュール対策)
597
- const jsonText = fs.readFileSync(json, 'utf-8');
598
- const obj = JSON.parse(jsonText);
599
- if (!obj.main) {
600
- return '';
601
- }
602
- const mainFile = path.resolve(path.join(pathTest, obj.main));
603
- return mainFile;
604
- }
605
- return '';
606
- };
607
- // URL指定か?
608
- if (pname.substring(0, 8) === 'https://') {
609
- return pname;
610
- }
611
- // 各パスを検索していく
612
- const p1 = pname.substring(0, 1);
613
- // フルパス指定か?
614
- if (p1 === '/' || pname.substring(1, 3).toLowerCase() === ':\\' || pname.substring(0, 6) === 'file:/') {
615
- const fileFullpath = fCheckEx(pname, 'フルパス');
616
- if (fileFullpath) {
617
- return fileFullpath;
618
- }
619
- return ''; // フルパスの場合別のフォルダは調べない
620
- }
621
- // 相対パスか?
622
- if (p1 === '.' || pname.indexOf('/') >= 0) {
623
- // 相対パス指定なので、なでしこのプログラムからの相対指定を調べる
624
- const pathRelative = path.join(path.resolve(path.dirname(filename)), pname);
625
- const fileRelative = fCheckEx(pathRelative, '相対パス');
626
- if (fileRelative) {
627
- return fileRelative;
628
- }
629
- return ''; // 相対パスの場合も別のフォルダは調べない
630
- }
631
- // plugin_xxx.mjs のようにファイル名のみが指定された場合のみ、いくつかのパスを調べる
632
- // 母艦パス(元ファイルと同じフォルダ)か?
633
- const testScriptPath = path.join(path.resolve(path.dirname(filename)), pname);
634
- const fileScript = fCheckEx(testScriptPath, '母艦パス');
635
- if (fileScript) {
636
- return fileScript;
637
- }
638
- // ランタイムパス/src/<plugin>
639
- if (pname.match(/^plugin_[a-z0-9_]+\.mjs/)) {
640
- // cnako3mod.mjs は ランタイム/src に配置されていることが前提
641
- const pathRoot = path.resolve(__dirname, '..');
642
- const pathRuntimeSrc = path.join(pathRoot, 'src', pname);
643
- const fileRuntimeSrc = fCheckEx(pathRuntimeSrc, 'CNAKO3パス');
644
- if (fileRuntimeSrc) {
645
- return fileRuntimeSrc;
646
- }
647
- // ランタイム/core/src/<plugin>
648
- const pathCore = path.join(pathRoot, 'core', 'src', pname);
649
- const fileCore = fCheckEx(pathCore, 'CNAKO3パス');
650
- if (fileCore) {
651
- return fileCore;
652
- }
653
- }
654
- // 環境変数をチェック
655
- // 環境変数 NAKO_LIB か?
656
- if (process.env.NAKO_LIB) {
657
- const NAKO_LIB = path.join(path.resolve(process.env.NAKO_LIB), pname);
658
- const fileLib = fCheckEx(NAKO_LIB, 'NAKO_LIB');
659
- if (fileLib) {
660
- return fileLib;
661
- }
662
- }
663
- // ランタイムパス/node_modules/<plugin>
664
- const pathRuntime = path.join(path.dirname(path.resolve(__dirname)));
665
- const pathRuntimePname = path.join(pathRuntime, 'node_modules', pname);
666
- const fileRuntime = fCheckEx(pathRuntimePname, 'runtime');
667
- if (fileRuntime) {
668
- return fileRuntime;
669
- }
670
- // ランタイムと同じ配置 | ランタイムパス/../<plugin>
671
- const runtimeLib = path.join(pathRuntime, '..', pname);
672
- const fileLib = fCheckEx(runtimeLib, 'runtimeLib');
673
- if (fileLib) {
674
- return fileLib;
675
- }
676
- // nadesiko3core | ランタイムパス/node_modules/nadesiko3core/src/<plugin>
677
- const pathRuntimeSrc2 = path.join(pathRuntime, 'node_modules', 'nadesiko3core', 'src', pname); // cnako3mod.mjs は ランタイム/src に配置されていることが前提
678
- const fileRuntimeSrc2 = fCheckEx(pathRuntimeSrc2, 'nadesiko3core');
679
- if (fileRuntimeSrc2) {
680
- return fileRuntimeSrc2;
681
- }
682
- // 環境変数 NAKO_HOMEか?
683
- if (process.env.NAKO_HOME) {
684
- const NAKO_HOME = path.join(path.resolve(process.env.NAKO_HOME), 'node_modules', pname);
685
- const fileHome = fCheckEx(NAKO_HOME, 'NAKO_HOME');
686
- if (fileHome) {
687
- return fileHome;
688
- }
689
- // NAKO_HOME/src ?
690
- const pathNakoHomeSrc = path.join(NAKO_HOME, 'src', pname);
691
- const fileNakoHomeSrc = fCheckEx(pathNakoHomeSrc, 'NAKO_HOME/src');
692
- if (fileNakoHomeSrc) {
693
- return fileNakoHomeSrc;
694
- }
695
- }
696
- // 環境変数 NODE_PATH (global) 以下にあるか?
697
- if (process.env.NODE_PATH) {
698
- const pathNode = path.join(path.resolve(process.env.NODE_PATH), pname);
699
- const fileNode = fCheckEx(pathNode, 'NODE_PATH');
700
- if (fileNode) {
701
- return fileNode;
702
- }
703
- }
704
- // Nodeのパス検索には任せない(importで必ず失敗するので)
705
- return '';
706
- }
707
- }
1
+ /**
2
+ * コマンドライン版のなでしこ3をモジュールとして定義
3
+ * 実際には cnako3.mjs から読み込まれる
4
+ */
5
+ import fs from 'fs';
6
+ import fse from 'fs-extra';
7
+ import { exec } from 'child_process';
8
+ import path from 'path';
9
+ import { NakoCompiler } from '../core/src/nako3.mjs';
10
+ import { NakoImportError } from '../core/src/nako_errors.mjs';
11
+ import { CompilerOptions } from '../core/src/nako_types.mjs';
12
+ import nakoVersion from './nako_version.mjs';
13
+ import PluginNode from './plugin_node.mjs';
14
+ import app from './commander_ja.mjs';
15
+ import fetch from 'node-fetch';
16
+ import { NakoGenOptions } from '../core/src/nako_gen.mjs';
17
+ // __dirname のために
18
+ import url from 'url';
19
+ const __filename = url.fileURLToPath(import.meta.url);
20
+ const __dirname = path.dirname(__filename);
21
+ /** CNako3 */
22
+ export class CNako3 extends NakoCompiler {
23
+ constructor(opts = { nostd: false }) {
24
+ super({ useBasicPlugin: !opts.nostd });
25
+ this.debug = false;
26
+ this.filename = 'main.nako3';
27
+ this.version = nakoVersion.version;
28
+ if (!opts.nostd) {
29
+ this.addPluginFile('PluginNode', path.join(__dirname, 'plugin_node.mjs'), PluginNode);
30
+ }
31
+ // 必要な定数を設定
32
+ this.addListener('beforeRun', (g) => {
33
+ g.__varslist[0]['ナデシコ種類'] = 'cnako3';
34
+ g.__varslist[0]['ナデシコバージョン'] = this.version;
35
+ });
36
+ }
37
+ // CNAKO3で使えるコマンドを登録する
38
+ registerCommands() {
39
+ // コマンド引数がないならば、ヘルプを表示(-hはcommandarにデフォルト用意されている)
40
+ if (process.argv.length <= 2) {
41
+ process.argv.push('-h');
42
+ }
43
+ const verInfo = `v${nakoVersion.version}`;
44
+ // commanderを使って引数を解析する
45
+ app
46
+ .title('日本語プログラミング言語「なでしこ」' + verInfo)
47
+ .version(verInfo, '-v, --version')
48
+ .usage('[オプション] 入力ファイル.nako3')
49
+ .option('-h, --help', 'コマンドの使い方を表示')
50
+ .option('-w, --warn', '警告を表示する')
51
+ .option('-d, --debug', 'デバッグモードの指定')
52
+ .option('-D, --trace', '詳細デバッグモードの指定')
53
+ .option('-c, --compile', 'コンパイルモードの指定')
54
+ .option('-t, --test', 'コンパイルモードの指定 (テスト用コードを出力)')
55
+ .option('-r, --run', 'コンパイルモードでも実行する')
56
+ .option('-e, --eval [src]', '直接プログラムを実行するワンライナーモード')
57
+ .option('-o, --output', '出力ファイル名の指定')
58
+ .option('-s, --silent', 'サイレントモードの指定')
59
+ .option('-l, --repl', '対話シェル(REPL)の実行')
60
+ .option('-b, --browsers', '対応機器/Webブラウザを表示する')
61
+ .option('-m, --man [command]', 'マニュアルを表示する')
62
+ .option('-p, --speed', 'スピード優先モードの指定')
63
+ .option('-A, --ast', 'パースした結果をASTで出力する')
64
+ // .option('-h, --help', '使い方を表示する')
65
+ // .option('-v, --version', 'バージョンを表示する')
66
+ .parse(process.argv);
67
+ return app;
68
+ }
69
+ /** コマンドライン引数を解析 */
70
+ checkArguments() {
71
+ const app = this.registerCommands();
72
+ let logLevel = 'error';
73
+ if (app.trace) {
74
+ logLevel = 'trace';
75
+ }
76
+ else if (app.debug) {
77
+ logLevel = 'debug';
78
+ }
79
+ else if (app.warn) {
80
+ logLevel = 'warn';
81
+ }
82
+ this.getLogger().addListener(logLevel, ({ level, nodeConsole }) => {
83
+ console.log(nodeConsole);
84
+ });
85
+ const args = {
86
+ compile: app.compile || false,
87
+ run: app.run || false,
88
+ source: app.eval || '',
89
+ man: app.man || '',
90
+ one_liner: app.eval || false,
91
+ debug: this.debug || false,
92
+ trace: app.trace,
93
+ warn: app.warn,
94
+ repl: app.repl || false,
95
+ test: app.test || false,
96
+ browsers: app.browsers || false,
97
+ speed: app.speed || false,
98
+ ast: app.ast || false
99
+ };
100
+ args.mainfile = app.args[0];
101
+ args.output = app.output;
102
+ // todo: ESModule 対応の '.mjs' のコードを吐くように修正 #1217
103
+ const ext = '.mjs';
104
+ if (/\.(nako|nako3|txt|bak)$/.test(args.mainfile)) {
105
+ if (!args.output) {
106
+ if (args.test) {
107
+ args.output = args.mainfile.replace(/\.(nako|nako3)$/, '.spec' + ext);
108
+ }
109
+ else {
110
+ args.output = args.mainfile.replace(/\.(nako|nako3)$/, ext);
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ if (!args.output) {
116
+ if (args.test) {
117
+ args.output = args.mainfile + '.spec' + ext;
118
+ }
119
+ else {
120
+ args.output = args.mainfile + ext;
121
+ }
122
+ }
123
+ args.mainfile += '.nako3';
124
+ }
125
+ return args;
126
+ }
127
+ // 実行する
128
+ async execCommand() {
129
+ // コマンドを解析
130
+ const opt = this.checkArguments();
131
+ // 使い方の表示か?
132
+ if (opt.man) {
133
+ this.cnakoMan(opt.man);
134
+ return;
135
+ }
136
+ // 対応ブラウザを表示する
137
+ if (opt.browsers) {
138
+ this.cnakoBrowsers();
139
+ return;
140
+ }
141
+ // REPLを実行する
142
+ if (opt.repl) {
143
+ this.cnakoRepl(opt);
144
+ return;
145
+ }
146
+ // ワンライナーで実行する
147
+ if (opt.one_liner) {
148
+ this.cnakoOneLiner(opt);
149
+ return;
150
+ }
151
+ // メインプログラムを読み込む
152
+ this.filename = opt.mainfile;
153
+ const src = fs.readFileSync(opt.mainfile, 'utf-8');
154
+ if (opt.compile) {
155
+ await this.nakoCompile(opt, src, false);
156
+ return;
157
+ }
158
+ // ASTを出力する
159
+ if (opt.ast) {
160
+ try {
161
+ await this.loadDependencies(src, opt.mainfile, '');
162
+ }
163
+ catch (err) {
164
+ if (this.numFailures > 0) {
165
+ this.logger.error(err);
166
+ process.exit(1);
167
+ }
168
+ }
169
+ this.outputAST(opt, src);
170
+ return;
171
+ }
172
+ // テストを実行する
173
+ if (opt.test) {
174
+ try {
175
+ await this.loadDependencies(src, opt.mainfile, '');
176
+ this.test(src, opt.mainfile);
177
+ return;
178
+ }
179
+ catch (e) {
180
+ if (this.numFailures > 0) {
181
+ this.logger.error(e);
182
+ process.exit(1);
183
+ }
184
+ }
185
+ }
186
+ // ファイルを読んで実行する
187
+ try {
188
+ // コンパイルと実行を行うメソッド
189
+ const g = await this.runAsync(src, opt.mainfile);
190
+ return g;
191
+ }
192
+ catch (e) {
193
+ this.logger.error(e);
194
+ // 文法エラーなどがあった場合
195
+ if (opt.debug || opt.trace) {
196
+ throw e;
197
+ }
198
+ }
199
+ }
200
+ /**
201
+ * コンパイルモードの場合
202
+ * @param opt
203
+ * @param {string} src
204
+ * @param {boolean} isTest
205
+ */
206
+ async nakoCompile(opt, src, isTest) {
207
+ // 依存ライブラリなどを読み込む
208
+ await this.loadDependencies(src, this.filename, '');
209
+ // JSにコンパイル
210
+ const genOpt = new NakoGenOptions(isTest, ['plugin_node.mjs'], 'self.__varslist[0][\'ナデシコ種類\']=\'cnako3\';');
211
+ const jscode = this.compileStandalone(src, this.filename, genOpt);
212
+ console.log(opt.output);
213
+ fs.writeFileSync(opt.output, jscode, 'utf-8');
214
+ // 実行に必要なファイルをコピー
215
+ const nakoRuntime = __dirname;
216
+ const outRuntime = path.join(path.dirname(opt.output), 'nako3runtime');
217
+ if (!fs.existsSync(outRuntime)) {
218
+ fs.mkdirSync(outRuntime);
219
+ }
220
+ // from ./src
221
+ for (const mod of ['nako_version.mjs', 'plugin_node.mjs']) {
222
+ fs.copyFileSync(path.join(nakoRuntime, mod), path.join(outRuntime, mod));
223
+ }
224
+ // from nadesiko3core/src
225
+ const srcDir = path.join(__dirname, '..', 'node_modules', 'nadesiko3core', 'src');
226
+ const baseFiles = ['nako_errors.mjs', 'nako_core_version.mjs',
227
+ 'plugin_system.mjs', 'plugin_math.mjs', 'plugin_promise.mjs', 'plugin_test.mjs', 'plugin_csv.mjs', 'nako_csv.mjs'];
228
+ for (const mod of baseFiles) {
229
+ fs.copyFileSync(path.join(srcDir, mod), path.join(outRuntime, mod));
230
+ }
231
+ // or 以下のコピーだと依存ファイルがコピーされない package.jsonを見てコピーする必要がある
232
+ const orgModule = path.join(__dirname, '..', 'node_modules');
233
+ const dirNodeModules = path.join(path.dirname(opt.output), 'node_modules');
234
+ const modlist = ['fs-extra', 'iconv-lite', 'opener', 'node-fetch'];
235
+ const copied = {};
236
+ // 再帰的に必要なモジュールをコピーする
237
+ const copyModule = (mod) => {
238
+ if (copied[mod]) {
239
+ return;
240
+ }
241
+ copied[mod] = true;
242
+ // ライブラリ自身をコピー
243
+ fse.copySync(path.join(orgModule, mod), path.join(dirNodeModules, mod));
244
+ // 依存ライブラリをコピー
245
+ const packageFile = path.join(orgModule, mod, 'package.json');
246
+ const jsonStr = fs.readFileSync(packageFile, 'utf-8');
247
+ const jsonData = JSON.parse(jsonStr);
248
+ // サブモジュールをコピー
249
+ for (const smod in jsonData.dependencies) {
250
+ copyModule(smod);
251
+ }
252
+ };
253
+ for (const mod of modlist) {
254
+ copyModule(mod);
255
+ }
256
+ if (opt.run) {
257
+ exec(`node ${opt.output}`, function (err, stdout, stderr) {
258
+ if (err) {
259
+ console.log('[ERROR]', stderr);
260
+ }
261
+ console.log(stdout);
262
+ });
263
+ }
264
+ }
265
+ // ワンライナーの場合
266
+ async cnakoOneLiner(opt) {
267
+ const org = opt.source;
268
+ try {
269
+ if (opt.source.indexOf('表示') < 0) {
270
+ opt.source = '' + opt.source + 'を表示。';
271
+ }
272
+ await this.runAsync(opt.source, 'main.nako3');
273
+ }
274
+ catch (e) {
275
+ // エラーになったら元のワンライナーで再挑戦
276
+ try {
277
+ if (opt.source !== org) {
278
+ await this.runAsync(org, 'main.nako3');
279
+ }
280
+ else {
281
+ throw e;
282
+ }
283
+ }
284
+ catch (e) {
285
+ if (this.debug) {
286
+ throw e;
287
+ }
288
+ else {
289
+ console.error(e.message);
290
+ }
291
+ }
292
+ }
293
+ }
294
+ /**
295
+ * ASTを出力
296
+ * @param opt
297
+ * @param {string} src
298
+ */
299
+ outputAST(opt, src) {
300
+ const ast = this.parse(src, opt.mainfile);
301
+ const makeIndent = (level) => {
302
+ let s = '';
303
+ for (let i = 0; i < level; i++) {
304
+ s += ' ';
305
+ }
306
+ return s;
307
+ };
308
+ const trim = (s) => { return s.replace(/(^\s+|\s+$)/g, ''); };
309
+ /**
310
+ * AST文字列に変換して返す
311
+ * @param {*} ast
312
+ * @param {number} level
313
+ * @return {string}
314
+ */
315
+ const outAST = (ast, level) => {
316
+ if (typeof (ast) === 'string') {
317
+ return makeIndent(level) + '"' + ast + '"';
318
+ }
319
+ if (typeof (ast) === 'number') {
320
+ return makeIndent(level) + ast;
321
+ }
322
+ if (ast instanceof Array) {
323
+ const s = makeIndent(level) + '[\n';
324
+ const sa = [];
325
+ ast.forEach((a) => {
326
+ sa.push(outAST(a, level + 1));
327
+ });
328
+ return s + sa.join(',\n') + '\n' + makeIndent(level) + ']';
329
+ }
330
+ if (ast instanceof Object) {
331
+ const s = makeIndent(level) + '{\n';
332
+ const sa = [];
333
+ for (const key in ast) {
334
+ const sv = trim(outAST(ast[key], level + 1));
335
+ const so = makeIndent(level + 1) + '"' + key + '": ' + sv;
336
+ sa.push(so);
337
+ }
338
+ return s + sa.join(',\n') + '\n' + makeIndent(level) + '}';
339
+ }
340
+ return makeIndent(level) + ast;
341
+ };
342
+ console.log(outAST(ast, 0));
343
+ }
344
+ // REPL(対話実行環境)の場合
345
+ async cnakoRepl(_opt) {
346
+ const fname = path.join(__dirname, 'repl.nako3');
347
+ const src = fs.readFileSync(fname, 'utf-8');
348
+ await this.runAsync(src, 'main.nako3');
349
+ }
350
+ // マニュアルを表示する
351
+ cnakoMan(command) {
352
+ try {
353
+ const pathCommands = path.join(__dirname, '../release/command_cnako3.json');
354
+ const commands = JSON.parse(fs.readFileSync(pathCommands, 'utf-8'));
355
+ const data = commands[command];
356
+ for (const key in data) {
357
+ console.log(`${key}: ${data[key]}`);
358
+ }
359
+ }
360
+ catch (e) {
361
+ if (e.code === 'MODULE_NOT_FOUND') {
362
+ console.log('コマンド一覧がないため、マニュアルを表示できません。以下のコマンドでコマンド一覧を生成してください。\n$ npm run build');
363
+ }
364
+ else {
365
+ throw e;
366
+ }
367
+ }
368
+ }
369
+ // 対応機器/Webブラウザを表示する
370
+ cnakoBrowsers() {
371
+ const fileMD = path.resolve(__dirname, '../doc', 'browsers.md');
372
+ console.log(fs.readFileSync(fileMD, 'utf-8'));
373
+ }
374
+ // (js|nako3) loader
375
+ getLoaderTools() {
376
+ /** @type {string[]} */
377
+ const log = [];
378
+ const tools = {
379
+ resolvePath: (name, token, fromFile) => {
380
+ // 最初に拡張子があるかどうかをチェック
381
+ // JSプラグインか?
382
+ if (/\.(js|mjs)(\.txt)?$/.test(name)) {
383
+ const jspath = CNako3.findJSPluginFile(name, fromFile, __dirname, log);
384
+ if (jspath === '') {
385
+ throw new NakoImportError(`JSプラグイン『${name}』が見つかりません。以下のパスを検索しました。\n${log.join('\n')}`, token.file, token.line);
386
+ }
387
+ return { filePath: jspath, type: 'js' };
388
+ }
389
+ // なでしこプラグインか?
390
+ if (/\.(nako3|nako)(\.txt)?$/.test(name)) {
391
+ // ファイルかHTTPか
392
+ if (name.startsWith('http://') || name.startsWith('https://')) {
393
+ return { filePath: name, type: 'nako3' };
394
+ }
395
+ if (path.isAbsolute(name)) {
396
+ return { filePath: path.resolve(name), type: 'nako3' };
397
+ }
398
+ else {
399
+ // filename が undefined のとき token.file が undefined になる。
400
+ if (token.file === undefined) {
401
+ throw new Error('ファイル名を指定してください。');
402
+ }
403
+ const dir = path.dirname(fromFile);
404
+ return { filePath: path.resolve(path.join(dir, name)), type: 'nako3' };
405
+ }
406
+ }
407
+ // 拡張子がない、あるいは、(.js|.mjs|.nako3|.nako)以外はJSモジュールと見なす
408
+ const jspath2 = CNako3.findJSPluginFile(name, fromFile, __dirname, log);
409
+ if (jspath2 === '') {
410
+ throw new NakoImportError(`JSプラグイン『${name}』が見つかりません。以下のパスを検索しました。\n${log.join('\n')}`, token.file, token.line);
411
+ }
412
+ return { filePath: jspath2, type: 'js' };
413
+ },
414
+ readNako3: (name, token) => {
415
+ const loader = { task: null };
416
+ // ファイルかHTTPか
417
+ if (name.startsWith('http://') || name.startsWith('https://')) {
418
+ // Webのファイルを非同期で読み込む
419
+ loader.task = (async () => {
420
+ const res = await fetch(name);
421
+ if (!res.ok) {
422
+ throw new NakoImportError(`『${name}』からのダウンロードに失敗しました: ${res.status} ${res.statusText}`, token.file, token.line);
423
+ }
424
+ return await res.text();
425
+ })();
426
+ }
427
+ else {
428
+ // ファイルを非同期で読み込む
429
+ // ファイルチェックだけ先に実行
430
+ if (!fs.existsSync(name)) {
431
+ throw new NakoImportError(`ファイル ${name} が存在しません。`, token.file, token.line);
432
+ }
433
+ loader.task = (new Promise((resolve, reject) => {
434
+ fs.readFile(name, { encoding: 'utf-8' }, (err, data) => {
435
+ if (err) {
436
+ return reject(err);
437
+ }
438
+ resolve(data);
439
+ });
440
+ }));
441
+ }
442
+ // 非同期で読み込む
443
+ return loader;
444
+ },
445
+ readJs: (filePath, token) => {
446
+ const loader = { task: null };
447
+ if (process.platform === 'win32') {
448
+ if (filePath.substring(1, 3) === ':\\') {
449
+ filePath = 'file://' + filePath;
450
+ }
451
+ }
452
+ // URLからの読み取り
453
+ // ファイルかHTTPか
454
+ if (filePath.startsWith('http://') || filePath.startsWith('https://')) {
455
+ // 動的 import が http 未対応のため、一度、Webのファイルを非同期で読み込んで/tmpに保存してから動的importを行う
456
+ loader.task = (new Promise((resolve, reject) => {
457
+ // 一時フォルダを得る
458
+ const osTmpDir = (process.platform === 'win32') ? process.env.TEMP : '/tmp';
459
+ const osTmpDir2 = (osTmpDir) || path.join('./tmp');
460
+ const tmpDir = path.join(osTmpDir2, 'com.nadesi.v3.cnako');
461
+ const tmpFile = path.join(tmpDir, filePath.replace(/[^a-zA-Z0-9_.]/g, '_'));
462
+ if (!fs.existsSync(tmpDir)) {
463
+ fs.mkdirSync(tmpDir, { recursive: true });
464
+ }
465
+ // WEBからダウンロード
466
+ fetch(filePath)
467
+ .then((res) => {
468
+ return res.text();
469
+ })
470
+ .then((txt) => {
471
+ // 一時ファイルに保存
472
+ try {
473
+ fs.writeFileSync(tmpFile, txt, 'utf-8');
474
+ }
475
+ catch (err) {
476
+ const err2 = new NakoImportError(`URL『${filePath}』からダウンロードしたJSファイルがキャッシュに書き込めません。${err}`, token.file, token.line);
477
+ reject(err2);
478
+ }
479
+ })
480
+ .then(() => {
481
+ // 一時ファイルから読み込む
482
+ import(tmpFile).then((mod) => {
483
+ // プラグインは export default で宣言
484
+ const obj = Object.assign({}, mod);
485
+ resolve(() => {
486
+ return obj.default;
487
+ });
488
+ }).catch((err) => {
489
+ const err2 = new NakoImportError(`URL『${filePath}』からダウンロードしたはずのJSファイル読み込めません。${err}`, token.file, token.line);
490
+ reject(err2);
491
+ });
492
+ })
493
+ .catch((err) => {
494
+ const err2 = new NakoImportError(`URL『${filePath}』からJSファイルが読み込めません。${err}`, token.file, token.line);
495
+ reject(err2);
496
+ });
497
+ }));
498
+ return loader;
499
+ }
500
+ loader.task = (new Promise((resolve, reject) => {
501
+ import(filePath).then((mod) => {
502
+ // プラグインは export default で宣言
503
+ const obj = Object.assign({}, mod);
504
+ resolve(() => { return obj.default; });
505
+ }).catch((err) => {
506
+ const err2 = new NakoImportError(`ファイル『${filePath}』が読み込めません。${err}`, token.file, token.line);
507
+ reject(err2);
508
+ });
509
+ }));
510
+ return loader;
511
+ }
512
+ };
513
+ return tools;
514
+ }
515
+ /**
516
+ * @param {string} code
517
+ * @param {string} filename
518
+ * @param {string} preCode
519
+ * @returns {Promise<void>}
520
+ */
521
+ async loadDependencies(code, filename, preCode) {
522
+ const tools = this.getLoaderTools();
523
+ await super._loadDependencies(code, filename, preCode, tools);
524
+ }
525
+ /**
526
+ * @param code
527
+ * @param fname
528
+ * @param [preCode]
529
+ */
530
+ async runAsync(code, fname, options = new CompilerOptions()) {
531
+ // 取り込む文の処理
532
+ try {
533
+ await this.loadDependencies(code, fname, options.preCode);
534
+ }
535
+ catch (err) {
536
+ // 読み込みエラーは報告のみして続けて実行してみる
537
+ this.getLogger().error(err);
538
+ }
539
+ // 実行
540
+ return super.runAsync(code, fname, options);
541
+ }
542
+ /**
543
+ * プラグインファイルの検索を行う
544
+ * @param {string} pname プラグインの名前
545
+ * @param {string} filename 取り込み元ファイル名
546
+ * @param {string} srcDir このファイルが存在するディレクトリ
547
+ * @param {string[]} [log]
548
+ * @return {string} フルパス、失敗した時は、''を返す
549
+ */
550
+ static findJSPluginFile(pname, filename, srcDir, log = []) {
551
+ log.length = 0;
552
+ const cachePath = {};
553
+ /** キャッシュ付きでファイルがあるか検索 */
554
+ const exists = (f) => {
555
+ // 同じパスを何度も検索することがないように
556
+ if (cachePath[f]) {
557
+ return cachePath[f];
558
+ }
559
+ try {
560
+ // ファイルがないと例外が出る
561
+ const stat = fs.statSync(f);
562
+ const b = !!(stat && stat.isFile());
563
+ cachePath[f] = b;
564
+ return b;
565
+ }
566
+ catch (err) {
567
+ return false;
568
+ }
569
+ };
570
+ /** 普通にファイルをチェック
571
+ * @param {string} pathTest
572
+ * @param {string} desc
573
+ * @returns {boolean}
574
+ */
575
+ const fCheck = (pathTest, desc) => {
576
+ // 素直に指定されたパスをチェック
577
+ const bExists = exists(pathTest);
578
+ log.push(`- (${desc}) ${pathTest}, ${bExists}`);
579
+ return bExists;
580
+ };
581
+ /** 通常 + package.json のパスを調べる
582
+ * @param {string} pathTest
583
+ * @param {string} desc
584
+ * @returns {string}
585
+ */
586
+ const fCheckEx = (pathTest, desc) => {
587
+ // 直接JSファイルが指定された?
588
+ if (/\.(js|mjs)$/.test(pathTest)) {
589
+ if (fCheck(pathTest, desc)) {
590
+ return pathTest;
591
+ }
592
+ }
593
+ // 指定パスのpackage.jsonを調べる
594
+ const json = path.join(pathTest, 'package.json');
595
+ if (fCheck(json, desc + '/package.json')) {
596
+ // package.jsonを見つけたので、メインファイルを調べて取り込む (CommonJSモジュール対策)
597
+ const jsonText = fs.readFileSync(json, 'utf-8');
598
+ const obj = JSON.parse(jsonText);
599
+ if (!obj.main) {
600
+ return '';
601
+ }
602
+ const mainFile = path.resolve(path.join(pathTest, obj.main));
603
+ return mainFile;
604
+ }
605
+ return '';
606
+ };
607
+ // URL指定か?
608
+ if (pname.substring(0, 8) === 'https://') {
609
+ return pname;
610
+ }
611
+ // 各パスを検索していく
612
+ const p1 = pname.substring(0, 1);
613
+ // フルパス指定か?
614
+ if (p1 === '/' || pname.substring(1, 3).toLowerCase() === ':\\' || pname.substring(0, 6) === 'file:/') {
615
+ const fileFullpath = fCheckEx(pname, 'フルパス');
616
+ if (fileFullpath) {
617
+ return fileFullpath;
618
+ }
619
+ return ''; // フルパスの場合別のフォルダは調べない
620
+ }
621
+ // 相対パスか?
622
+ if (p1 === '.' || pname.indexOf('/') >= 0) {
623
+ // 相対パス指定なので、なでしこのプログラムからの相対指定を調べる
624
+ const pathRelative = path.join(path.resolve(path.dirname(filename)), pname);
625
+ const fileRelative = fCheckEx(pathRelative, '相対パス');
626
+ if (fileRelative) {
627
+ return fileRelative;
628
+ }
629
+ return ''; // 相対パスの場合も別のフォルダは調べない
630
+ }
631
+ // plugin_xxx.mjs のようにファイル名のみが指定された場合のみ、いくつかのパスを調べる
632
+ // 母艦パス(元ファイルと同じフォルダ)か?
633
+ const testScriptPath = path.join(path.resolve(path.dirname(filename)), pname);
634
+ const fileScript = fCheckEx(testScriptPath, '母艦パス');
635
+ if (fileScript) {
636
+ return fileScript;
637
+ }
638
+ // ランタイムパス/src/<plugin>
639
+ if (pname.match(/^plugin_[a-z0-9_]+\.mjs/)) {
640
+ // cnako3mod.mjs は ランタイム/src に配置されていることが前提
641
+ const pathRoot = path.resolve(__dirname, '..');
642
+ const pathRuntimeSrc = path.join(pathRoot, 'src', pname);
643
+ const fileRuntimeSrc = fCheckEx(pathRuntimeSrc, 'CNAKO3パス');
644
+ if (fileRuntimeSrc) {
645
+ return fileRuntimeSrc;
646
+ }
647
+ // ランタイム/core/src/<plugin>
648
+ const pathCore = path.join(pathRoot, 'core', 'src', pname);
649
+ const fileCore = fCheckEx(pathCore, 'CNAKO3パス');
650
+ if (fileCore) {
651
+ return fileCore;
652
+ }
653
+ }
654
+ // 環境変数をチェック
655
+ // 環境変数 NAKO_LIB か?
656
+ if (process.env.NAKO_LIB) {
657
+ const NAKO_LIB = path.join(path.resolve(process.env.NAKO_LIB), pname);
658
+ const fileLib = fCheckEx(NAKO_LIB, 'NAKO_LIB');
659
+ if (fileLib) {
660
+ return fileLib;
661
+ }
662
+ }
663
+ // ランタイムパス/node_modules/<plugin>
664
+ const pathRuntime = path.join(path.dirname(path.resolve(__dirname)));
665
+ const pathRuntimePname = path.join(pathRuntime, 'node_modules', pname);
666
+ const fileRuntime = fCheckEx(pathRuntimePname, 'runtime');
667
+ if (fileRuntime) {
668
+ return fileRuntime;
669
+ }
670
+ // ランタイムと同じ配置 | ランタイムパス/../<plugin>
671
+ const runtimeLib = path.join(pathRuntime, '..', pname);
672
+ const fileLib = fCheckEx(runtimeLib, 'runtimeLib');
673
+ if (fileLib) {
674
+ return fileLib;
675
+ }
676
+ // nadesiko3core | ランタイムパス/node_modules/nadesiko3core/src/<plugin>
677
+ const pathRuntimeSrc2 = path.join(pathRuntime, 'node_modules', 'nadesiko3core', 'src', pname); // cnako3mod.mjs は ランタイム/src に配置されていることが前提
678
+ const fileRuntimeSrc2 = fCheckEx(pathRuntimeSrc2, 'nadesiko3core');
679
+ if (fileRuntimeSrc2) {
680
+ return fileRuntimeSrc2;
681
+ }
682
+ // 環境変数 NAKO_HOMEか?
683
+ if (process.env.NAKO_HOME) {
684
+ const NAKO_HOME = path.join(path.resolve(process.env.NAKO_HOME), 'node_modules', pname);
685
+ const fileHome = fCheckEx(NAKO_HOME, 'NAKO_HOME');
686
+ if (fileHome) {
687
+ return fileHome;
688
+ }
689
+ // NAKO_HOME/src ?
690
+ const pathNakoHomeSrc = path.join(NAKO_HOME, 'src', pname);
691
+ const fileNakoHomeSrc = fCheckEx(pathNakoHomeSrc, 'NAKO_HOME/src');
692
+ if (fileNakoHomeSrc) {
693
+ return fileNakoHomeSrc;
694
+ }
695
+ }
696
+ // 環境変数 NODE_PATH (global) 以下にあるか?
697
+ if (process.env.NODE_PATH) {
698
+ const pathNode = path.join(path.resolve(process.env.NODE_PATH), pname);
699
+ const fileNode = fCheckEx(pathNode, 'NODE_PATH');
700
+ if (fileNode) {
701
+ return fileNode;
702
+ }
703
+ }
704
+ // Nodeのパス検索には任せない(importで必ず失敗するので)
705
+ return '';
706
+ }
707
+ }