piral-cli 0.15.0-beta.4549 → 0.15.0-beta.4579

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 (45) hide show
  1. package/lib/apps/build-pilet.js +5 -5
  2. package/lib/apps/build-pilet.js.map +1 -1
  3. package/lib/apps/debug-pilet.js +2 -1
  4. package/lib/apps/debug-pilet.js.map +1 -1
  5. package/lib/apps/publish-pilet.js +2 -2
  6. package/lib/apps/publish-pilet.js.map +1 -1
  7. package/lib/apps/validate-pilet.js +4 -14
  8. package/lib/apps/validate-pilet.js.map +1 -1
  9. package/lib/common/importmap.js.map +1 -1
  10. package/lib/common/inspect.d.ts +5 -5
  11. package/lib/common/inspect.js.map +1 -1
  12. package/lib/common/package.d.ts +12 -15
  13. package/lib/common/package.js +50 -29
  14. package/lib/common/package.js.map +1 -1
  15. package/lib/messages.d.ts +44 -0
  16. package/lib/messages.js +49 -2
  17. package/lib/messages.js.map +1 -1
  18. package/lib/rules/pilet-has-externals-as-peers.js +2 -1
  19. package/lib/rules/pilet-has-externals-as-peers.js.map +1 -1
  20. package/lib/rules/pilet-has-no-self-reference.js +1 -1
  21. package/lib/rules/pilet-has-no-self-reference.js.map +1 -1
  22. package/lib/rules/pilet-stays-small.js +1 -1
  23. package/lib/rules/pilet-stays-small.js.map +1 -1
  24. package/lib/rules/pilet-uses-latest-piral.js +1 -1
  25. package/lib/rules/pilet-uses-latest-piral.js.map +1 -1
  26. package/lib/types/common.d.ts +44 -7
  27. package/lib/types/common.js.map +1 -1
  28. package/lib/types/internal.d.ts +8 -20
  29. package/package.json +2 -2
  30. package/src/apps/build-pilet.ts +8 -6
  31. package/src/apps/debug-pilet.ts +2 -2
  32. package/src/apps/publish-pilet.ts +2 -2
  33. package/src/apps/validate-pilet.ts +5 -4
  34. package/src/common/importmap.ts +1 -6
  35. package/src/common/inspect.ts +5 -5
  36. package/src/common/package.test.ts +9 -2
  37. package/src/common/package.ts +80 -50
  38. package/src/messages.ts +47 -0
  39. package/src/rules/pilet-has-externals-as-peers.test.ts +2 -2
  40. package/src/rules/pilet-has-externals-as-peers.ts +2 -1
  41. package/src/rules/pilet-has-no-self-reference.ts +1 -1
  42. package/src/rules/pilet-stays-small.ts +1 -1
  43. package/src/rules/pilet-uses-latest-piral.ts +1 -1
  44. package/src/types/common.ts +51 -8
  45. package/src/types/internal.ts +11 -19
@@ -18,10 +18,21 @@ import {
18
18
  FileInfo,
19
19
  PiletsInfo,
20
20
  TemplateFileLocation,
21
- PackageData,
21
+ PiletPackageData,
22
+ PiralPackageData,
22
23
  SharedDependency,
24
+ PiletDefinition,
25
+ AppDefinition,
26
+ PiralInstancePackageData,
23
27
  } from '../types';
24
28
 
29
+ export interface PiralInstanceData {
30
+ packageName: Framework;
31
+ language: SourceLanguage;
32
+ reactVersion: number;
33
+ reactRouterVersion: number;
34
+ }
35
+
25
36
  async function appendBundler(devDependencies: Record<string, string>, bundler: string, proposedVersion: string) {
26
37
  if (bundler && bundler !== 'none') {
27
38
  if (isValidDependency(bundler)) {
@@ -140,49 +151,56 @@ export function getPiralPath(root: string, name: string) {
140
151
  return dirname(path);
141
152
  }
142
153
 
143
- function findPackage(pck: string | Array<string>, baseDir: string) {
144
- if (Array.isArray(pck)) {
145
- for (const item of pck) {
146
- const result = findPackage(item, baseDir);
147
-
148
- if (result) {
149
- return result;
150
- }
154
+ function findPiralInstances(
155
+ proposedApps: Array<string>,
156
+ piletPackage: PiletPackageData,
157
+ piletDefinition: undefined | PiletDefinition,
158
+ baseDir: string,
159
+ ): Array<PiralInstancePackageData> {
160
+ if (proposedApps) {
161
+ // do nothing
162
+ } else if (piletDefinition) {
163
+ const availableApps = Object.keys(piletDefinition.piralInstances || {});
164
+ proposedApps = availableApps.filter((m) => piletDefinition.piralInstances[m].selected);
165
+
166
+ if (proposedApps.length === 0) {
167
+ proposedApps = availableApps.slice(0, 1);
151
168
  }
152
169
  } else {
153
- const path = findPackageRoot(pck, baseDir);
154
-
155
- if (path) {
156
- log('generalDebug_0003', `Following the app package in "${path}" ...`);
157
- const appPackage = require(path);
158
- const root = dirname(path);
159
- const relPath = appPackage && appPackage.app;
160
- appPackage.app = relPath && resolve(root, relPath);
161
- appPackage.root = root;
162
- return appPackage;
163
- }
170
+ proposedApps = [piletPackage.piral?.name].filter(Boolean);
164
171
  }
165
172
 
166
- return undefined;
173
+ if (proposedApps.length > 0) {
174
+ return proposedApps.map((proposedApp) => {
175
+ const path = findPackageRoot(proposedApp, baseDir);
176
+
177
+ if (path) {
178
+ log('generalDebug_0003', `Following the app package in "${path}" ...`);
179
+ const appPackage = require(path);
180
+ const root = dirname(path);
181
+ const relPath = appPackage && appPackage.app;
182
+ appPackage.app = relPath && resolve(root, relPath);
183
+ appPackage.root = root;
184
+ return appPackage;
185
+ }
186
+
187
+ fail('appInstanceNotFound_0010', proposedApp);
188
+ });
189
+ }
190
+
191
+ return [];
167
192
  }
168
193
 
169
- export function readPiralPackage(root: string, name: string): Promise<PackageData> {
194
+ export function readPiralPackage(root: string, name: string): Promise<PiralPackageData> {
170
195
  log('generalDebug_0003', `Reading the piral package in "${root}" ...`);
171
196
  const path = getPiralPath(root, name);
172
197
  return readJson(path, 'package.json');
173
198
  }
174
199
 
175
- export interface PiralPackageData {
176
- packageName: Framework;
177
- language: SourceLanguage;
178
- reactVersion: number;
179
- reactRouterVersion: number;
180
- }
181
-
182
200
  export async function patchPiralPackage(
183
201
  root: string,
184
202
  app: string,
185
- data: PiralPackageData,
203
+ data: PiralInstanceData,
186
204
  version: string,
187
205
  bundler?: string,
188
206
  ) {
@@ -196,7 +214,7 @@ export async function patchPiralPackage(
196
214
  log('generalDebug_0003', `Succesfully patched the pilet.json.`);
197
215
  }
198
216
 
199
- export async function getPiralPackage(app: string, data: PiralPackageData, version: string, bundler?: string) {
217
+ export async function getPiralPackage(app: string, data: PiralInstanceData, version: string, bundler?: string) {
200
218
  const framework = data.packageName;
201
219
  const devDependencies = {
202
220
  ...getDevDependencies(
@@ -359,7 +377,7 @@ function tryFindPackageVersion(packageName: string): string {
359
377
  export async function copyPiralFiles(
360
378
  root: string,
361
379
  name: string,
362
- piralInfo: PackageData,
380
+ piralInfo: PiralPackageData,
363
381
  forceOverwrite: ForceOverwrite,
364
382
  variables: Record<string, string>,
365
383
  originalFiles?: Array<FileInfo>,
@@ -379,7 +397,7 @@ export async function copyPiralFiles(
379
397
  await copyFiles(files, forceOverwrite, originalFiles, variables);
380
398
  }
381
399
 
382
- export function getPiletsInfo(piralInfo: Partial<PackageData>): PiletsInfo {
400
+ export function getPiletsInfo(piralInfo: Partial<PiralPackageData>): PiletsInfo {
383
401
  const {
384
402
  files = [],
385
403
  scripts = {},
@@ -538,7 +556,7 @@ export async function patchPiletPackage(
538
556
  root: string,
539
557
  name: string,
540
558
  version: string,
541
- piralInfo: PackageData,
559
+ piralInfo: PiralPackageData,
542
560
  fromEmulator: boolean,
543
561
  newInfo?: { language: SourceLanguage; bundler: string },
544
562
  ) {
@@ -560,7 +578,7 @@ export async function getPiletPackage(
560
578
  root: string,
561
579
  name: string,
562
580
  version: string,
563
- piralInfo: PackageData,
581
+ piralInfo: PiralPackageData,
564
582
  fromEmulator: boolean,
565
583
  newInfo?: { language: SourceLanguage; bundler: string },
566
584
  ) {
@@ -624,7 +642,7 @@ export async function getPiletPackage(
624
642
  /**
625
643
  * Returns true if its an emulator package, otherwise it has to be a "raw" app shell.
626
644
  */
627
- export function checkAppShellPackage(appPackage: PackageData) {
645
+ export function checkAppShellPackage(appPackage: PiralPackageData) {
628
646
  const { piralCLI = { generated: false, version: cliVersion } } = appPackage;
629
647
 
630
648
  if (piralCLI.generated) {
@@ -665,7 +683,9 @@ export function combinePiletExternals(
665
683
  }
666
684
 
667
685
  export async function retrievePiletData(target: string, app?: string) {
668
- const packageJson = await findFile(target, 'package.json');
686
+ const piletJson = await findFile(target, 'pilet.json');
687
+ const proposedRoot = piletJson ? dirname(piletJson) : target;
688
+ const packageJson = await findFile(proposedRoot, 'package.json');
669
689
 
670
690
  if (!packageJson) {
671
691
  fail('packageJsonMissing_0075');
@@ -673,18 +693,31 @@ export async function retrievePiletData(target: string, app?: string) {
673
693
 
674
694
  const root = dirname(packageJson);
675
695
  const piletPackage = require(packageJson);
676
- const appPackage = findPackage(
677
- app || (piletPackage.piral && piletPackage.piral.name) || Object.keys(piletPackage.devDependencies),
678
- target,
679
- );
680
- const appFile: string = appPackage?.app;
681
- const appRoot: string = appPackage?.root;
696
+ const piletDefinition = piletJson && require(piletJson);
697
+ const appPackages = findPiralInstances(app && [app], piletPackage, piletDefinition, target);
698
+ const apps: Array<AppDefinition> = [];
699
+
700
+ if (appPackages.length === 0) {
701
+ fail('appInstancesNotGiven_0012');
702
+ }
703
+
704
+ for (const appPackage of appPackages) {
705
+ const appFile: string = appPackage?.app;
706
+ const appRoot: string = appPackage?.root;
682
707
 
683
- if (!appFile || !appRoot) {
684
- fail('appInstanceInvalid_0011');
708
+ if (!appFile || !appRoot) {
709
+ fail('appInstanceInvalid_0011');
710
+ }
711
+
712
+ const emulator = checkAppShellPackage(appPackage);
713
+ apps.push({
714
+ appPackage,
715
+ appFile,
716
+ appRoot,
717
+ emulator,
718
+ });
685
719
  }
686
720
 
687
- const emulator = checkAppShellPackage(appPackage);
688
721
  const importmap = await readImportmap(root, piletPackage);
689
722
 
690
723
  return {
@@ -694,11 +727,8 @@ export async function retrievePiletData(target: string, app?: string) {
694
727
  peerModules: piletPackage.peerModules || [],
695
728
  ignored: checkArrayOrUndefined(piletPackage, 'preservedDependencies'),
696
729
  importmap,
697
- appFile,
698
- appRoot,
730
+ apps,
699
731
  piletPackage,
700
- appPackage,
701
- emulator,
702
732
  root,
703
733
  };
704
734
  }
package/src/messages.ts CHANGED
@@ -180,6 +180,53 @@ export function appInstanceInvalid_0011(): QuickMessage {
180
180
  return [LogLevels.error, '0011', `Could not find a valid Piral instance.`];
181
181
  }
182
182
 
183
+ /**
184
+ * @kind Error
185
+ *
186
+ * @summary
187
+ * Reported when no valid Piral instance was specified.
188
+ *
189
+ * @abstract
190
+ * The Piral instance is defined either in the package.json or in the pilet.json.
191
+ *
192
+ * The resolution of the Piral instance is done via the `require.resolve` function of Node.js. Thus, if the defined module is simply not yet installed this error will be shown.
193
+ *
194
+ * @see
195
+ * - [npm i](https://docs.npmjs.com/cli/install)
196
+ * - [npm install is missing modules](https://stackoverflow.com/questions/24652681/npm-install-is-missing-modules)
197
+ *
198
+ * @example
199
+ * Assuming that the available pilet.json of your pilet contains content such as:
200
+ *
201
+ * ```json
202
+ * {
203
+ * // ...
204
+ * "piralInstances": {
205
+ * "my-app-shell": {}
206
+ * }
207
+ * }
208
+ * ```
209
+ *
210
+ * However, running
211
+ *
212
+ * ```sh
213
+ * ls node_modules/my-app-shell
214
+ * ```
215
+ *
216
+ * returns an error.
217
+ *
218
+ * To mitigate it try running
219
+ *
220
+ * ```sh
221
+ * npm i
222
+ * ```
223
+ *
224
+ * which will install all dependencies.
225
+ */
226
+ export function appInstancesNotGiven_0012(): QuickMessage {
227
+ return [LogLevels.error, '0012', `No Piral instances have been provided.`];
228
+ }
229
+
183
230
  /**
184
231
  * @kind Error
185
232
  *
@@ -38,13 +38,13 @@ describe('Rule pilet-has-externals-as-peers', () => {
38
38
 
39
39
  function createContext(externals: string[]): PiletRuleContext {
40
40
  return {
41
- data: {
41
+ apps: [{
42
42
  appPackage: {
43
43
  pilets: {
44
44
  externals,
45
45
  },
46
46
  },
47
- },
47
+ }],
48
48
  peerDependencies,
49
49
  peerModules,
50
50
  entry: 'test-entry',
@@ -21,7 +21,8 @@ Received: Missing "${missingNames.join('", "')}".
21
21
  */
22
22
  export default async function (context: PiletRuleContext, options: Options = 'ignore') {
23
23
  if (options !== 'ignore') {
24
- const externals = await retrieveExternals(context.data.appRoot, context.data.appPackage);
24
+ const [app] = context.apps;
25
+ const externals = await retrieveExternals(app.appRoot, app.appPackage);
25
26
  const markedPeerDependencies = Object.keys(context.peerDependencies);
26
27
  const markedPeerModules = context.peerModules;
27
28
  const missingExternals = externals
@@ -8,7 +8,7 @@ export type Options = 'ignore' | 'active';
8
8
  */
9
9
  export default async function (context: PiletRuleContext, options: Options = 'ignore') {
10
10
  if (options !== 'ignore') {
11
- const { name } = context.data.appPackage;
11
+ const { name } = context.apps[0].appPackage;
12
12
  const names = ['piral', 'piral-core', 'piral-base', name];
13
13
  const files = await getSourceFiles(context.entry);
14
14
  const testers: Array<RegExp> = [];
@@ -50,7 +50,7 @@ function getFileSizeInKB(path: string) {
50
50
  export default async function (context: PiletRuleContext, options: Options = -50) {
51
51
  if (options !== 0 && typeof options === 'number') {
52
52
  const maxSize = Math.abs(options);
53
- const { main } = context.data.piletPackage;
53
+ const { main } = context.piletPackage;
54
54
  const path = await getPiletMainPath(main, context.root);
55
55
 
56
56
  if (path) {
@@ -8,7 +8,7 @@ export type Options = 'suggest' | 'required' | 'ignore';
8
8
  */
9
9
  export default async function (context: PiletRuleContext, options: Options = 'suggest') {
10
10
  if (options !== 'ignore') {
11
- const { name, version } = context.data.appPackage;
11
+ const { name, version } = context.apps[0].appPackage;
12
12
  const demanded = (context.devDependencies && context.devDependencies[name]) || '';
13
13
  const isfixed = demanded.startsWith('git+') || demanded.startsWith('file:');
14
14
 
@@ -1,3 +1,52 @@
1
+ export interface Importmap {
2
+ imports: Record<string, string>;
3
+ inherit: Array<string>;
4
+ }
5
+
6
+ export interface PackageData {
7
+ name: string;
8
+ version: string;
9
+ description: string;
10
+ importmap?: Importmap;
11
+ main: string;
12
+ author:
13
+ | string
14
+ | {
15
+ name?: string;
16
+ url?: string;
17
+ email?: string;
18
+ };
19
+ dependencies: Record<string, string>;
20
+ peerDependencies: Record<string, string>;
21
+ devDependencies: Record<string, string>;
22
+ }
23
+
24
+ // Shape of the package.json of a pilet
25
+ export interface PiletPackageData extends PackageData {
26
+ piral?: {
27
+ name: string;
28
+ };
29
+ custom?: any;
30
+ }
31
+
32
+ // Shape of the package.json of a Piral instance or emulator
33
+ export interface PiralPackageData extends PackageData {
34
+ pilets?: PiletsInfo;
35
+ piralCLI?: { generated: boolean; version: string };
36
+ }
37
+
38
+ export interface PiralInstancePackageData extends PiralPackageData {
39
+ root: string;
40
+ app: string;
41
+ }
42
+
43
+ export interface AppDefinition {
44
+ appPackage: PiralInstancePackageData;
45
+ appFile: string;
46
+ appRoot: string;
47
+ emulator: boolean;
48
+ }
49
+
1
50
  export enum LogLevels {
2
51
  /**
3
52
  * Logging disabled
@@ -101,14 +150,8 @@ export interface PiralRuleContext extends RuleContext {
101
150
  }
102
151
 
103
152
  export interface PiletRuleContext extends RuleContext {
104
- data: PiralData;
153
+ apps: Array<AppDefinition>;
154
+ piletPackage: any;
105
155
  peerModules: Array<string>;
106
156
  importmap: Array<SharedDependency>;
107
157
  }
108
-
109
- export interface PiralData {
110
- appRoot: string;
111
- appFile: string;
112
- appPackage: any;
113
- piletPackage: any;
114
- }
@@ -1,23 +1,15 @@
1
- import type { LogLevels, PiletsInfo } from './common';
1
+ import type { LogLevels } from './common';
2
2
 
3
- export interface PackageData {
4
- name: string;
5
- version: string;
6
- description: string;
7
- main: string;
8
- author:
9
- | string
10
- | {
11
- name?: string;
12
- url?: string;
13
- email?: string;
14
- };
15
- custom?: any;
16
- pilets?: PiletsInfo;
17
- piralCLI?: { generated: boolean; version: string };
18
- dependencies: Record<string, string>;
19
- peerDependencies: Record<string, string>;
20
- devDependencies: Record<string, string>;
3
+ /**
4
+ * Shape of the pilet.json
5
+ */
6
+ export interface PiletDefinition {
7
+ piralInstances?: Record<
8
+ string,
9
+ {
10
+ selected?: boolean;
11
+ }
12
+ >;
21
13
  }
22
14
 
23
15
  export interface PackageFiles {