piral-cli 0.13.8 → 0.14.0-beta.3156

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 (202) hide show
  1. package/README.md +5 -2
  2. package/lib/api.js +23 -23
  3. package/lib/api.js.map +1 -1
  4. package/lib/apps/build-pilet.js +18 -16
  5. package/lib/apps/build-pilet.js.map +1 -1
  6. package/lib/apps/build-piral.js +35 -34
  7. package/lib/apps/build-piral.js.map +1 -1
  8. package/lib/apps/debug-pilet.js +28 -27
  9. package/lib/apps/debug-pilet.js.map +1 -1
  10. package/lib/apps/debug-piral.js +15 -14
  11. package/lib/apps/debug-piral.js.map +1 -1
  12. package/lib/apps/declaration-piral.js +5 -3
  13. package/lib/apps/declaration-piral.js.map +1 -1
  14. package/lib/apps/new-pilet.js +43 -39
  15. package/lib/apps/new-pilet.js.map +1 -1
  16. package/lib/apps/new-piral.js +21 -20
  17. package/lib/apps/new-piral.js.map +1 -1
  18. package/lib/apps/pack-pilet.js +6 -4
  19. package/lib/apps/pack-pilet.js.map +1 -1
  20. package/lib/apps/publish-pilet.js +48 -47
  21. package/lib/apps/publish-pilet.js.map +1 -1
  22. package/lib/apps/publish-piral.js +23 -22
  23. package/lib/apps/publish-piral.js.map +1 -1
  24. package/lib/apps/upgrade-pilet.js +34 -33
  25. package/lib/apps/upgrade-pilet.js.map +1 -1
  26. package/lib/apps/upgrade-piral.js +20 -19
  27. package/lib/apps/upgrade-piral.js.map +1 -1
  28. package/lib/apps/validate-pilet.js +13 -11
  29. package/lib/apps/validate-pilet.js.map +1 -1
  30. package/lib/apps/validate-piral.js +12 -10
  31. package/lib/apps/validate-piral.js.map +1 -1
  32. package/lib/build/bundler-calls.d.ts +3 -0
  33. package/lib/build/bundler-calls.js +107 -0
  34. package/lib/build/bundler-calls.js.map +1 -0
  35. package/lib/build/run-build-pilet.d.ts +1 -0
  36. package/lib/build/run-build-pilet.js +65 -0
  37. package/lib/build/run-build-pilet.js.map +1 -0
  38. package/lib/build/run-build-piral.d.ts +1 -0
  39. package/lib/build/run-build-piral.js +66 -0
  40. package/lib/build/run-build-piral.js.map +1 -0
  41. package/lib/build/run-debug-mono-piral.d.ts +1 -0
  42. package/lib/build/run-debug-mono-piral.js +71 -0
  43. package/lib/build/run-debug-mono-piral.js.map +1 -0
  44. package/lib/build/run-debug-pilet.d.ts +1 -0
  45. package/lib/build/run-debug-pilet.js +90 -0
  46. package/lib/build/run-debug-pilet.js.map +1 -0
  47. package/lib/build/run-debug-piral.d.ts +1 -0
  48. package/lib/build/run-debug-piral.js +86 -0
  49. package/lib/build/run-debug-piral.js.map +1 -0
  50. package/lib/bundler.js +62 -25
  51. package/lib/bundler.js.map +1 -1
  52. package/lib/cli.js +2 -2
  53. package/lib/cli.js.map +1 -1
  54. package/lib/commands.js +12 -12
  55. package/lib/commands.js.map +1 -1
  56. package/lib/common/archive.js +8 -8
  57. package/lib/common/archive.js.map +1 -1
  58. package/lib/common/browser.js +2 -2
  59. package/lib/common/browser.js.map +1 -1
  60. package/lib/common/clients/lerna.js +4 -4
  61. package/lib/common/clients/lerna.js.map +1 -1
  62. package/lib/common/clients/npm.js +12 -12
  63. package/lib/common/clients/npm.js.map +1 -1
  64. package/lib/common/clients/pnpm.js +5 -5
  65. package/lib/common/clients/pnpm.js.map +1 -1
  66. package/lib/common/clients/yarn.js +5 -5
  67. package/lib/common/clients/yarn.js.map +1 -1
  68. package/lib/common/compatibility.js +9 -9
  69. package/lib/common/compatibility.js.map +1 -1
  70. package/lib/common/config.d.ts +5 -1
  71. package/lib/common/config.js +2 -1
  72. package/lib/common/config.js.map +1 -1
  73. package/lib/common/constants.d.ts +1 -1
  74. package/lib/common/constants.js +2 -2
  75. package/lib/common/constants.js.map +1 -1
  76. package/lib/common/declaration.js +19 -19
  77. package/lib/common/declaration.js.map +1 -1
  78. package/lib/common/emulator.js +24 -23
  79. package/lib/common/emulator.js.map +1 -1
  80. package/lib/common/envs.js +3 -3
  81. package/lib/common/envs.js.map +1 -1
  82. package/lib/common/hash.d.ts +1 -0
  83. package/lib/common/hash.js +8 -3
  84. package/lib/common/hash.js.map +1 -1
  85. package/lib/common/http.d.ts +1 -0
  86. package/lib/common/http.js +28 -14
  87. package/lib/common/http.js.map +1 -1
  88. package/lib/common/injectors.js +4 -4
  89. package/lib/common/injectors.js.map +1 -1
  90. package/lib/common/inspect.js +4 -4
  91. package/lib/common/inspect.js.map +1 -1
  92. package/lib/common/io.js +77 -73
  93. package/lib/common/io.js.map +1 -1
  94. package/lib/common/language.js +2 -2
  95. package/lib/common/language.js.map +1 -1
  96. package/lib/common/log.js +8 -8
  97. package/lib/common/log.js.map +1 -1
  98. package/lib/common/npm.d.ts +3 -1
  99. package/lib/common/npm.js +68 -48
  100. package/lib/common/npm.js.map +1 -1
  101. package/lib/common/pack.js +18 -18
  102. package/lib/common/pack.js.map +1 -1
  103. package/lib/common/package.d.ts +4 -2
  104. package/lib/common/package.js +182 -81
  105. package/lib/common/package.js.map +1 -1
  106. package/lib/common/patcher.js +20 -20
  107. package/lib/common/patcher.js.map +1 -1
  108. package/lib/common/port.js +3 -3
  109. package/lib/common/port.js.map +1 -1
  110. package/lib/common/rules.js +4 -4
  111. package/lib/common/rules.js.map +1 -1
  112. package/lib/common/scaffold.js +15 -15
  113. package/lib/common/scaffold.js.map +1 -1
  114. package/lib/common/scripts.js +5 -5
  115. package/lib/common/scripts.js.map +1 -1
  116. package/lib/common/template.js +4 -4
  117. package/lib/common/template.js.map +1 -1
  118. package/lib/external/index.js +1369 -611
  119. package/lib/helpers.js +2 -2
  120. package/lib/helpers.js.map +1 -1
  121. package/lib/inject.js +2 -2
  122. package/lib/inject.js.map +1 -1
  123. package/lib/injectors/pilet.d.ts +1 -10
  124. package/lib/injectors/pilet.js +74 -27
  125. package/lib/injectors/pilet.js.map +1 -1
  126. package/lib/injectors/piral.d.ts +4 -2
  127. package/lib/injectors/piral.js +20 -5
  128. package/lib/injectors/piral.js.map +1 -1
  129. package/lib/messages.d.ts +11 -3
  130. package/lib/messages.js +11 -3
  131. package/lib/messages.js.map +1 -1
  132. package/lib/pb-cli.js +1 -1
  133. package/lib/pb-cli.js.map +1 -1
  134. package/lib/pilet-cli.js +1 -1
  135. package/lib/pilet-cli.js.map +1 -1
  136. package/lib/piral-cli.js +1 -1
  137. package/lib/piral-cli.js.map +1 -1
  138. package/lib/plugin.js +10 -10
  139. package/lib/plugin.js.map +1 -1
  140. package/lib/release.js +3 -3
  141. package/lib/release.js.map +1 -1
  142. package/lib/resolvers.js +13 -13
  143. package/lib/resolvers.js.map +1 -1
  144. package/lib/rules/index.js +7 -7
  145. package/lib/rules/index.js.map +1 -1
  146. package/lib/rules/pilet-has-externals-as-peers.js +3 -3
  147. package/lib/rules/pilet-has-externals-as-peers.js.map +1 -1
  148. package/lib/rules/pilet-has-no-self-reference.js +1 -1
  149. package/lib/rules/pilet-has-no-self-reference.js.map +1 -1
  150. package/lib/rules/pilet-stays-small.js +3 -3
  151. package/lib/rules/pilet-stays-small.js.map +1 -1
  152. package/lib/rules/pilet-uses-latest-piral.js +1 -1
  153. package/lib/rules/pilet-uses-latest-piral.js.map +1 -1
  154. package/lib/rules/piral-entry-ends-with-html.js +1 -1
  155. package/lib/rules/piral-entry-ends-with-html.js.map +1 -1
  156. package/lib/rules/piral-has-valid-files.js +5 -5
  157. package/lib/rules/piral-has-valid-files.js.map +1 -1
  158. package/lib/runner.js +1 -1
  159. package/lib/runner.js.map +1 -1
  160. package/lib/select.js +6 -6
  161. package/lib/select.js.map +1 -1
  162. package/lib/types/common.d.ts +8 -0
  163. package/lib/types/public.d.ts +66 -12
  164. package/package.json +3 -3
  165. package/src/apps/build-pilet.ts +7 -4
  166. package/src/apps/build-piral.ts +6 -5
  167. package/src/apps/debug-pilet.ts +3 -2
  168. package/src/apps/debug-piral.ts +2 -1
  169. package/src/apps/declaration-piral.ts +3 -1
  170. package/src/apps/new-pilet.ts +7 -3
  171. package/src/apps/new-piral.ts +2 -1
  172. package/src/apps/pack-pilet.ts +3 -1
  173. package/src/apps/publish-pilet.ts +6 -5
  174. package/src/apps/publish-piral.ts +3 -2
  175. package/src/apps/upgrade-pilet.ts +5 -4
  176. package/src/apps/upgrade-piral.ts +2 -1
  177. package/src/apps/validate-pilet.ts +5 -2
  178. package/src/apps/validate-piral.ts +3 -1
  179. package/src/build/bundler-calls.ts +121 -0
  180. package/src/build/run-build-pilet.ts +89 -0
  181. package/src/build/run-build-piral.ts +88 -0
  182. package/src/build/run-debug-mono-piral.ts +87 -0
  183. package/src/build/run-debug-pilet.ts +114 -0
  184. package/src/build/run-debug-piral.ts +106 -0
  185. package/src/bundler.test.ts +83 -68
  186. package/src/bundler.ts +52 -12
  187. package/src/common/config.ts +6 -1
  188. package/src/common/constants.ts +1 -1
  189. package/src/common/emulator.ts +1 -0
  190. package/src/common/hash.ts +5 -0
  191. package/src/common/http.ts +18 -4
  192. package/src/common/io.ts +4 -0
  193. package/src/common/npm.test.ts +14 -14
  194. package/src/common/npm.ts +21 -2
  195. package/src/common/package.ts +120 -9
  196. package/src/helpers.ts +2 -2
  197. package/src/injectors/pilet.ts +82 -22
  198. package/src/injectors/piral.test.ts +6 -5
  199. package/src/injectors/piral.ts +21 -2
  200. package/src/messages.ts +23 -15
  201. package/src/types/common.ts +9 -0
  202. package/src/types/public.ts +71 -20
@@ -81,6 +81,7 @@ export async function createEmulatorSources(
81
81
  ...piralPkg.dependencies,
82
82
  ...externalDependencies,
83
83
  },
84
+ sharedDependencies: allExternals,
84
85
  repository: piralPkg.repository,
85
86
  bugs: piralPkg.bugs,
86
87
  author: piralPkg.author,
@@ -7,3 +7,8 @@ export function computeHash(content: string | Buffer = '') {
7
7
  export function computeMd5(content: string | Buffer = '') {
8
8
  return createHash('md5').update(content).digest('hex');
9
9
  }
10
+
11
+ export function computeIntegrity(content: string) {
12
+ const sum = createHash('sha256').update(content).digest('base64');
13
+ return `sha256-${sum}`;
14
+ }
@@ -4,9 +4,13 @@ import { Stream } from 'stream';
4
4
  import { platform, tmpdir } from 'os';
5
5
  import { createWriteStream } from 'fs';
6
6
  import { log } from './log';
7
+ import { computeHash } from './hash';
7
8
  import { axios, FormData } from '../external';
8
9
 
9
10
  const os = platform();
11
+ const standardHeaders = {
12
+ 'user-agent': `piral-cli/http.node-${os}`,
13
+ };
10
14
 
11
15
  function getMessage(body: string | { message?: string }) {
12
16
  if (typeof body === 'string') {
@@ -36,14 +40,24 @@ function streamToFile(source: Stream, target: string) {
36
40
  });
37
41
  }
38
42
 
43
+ export function getHashFromUrl(target: string): Promise<string> {
44
+ return axios.default
45
+ .get<Buffer>(target, {
46
+ responseType: 'arraybuffer',
47
+ headers: standardHeaders,
48
+ })
49
+ .then((res) => {
50
+ log('generalDebug_0003', `Received the contents from "${target}" (status: ${res.status}).`);
51
+ return computeHash(res.data);
52
+ });
53
+ }
54
+
39
55
  export function downloadFile(target: string, ca?: Buffer): Promise<Array<string>> {
40
56
  const httpsAgent = ca ? new Agent({ ca }) : undefined;
41
57
  return axios.default
42
58
  .get<Stream>(target, {
43
59
  responseType: 'stream',
44
- headers: {
45
- 'user-agent': `piral-cli/http.node-${os}`,
46
- },
60
+ headers: standardHeaders,
47
61
  httpsAgent,
48
62
  })
49
63
  .then((res) => {
@@ -80,7 +94,7 @@ export function postFile(
80
94
 
81
95
  const headers: Record<string, string> = {
82
96
  ...form.getHeaders(),
83
- 'user-agent': `piral-cli/http.node-${os}`,
97
+ ...standardHeaders,
84
98
  };
85
99
 
86
100
  if (key) {
package/src/common/io.ts CHANGED
@@ -182,6 +182,7 @@ function matchPattern(baseDir: string, pattern: string) {
182
182
  {
183
183
  cwd: baseDir,
184
184
  nodir: true,
185
+ absolute: true,
185
186
  },
186
187
  (err, files) => {
187
188
  if (err) {
@@ -235,6 +236,9 @@ export async function matchAnyPilet(baseDir: string, patterns: Array<string>) {
235
236
  if (!results.length) {
236
237
  log('generalDebug_0003', `Found no potential entry points using "${pattern}".`);
237
238
  } else {
239
+ //TODO -> shouldn't take the first one,
240
+ // should be the first one, yes, but, PER pilet
241
+ // so that multiple pilets can be considered, too
238
242
  log('generalDebug_0003', `Found ${results.length} potential entry points in "${pattern}".`);
239
243
 
240
244
  for (const result of results) {
@@ -409,30 +409,30 @@ describe('NPM Module', () => {
409
409
  it('makeExternals without externals returns coreExternals', () => {
410
410
  const externals = makeExternals();
411
411
  expect(externals).toEqual([
412
- '@dbeining/react-atom',
413
- '@libre/atom',
414
- 'history',
415
412
  'react',
416
413
  'react-dom',
417
414
  'react-router',
418
415
  'react-router-dom',
416
+ 'history',
419
417
  'tslib',
420
418
  'path-to-regexp',
419
+ '@libre/atom',
420
+ '@dbeining/react-atom',
421
421
  ]);
422
422
  });
423
423
 
424
424
  it('makeExternals with no externals returns coreExternals', () => {
425
425
  const externals = makeExternals([]);
426
426
  expect(externals).toEqual([
427
- '@dbeining/react-atom',
428
- '@libre/atom',
429
- 'history',
430
427
  'react',
431
428
  'react-dom',
432
429
  'react-router',
433
430
  'react-router-dom',
431
+ 'history',
434
432
  'tslib',
435
433
  'path-to-regexp',
434
+ '@libre/atom',
435
+ '@dbeining/react-atom',
436
436
  ]);
437
437
  });
438
438
 
@@ -446,15 +446,15 @@ describe('NPM Module', () => {
446
446
  expect(externals).toEqual([
447
447
  'foo',
448
448
  'bar',
449
- '@dbeining/react-atom',
450
- '@libre/atom',
451
- 'history',
452
449
  'react',
453
450
  'react-dom',
454
451
  'react-router',
455
452
  'react-router-dom',
453
+ 'history',
456
454
  'tslib',
457
455
  'path-to-regexp',
456
+ '@libre/atom',
457
+ '@dbeining/react-atom',
458
458
  ]);
459
459
  });
460
460
 
@@ -463,14 +463,14 @@ describe('NPM Module', () => {
463
463
  expect(externals).toEqual([
464
464
  'react',
465
465
  'foo',
466
- '@dbeining/react-atom',
467
- '@libre/atom',
468
- 'history',
469
466
  'react-dom',
470
467
  'react-router',
471
468
  'react-router-dom',
469
+ 'history',
472
470
  'tslib',
473
471
  'path-to-regexp',
472
+ '@libre/atom',
473
+ '@dbeining/react-atom',
474
474
  ]);
475
475
  });
476
476
 
@@ -479,13 +479,13 @@ describe('NPM Module', () => {
479
479
  expect(externals).toEqual([
480
480
  'react',
481
481
  'react-calendar',
482
- '@dbeining/react-atom',
483
- '@libre/atom',
484
482
  'react-dom',
485
483
  'react-router',
486
484
  'react-router-dom',
487
485
  'tslib',
488
486
  'path-to-regexp',
487
+ '@libre/atom',
488
+ '@dbeining/react-atom',
489
489
  ]);
490
490
  });
491
491
 
package/src/common/npm.ts CHANGED
@@ -2,8 +2,8 @@ import { resolve, relative } from 'path';
2
2
  import { createReadStream, existsSync, access, constants } from 'fs';
3
3
  import { log, fail } from './log';
4
4
  import { config } from './config';
5
+ import { legacyCoreExternals } from './constants';
5
6
  import { inspectPackage } from './inspect';
6
- import { coreExternals } from './constants';
7
7
  import { readJson, checkExists, findFile } from './io';
8
8
  import { clientTypeKeys } from '../helpers';
9
9
  import { PackageType, NpmClientType } from '../types';
@@ -388,7 +388,26 @@ export function getPackageVersion(
388
388
  }
389
389
  }
390
390
 
391
- export function makeExternals(externals?: Array<string>) {
391
+ export function getCoreExternals(): Array<string> {
392
+ try {
393
+ return require('piral-core/package.json').sharedDependencies || [];
394
+ } catch {
395
+ return [];
396
+ }
397
+ }
398
+
399
+ export function makePiletExternals(externals: Array<string>, fromEmulator: boolean, piralInfo: any) {
400
+ if (fromEmulator) {
401
+ const { sharedDependencies = makeExternals(externals, true) } = piralInfo;
402
+ return sharedDependencies;
403
+ } else {
404
+ return makeExternals(externals);
405
+ }
406
+ }
407
+
408
+ export function makeExternals(externals?: Array<string>, legacy = false) {
409
+ const coreExternals = legacy ? legacyCoreExternals : getCoreExternals();
410
+
392
411
  if (externals && Array.isArray(externals)) {
393
412
  const [include, exclude] = externals.reduce<[Array<string>, Array<string>]>(
394
413
  (prev, curr) => {
@@ -5,12 +5,13 @@ import { unpackTarball } from './archive';
5
5
  import { getDependencies, getDevDependencies } from './language';
6
6
  import { SourceLanguage, ForceOverwrite } from './enums';
7
7
  import { checkAppShellCompatibility } from './compatibility';
8
+ import { deepMerge } from './merge';
9
+ import { getHashFromUrl } from './http';
10
+ import { isGitPackage, isLocalPackage, makeGitUrl, makeFilePath, makePiletExternals } from './npm';
8
11
  import { filesTar, filesOnceTar, declarationEntryExtensions } from './constants';
9
- import { getHash, checkIsDirectory, matchFiles, getFileNames } from './io';
12
+ import { getHash, checkIsDirectory, matchFiles } from './io';
10
13
  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
- import { deepMerge } from './merge';
14
+ import { Framework, FileInfo, PiletsInfo, TemplateFileLocation, SharedDependency } from '../types';
14
15
 
15
16
  function getDependencyVersion(
16
17
  name: string,
@@ -86,10 +87,15 @@ async function getMatchingFiles(
86
87
  }
87
88
 
88
89
  export function getPiralPath(root: string, name: string) {
89
- const path = require.resolve(`${name}/package.json`, {
90
- paths: [root],
91
- });
92
- return dirname(path);
90
+ try {
91
+ const path = require.resolve(`${name}/package.json`, {
92
+ paths: [root],
93
+ });
94
+ return dirname(path);
95
+ } catch (ex) {
96
+ log('generalDebug_0003', `Could not resolve the Piral path of "${name}" in "${root}": ${ex}.`);
97
+ fail('invalidPiralReference_0043');
98
+ }
93
99
  }
94
100
 
95
101
  export function findPackageRoot(pck: string, baseDir: string) {
@@ -431,6 +437,7 @@ export async function patchPiletPackage(
431
437
  name: string,
432
438
  version: string,
433
439
  piralInfo: any,
440
+ fromEmulator: boolean,
434
441
  newInfo?: { language: SourceLanguage; bundler: string },
435
442
  ) {
436
443
  log('generalDebug_0003', `Patching the package.json in "${root}" ...`);
@@ -444,7 +451,6 @@ export async function patchPiletPackage(
444
451
  ...piralInfo.dependencies,
445
452
  };
446
453
  const typeDependencies = newInfo ? getDevDependencies(newInfo.language) : {};
447
- const allExternals = makeExternals(externals);
448
454
  const scripts = newInfo
449
455
  ? {
450
456
  start: 'pilet debug',
@@ -454,6 +460,7 @@ export async function patchPiletPackage(
454
460
  }
455
461
  : info.scripts;
456
462
  const peerModules = [];
463
+ const allExternals = makePiletExternals(externals, fromEmulator, piralInfo);
457
464
  const peerDependencies = {
458
465
  ...allExternals.reduce((deps, name) => {
459
466
  const valid = isValidDependency(name);
@@ -528,6 +535,108 @@ export function checkAppShellPackage(appPackage: any) {
528
535
  return false;
529
536
  }
530
537
 
538
+ function tryResolve(baseDir: string, name: string) {
539
+ try {
540
+ return require.resolve(name, {
541
+ paths: [baseDir],
542
+ });
543
+ } catch (ex) {
544
+ log('generalDebug_0003', `Could not resolve the package "${name}" in "${baseDir}": ${ex}`);
545
+ return undefined;
546
+ }
547
+ }
548
+
549
+ interface Importmap {
550
+ imports: Record<string, string>;
551
+ }
552
+
553
+ function normalizeDepName(s: string) {
554
+ return (s.startsWith('@') ? s.substr(1) : s).replace(/[\/\.]/g, '-').replace(/(\-)+/, '-');
555
+ }
556
+
557
+ async function resolveImportmap(dir: string, importmap: Importmap) {
558
+ const dependencies: Array<SharedDependency> = [];
559
+ const sharedImports = importmap?.imports;
560
+
561
+ if (typeof sharedImports === 'object' && sharedImports) {
562
+ for (const depName of Object.keys(sharedImports)) {
563
+ const url = sharedImports[depName];
564
+ const assetName = normalizeDepName(depName);
565
+
566
+ if (typeof url !== 'string') {
567
+ } else if (/^https?:\/\//.test(url)) {
568
+ const hash = await getHashFromUrl(url);
569
+
570
+ dependencies.push({
571
+ id: `${depName}@${hash}`,
572
+ entry: url,
573
+ name: depName,
574
+ ref: url,
575
+ type: 'remote',
576
+ });
577
+ } else if (url === depName) {
578
+ const entry = tryResolve(dir, depName);
579
+
580
+ if (entry) {
581
+ const packageJson = await findFile(dirname(entry), 'package.json');
582
+ const details = require(packageJson);
583
+
584
+ dependencies.push({
585
+ id: `${depName}@${details.version}`,
586
+ entry,
587
+ ref: `${assetName}.js`,
588
+ name: depName,
589
+ type: 'local',
590
+ });
591
+ }
592
+ } else {
593
+ const entry = resolve(dir, url);
594
+ const exists = await checkExists(entry);
595
+
596
+ if (exists) {
597
+ const packageJson = await findFile(dirname(entry), 'package.json');
598
+
599
+ if (packageJson) {
600
+ const details = require(packageJson);
601
+
602
+ dependencies.push({
603
+ id: `${depName}@${details.version}`,
604
+ entry,
605
+ name: depName,
606
+ ref: `${assetName}.js`,
607
+ type: 'local',
608
+ });
609
+ } else {
610
+ const hash = await getHash(entry);
611
+
612
+ dependencies.push({
613
+ id: `${depName}@${hash}`,
614
+ entry,
615
+ name: depName,
616
+ ref: `${assetName}.js`,
617
+ type: 'local',
618
+ });
619
+ }
620
+ }
621
+ }
622
+ }
623
+ }
624
+
625
+ return dependencies;
626
+ }
627
+
628
+ export async function readImportmap(dir: string, packageDetails: any) {
629
+ const importmap = packageDetails.importmap;
630
+
631
+ if (typeof importmap === 'string') {
632
+ const content = await readJson(dir, importmap);
633
+ const baseDir = dirname(resolve(dir, importmap));
634
+ return resolveImportmap(baseDir, content);
635
+ }
636
+
637
+ return resolveImportmap(dir, importmap);
638
+ }
639
+
531
640
  export async function retrievePiletData(target: string, app?: string) {
532
641
  const packageJson = await findFile(target, 'package.json');
533
642
 
@@ -548,6 +657,7 @@ export async function retrievePiletData(target: string, app?: string) {
548
657
  }
549
658
 
550
659
  const emulator = checkAppShellPackage(appPackage);
660
+ const importmap = await readImportmap(root, piletPackage);
551
661
 
552
662
  return {
553
663
  dependencies: piletPackage.dependencies || {},
@@ -555,6 +665,7 @@ export async function retrievePiletData(target: string, app?: string) {
555
665
  peerDependencies: piletPackage.peerDependencies || {},
556
666
  peerModules: piletPackage.peerModules || [],
557
667
  ignored: checkArrayOrUndefined(piletPackage, 'preservedDependencies'),
668
+ importmap,
558
669
  appFile,
559
670
  piletPackage,
560
671
  appPackage,
package/src/helpers.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { ForceOverwrite, SourceLanguage } from './common/enums';
2
2
  import { Framework, NpmClientType, PiletSchemaVersion, PiletPublishSource, PiralBuildType } from './types';
3
3
 
4
- export const schemaKeys: Array<PiletSchemaVersion> = ['v0', 'v1', 'none'];
4
+ export const schemaKeys: Array<PiletSchemaVersion> = ['v0', 'v1', 'v2', 'none'];
5
5
  export const fromKeys: Array<PiletPublishSource> = ['local', 'remote', 'npm'];
6
6
  export const buildTypeKeys: Array<PiralBuildType> = ['all', 'release', 'emulator', 'emulator-sources'];
7
7
  export const clientTypeKeys: Array<NpmClientType> = ['npm', 'pnpm', 'yarn'];
8
- export const bundlerKeys: Array<string> = ['none', 'parcel', 'webpack', 'webpack5'];
8
+ export const bundlerKeys: Array<string> = ['none', 'parcel', 'webpack', 'webpack5', 'esbuild'];
9
9
  export const availableBundlers: Array<string> = [];
10
10
  export const availableReleaseProviders: Array<string> = [];
11
11
  export const frameworkKeys: Array<Framework> = ['piral', 'piral-core', 'piral-base'];
@@ -1,15 +1,17 @@
1
+ import { URL } from 'url';
1
2
  import { join } from 'path';
2
3
  import { EventEmitter } from 'events';
3
4
  import { readFileSync, existsSync, statSync } from 'fs';
4
5
  import { KrasInjector, KrasResponse, KrasRequest, KrasInjectorConfig, KrasConfiguration, KrasResult } from 'kras';
6
+ import { computeHash, computeIntegrity } from '../common/hash';
7
+ import { log } from '../common/log';
5
8
  import { axios, mime } from '../external';
6
9
  import { Bundler } from '../types';
7
- import { log } from '../common/log';
8
10
 
9
11
  interface Pilet {
10
12
  bundler: Bundler;
11
13
  root: string;
12
- requireRef?: string;
14
+ meta: PiletMetadata;
13
15
  }
14
16
 
15
17
  export interface PiletInjectorConfig extends KrasInjectorConfig {
@@ -24,6 +26,80 @@ interface PiletMetadata {
24
26
  [key: string]: unknown;
25
27
  }
26
28
 
29
+ const checkV1 = /^\/\/\s*@pilet\s+v:1\s*\(([A-Za-z0-9\_\:\-]+)\)/;
30
+ const checkV2 = /^\/\/\s*@pilet\s+v:2\s*(?:\(([A-Za-z0-9\_\:\-]+),\s*(.*)\))?/;
31
+
32
+ function getDependencies(deps: string, basePath: string) {
33
+ try {
34
+ const depMap = JSON.parse(deps);
35
+
36
+ if (depMap && typeof depMap === 'object') {
37
+ return Object.keys(depMap).reduce((obj, depName) => {
38
+ const depUrl = depMap[depName];
39
+
40
+ if (typeof depUrl === 'string') {
41
+ const url = new URL(depUrl, basePath);
42
+ obj[depName] = url.href;
43
+ }
44
+
45
+ return obj;
46
+ }, {});
47
+ }
48
+ } catch {}
49
+
50
+ return {};
51
+ }
52
+
53
+ function getPiletSpecMeta(target: string, basePath: string) {
54
+ if (existsSync(target) && statSync(target).isFile()) {
55
+ const content = readFileSync(target, 'utf8');
56
+
57
+ if (checkV1.test(content)) {
58
+ // uses single argument; requireRef (required)
59
+ const [, requireRef] = checkV1.exec(content);
60
+ return {
61
+ spec: 'v1',
62
+ requireRef,
63
+ integrity: computeIntegrity(content),
64
+ };
65
+ } else if (checkV2.test(content)) {
66
+ // uses two arguments; requireRef and dependencies as JSON (required)
67
+ const [, requireRef, plainDependencies] = checkV2.exec(content);
68
+ return {
69
+ spec: 'v2',
70
+ requireRef,
71
+ dependencies: getDependencies(plainDependencies, basePath),
72
+ };
73
+ } else {
74
+ return {
75
+ spec: 'v0',
76
+ hash: computeHash(content),
77
+ noCache: true,
78
+ };
79
+ }
80
+ }
81
+
82
+ return {};
83
+ }
84
+
85
+ function fillPiletMeta(pilet: Pilet, basePath: string) {
86
+ const { root, bundler } = pilet;
87
+ const def = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
88
+ const file = bundler.bundle.name.replace(/^[\/\\]/, '');
89
+ const target = join(bundler.bundle.dir, file);
90
+ const url = new URL(file, basePath);
91
+ const meta = {
92
+ name: def.name,
93
+ version: def.version,
94
+ link: `${url.href}?updated=${Date.now()}`,
95
+ custom: def.custom,
96
+ ...getPiletSpecMeta(target, basePath),
97
+ };
98
+
99
+ pilet.meta = meta;
100
+ return JSON.stringify(meta);
101
+ }
102
+
27
103
  export default class PiletInjector implements KrasInjector {
28
104
  public config: PiletInjectorConfig;
29
105
  private piletApi: string;
@@ -49,9 +125,9 @@ export default class PiletInjector implements KrasInjector {
49
125
  });
50
126
 
51
127
  pilets.forEach((p, i) =>
52
- p.bundler.on(({ requireRef, version }) => {
53
- p.requireRef = version === 'v1' ? requireRef : undefined;
54
- const meta = JSON.stringify(this.getMetaOf(i));
128
+ p.bundler.on(() => {
129
+ const basePath = `${this.piletApi}/${i}/`;
130
+ const meta = fillPiletMeta(p, basePath);
55
131
 
56
132
  for (const id of Object.keys(cbs)) {
57
133
  cbs[id](meta);
@@ -77,25 +153,9 @@ export default class PiletInjector implements KrasInjector {
77
153
 
78
154
  setOptions() {}
79
155
 
80
- getMetaOf(index: number) {
81
- const { api, pilets } = this.config;
82
- const { bundler, root, requireRef } = pilets[index];
83
- const def = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
84
- const file = bundler.bundle.name.replace(/^\//, '');
85
- return {
86
- name: def.name,
87
- version: def.version,
88
- link: `${this.piletApi}/${index}/${file}`,
89
- hash: bundler.bundle.hash,
90
- requireRef,
91
- noCache: true,
92
- custom: def.custom,
93
- };
94
- }
95
-
96
156
  async getMeta() {
97
157
  const { pilets, feed } = this.config;
98
- const localPilets = pilets.map((_, i) => this.getMetaOf(i));
158
+ const localPilets = pilets.map((pilet) => pilet.meta).filter(Boolean);
99
159
  const mergedPilets = this.mergePilets(localPilets, await this.loadRemoteFeed(feed));
100
160
 
101
161
  if (mergedPilets.length === 1) {
@@ -1,5 +1,6 @@
1
- import PiralInjector, { PiralInjectorConfig } from './piral';
1
+ import PiralInjector from './piral';
2
2
  import { KrasRequest, KrasResult } from 'kras';
3
+ import { EventEmitter } from 'events';
3
4
 
4
5
  const bundlerMock = {
5
6
  pending: true,
@@ -36,7 +37,7 @@ describe('Piral-CLI piral injector', () => {
36
37
  bundler,
37
38
  active: true,
38
39
  };
39
- const injector = new PiralInjector(config);
40
+ const injector = new PiralInjector(config, undefined, new EventEmitter());
40
41
  expect(injector.active).toBeTruthy();
41
42
  });
42
43
 
@@ -46,7 +47,7 @@ describe('Piral-CLI piral injector', () => {
46
47
  bundler: bundlerMock,
47
48
  active: true,
48
49
  };
49
- const injector = new PiralInjector(config);
50
+ const injector = new PiralInjector(config, undefined, new EventEmitter());
50
51
 
51
52
  // Act
52
53
  injector.active = false;
@@ -64,7 +65,7 @@ describe('Piral-CLI piral injector', () => {
64
65
  bundler: bundlerMock,
65
66
  active: true,
66
67
  };
67
- const injector = new PiralInjector(config);
68
+ const injector = new PiralInjector(config, undefined, new EventEmitter());
68
69
 
69
70
  // Act
70
71
  const res = injector.sendResponse('some/nice/invalid/path', 'sometarget.file', 'someDir', 'localhost:1234');
@@ -79,7 +80,7 @@ describe('Piral-CLI piral injector', () => {
79
80
  bundler: bundlerMock,
80
81
  active: true,
81
82
  };
82
- const injector = new PiralInjector(config);
83
+ const injector = new PiralInjector(config, undefined, new EventEmitter());
83
84
  const request: KrasRequest = {
84
85
  content: 'someFakeContent',
85
86
  headers: {},
@@ -1,6 +1,7 @@
1
1
  import { join } from 'path';
2
+ import { EventEmitter } from 'events';
2
3
  import { readFileSync, existsSync, statSync } from 'fs';
3
- import { KrasInjector, KrasResponse, KrasRequest, KrasInjectorConfig } from 'kras';
4
+ import { KrasInjector, KrasResponse, KrasRequest, KrasInjectorConfig, KrasConfiguration } from 'kras';
4
5
  import { mime } from '../external';
5
6
  import { Bundler } from '../types';
6
7
 
@@ -16,8 +17,26 @@ export interface PiralInjectorConfig extends KrasInjectorConfig {
16
17
  export default class PiralInjector implements KrasInjector {
17
18
  public config: PiralInjectorConfig;
18
19
 
19
- constructor(options: PiralInjectorConfig) {
20
+ constructor(options: PiralInjectorConfig, _config: KrasConfiguration, core: EventEmitter) {
20
21
  this.config = options;
22
+ const api = '/$events';
23
+ const cbs = {};
24
+
25
+ core.on('user-connected', (e) => {
26
+ if (e.target === '*' && e.url === api.substr(1)) {
27
+ cbs[e.id] = (msg: string) => e.ws.send(msg);
28
+ }
29
+ });
30
+
31
+ core.on('user-disconnected', (e) => {
32
+ delete cbs[e.id];
33
+ });
34
+
35
+ this.config.bundler.on((args) => {
36
+ for (const id of Object.keys(cbs)) {
37
+ cbs[id](JSON.stringify(args));
38
+ }
39
+ });
21
40
  }
22
41
 
23
42
  get active() {