nadesiko3 3.3.45 → 3.3.49

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