piral-cli 0.15.0-alpha.4396 → 0.15.0-beta.4411

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 (75) hide show
  1. package/lib/apps/build-pilet.js +3 -2
  2. package/lib/apps/build-pilet.js.map +1 -1
  3. package/lib/apps/debug-pilet.js +3 -2
  4. package/lib/apps/debug-pilet.js.map +1 -1
  5. package/lib/apps/new-pilet.js +3 -0
  6. package/lib/apps/new-pilet.js.map +1 -1
  7. package/lib/apps/new-piral.js +4 -0
  8. package/lib/apps/new-piral.js.map +1 -1
  9. package/lib/apps/publish-pilet.js +3 -2
  10. package/lib/apps/publish-pilet.js.map +1 -1
  11. package/lib/cli.js +4 -2
  12. package/lib/cli.js.map +1 -1
  13. package/lib/common/archive.d.ts +4 -0
  14. package/lib/common/archive.js +23 -2
  15. package/lib/common/archive.js.map +1 -1
  16. package/lib/common/clients/index.js +13 -7
  17. package/lib/common/clients/index.js.map +1 -1
  18. package/lib/common/clients/lerna.d.ts +1 -1
  19. package/lib/common/clients/lerna.js +2 -2
  20. package/lib/common/clients/lerna.js.map +1 -1
  21. package/lib/common/clients/npm.d.ts +1 -1
  22. package/lib/common/clients/npm.js +2 -2
  23. package/lib/common/clients/npm.js.map +1 -1
  24. package/lib/common/clients/pnp.d.ts +1 -1
  25. package/lib/common/clients/pnp.js +2 -2
  26. package/lib/common/clients/pnp.js.map +1 -1
  27. package/lib/common/clients/pnpm.d.ts +1 -1
  28. package/lib/common/clients/pnpm.js +2 -2
  29. package/lib/common/clients/pnpm.js.map +1 -1
  30. package/lib/common/clients/rush.d.ts +1 -1
  31. package/lib/common/clients/rush.js +2 -2
  32. package/lib/common/clients/rush.js.map +1 -1
  33. package/lib/common/clients/yarn.d.ts +1 -1
  34. package/lib/common/clients/yarn.js +2 -2
  35. package/lib/common/clients/yarn.js.map +1 -1
  36. package/lib/common/emoji.js +8 -8
  37. package/lib/common/emoji.js.map +1 -1
  38. package/lib/common/importmap.js +4 -1
  39. package/lib/common/importmap.js.map +1 -1
  40. package/lib/common/inspect.d.ts +3 -1
  41. package/lib/common/inspect.js +9 -10
  42. package/lib/common/inspect.js.map +1 -1
  43. package/lib/common/io.d.ts +2 -1
  44. package/lib/common/io.js +28 -16
  45. package/lib/common/io.js.map +1 -1
  46. package/lib/common/pack.js +27 -17
  47. package/lib/common/pack.js.map +1 -1
  48. package/lib/common/package.d.ts +3 -3
  49. package/lib/common/package.js +33 -21
  50. package/lib/common/package.js.map +1 -1
  51. package/lib/external/index.js +79 -79
  52. package/lib/types/common.d.ts +1 -0
  53. package/package.json +2 -2
  54. package/src/apps/build-pilet.ts +4 -2
  55. package/src/apps/debug-pilet.ts +4 -2
  56. package/src/apps/new-pilet.ts +8 -0
  57. package/src/apps/new-piral.ts +12 -0
  58. package/src/apps/publish-pilet.ts +4 -2
  59. package/src/cli.ts +6 -3
  60. package/src/common/archive.ts +28 -1
  61. package/src/common/clients/index.ts +9 -3
  62. package/src/common/clients/lerna.ts +2 -2
  63. package/src/common/clients/npm.ts +2 -2
  64. package/src/common/clients/pnp.ts +2 -2
  65. package/src/common/clients/pnpm.ts +2 -2
  66. package/src/common/clients/rush.ts +2 -2
  67. package/src/common/clients/yarn.ts +2 -2
  68. package/src/common/emoji.ts +8 -8
  69. package/src/common/importmap.ts +6 -1
  70. package/src/common/inspect.ts +8 -10
  71. package/src/common/io.ts +19 -7
  72. package/src/common/pack.test.ts +10 -13
  73. package/src/common/pack.ts +43 -21
  74. package/src/common/package.ts +50 -26
  75. package/src/types/common.ts +1 -0
package/src/common/io.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import * as rimraf from 'rimraf';
2
2
  import { transpileModule, ModuleKind, ModuleResolutionKind, ScriptTarget, JsxEmit } from 'typescript';
3
3
  import { join, resolve, basename, dirname, extname, isAbsolute, sep } from 'path';
4
- import { exists, mkdir, lstat, unlink, mkdirSync, statSync } from 'fs';
5
- import { writeFile, readFile, readdir, copyFile, constants } from 'fs';
4
+ import { exists, lstat, unlink, statSync } from 'fs';
5
+ import { mkdtemp, mkdir, mkdirSync, constants } from 'fs';
6
+ import { writeFile, readFile, readdir, copyFile } from 'fs';
6
7
  import { log } from './log';
7
8
  import { deepMerge } from './merge';
8
9
  import { nodeVersion } from './info';
@@ -135,6 +136,18 @@ export async function getEntryFiles(content: string, basePath: string) {
135
136
  return results;
136
137
  }
137
138
 
139
+ export function makeTempDir(prefix: string) {
140
+ return new Promise<string>((resolve, reject) =>
141
+ mkdtemp(prefix, (err, folder) => {
142
+ if (err) {
143
+ reject(err);
144
+ } else {
145
+ resolve(folder);
146
+ }
147
+ }),
148
+ );
149
+ }
150
+
138
151
  export function checkExists(target: string) {
139
152
  return new Promise<boolean>((resolve) => {
140
153
  if (target !== undefined) {
@@ -174,15 +187,14 @@ export function getFileNames(target: string) {
174
187
  });
175
188
  }
176
189
 
177
- export async function findFile(topDir: string, fileName: string): Promise<string> {
190
+ export async function findFile(topDir: string, fileName: string, stopDir = resolve(topDir, '/')): Promise<string> {
178
191
  const path = join(topDir, fileName);
179
192
  const exists = await checkExists(path);
180
193
 
181
194
  if (!exists) {
182
- const parentDir = resolve(topDir, '..');
183
-
184
- if (parentDir !== topDir) {
185
- return await findFile(parentDir, fileName);
195
+ if (topDir !== stopDir) {
196
+ const parentDir = resolve(topDir, '..');
197
+ return await findFile(parentDir, fileName, stopDir);
186
198
  }
187
199
 
188
200
  return undefined;
@@ -1,21 +1,18 @@
1
1
  import { createPiletPackage } from './pack';
2
2
  import { resolve } from 'path';
3
3
 
4
- let json = {};
4
+ let json: any = {};
5
5
 
6
6
  jest.mock('./io', () => ({
7
- readJson: (dir: string, source: string) => {
8
- return json;
9
- },
10
- move: (source: string, target: string, forceOverwrite?: any) => {
11
- return Promise.resolve('foo');
12
- },
7
+ readJson: jest.fn(() => json),
8
+ removeDirectory: jest.fn(() => Promise.resolve()),
9
+ makeTempDir: jest.fn(() => Promise.resolve('')),
10
+ copy: jest.fn(() => Promise.resolve()),
11
+ checkExists: jest.fn(() => Promise.resolve(true)),
13
12
  }));
14
13
 
15
- jest.mock('./npm', () => ({
16
- createNpmPackage: (target?: string) => {
17
- return Promise.resolve(target);
18
- },
14
+ jest.mock('./archive', () => ({
15
+ createTgz: jest.fn(() => Promise.resolve()),
19
16
  }));
20
17
 
21
18
  describe('Pack Module', () => {
@@ -51,7 +48,7 @@ describe('Pack Module', () => {
51
48
 
52
49
  it('createPilePackage source <> target', async () => {
53
50
  json = { name: 'foo', version: '1.0.0' };
54
- const path = resolve('./', 'foo-1.0.0.tgz');
55
- await expect(createPiletPackage('./', '', 'test')).resolves.toEqual('foo');
51
+ const path = resolve('./', 'test', 'foo-1.0.0.tgz');
52
+ await expect(createPiletPackage('./', '', 'test')).resolves.toEqual(path);
56
53
  });
57
54
  });
@@ -1,20 +1,22 @@
1
- import { resolve, join } from 'path';
1
+ import { tmpdir } from 'os';
2
+ import { resolve, relative, join, dirname, basename } from 'path';
3
+ import { createTgz } from './archive';
2
4
  import { log, progress, fail } from './log';
3
- import { readJson, move } from './io';
4
- import { createNpmPackage } from './npm';
5
- import { ForceOverwrite } from './enums';
6
-
7
- async function getFile(root: string, name: string, dest: string) {
8
- const proposed = join(root, name);
9
-
10
- if (dest !== root) {
11
- log('generalDebug_0003', `Moving file from "${root}" to "${dest}" ...`);
12
- const file = await move(proposed, dest, ForceOverwrite.yes);
13
- log('generalDebug_0003', 'Successfully moved file.');
14
- return file;
5
+ import { readJson, copy, removeDirectory, checkExists, makeTempDir } from './io';
6
+ import { getPossiblePiletMainPaths } from './inspect';
7
+
8
+ async function getPiletContentDir(root: string, packageData: any) {
9
+ const paths = getPossiblePiletMainPaths(packageData);
10
+
11
+ for (const path of paths) {
12
+ const file = resolve(root, path);
13
+
14
+ if (await checkExists(file)) {
15
+ return dirname(file);
16
+ }
15
17
  }
16
18
 
17
- return proposed;
19
+ return root;
18
20
  }
19
21
 
20
22
  export async function createPiletPackage(baseDir: string, source: string, target: string) {
@@ -36,13 +38,33 @@ export async function createPiletPackage(baseDir: string, source: string, target
36
38
  fail('packageJsonMissingVersion_0022');
37
39
  }
38
40
 
39
- progress(`Packing pilet in ${dest} ...`);
40
- log('generalDebug_0003', 'Creating package ...');
41
- await createNpmPackage(root);
42
- log('generalDebug_0003', 'Successfully created package.');
43
- const name = `${pckg.name}-${pckg.version}.tgz`.replace(/@/g, '').replace(/\//g, '-');
44
- log('generalDebug_0003', `Assumed package name "${name}".`);
45
- const file = await getFile(root, name, dest);
41
+ const isFileTarget = target.endsWith('.tgz');
42
+ progress(`Packing pilet in "${root}" ...`);
43
+
44
+ const pckgName = pckg.name.replace(/@/g, '').replace(/\//g, '-');
45
+ const id = `${pckgName}-${pckg.version}`;
46
+ const name = isFileTarget ? basename(target) : `${id}.tgz`;
47
+ const file = isFileTarget ? dest : resolve(dest, name);
48
+ log('generalDebug_0003', `Assume package name "${name}".`);
49
+
50
+ const content = await getPiletContentDir(root, pckg);
51
+ const files = [resolve(root, 'package.json'), content];
52
+ const prefix = join(tmpdir(), `${id}-`);
53
+ const cwd = await makeTempDir(prefix);
54
+ log('generalDebug_0003', `Creating package with content from "${content}" ...`);
55
+
56
+ await Promise.all(files.map((file) => copy(file, resolve(cwd, relative(root, file)))));
57
+
58
+ await createTgz(
59
+ file,
60
+ cwd,
61
+ files.map((f) => relative(root, f)),
62
+ );
63
+
64
+ log('generalDebug_0003', `Successfully created package from "${cwd}".`);
65
+
66
+ await removeDirectory(cwd);
67
+
46
68
  log('generalDebug_0003', `Packed file "${file}".`);
47
69
  return file;
48
70
  }
@@ -12,7 +12,15 @@ import { getHash, checkIsDirectory, matchFiles } from './io';
12
12
  import { readJson, copy, updateExistingJson, findFile, checkExists } from './io';
13
13
  import { isGitPackage, isLocalPackage, makeGitUrl, makeFilePath } from './npm';
14
14
  import { makePiletExternals, makeExternals, findPackageRoot } from './npm';
15
- import { SourceLanguage, Framework, FileInfo, PiletsInfo, TemplateFileLocation, PackageData } from '../types';
15
+ import {
16
+ SourceLanguage,
17
+ Framework,
18
+ FileInfo,
19
+ PiletsInfo,
20
+ TemplateFileLocation,
21
+ PackageData,
22
+ SharedDependency,
23
+ } from '../types';
16
24
 
17
25
  function appendBundler(devDependencies: Record<string, string>, bundler: string, version: string) {
18
26
  if (bundler && bundler !== 'none') {
@@ -182,7 +190,6 @@ export function getPiralPackage(app: string, data: PiralPackageData, version: st
182
190
  framework === 'piral-native' && 'piral-native', // this we also only take if we selected piral-native
183
191
  ].filter(Boolean),
184
192
  },
185
- pilets: getPiletsInfo({}),
186
193
  dependencies,
187
194
  devDependencies,
188
195
  };
@@ -501,10 +508,6 @@ export async function patchPiletPackage(
501
508
  ) {
502
509
  log('generalDebug_0003', `Patching the package.json in "${root}" ...`);
503
510
  const { externals, packageOverrides, ...info } = getPiletsInfo(piralInfo);
504
- const piral = {
505
- comment: 'Keep this section to use the Piral CLI.',
506
- name,
507
- };
508
511
  const piralDependencies = {
509
512
  ...piralInfo.devDependencies,
510
513
  ...piralInfo.dependencies,
@@ -518,21 +521,7 @@ export async function patchPiletPackage(
518
521
  ...info.scripts,
519
522
  }
520
523
  : info.scripts;
521
- const peerModules = [];
522
524
  const allExternals = makePiletExternals(root, piralDependencies, externals, fromEmulator, piralInfo);
523
- const peerDependencies = {
524
- ...allExternals.reduce((deps, name) => {
525
- const valid = isValidDependency(name);
526
- deps[name] = valid ? '*' : undefined;
527
-
528
- if (!valid) {
529
- peerModules.push(name);
530
- }
531
-
532
- return deps;
533
- }, {}),
534
- [name]: `*`,
535
- };
536
525
  const devDependencies: Record<string, string> = {
537
526
  ...Object.keys(typeDependencies).reduce((deps, name) => {
538
527
  deps[name] = piralDependencies[name] || typeDependencies[name];
@@ -562,19 +551,26 @@ export async function patchPiletPackage(
562
551
  appendBundler(devDependencies, bundler, version);
563
552
  }
564
553
 
565
- const packageContent = deepMerge(packageOverrides, {
566
- piral,
554
+ await updateExistingJson(root, 'package.json', deepMerge(packageOverrides, {
555
+ importmap: {
556
+ imports: {},
557
+ inherit: [name],
558
+ },
567
559
  devDependencies,
568
- peerDependencies,
569
- peerModules,
570
560
  dependencies: {
571
561
  [name]: undefined,
572
562
  },
573
563
  scripts,
574
- });
564
+ }));
575
565
 
576
- await updateExistingJson(root, 'package.json', packageContent);
577
566
  log('generalDebug_0003', `Succesfully patched the package.json.`);
567
+
568
+ await updateExistingJson(root, 'pilet.json', {
569
+ piralInstances: {
570
+ [name]: {},
571
+ },
572
+ });
573
+ log('generalDebug_0003', `Succesfully patched the pilet.json.`);
578
574
  }
579
575
 
580
576
  /**
@@ -592,6 +588,34 @@ export function checkAppShellPackage(appPackage: PackageData) {
592
588
  return false;
593
589
  }
594
590
 
591
+ export function combinePiletExternals(
592
+ appShells: Array<string>,
593
+ peerDependencies: Record<string, string>,
594
+ peerModules: Array<string>,
595
+ importmap: Array<SharedDependency>,
596
+ ) {
597
+ const externals = [...Object.keys(peerDependencies), ...peerModules];
598
+
599
+ for (let i = importmap.length; i--; ) {
600
+ const entry = importmap[i];
601
+
602
+ // if the entry has no parents, i.e., it was explicitly mentioned in the importmap
603
+ // then keep it in the importmap (=> prefer the distributed approach, which will always work)
604
+ if (Array.isArray(entry.parents)) {
605
+ // only accept entry as a centrally shared dependency if the entry appears in all
606
+ // mentioned / referenced app shells
607
+ // in other cases (e.g., if one app shell does not share this) use the distributed
608
+ // mechanism to ensure that the dependency can also be resolved in this shell
609
+ if (appShells.every((app) => entry.parents.includes(app))) {
610
+ externals.push(entry.name);
611
+ importmap.splice(i, 1);
612
+ }
613
+ }
614
+ }
615
+
616
+ return externals;
617
+ }
618
+
595
619
  export async function retrievePiletData(target: string, app?: string) {
596
620
  const packageJson = await findFile(target, 'package.json');
597
621
 
@@ -84,6 +84,7 @@ export interface SharedDependency {
84
84
  ref: string;
85
85
  type: 'local' | 'remote';
86
86
  entry: string;
87
+ parents?: Array<string>;
87
88
  }
88
89
 
89
90
  export interface RuleRunner<T extends RuleContext> {