nadesiko3 3.3.49 → 3.3.52

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