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