piral-cli 1.0.0-pre.2296 → 1.0.1-beta.5640

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 (408) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +30 -7
  3. package/lib/api.js +23 -23
  4. package/lib/api.js.map +1 -1
  5. package/lib/apps/add-piral-instance-pilet.d.ts +26 -0
  6. package/lib/apps/add-piral-instance-pilet.js +53 -0
  7. package/lib/apps/add-piral-instance-pilet.js.map +1 -0
  8. package/lib/apps/build-pilet.d.ts +33 -2
  9. package/lib/apps/build-pilet.js +159 -32
  10. package/lib/apps/build-pilet.js.map +1 -1
  11. package/lib/apps/build-piral.d.ts +18 -1
  12. package/lib/apps/build-piral.js +68 -52
  13. package/lib/apps/build-piral.js.map +1 -1
  14. package/lib/apps/debug-pilet.d.ts +38 -2
  15. package/lib/apps/debug-pilet.js +150 -108
  16. package/lib/apps/debug-pilet.js.map +1 -1
  17. package/lib/apps/debug-piral.d.ts +23 -0
  18. package/lib/apps/debug-piral.js +63 -52
  19. package/lib/apps/debug-piral.js.map +1 -1
  20. package/lib/apps/declaration-piral.js +5 -3
  21. package/lib/apps/declaration-piral.js.map +1 -1
  22. package/lib/apps/index.d.ts +3 -0
  23. package/lib/apps/index.js +8 -1
  24. package/lib/apps/index.js.map +1 -1
  25. package/lib/apps/new-pilet.d.ts +7 -3
  26. package/lib/apps/new-pilet.js +47 -52
  27. package/lib/apps/new-pilet.js.map +1 -1
  28. package/lib/apps/new-piral.d.ts +7 -3
  29. package/lib/apps/new-piral.js +33 -23
  30. package/lib/apps/new-piral.js.map +1 -1
  31. package/lib/apps/pack-pilet.d.ts +9 -0
  32. package/lib/apps/pack-pilet.js +6 -13
  33. package/lib/apps/pack-pilet.js.map +1 -1
  34. package/lib/apps/publish-pilet.d.ts +23 -3
  35. package/lib/apps/publish-pilet.js +112 -62
  36. package/lib/apps/publish-pilet.js.map +1 -1
  37. package/lib/apps/publish-piral.d.ts +6 -2
  38. package/lib/apps/publish-piral.js +30 -26
  39. package/lib/apps/publish-piral.js.map +1 -1
  40. package/lib/apps/remove-piral-instance-pilet.d.ts +22 -0
  41. package/lib/apps/remove-piral-instance-pilet.js +52 -0
  42. package/lib/apps/remove-piral-instance-pilet.js.map +1 -0
  43. package/lib/apps/run-emulator-piral.d.ts +34 -0
  44. package/lib/apps/run-emulator-piral.js +91 -0
  45. package/lib/apps/run-emulator-piral.js.map +1 -0
  46. package/lib/apps/upgrade-pilet.d.ts +6 -2
  47. package/lib/apps/upgrade-pilet.js +41 -43
  48. package/lib/apps/upgrade-pilet.js.map +1 -1
  49. package/lib/apps/upgrade-piral.d.ts +2 -2
  50. package/lib/apps/upgrade-piral.js +21 -20
  51. package/lib/apps/upgrade-piral.js.map +1 -1
  52. package/lib/apps/validate-pilet.js +30 -36
  53. package/lib/apps/validate-pilet.js.map +1 -1
  54. package/lib/apps/validate-piral.js +13 -10
  55. package/lib/apps/validate-piral.js.map +1 -1
  56. package/lib/build/bundler-calls.d.ts +3 -0
  57. package/lib/build/bundler-calls.js +117 -0
  58. package/lib/build/bundler-calls.js.map +1 -0
  59. package/lib/build/run-build-pilet.d.ts +1 -0
  60. package/lib/build/run-build-pilet.js +65 -0
  61. package/lib/build/run-build-pilet.js.map +1 -0
  62. package/lib/build/run-build-piral.d.ts +1 -0
  63. package/lib/build/run-build-piral.js +67 -0
  64. package/lib/build/run-build-piral.js.map +1 -0
  65. package/lib/build/run-debug-mono-piral.d.ts +1 -0
  66. package/lib/build/run-debug-mono-piral.js +70 -0
  67. package/lib/build/run-debug-mono-piral.js.map +1 -0
  68. package/lib/build/run-debug-pilet.d.ts +1 -0
  69. package/lib/build/run-debug-pilet.js +89 -0
  70. package/lib/build/run-debug-pilet.js.map +1 -0
  71. package/lib/build/run-debug-piral.d.ts +1 -0
  72. package/lib/build/run-debug-piral.js +85 -0
  73. package/lib/build/run-debug-piral.js.map +1 -0
  74. package/lib/bundler.js +75 -26
  75. package/lib/bundler.js.map +1 -1
  76. package/lib/cli.js +7 -4
  77. package/lib/cli.js.map +1 -1
  78. package/lib/commands.js +266 -46
  79. package/lib/commands.js.map +1 -1
  80. package/lib/common/archive.d.ts +4 -0
  81. package/lib/common/archive.js +31 -10
  82. package/lib/common/archive.js.map +1 -1
  83. package/lib/common/browser.d.ts +2 -1
  84. package/lib/common/browser.js +17 -8
  85. package/lib/common/browser.js.map +1 -1
  86. package/lib/common/clients/index.d.ts +21 -0
  87. package/lib/common/clients/index.js +48 -0
  88. package/lib/common/clients/index.js.map +1 -0
  89. package/lib/common/clients/lerna.d.ts +7 -1
  90. package/lib/common/clients/lerna.js +93 -7
  91. package/lib/common/clients/lerna.js.map +1 -1
  92. package/lib/common/clients/npm.d.ts +6 -1
  93. package/lib/common/clients/npm.js +66 -18
  94. package/lib/common/clients/npm.js.map +1 -1
  95. package/lib/common/clients/pnp.d.ts +7 -0
  96. package/lib/common/clients/pnp.js +104 -0
  97. package/lib/common/clients/pnp.js.map +1 -0
  98. package/lib/common/clients/pnpm.d.ts +5 -0
  99. package/lib/common/clients/pnpm.js +55 -6
  100. package/lib/common/clients/pnpm.js.map +1 -1
  101. package/lib/common/clients/rush.d.ts +7 -0
  102. package/lib/common/clients/rush.js +127 -0
  103. package/lib/common/clients/rush.js.map +1 -0
  104. package/lib/common/clients/yarn.d.ts +5 -0
  105. package/lib/common/clients/yarn.js +57 -6
  106. package/lib/common/clients/yarn.js.map +1 -1
  107. package/lib/common/compatibility.js +14 -9
  108. package/lib/common/compatibility.js.map +1 -1
  109. package/lib/common/config.d.ts +52 -1
  110. package/lib/common/config.js +10 -30
  111. package/lib/common/config.js.map +1 -1
  112. package/lib/common/constants.d.ts +10 -2
  113. package/lib/common/constants.js +22 -4
  114. package/lib/common/constants.js.map +1 -1
  115. package/lib/common/declaration.d.ts +2 -1
  116. package/lib/common/declaration.js +113 -56
  117. package/lib/common/declaration.js.map +1 -1
  118. package/lib/common/emoji.d.ts +8 -0
  119. package/lib/common/emoji.js +10 -1
  120. package/lib/common/emoji.js.map +1 -1
  121. package/lib/common/emulator.d.ts +2 -2
  122. package/lib/common/emulator.js +45 -32
  123. package/lib/common/emulator.js.map +1 -1
  124. package/lib/common/enums.d.ts +0 -4
  125. package/lib/common/enums.js +1 -6
  126. package/lib/common/enums.js.map +1 -1
  127. package/lib/common/envs.js +16 -5
  128. package/lib/common/envs.js.map +1 -1
  129. package/lib/common/hash.d.ts +1 -0
  130. package/lib/common/hash.js +8 -3
  131. package/lib/common/hash.js.map +1 -1
  132. package/lib/common/http.d.ts +5 -2
  133. package/lib/common/http.js +65 -23
  134. package/lib/common/http.js.map +1 -1
  135. package/lib/common/importmap.d.ts +2 -0
  136. package/lib/common/importmap.js +202 -0
  137. package/lib/common/importmap.js.map +1 -0
  138. package/lib/common/index.d.ts +10 -0
  139. package/lib/common/index.js +15 -1
  140. package/lib/common/index.js.map +1 -1
  141. package/lib/common/info.d.ts +5 -0
  142. package/lib/common/info.js +9 -2
  143. package/lib/common/info.js.map +1 -1
  144. package/lib/common/injectors.d.ts +36 -8
  145. package/lib/common/injectors.js +46 -19
  146. package/lib/common/injectors.js.map +1 -1
  147. package/lib/common/inspect.d.ts +5 -3
  148. package/lib/common/inspect.js +15 -15
  149. package/lib/common/inspect.js.map +1 -1
  150. package/lib/common/interactive.d.ts +9 -0
  151. package/lib/common/interactive.js +60 -1
  152. package/lib/common/interactive.js.map +1 -1
  153. package/lib/common/io.d.ts +9 -3
  154. package/lib/common/io.js +219 -116
  155. package/lib/common/io.js.map +1 -1
  156. package/lib/common/language.d.ts +9 -6
  157. package/lib/common/language.js +68 -12
  158. package/lib/common/language.js.map +1 -1
  159. package/lib/common/log.d.ts +5 -3
  160. package/lib/common/log.js +34 -48
  161. package/lib/common/log.js.map +1 -1
  162. package/lib/common/npm.d.ts +17 -16
  163. package/lib/common/npm.js +262 -158
  164. package/lib/common/npm.js.map +1 -1
  165. package/lib/common/pack.js +47 -26
  166. package/lib/common/pack.js.map +1 -1
  167. package/lib/common/package.d.ts +43 -20
  168. package/lib/common/package.js +377 -213
  169. package/lib/common/package.js.map +1 -1
  170. package/lib/common/parallel.d.ts +1 -0
  171. package/lib/common/parallel.js +29 -0
  172. package/lib/common/parallel.js.map +1 -0
  173. package/lib/common/patcher.js +20 -20
  174. package/lib/common/patcher.js.map +1 -1
  175. package/lib/common/platform.d.ts +13 -0
  176. package/lib/common/platform.js +29 -0
  177. package/lib/common/platform.js.map +1 -0
  178. package/lib/common/port.d.ts +1 -0
  179. package/lib/common/port.js +14 -4
  180. package/lib/common/port.js.map +1 -1
  181. package/lib/common/rules.js +4 -4
  182. package/lib/common/rules.js.map +1 -1
  183. package/lib/common/scaffold.d.ts +18 -4
  184. package/lib/common/scaffold.js +59 -36
  185. package/lib/common/scaffold.js.map +1 -1
  186. package/lib/common/scripts.js +8 -8
  187. package/lib/common/scripts.js.map +1 -1
  188. package/lib/common/shell.d.ts +2 -0
  189. package/lib/common/shell.js +33 -0
  190. package/lib/common/shell.js.map +1 -0
  191. package/lib/common/spec.d.ts +29 -0
  192. package/lib/common/spec.js +69 -0
  193. package/lib/common/spec.js.map +1 -0
  194. package/lib/common/template.d.ts +1 -2
  195. package/lib/common/template.js +23 -22
  196. package/lib/common/template.js.map +1 -1
  197. package/lib/common/url.d.ts +1 -0
  198. package/lib/common/url.js +17 -0
  199. package/lib/common/url.js.map +1 -0
  200. package/lib/common/utils.d.ts +1 -0
  201. package/lib/common/utils.js +8 -0
  202. package/lib/common/utils.js.map +1 -0
  203. package/lib/common/version.d.ts +2 -0
  204. package/lib/common/version.js +130 -0
  205. package/lib/common/version.js.map +1 -0
  206. package/lib/common/watcher.d.ts +13 -0
  207. package/lib/common/watcher.js +78 -0
  208. package/lib/common/watcher.js.map +1 -0
  209. package/lib/external/index.js +59413 -57862
  210. package/lib/helpers.d.ts +6 -6
  211. package/lib/helpers.js +10 -25
  212. package/lib/helpers.js.map +1 -1
  213. package/lib/index.js +5 -1
  214. package/lib/index.js.map +1 -1
  215. package/lib/inject.js +2 -2
  216. package/lib/inject.js.map +1 -1
  217. package/lib/injectors/pilet-injector.d.ts +75 -0
  218. package/lib/injectors/pilet-injector.js +246 -0
  219. package/lib/injectors/pilet-injector.js.map +1 -0
  220. package/lib/injectors/{piral.d.ts → piral-injector.d.ts} +10 -2
  221. package/lib/injectors/piral-injector.js +97 -0
  222. package/lib/injectors/piral-injector.js.map +1 -0
  223. package/lib/messages.d.ts +567 -47
  224. package/lib/messages.js +638 -51
  225. package/lib/messages.js.map +1 -1
  226. package/lib/pb-cli.js +1 -1
  227. package/lib/pb-cli.js.map +1 -1
  228. package/lib/pilet-cli.js +1 -1
  229. package/lib/pilet-cli.js.map +1 -1
  230. package/lib/piral-cli.js +1 -1
  231. package/lib/piral-cli.js.map +1 -1
  232. package/lib/platforms/node.d.ts +8 -0
  233. package/lib/platforms/node.js +30 -0
  234. package/lib/platforms/node.js.map +1 -0
  235. package/lib/platforms/web.d.ts +8 -0
  236. package/lib/platforms/web.js +120 -0
  237. package/lib/platforms/web.js.map +1 -0
  238. package/lib/plugin.js +36 -11
  239. package/lib/plugin.js.map +1 -1
  240. package/lib/questionnaire.d.ts +1 -1
  241. package/lib/questionnaire.js +9 -4
  242. package/lib/questionnaire.js.map +1 -1
  243. package/lib/release.d.ts +1 -1
  244. package/lib/release.js +38 -6
  245. package/lib/release.js.map +1 -1
  246. package/lib/resolvers.js +13 -13
  247. package/lib/resolvers.js.map +1 -1
  248. package/lib/rules/index.js +8 -8
  249. package/lib/rules/index.js.map +1 -1
  250. package/lib/rules/pilet-has-externals-as-peers.d.ts +1 -1
  251. package/lib/rules/pilet-has-externals-as-peers.js +9 -7
  252. package/lib/rules/pilet-has-externals-as-peers.js.map +1 -1
  253. package/lib/rules/pilet-has-no-self-reference.d.ts +1 -1
  254. package/lib/rules/pilet-has-no-self-reference.js +2 -2
  255. package/lib/rules/pilet-has-no-self-reference.js.map +1 -1
  256. package/lib/rules/pilet-has-no-third-party-dependency.d.ts +1 -1
  257. package/lib/rules/pilet-stays-small.d.ts +1 -1
  258. package/lib/rules/pilet-stays-small.js +4 -4
  259. package/lib/rules/pilet-stays-small.js.map +1 -1
  260. package/lib/rules/pilet-uses-latest-piral.d.ts +1 -1
  261. package/lib/rules/pilet-uses-latest-piral.js +18 -9
  262. package/lib/rules/pilet-uses-latest-piral.js.map +1 -1
  263. package/lib/rules/piral-depends-on-piral.d.ts +1 -1
  264. package/lib/rules/piral-entry-ends-with-html.d.ts +1 -1
  265. package/lib/rules/piral-entry-ends-with-html.js +1 -1
  266. package/lib/rules/piral-entry-ends-with-html.js.map +1 -1
  267. package/lib/rules/piral-has-valid-devDependencies.d.ts +1 -1
  268. package/lib/rules/piral-has-valid-externals.d.ts +1 -1
  269. package/lib/rules/piral-has-valid-externals.js +10 -24
  270. package/lib/rules/piral-has-valid-externals.js.map +1 -1
  271. package/lib/rules/piral-has-valid-files.d.ts +1 -1
  272. package/lib/rules/piral-has-valid-files.js +5 -5
  273. package/lib/rules/piral-has-valid-files.js.map +1 -1
  274. package/lib/rules/piral-has-valid-scripts.d.ts +1 -1
  275. package/lib/runner.js +1 -1
  276. package/lib/runner.js.map +1 -1
  277. package/lib/select.js +6 -6
  278. package/lib/select.js.map +1 -1
  279. package/lib/types/common.d.ts +88 -8
  280. package/lib/types/common.js.map +1 -1
  281. package/lib/types/index.js +5 -1
  282. package/lib/types/index.js.map +1 -1
  283. package/lib/types/internal.d.ts +12 -13
  284. package/lib/types/public.d.ts +89 -23
  285. package/package.json +19 -16
  286. package/src/apps/add-piral-instance-pilet.ts +98 -0
  287. package/src/apps/build-pilet.ts +286 -38
  288. package/src/apps/build-piral.test.ts +20 -7
  289. package/src/apps/build-piral.ts +90 -39
  290. package/src/apps/debug-pilet.ts +229 -123
  291. package/src/apps/debug-piral.ts +121 -68
  292. package/src/apps/declaration-piral.ts +4 -2
  293. package/src/apps/index.ts +3 -0
  294. package/src/apps/new-pilet.test.ts +28 -11
  295. package/src/apps/new-pilet.ts +60 -48
  296. package/src/apps/new-piral.test.ts +25 -13
  297. package/src/apps/new-piral.ts +43 -19
  298. package/src/apps/pack-pilet.test.ts +2 -2
  299. package/src/apps/pack-pilet.ts +12 -10
  300. package/src/apps/publish-pilet.ts +148 -43
  301. package/src/apps/publish-piral.ts +38 -11
  302. package/src/apps/remove-piral-instance-pilet.ts +92 -0
  303. package/src/apps/run-emulator-piral.ts +154 -0
  304. package/src/apps/upgrade-pilet.ts +37 -31
  305. package/src/apps/upgrade-piral.ts +9 -7
  306. package/src/apps/validate-pilet.ts +30 -22
  307. package/src/apps/validate-piral.ts +5 -2
  308. package/src/build/bundler-calls.ts +133 -0
  309. package/src/build/run-build-pilet.ts +91 -0
  310. package/src/build/run-build-piral.ts +93 -0
  311. package/src/build/run-debug-mono-piral.ts +86 -0
  312. package/src/build/run-debug-pilet.ts +116 -0
  313. package/src/build/run-debug-piral.ts +105 -0
  314. package/src/bundler.test.ts +86 -68
  315. package/src/bundler.ts +74 -19
  316. package/src/cli.ts +6 -2
  317. package/src/commands.ts +273 -44
  318. package/src/common/archive.test.ts +51 -45
  319. package/src/common/archive.ts +28 -1
  320. package/src/common/browser.test.ts +20 -10
  321. package/src/common/browser.ts +13 -6
  322. package/src/common/clients/index.ts +41 -0
  323. package/src/common/clients/lerna.ts +85 -1
  324. package/src/common/clients/npm.ts +56 -14
  325. package/src/common/clients/pnp.ts +84 -0
  326. package/src/common/clients/pnpm.ts +45 -1
  327. package/src/common/clients/rush.ts +118 -0
  328. package/src/common/clients/yarn.ts +49 -3
  329. package/src/common/compatibility.ts +8 -1
  330. package/src/common/config.ts +57 -22
  331. package/src/common/constants.ts +21 -3
  332. package/src/common/declaration.ts +124 -50
  333. package/src/common/emoji.ts +9 -0
  334. package/src/common/emulator.ts +43 -23
  335. package/src/common/enums.ts +0 -5
  336. package/src/common/envs.ts +15 -3
  337. package/src/common/hash.ts +5 -0
  338. package/src/common/http.test.ts +76 -64
  339. package/src/common/http.ts +88 -24
  340. package/src/common/importmap.ts +242 -0
  341. package/src/common/index.ts +10 -0
  342. package/src/common/info.ts +9 -1
  343. package/src/common/injectors.ts +49 -22
  344. package/src/common/inspect.ts +13 -14
  345. package/src/common/interactive.test.ts +6 -0
  346. package/src/common/interactive.ts +69 -1
  347. package/src/common/io.ts +199 -82
  348. package/src/common/language.ts +87 -10
  349. package/src/common/log.ts +32 -46
  350. package/src/common/npm.test.ts +187 -166
  351. package/src/common/npm.ts +263 -119
  352. package/src/common/pack.test.ts +11 -13
  353. package/src/common/pack.ts +60 -21
  354. package/src/common/package.test.ts +59 -49
  355. package/src/common/package.ts +406 -167
  356. package/src/common/parallel.test.ts +28 -0
  357. package/src/common/parallel.ts +21 -0
  358. package/src/common/platform.ts +31 -0
  359. package/src/common/port.test.ts +4 -1
  360. package/src/common/port.ts +10 -0
  361. package/src/common/rules.test.ts +3 -3
  362. package/src/common/scaffold.ts +69 -41
  363. package/src/common/scripts.ts +5 -4
  364. package/src/common/shell.ts +34 -0
  365. package/src/common/spec.ts +69 -0
  366. package/src/common/template.ts +20 -18
  367. package/src/common/url.test.ts +28 -0
  368. package/src/common/url.ts +15 -0
  369. package/src/common/utils.ts +3 -0
  370. package/src/common/version.test.ts +233 -0
  371. package/src/common/version.ts +142 -0
  372. package/src/common/watcher.ts +89 -0
  373. package/src/external/index.test.ts +2 -2
  374. package/src/external/index.ts +7 -3
  375. package/src/external/resolve.ts +29 -0
  376. package/src/helpers.test.ts +2 -22
  377. package/src/helpers.ts +21 -30
  378. package/src/injectors/{pilet.test.ts → pilet-injector.test.ts} +13 -4
  379. package/src/injectors/pilet-injector.ts +346 -0
  380. package/src/injectors/{piral.test.ts → piral-injector.test.ts} +14 -5
  381. package/src/injectors/piral-injector.ts +130 -0
  382. package/src/messages.ts +649 -63
  383. package/src/platforms/node.ts +16 -0
  384. package/src/platforms/web.ts +148 -0
  385. package/src/plugin.ts +34 -5
  386. package/src/questionnaire.ts +17 -3
  387. package/src/release.ts +47 -5
  388. package/src/rules/index.ts +1 -1
  389. package/src/rules/pilet-has-externals-as-peers.test.ts +3 -2
  390. package/src/rules/pilet-has-externals-as-peers.ts +9 -7
  391. package/src/rules/pilet-has-no-self-reference.ts +1 -1
  392. package/src/rules/pilet-stays-small.ts +1 -1
  393. package/src/rules/pilet-uses-latest-piral.ts +22 -12
  394. package/src/rules/piral-has-valid-externals.ts +10 -29
  395. package/src/types/common.ts +99 -9
  396. package/src/types/internal.ts +14 -14
  397. package/src/types/public.ts +93 -27
  398. package/lib/external/child.js +0 -144
  399. package/lib/external/classes.trie +0 -0
  400. package/lib/external/xdg-open +0 -1066
  401. package/lib/injectors/pilet.d.ts +0 -47
  402. package/lib/injectors/pilet.js +0 -187
  403. package/lib/injectors/pilet.js.map +0 -1
  404. package/lib/injectors/piral.js +0 -62
  405. package/lib/injectors/piral.js.map +0 -1
  406. package/src/injectors/pilet.ts +0 -219
  407. package/src/injectors/piral.ts +0 -76
  408. package/templates/piral-index.js.ejs +0 -16
@@ -2,15 +2,86 @@ import { resolve, join, extname, basename, dirname, relative } from 'path';
2
2
  import { log, fail } from './log';
3
3
  import { cliVersion } from './info';
4
4
  import { unpackTarball } from './archive';
5
- import { getDevDependencies } from './language';
6
- import { SourceLanguage, ForceOverwrite } from './enums';
5
+ import { ForceOverwrite } from './enums';
7
6
  import { checkAppShellCompatibility } from './compatibility';
8
- import { filesTar, filesOnceTar, declarationEntryExtensions } from './constants';
9
- import { getHash, checkIsDirectory, matchFiles, getFileNames } from './io';
10
- import { readJson, copy, updateExistingJson, findFile, checkExists } from './io';
11
- import { Framework, FileInfo, PiletsInfo, TemplateFileLocation } from '../types';
12
- import { isGitPackage, isLocalPackage, makeGitUrl, makeFilePath, makeExternals } from './npm';
13
7
  import { deepMerge } from './merge';
8
+ import { onlyUnique } from './utils';
9
+ import { readImportmap } from './importmap';
10
+ import { getHash, checkIsDirectory, matchFiles } from './io';
11
+ import { readJson, copy, updateExistingJson, findFile, checkExists } from './io';
12
+ import { isGitPackage, isLocalPackage, makeGitUrl, makeFilePath, tryResolvePackage, isNpmPackage } from './npm';
13
+ import { makePiletExternals, makeExternals, findPackageRoot, findSpecificVersion, makeNpmAlias } from './npm';
14
+ import { getModulePath } from '../external';
15
+ import { getDependencies, getDependencyPackages, getDevDependencies } from './language';
16
+ import { getDevDependencyPackages, getFrameworkDependencies } from './language';
17
+ import {
18
+ declarationEntryExtensions,
19
+ piralJsonSchemaUrl,
20
+ piletJsonSchemaUrl,
21
+ filesTar,
22
+ filesOnceTar,
23
+ bundlerNames,
24
+ frameworkLibs,
25
+ piralJson,
26
+ piletJson,
27
+ packageJson,
28
+ } from './constants';
29
+ import {
30
+ SourceLanguage,
31
+ Framework,
32
+ FileInfo,
33
+ PiletsInfo,
34
+ TemplateFileLocation,
35
+ PiletPackageData,
36
+ PiralPackageData,
37
+ SharedDependency,
38
+ PiletDefinition,
39
+ AppDefinition,
40
+ PiralInstancePackageData,
41
+ } from '../types';
42
+
43
+ export interface PiralInstanceData {
44
+ packageName: Framework;
45
+ language: SourceLanguage;
46
+ reactVersion: number;
47
+ reactRouterVersion: number;
48
+ }
49
+
50
+ async function appendBundler(devDependencies: Record<string, string>, bundler: string, proposedVersion: string) {
51
+ if (bundler && bundler !== 'none') {
52
+ if (isValidDependency(bundler)) {
53
+ const sep = bundler.indexOf('@', 1);
54
+ const hasVersion = sep !== -1;
55
+ const proposedName = bundler.substring(0, hasVersion ? sep : bundler.length);
56
+ const givenVersion = hasVersion ? bundler.substring(sep + 1) : proposedVersion;
57
+ const name = bundlerNames.includes(proposedName as any) ? `piral-cli-${bundler}` : proposedName;
58
+ const versions = new Set([
59
+ givenVersion,
60
+ givenVersion.includes('-beta.') && 'next',
61
+ givenVersion.includes('-alpha.') && 'canary',
62
+ givenVersion.includes('.') && givenVersion.split('.').slice(0, 2).join('.'),
63
+ 'latest',
64
+ ]);
65
+
66
+ for (const version of versions) {
67
+ if (version) {
68
+ const isAvailable = await findSpecificVersion(name, version);
69
+
70
+ // only if something was returned we know that the version exists; so we can take it.
71
+ if (isAvailable) {
72
+ devDependencies[name] = version;
73
+ return;
74
+ }
75
+ }
76
+ }
77
+
78
+ log('generalWarning_0001', `Could not find a valid version for the provided bundler "${bundler}".'`);
79
+ } else {
80
+ //Error case - print warning and ignore
81
+ log('generalWarning_0001', `The provided bundler name "${bundler}" does not refer to a valid package name.'`);
82
+ }
83
+ }
84
+ }
14
85
 
15
86
  function getDependencyVersion(
16
87
  name: string,
@@ -86,101 +157,157 @@ async function getMatchingFiles(
86
157
  }
87
158
 
88
159
  export function getPiralPath(root: string, name: string) {
89
- const path = require.resolve(`${name}/package.json`, {
90
- paths: [root],
91
- });
160
+ const path = findPackageRoot(name, root);
161
+
162
+ if (!path) {
163
+ fail('invalidPiralReference_0043');
164
+ }
165
+
92
166
  return dirname(path);
93
167
  }
94
168
 
95
- export function findPackageRoot(pck: string, baseDir: string) {
96
- try {
97
- return require.resolve(`${pck}/package.json`, {
98
- paths: [baseDir],
99
- });
100
- } catch (ex) {
101
- log('generalDebug_0003', `Could not find the package root in "${baseDir}": ${ex}.`);
102
- return undefined;
169
+ export async function findPiralInstance(
170
+ proposedApp: string,
171
+ baseDir: string,
172
+ port: number,
173
+ ): Promise<PiralInstancePackageData> {
174
+ const path = findPackageRoot(proposedApp, baseDir);
175
+
176
+ if (path) {
177
+ log('generalDebug_0003', `Following the app package in "${path}" ...`);
178
+ const root = dirname(path);
179
+ const appPackage = await readJson(root, basename(path));
180
+ const relPath = appPackage && appPackage.app;
181
+ appPackage.app = relPath && resolve(root, relPath);
182
+ appPackage.root = root;
183
+ appPackage.port = port;
184
+ return appPackage;
103
185
  }
104
- }
105
186
 
106
- function findPackage(pck: string | Array<string>, baseDir: string) {
107
- if (Array.isArray(pck)) {
108
- for (const item of pck) {
109
- const result = findPackage(item, baseDir);
187
+ fail('appInstanceNotFound_0010', proposedApp);
188
+ }
110
189
 
111
- if (result) {
112
- return result;
113
- }
190
+ export async function findPiralInstances(
191
+ proposedApps: Array<string>,
192
+ piletPackage: PiletPackageData,
193
+ piletDefinition: undefined | PiletDefinition,
194
+ baseDir: string,
195
+ ) {
196
+ if (proposedApps) {
197
+ // do nothing
198
+ } else if (piletDefinition) {
199
+ const availableApps = Object.keys(piletDefinition.piralInstances || {});
200
+ proposedApps = availableApps.filter((m) => piletDefinition.piralInstances[m].selected);
201
+
202
+ if (proposedApps.length === 0) {
203
+ proposedApps = availableApps.slice(0, 1);
114
204
  }
115
205
  } else {
116
- const path = findPackageRoot(pck, baseDir);
117
-
118
- if (path) {
119
- log('generalDebug_0003', `Following the app package in "${path}" ...`);
120
- const appPackage = require(path);
121
- const relPath = appPackage && appPackage.app;
122
- appPackage.app = relPath && resolve(dirname(path), relPath);
123
- return appPackage;
124
- }
206
+ proposedApps = [piletPackage.piral?.name].filter(Boolean);
125
207
  }
126
208
 
127
- return undefined;
209
+ if (proposedApps.length > 0) {
210
+ return Promise.all(
211
+ proposedApps.map((proposedApp) =>
212
+ findPiralInstance(proposedApp, baseDir, piletDefinition?.piralInstances?.[proposedApp]?.port ?? 0),
213
+ ),
214
+ );
215
+ }
216
+
217
+ return [];
128
218
  }
129
219
 
130
- export function readPiralPackage(root: string, name: string) {
220
+ export function readPiralPackage(root: string, name: string): Promise<PiralPackageData> {
131
221
  log('generalDebug_0003', `Reading the piral package in "${root}" ...`);
132
222
  const path = getPiralPath(root, name);
133
223
  return readJson(path, 'package.json');
134
224
  }
135
225
 
136
- export function getPiralPackage(
226
+ export async function patchPiralPackage(
227
+ root: string,
137
228
  app: string,
138
- language: SourceLanguage,
229
+ data: PiralInstanceData,
139
230
  version: string,
140
- framework: Framework,
141
231
  bundler?: string,
142
232
  ) {
143
- const typings = framework === 'piral-base' ? {} : undefined;
233
+ log('generalDebug_0003', `Patching the package.json in "${root}" ...`);
234
+ const pkg = await getPiralPackage(app, data, version, bundler);
235
+
236
+ await updateExistingJson(root, 'package.json', pkg);
237
+ log('generalDebug_0003', `Succesfully patched the package.json.`);
238
+
239
+ await updateExistingJson(root, piralJson, {
240
+ $schema: piralJsonSchemaUrl,
241
+ pilets: getPiletsInfo({}),
242
+ });
243
+ log('generalDebug_0003', `Succesfully patched the pilet.json.`);
244
+ }
245
+
246
+ export async function getPiralPackage(app: string, data: PiralInstanceData, version: string, bundler?: string) {
247
+ const framework = data.packageName;
144
248
  const devDependencies = {
145
- ...getDevDependencies(language, typings),
249
+ ...getDevDependencies(
250
+ data.language,
251
+ getDevDependencyPackages(framework, data.reactVersion, data.reactRouterVersion),
252
+ ),
146
253
  'piral-cli': `${version}`,
147
254
  };
255
+ const dependencies = {
256
+ ...getFrameworkDependencies(framework, version),
257
+ ...getDependencies(data.language, getDependencyPackages(framework, data.reactVersion, data.reactRouterVersion)),
258
+ };
148
259
 
149
- if (bundler && bundler !== 'none') {
150
- devDependencies[`piral-cli-${bundler}`] = `${version}`;
151
- }
260
+ await appendBundler(devDependencies, bundler, version);
152
261
 
153
262
  return {
154
263
  app,
155
264
  scripts: {
156
265
  start: 'piral debug',
157
266
  build: 'piral build',
267
+ postinstall: 'piral declaration',
158
268
  },
159
- pilets: getPiletsInfo({}),
269
+ types: 'dist/index.d.ts',
270
+ importmap: {
271
+ imports: {},
272
+ inherit: [
273
+ 'piral-base', // this we take in any case
274
+ framework !== 'piral-base' && 'piral-core', // this we take unless we selected piral-base, then obviously core is not invited to the party
275
+ framework === 'piral' && 'piral', // this we take only if we selected piral
276
+ ].filter(Boolean),
277
+ },
278
+ dependencies,
160
279
  devDependencies,
161
280
  };
162
281
  }
163
282
 
164
- async function getAvailableFiles(root: string, name: string, tarBall: string): Promise<Array<FileDescriptor>> {
283
+ async function getAvailableFiles(
284
+ root: string,
285
+ name: string,
286
+ dirName: string,
287
+ fileMap: Array<TemplateFileLocation>,
288
+ ): Promise<Array<FileDescriptor>> {
165
289
  const source = getPiralPath(root, name);
166
- log('generalDebug_0003', `Checking if "files.tar" exists in "${source}" ...`);
167
- const exists = await checkExists(resolve(source, `${tarBall}.tar`));
290
+ const tgz = `${dirName}.tar`;
291
+ log('generalDebug_0003', `Checking if "${tgz}" exists in "${source}" ...`);
292
+ const exists = await checkExists(resolve(source, tgz));
168
293
 
169
294
  if (exists) {
170
- await unpackTarball(source, `${tarBall}.tar`);
295
+ await unpackTarball(source, tgz);
171
296
  }
172
297
 
173
298
  log('generalDebug_0003', `Get matching files from "${source}".`);
174
- const base = resolve(source, tarBall);
299
+ const base = resolve(source, dirName);
175
300
  const files = await matchFiles(base, '**/*');
301
+
176
302
  return files.map((file) => ({
177
303
  sourcePath: file,
178
304
  targetPath: resolve(root, relative(base, file)),
179
305
  }));
180
306
  }
181
307
 
182
- export async function getFileStats(root: string, name: string) {
183
- const files = await getAvailableFiles(root, name, filesTar);
308
+ export async function getFileStats(root: string, name: string, fileMap: Array<TemplateFileLocation> = []) {
309
+ const files = await getAvailableFiles(root, name, filesTar, fileMap);
310
+
184
311
  return await Promise.all(
185
312
  files.map(async (file) => {
186
313
  const { sourcePath, targetPath } = file;
@@ -201,6 +328,7 @@ async function copyFiles(
201
328
  subfiles: Array<FileDescriptor>,
202
329
  forceOverwrite: ForceOverwrite,
203
330
  originalFiles: Array<FileInfo>,
331
+ variables?: Record<string, string>,
204
332
  ) {
205
333
  for (const subfile of subfiles) {
206
334
  const { sourcePath, targetPath } = subfile;
@@ -221,6 +349,7 @@ export async function copyScaffoldingFiles(
221
349
  target: string,
222
350
  files: Array<string | TemplateFileLocation>,
223
351
  piralInfo?: any,
352
+ variables?: Record<string, string>,
224
353
  ) {
225
354
  log('generalDebug_0003', `Copying the scaffolding files ...`);
226
355
  const allFiles: Array<FileDescriptor> = [];
@@ -234,7 +363,7 @@ export async function copyScaffoldingFiles(
234
363
  await extendPackageOverridesFromTemplateFragment(target, piralInfo, allFiles);
235
364
  }
236
365
 
237
- await copyFiles(allFiles, ForceOverwrite.yes, []);
366
+ await copyFiles(allFiles, ForceOverwrite.yes, [], variables);
238
367
  }
239
368
 
240
369
  async function extendPackageOverridesFromTemplateFragment(root: string, piralInfo: any, files: Array<FileDescriptor>) {
@@ -263,31 +392,47 @@ async function extendPackageOverridesFromTemplateFragment(root: string, piralInf
263
392
  }
264
393
  }
265
394
 
395
+ function isTemplateFileLocation(item: string | TemplateFileLocation): item is TemplateFileLocation {
396
+ return typeof item === 'object';
397
+ }
398
+
399
+ function tryFindPackageVersion(packageName: string): string {
400
+ try {
401
+ const { version } = require(`${packageName}/package.json`);
402
+ return version;
403
+ } catch {
404
+ return undefined;
405
+ }
406
+ }
407
+
266
408
  export async function copyPiralFiles(
267
409
  root: string,
268
410
  name: string,
269
- piralInfo: any,
411
+ piralInfo: PiralPackageData,
270
412
  forceOverwrite: ForceOverwrite,
413
+ variables: Record<string, string>,
271
414
  originalFiles?: Array<FileInfo>,
272
415
  ) {
273
416
  log('generalDebug_0003', `Copying the Piral files ...`);
274
- const files = await getAvailableFiles(root, name, filesTar);
417
+ const { files: _files } = getPiletsInfo(piralInfo);
418
+ const fileMap = _files.filter(isTemplateFileLocation);
419
+ const files = await getAvailableFiles(root, name, filesTar, fileMap);
275
420
 
276
421
  if (originalFiles === undefined) {
277
- const initialFiles = await getAvailableFiles(root, name, filesOnceTar);
422
+ const initialFiles = await getAvailableFiles(root, name, filesOnceTar, fileMap);
278
423
  files.push(...initialFiles);
279
424
  originalFiles = [];
280
425
  }
281
426
 
282
427
  await extendPackageOverridesFromTemplateFragment(root, piralInfo, files);
283
- await copyFiles(files, forceOverwrite, originalFiles);
428
+ await copyFiles(files, forceOverwrite, originalFiles, variables);
284
429
  }
285
430
 
286
- export function getPiletsInfo(piralInfo: any): PiletsInfo {
431
+ export function getPiletsInfo(piralInfo: Partial<PiralPackageData>): PiletsInfo {
287
432
  const {
288
433
  files = [],
289
- externals = [],
290
434
  scripts = {},
435
+ template = 'default',
291
436
  validators = {},
292
437
  devDependencies = {},
293
438
  preScaffold = '',
@@ -299,8 +444,8 @@ export function getPiletsInfo(piralInfo: any): PiletsInfo {
299
444
 
300
445
  return {
301
446
  files,
302
- externals,
303
447
  scripts,
448
+ template,
304
449
  validators,
305
450
  devDependencies,
306
451
  preScaffold,
@@ -350,37 +495,88 @@ function checkArrayOrUndefined(obj: Record<string, any>, key: string) {
350
495
  return undefined;
351
496
  }
352
497
 
353
- export function findDependencyVersion(
498
+ export async function findDependencyVersion(
354
499
  pckg: Record<string, any>,
355
500
  rootPath: string,
356
- packageName: string,
501
+ dependency: SharedDependency,
357
502
  ): Promise<string> {
358
503
  const { devDependencies = {}, dependencies = {} } = pckg;
504
+ const packageName = dependency.name;
359
505
  const desiredVersion = dependencies[packageName] ?? devDependencies[packageName];
506
+ const [parent] = dependency.parents || [];
360
507
 
361
508
  if (desiredVersion) {
362
- if (isGitPackage(desiredVersion)) {
363
- return Promise.resolve(makeGitUrl(desiredVersion));
509
+ if (isNpmPackage(desiredVersion)) {
510
+ return desiredVersion;
511
+ } else if (isGitPackage(desiredVersion)) {
512
+ return makeGitUrl(desiredVersion);
364
513
  } else if (isLocalPackage(rootPath, desiredVersion)) {
365
- return Promise.resolve(makeFilePath(rootPath, desiredVersion));
514
+ return makeFilePath(rootPath, desiredVersion);
515
+ }
516
+ }
517
+
518
+ if (parent) {
519
+ // in case the dependency came from another package (= parent)
520
+ // we should start the lookup in its directory (pnpm issue)
521
+ const parentPath = tryResolvePackage(parent, rootPath);
522
+
523
+ if (parentPath) {
524
+ rootPath = dirname(parentPath);
366
525
  }
367
526
  }
368
527
 
369
- return findPackageVersion(rootPath, packageName);
528
+ const version = await findPackageVersion(rootPath, packageName);
529
+
530
+ if (dependency.alias) {
531
+ return makeNpmAlias(dependency.alias, version);
532
+ }
533
+
534
+ return version;
370
535
  }
371
536
 
372
- export async function findPackageVersion(rootPath: string, packageName: string): Promise<string> {
373
- try {
374
- log('generalDebug_0003', `Finding the version of "${packageName}" in "${rootPath}".`);
375
- const moduleName = require.resolve(packageName, {
376
- paths: [rootPath],
377
- });
378
- const packageJson = await findFile(moduleName, 'package.json');
379
- return require(packageJson).version;
380
- } catch (e) {
381
- log('cannotResolveDependency_0053', packageName, rootPath);
382
- return 'latest';
537
+ export async function findPackageVersion(rootPath: string, packageName: string | Array<string>): Promise<string> {
538
+ const packages = Array.isArray(packageName) ? packageName : [packageName];
539
+
540
+ for (const pckg of packages) {
541
+ try {
542
+ log('generalDebug_0003', `Finding the version of "${packageName}" in "${rootPath}".`);
543
+ const moduleName = getModulePath(rootPath, pckg);
544
+ const packageJson = await findFile(moduleName, 'package.json');
545
+ const root = dirname(packageJson);
546
+ const { version } = await readJson(root, 'package.json');
547
+ return version;
548
+ } catch {}
549
+ }
550
+
551
+ log('cannotResolveDependency_0053', packages, rootPath);
552
+ return 'latest';
553
+ }
554
+
555
+ export function flattenExternals(dependencies: Array<SharedDependency>) {
556
+ return dependencies.map((m) => m.name).filter(onlyUnique);
557
+ }
558
+
559
+ export async function retrieveExternals(root: string, packageInfo: any): Promise<Array<SharedDependency>> {
560
+ const sharedDependencies = await readImportmap(root, packageInfo);
561
+
562
+ if (sharedDependencies.length === 0) {
563
+ const allDeps = {
564
+ ...packageInfo.devDependencies,
565
+ ...packageInfo.dependencies,
566
+ };
567
+ const deps = packageInfo.pilets?.externals;
568
+ const externals = await makeExternals(root, allDeps, deps);
569
+ return externals.map((ext) => ({
570
+ id: ext,
571
+ name: ext,
572
+ entry: ext,
573
+ type: 'local',
574
+ ref: undefined,
575
+ requireId: ext,
576
+ }));
383
577
  }
578
+
579
+ return sharedDependencies;
384
580
  }
385
581
 
386
582
  export async function retrievePiletsInfo(entryFile: string) {
@@ -390,29 +586,51 @@ export async function retrievePiletsInfo(entryFile: string) {
390
586
  fail('entryPointDoesNotExist_0073', entryFile);
391
587
  }
392
588
 
393
- const packageJson = await findFile(entryFile, 'package.json');
589
+ const packageJsonPath = await findFile(entryFile, packageJson);
394
590
 
395
- if (!packageJson) {
591
+ if (!packageJsonPath) {
396
592
  fail('packageJsonMissing_0074');
397
593
  }
398
594
 
399
- const packageInfo = require(packageJson);
595
+ const root = dirname(packageJsonPath);
596
+ const packageInfo = await readJson(root, packageJson);
597
+ const piralJsonPkg = await readJson(root, piralJson);
598
+ const pilets: PiletsInfo = {
599
+ ...getPiletsInfo(packageInfo),
600
+ ...piralJsonPkg.pilets,
601
+ };
602
+ const externals = await retrieveExternals(root, packageInfo);
603
+ const dependencies = {
604
+ std: packageInfo.dependencies || {},
605
+ dev: packageInfo.devDependencies || {},
606
+ peer: packageInfo.peerDependencies || {},
607
+ };
608
+ const framework = frameworkLibs.find((lib) => lib in dependencies.std || lib in dependencies.dev);
400
609
 
401
610
  return {
402
- ...getPiletsInfo(packageInfo),
611
+ ...pilets,
612
+ externals,
403
613
  name: packageInfo.name,
404
614
  version: packageInfo.version,
405
- dependencies: {
406
- std: packageInfo.dependencies || {},
407
- dev: packageInfo.devDependencies || {},
408
- peer: packageInfo.peerDependencies || {},
409
- },
615
+ framework,
616
+ dependencies,
410
617
  scripts: packageInfo.scripts,
411
618
  ignored: checkArrayOrUndefined(packageInfo, 'preservedDependencies'),
412
- root: dirname(packageJson),
619
+ root,
413
620
  };
414
621
  }
415
622
 
623
+ export function validateSharedDependencies(externals: Array<SharedDependency>) {
624
+ // See #591 - we should warn in case somebody shared piral packages
625
+ for (const external of externals) {
626
+ const name = external.name;
627
+
628
+ if (external.type === 'local' && name.startsWith('piral-') && name.indexOf('/') === -1) {
629
+ log('invalidSharedDependency_0029', name);
630
+ }
631
+ }
632
+ }
633
+
416
634
  export function isValidDependency(name: string) {
417
635
  // super simple check at the moment
418
636
  // just to filter out things like "redux-saga/effects" and "@scope/redux-saga/effects"
@@ -423,21 +641,41 @@ export async function patchPiletPackage(
423
641
  root: string,
424
642
  name: string,
425
643
  version: string,
426
- piralInfo: any,
644
+ piralInfo: PiralPackageData,
645
+ fromEmulator: boolean,
427
646
  newInfo?: { language: SourceLanguage; bundler: string },
428
647
  ) {
429
648
  log('generalDebug_0003', `Patching the package.json in "${root}" ...`);
430
- const { externals, packageOverrides, ...info } = getPiletsInfo(piralInfo);
431
- const piral = {
432
- comment: 'Keep this section to use the Piral CLI.',
433
- name,
434
- };
649
+ const pkg = await getPiletPackage(root, name, version, piralInfo, fromEmulator, newInfo);
650
+
651
+ await updateExistingJson(root, packageJson, pkg);
652
+ log('generalDebug_0003', `Succesfully patched the package.json.`);
653
+
654
+ await updateExistingJson(root, piletJson, {
655
+ $schema: piletJsonSchemaUrl,
656
+ piralInstances: {
657
+ [name]: {},
658
+ },
659
+ });
660
+ log('generalDebug_0003', `Succesfully patched the pilet.json.`);
661
+ }
662
+
663
+ async function getPiletPackage(
664
+ root: string,
665
+ name: string,
666
+ version: string,
667
+ piralInfo: PiralPackageData,
668
+ fromEmulator: boolean,
669
+ newInfo?: { language: SourceLanguage; bundler: string },
670
+ ) {
671
+ const { piralCLI = { version: cliVersion } } = piralInfo;
672
+ const { packageOverrides, ...info } = getPiletsInfo(piralInfo);
435
673
  const piralDependencies = {
436
674
  ...piralInfo.devDependencies,
437
675
  ...piralInfo.dependencies,
438
676
  };
677
+ const toolVersion = piralCLI.version;
439
678
  const typeDependencies = newInfo ? getDevDependencies(newInfo.language) : {};
440
- const allExternals = makeExternals(externals);
441
679
  const scripts = newInfo
442
680
  ? {
443
681
  start: 'pilet debug',
@@ -446,21 +684,8 @@ export async function patchPiletPackage(
446
684
  ...info.scripts,
447
685
  }
448
686
  : info.scripts;
449
- const peerModules = [];
450
- const peerDependencies = {
451
- ...allExternals.reduce((deps, name) => {
452
- const valid = isValidDependency(name);
453
- deps[name] = valid ? '*' : undefined;
454
-
455
- if (!valid) {
456
- peerModules.push(name);
457
- }
458
-
459
- return deps;
460
- }, {}),
461
- [name]: `*`,
462
- };
463
- const devDependencies = {
687
+ const allExternals = await makePiletExternals(root, piralDependencies, fromEmulator, piralInfo);
688
+ const devDependencies: Record<string, string> = {
464
689
  ...Object.keys(typeDependencies).reduce((deps, name) => {
465
690
  deps[name] = piralDependencies[name] || typeDependencies[name];
466
691
  return deps;
@@ -470,7 +695,7 @@ export async function patchPiletPackage(
470
695
  return deps;
471
696
  }, {}),
472
697
  ...allExternals.filter(isValidDependency).reduce((deps, name) => {
473
- const version = piralDependencies[name];
698
+ const version = piralDependencies[name] || tryFindPackageVersion(name);
474
699
 
475
700
  if (version || newInfo) {
476
701
  // set only if we have an explicit version or we are in the scaffolding case
@@ -480,36 +705,30 @@ export async function patchPiletPackage(
480
705
  return deps;
481
706
  }, {}),
482
707
  [name]: `${version || piralInfo.version}`,
708
+ ['piral-cli']: toolVersion,
483
709
  };
484
710
 
485
711
  if (newInfo) {
486
- const bundler = newInfo.bundler;
487
- devDependencies['piral-cli'] = `^${cliVersion}`;
488
-
489
- if (bundler && bundler !== 'none') {
490
- devDependencies[`piral-cli-${bundler}`] = `^${cliVersion}`;
491
- }
712
+ await appendBundler(devDependencies, newInfo.bundler, toolVersion);
492
713
  }
493
714
 
494
- const packageContent = deepMerge(packageOverrides, {
495
- piral,
715
+ return deepMerge(packageOverrides, {
716
+ importmap: {
717
+ imports: {},
718
+ inherit: [name],
719
+ },
496
720
  devDependencies,
497
- peerDependencies,
498
- peerModules,
499
721
  dependencies: {
500
722
  [name]: undefined,
501
723
  },
502
724
  scripts,
503
725
  });
504
-
505
- await updateExistingJson(root, 'package.json', packageContent);
506
- log('generalDebug_0003', `Succesfully patched the package.json.`);
507
726
  }
508
727
 
509
728
  /**
510
729
  * Returns true if its an emulator package, otherwise it has to be a "raw" app shell.
511
730
  */
512
- export function checkAppShellPackage(appPackage: any) {
731
+ export function checkAppShellPackage(appPackage: PiralPackageData) {
513
732
  const { piralCLI = { generated: false, version: cliVersion } } = appPackage;
514
733
 
515
734
  if (piralCLI.generated) {
@@ -521,26 +740,73 @@ export function checkAppShellPackage(appPackage: any) {
521
740
  return false;
522
741
  }
523
742
 
524
- export async function retrievePiletData(target: string, app?: string) {
525
- const packageJson = await findFile(target, 'package.json');
743
+ export function combinePiletExternals(
744
+ appShells: Array<string>,
745
+ peerDependencies: Record<string, string>,
746
+ peerModules: Array<string>,
747
+ importmap: Array<SharedDependency>,
748
+ ) {
749
+ const externals = [...Object.keys(peerDependencies), ...peerModules];
750
+
751
+ for (let i = importmap.length; i--; ) {
752
+ const entry = importmap[i];
753
+
754
+ // if the entry has no parents, i.e., it was explicitly mentioned in the importmap
755
+ // then keep it in the importmap (=> prefer the distributed approach, which will always work)
756
+ if (Array.isArray(entry.parents)) {
757
+ // only accept entry as a centrally shared dependency if the entry appears in all
758
+ // mentioned / referenced app shells
759
+ // in other cases (e.g., if one app shell does not share this) use the distributed
760
+ // mechanism to ensure that the dependency can also be resolved in this shell
761
+ if (appShells.every((app) => entry.parents.includes(app))) {
762
+ externals.push(entry.name);
763
+ importmap.splice(i, 1);
764
+ }
765
+ }
766
+ }
767
+
768
+ return externals;
769
+ }
770
+
771
+ export async function findPiletRoot(proposedRoot: string) {
772
+ const packageJson = await findFile(proposedRoot, 'package.json');
526
773
 
527
774
  if (!packageJson) {
528
775
  fail('packageJsonMissing_0075');
529
776
  }
530
777
 
531
- const root = dirname(packageJson);
532
- const piletPackage = require(packageJson);
533
- const appPackage = findPackage(
534
- app || (piletPackage.piral && piletPackage.piral.name) || Object.keys(piletPackage.devDependencies),
535
- target,
536
- );
537
- const appFile: string = appPackage && appPackage.app;
778
+ return dirname(packageJson);
779
+ }
538
780
 
539
- if (!appFile) {
540
- fail('appInstanceInvalid_0011');
781
+ export async function retrievePiletData(target: string, app?: string) {
782
+ const piletJsonPath = await findFile(target, piletJson);
783
+ const proposedRoot = piletJsonPath ? dirname(piletJsonPath) : target;
784
+ const root = await findPiletRoot(proposedRoot);
785
+ const piletPackage = await readJson(root, 'package.json');
786
+ const piletDefinition: PiletDefinition = piletJsonPath && (await readJson(proposedRoot, piletJson));
787
+ const appPackages = await findPiralInstances(app && [app], piletPackage, piletDefinition, target);
788
+ const apps: Array<AppDefinition> = [];
789
+
790
+ for (const appPackage of appPackages) {
791
+ const appFile: string = appPackage?.app;
792
+ const appRoot: string = appPackage?.root;
793
+ const appPort = appPackage?.port;
794
+
795
+ if (!appFile || !appRoot) {
796
+ fail('appInstanceInvalid_0011');
797
+ }
798
+
799
+ const emulator = checkAppShellPackage(appPackage);
800
+ apps.push({
801
+ appPackage,
802
+ appFile,
803
+ appRoot,
804
+ emulator,
805
+ appPort,
806
+ });
541
807
  }
542
808
 
543
- const emulator = checkAppShellPackage(appPackage);
809
+ const importmap = await readImportmap(root, piletPackage);
544
810
 
545
811
  return {
546
812
  dependencies: piletPackage.dependencies || {},
@@ -548,37 +814,10 @@ export async function retrievePiletData(target: string, app?: string) {
548
814
  peerDependencies: piletPackage.peerDependencies || {},
549
815
  peerModules: piletPackage.peerModules || [],
550
816
  ignored: checkArrayOrUndefined(piletPackage, 'preservedDependencies'),
551
- appFile,
817
+ schema: piletDefinition?.schemaVersion,
818
+ importmap,
819
+ apps,
552
820
  piletPackage,
553
- appPackage,
554
- emulator,
555
821
  root,
556
822
  };
557
823
  }
558
-
559
- export async function findEntryModule(entryFile: string, target: string) {
560
- const entry = basename(entryFile);
561
- const files = await getFileNames(target);
562
- const preferences = ['.tsx', '.ts', '.jsx', '.js', '.mjs', '.cjs', '.esm', '.es', '.es6', '.html'];
563
- const results = [];
564
- log('generalDebug_0003', `Found ${files.length} potential entry points in "${target}".`);
565
-
566
- for (const file of files) {
567
- const ext = extname(file);
568
- const fullPath = join(target, file);
569
-
570
- if (file === entry) {
571
- return fullPath;
572
- } else if (file.replace(ext, '') === entry) {
573
- const prefIndex = preferences.indexOf(ext);
574
-
575
- if (prefIndex !== -1) {
576
- results[prefIndex] = fullPath;
577
- } else {
578
- results[preferences.length] = fullPath;
579
- }
580
- }
581
- }
582
-
583
- return results.filter(Boolean).shift() || entryFile;
584
- }