piral-cli 0.15.0-alpha.4036 → 0.15.0-alpha.4122

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 (79) hide show
  1. package/lib/apps/build-pilet.js.map +1 -1
  2. package/lib/apps/build-piral.js.map +1 -1
  3. package/lib/apps/debug-pilet.js.map +1 -1
  4. package/lib/apps/debug-piral.js.map +1 -1
  5. package/lib/apps/index.js +5 -1
  6. package/lib/apps/index.js.map +1 -1
  7. package/lib/apps/new-pilet.js +4 -3
  8. package/lib/apps/new-pilet.js.map +1 -1
  9. package/lib/build/bundler-calls.d.ts +1 -1
  10. package/lib/build/run-build-pilet.js.map +1 -1
  11. package/lib/build/run-build-piral.js.map +1 -1
  12. package/lib/build/run-debug-mono-piral.js +1 -1
  13. package/lib/build/run-debug-mono-piral.js.map +1 -1
  14. package/lib/build/run-debug-pilet.js.map +1 -1
  15. package/lib/build/run-debug-piral.js.map +1 -1
  16. package/lib/common/clients/npm.js +10 -4
  17. package/lib/common/clients/npm.js.map +1 -1
  18. package/lib/common/clients/pnpm.js +10 -4
  19. package/lib/common/clients/pnpm.js.map +1 -1
  20. package/lib/common/clients/yarn.js +10 -4
  21. package/lib/common/clients/yarn.js.map +1 -1
  22. package/lib/common/config.js.map +1 -1
  23. package/lib/common/constants.d.ts +1 -1
  24. package/lib/common/constants.js +1 -1
  25. package/lib/common/constants.js.map +1 -1
  26. package/lib/common/declaration.js +2 -1
  27. package/lib/common/declaration.js.map +1 -1
  28. package/lib/common/importmap.js +26 -16
  29. package/lib/common/importmap.js.map +1 -1
  30. package/lib/common/index.js +5 -1
  31. package/lib/common/index.js.map +1 -1
  32. package/lib/common/npm.d.ts +2 -0
  33. package/lib/common/npm.js +56 -7
  34. package/lib/common/npm.js.map +1 -1
  35. package/lib/common/package.d.ts +7 -1
  36. package/lib/common/package.js +40 -33
  37. package/lib/common/package.js.map +1 -1
  38. package/lib/common/scaffold.js.map +1 -1
  39. package/lib/external/index.js +482 -462
  40. package/lib/helpers.d.ts +1 -1
  41. package/lib/index.js +5 -1
  42. package/lib/index.js.map +1 -1
  43. package/lib/injectors/pilet.d.ts +1 -0
  44. package/lib/injectors/pilet.js +5 -5
  45. package/lib/injectors/pilet.js.map +1 -1
  46. package/lib/messages.js +5 -1
  47. package/lib/messages.js.map +1 -1
  48. package/lib/rules/pilet-has-externals-as-peers.js +1 -1
  49. package/lib/rules/pilet-has-externals-as-peers.js.map +1 -1
  50. package/lib/types/common.d.ts +3 -1
  51. package/lib/types/index.js +5 -1
  52. package/lib/types/index.js.map +1 -1
  53. package/lib/types/public.d.ts +1 -1
  54. package/package.json +2 -2
  55. package/src/apps/new-pilet.ts +4 -3
  56. package/src/build/bundler-calls.ts +1 -1
  57. package/src/build/run-build-pilet.ts +1 -1
  58. package/src/build/run-build-piral.ts +1 -1
  59. package/src/build/run-debug-mono-piral.ts +2 -2
  60. package/src/build/run-debug-pilet.ts +1 -1
  61. package/src/build/run-debug-piral.ts +1 -1
  62. package/src/common/clients/npm.ts +12 -4
  63. package/src/common/clients/pnpm.ts +12 -4
  64. package/src/common/clients/yarn.ts +12 -4
  65. package/src/common/config.ts +3 -3
  66. package/src/common/constants.ts +1 -1
  67. package/src/common/declaration.ts +2 -1
  68. package/src/common/importmap.ts +31 -15
  69. package/src/common/npm.test.ts +13 -0
  70. package/src/common/npm.ts +58 -6
  71. package/src/common/package.test.ts +3 -16
  72. package/src/common/package.ts +40 -30
  73. package/src/common/scaffold.ts +1 -0
  74. package/src/helpers.ts +1 -1
  75. package/src/injectors/pilet.ts +6 -5
  76. package/src/messages.ts +5 -1
  77. package/src/rules/pilet-has-externals-as-peers.ts +2 -2
  78. package/src/types/common.ts +3 -1
  79. package/src/types/public.ts +1 -1
package/src/common/npm.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { resolve, relative, dirname } from 'path';
1
+ import { resolve, relative, dirname, isAbsolute, sep } from 'path';
2
2
  import { createReadStream, existsSync, access, constants } from 'fs';
3
3
  import { log, fail } from './log';
4
4
  import { config } from './config';
@@ -354,12 +354,63 @@ export async function getCurrentPackageDetails(
354
354
  return [combinePackageRef(sourceName, desired, 'registry'), desired];
355
355
  }
356
356
 
357
+ function tryResolve(...args: Parameters<typeof require.resolve>) {
358
+ try {
359
+ return require.resolve(...args);
360
+ } catch {
361
+ return undefined;
362
+ }
363
+ }
364
+
365
+ export function tryResolvePackage(name: string, baseDir: string = undefined) {
366
+ let path = baseDir ? tryResolve(name, { paths: [baseDir] }) : tryResolve(name);
367
+ const root = baseDir || process.cwd();
368
+
369
+ if (!path) {
370
+ if (name.startsWith('.')) {
371
+ path = resolve(root, name);
372
+ } else if (isAbsolute(name)) {
373
+ path = name;
374
+ } else if (name.includes('/', name.startsWith('@') ? name.indexOf('/') + 1 : 0)) {
375
+ const parts = name.split('/');
376
+ const mainPart = name.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
377
+ const mainPath = baseDir ? tryResolve(mainPart, { paths: [baseDir] }) : tryResolve(mainPart);
378
+ const searchStr = `${sep}${mainPart.replace('/', sep)}${sep}`;
379
+
380
+ if (mainPath?.includes(searchStr)) {
381
+ const rest = name.startsWith('@') ? parts.slice(2) : parts.slice(1);
382
+ path = mainPath.substring(0, mainPath.indexOf(searchStr) + searchStr.length) + rest.join(sep);
383
+ }
384
+ } else {
385
+ path = resolve(root, 'node_modules', name);
386
+ }
387
+
388
+ if (!existsSync(path)) {
389
+ path = `${path}.js`;
390
+
391
+ if (!existsSync(path)) {
392
+ path = undefined;
393
+ }
394
+ }
395
+ }
396
+
397
+ if (!path) {
398
+ log('generalDebug_0003', `Could not resolve the package "${name}" in "${root}".`);
399
+ } else {
400
+ log('generalVerbose_0004', `Resolved the package "${name}" (from "${root}") to be "${path}".`);
401
+ }
402
+
403
+ return path;
404
+ }
405
+
406
+ export function findPackageRoot(pck: string, baseDir: string = undefined) {
407
+ return tryResolvePackage(`${pck}/package.json`, baseDir);
408
+ }
409
+
357
410
  export function isLinkedPackage(name: string, type: PackageType, hadVersion: boolean) {
358
411
  if (type === 'registry' && !hadVersion) {
359
- try {
360
- require.resolve(`${name}/package.json`);
361
- return true;
362
- } catch {}
412
+ const root = findPackageRoot(name);
413
+ return typeof root === 'string';
363
414
  }
364
415
 
365
416
  return false;
@@ -424,7 +475,8 @@ export function getPackageVersion(
424
475
  function getExternalsFrom(packageName: string): Array<string> | undefined {
425
476
  try {
426
477
  return require(`${packageName}/package.json`).sharedDependencies;
427
- } catch {
478
+ } catch (err) {
479
+ log('generalError_0002', `Could not get externals from "${packageName}": "${err}`);
428
480
  return undefined;
429
481
  }
430
482
  }
@@ -1,5 +1,4 @@
1
- import { resolve } from 'path';
2
- import { findPackageVersion, findPackageRoot, getPiralPackage, getPiletsInfo, retrievePiletData } from './package';
1
+ import { findPackageVersion, getPiralPackage, getPiletsInfo, retrievePiletData } from './package';
3
2
  import { cliVersion } from './info';
4
3
  import { SourceLanguage } from './enums';
5
4
 
@@ -14,22 +13,10 @@ describe('CLI package module', () => {
14
13
  expect(version).toBe('latest');
15
14
  });
16
15
 
17
- it('findPackageRoot correctly resolves the package root of parcel-bundler', () => {
18
- const dir = process.cwd();
19
- const version = findPackageRoot('webpack', dir);
20
- expect(version).toBe(resolve(dir, 'node_modules', 'webpack', 'package.json'));
21
- });
22
-
23
- it('findPackageRoot returns undefined for invalid package', () => {
24
- const dir = process.cwd();
25
- const version = findPackageRoot('foo-bar-not-exist', dir);
26
- expect(version).toBeUndefined();
27
- });
28
-
29
16
  it('getPiletsInfo returns pilets information about provided piralInfo', () => {
30
17
  const emptyPiletsInfo = {
31
18
  files: [],
32
- externals: [],
19
+ template: 'default',
33
20
  scripts: {},
34
21
  validators: {},
35
22
  devDependencies: {},
@@ -45,7 +32,7 @@ describe('CLI package module', () => {
45
32
  const piralInfo = {
46
33
  pilets: {
47
34
  files: ['foo.tgz', 'foo2.tgz'],
48
- externals: [],
35
+ template: 'default',
49
36
  scripts: {},
50
37
  validators: {},
51
38
  devDependencies: {},
@@ -8,10 +8,11 @@ import { checkAppShellCompatibility } from './compatibility';
8
8
  import { deepMerge } from './merge';
9
9
  import { applyTemplate } from './template';
10
10
  import { readImportmap } from './importmap';
11
- import { isGitPackage, isLocalPackage, makeGitUrl, makeFilePath, makePiletExternals, makeExternals } from './npm';
12
11
  import { filesTar, filesOnceTar, declarationEntryExtensions, bundlerNames } from './constants';
13
12
  import { getHash, checkIsDirectory, matchFiles } from './io';
14
13
  import { readJson, copy, updateExistingJson, findFile, checkExists } from './io';
14
+ import { isGitPackage, isLocalPackage, makeGitUrl, makeFilePath } from './npm';
15
+ import { makePiletExternals, makeExternals, findPackageRoot } from './npm';
15
16
  import { Framework, FileInfo, PiletsInfo, TemplateFileLocation } from '../types';
16
17
 
17
18
  function appendBundler(devDependencies: Record<string, string>, bundler: string, version: string) {
@@ -113,26 +114,13 @@ async function getMatchingFiles(
113
114
  }
114
115
 
115
116
  export function getPiralPath(root: string, name: string) {
116
- try {
117
- const path = require.resolve(`${name}/package.json`, {
118
- paths: [root],
119
- });
120
- return dirname(path);
121
- } catch (ex) {
122
- log('generalDebug_0003', `Could not resolve the Piral path of "${name}" in "${root}": ${ex}.`);
117
+ const path = findPackageRoot(name, root);
118
+
119
+ if (!path) {
123
120
  fail('invalidPiralReference_0043');
124
121
  }
125
- }
126
122
 
127
- export function findPackageRoot(pck: string, baseDir: string) {
128
- try {
129
- return require.resolve(`${pck}/package.json`, {
130
- paths: [baseDir],
131
- });
132
- } catch (ex) {
133
- log('generalDebug_0003', `Could not find the package root in "${baseDir}": ${ex}.`);
134
- return undefined;
135
- }
123
+ return dirname(path);
136
124
  }
137
125
 
138
126
  function findPackage(pck: string | Array<string>, baseDir: string) {
@@ -150,8 +138,10 @@ function findPackage(pck: string | Array<string>, baseDir: string) {
150
138
  if (path) {
151
139
  log('generalDebug_0003', `Following the app package in "${path}" ...`);
152
140
  const appPackage = require(path);
141
+ const root = dirname(path);
153
142
  const relPath = appPackage && appPackage.app;
154
- appPackage.app = relPath && resolve(dirname(path), relPath);
143
+ appPackage.app = relPath && resolve(root, relPath);
144
+ appPackage.root = root;
155
145
  return appPackage;
156
146
  }
157
147
  }
@@ -192,6 +182,15 @@ export function getPiralPackage(
192
182
  start: 'piral debug',
193
183
  build: 'piral build',
194
184
  },
185
+ importmap: {
186
+ imports: {},
187
+ inherit: [
188
+ 'piral-base', // this we take in any case
189
+ framework !== 'piral-base' && 'piral-core', // this we take unless we selected piral-base, then obviously core is not invited to the party
190
+ framework === 'piral' && 'piral', // this we take only if we selected piral
191
+ framework === 'piral-native' && 'piral-native', // this we also only take if we selected piral-native
192
+ ].filter(Boolean),
193
+ },
195
194
  pilets: getPiletsInfo({}),
196
195
  dependencies,
197
196
  devDependencies,
@@ -345,8 +344,8 @@ export async function copyPiralFiles(
345
344
  export function getPiletsInfo(piralInfo: any): PiletsInfo {
346
345
  const {
347
346
  files = [],
348
- externals = [],
349
347
  scripts = {},
348
+ template = 'default',
350
349
  validators = {},
351
350
  devDependencies = {},
352
351
  preScaffold = '',
@@ -358,8 +357,8 @@ export function getPiletsInfo(piralInfo: any): PiletsInfo {
358
357
 
359
358
  return {
360
359
  files,
361
- externals,
362
360
  scripts,
361
+ template,
363
362
  validators,
364
363
  devDependencies,
365
364
  preScaffold,
@@ -442,6 +441,21 @@ export async function findPackageVersion(rootPath: string, packageName: string):
442
441
  }
443
442
  }
444
443
 
444
+ export async function retrieveExternals(root: string, packageInfo: any) {
445
+ const sharedDependencies = await readImportmap(root, packageInfo);
446
+
447
+ if (sharedDependencies.length === 0) {
448
+ const allDeps = {
449
+ ...packageInfo.devDependencies,
450
+ ...packageInfo.dependencies,
451
+ };
452
+ const deps = packageInfo.pilets?.externals;
453
+ return makeExternals(allDeps, deps);
454
+ }
455
+
456
+ return sharedDependencies.map((m) => m.name);
457
+ }
458
+
445
459
  export async function retrievePiletsInfo(entryFile: string) {
446
460
  const exists = await checkExists(entryFile);
447
461
 
@@ -456,15 +470,9 @@ export async function retrievePiletsInfo(entryFile: string) {
456
470
  }
457
471
 
458
472
  const packageInfo = require(packageJson);
459
- const allDeps = {
460
- ...packageInfo.devDependencies,
461
- ...packageInfo.dependencies,
462
- };
463
473
  const info = getPiletsInfo(packageInfo);
464
474
  const root = dirname(packageJson);
465
- const sharedDependencies = await readImportmap(root, packageInfo);
466
- const deps = sharedDependencies.length > 0 ? sharedDependencies.map((m) => m.name) : info.externals;
467
- const externals = makeExternals(allDeps, deps);
475
+ const externals = await retrieveExternals(root, packageInfo);
468
476
 
469
477
  return {
470
478
  ...info,
@@ -602,9 +610,10 @@ export async function retrievePiletData(target: string, app?: string) {
602
610
  app || (piletPackage.piral && piletPackage.piral.name) || Object.keys(piletPackage.devDependencies),
603
611
  target,
604
612
  );
605
- const appFile: string = appPackage && appPackage.app;
613
+ const appFile: string = appPackage?.app;
614
+ const appRoot: string = appPackage?.root;
606
615
 
607
- if (!appFile) {
616
+ if (!appFile || !appRoot) {
608
617
  fail('appInstanceInvalid_0011');
609
618
  }
610
619
 
@@ -619,6 +628,7 @@ export async function retrievePiletData(target: string, app?: string) {
619
628
  ignored: checkArrayOrUndefined(piletPackage, 'preservedDependencies'),
620
629
  importmap,
621
630
  appFile,
631
+ appRoot,
622
632
  piletPackage,
623
633
  appPackage,
624
634
  emulator,
@@ -39,6 +39,7 @@ async function getTemplateFiles(
39
39
  } else {
40
40
  await installPackage(templatePackageName, __dirname, '--registry', registry);
41
41
  }
42
+
42
43
  const templateRunner = getTemplatePackage(templatePackageName);
43
44
  const logLevel = getLogLevel();
44
45
  const details = {
package/src/helpers.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ForceOverwrite, SourceLanguage } from './common/enums';
2
2
  import { bundlerNames, frameworkLibs } from './common/constants';
3
- import {
3
+ import type {
4
4
  Framework,
5
5
  NpmClientType,
6
6
  PiletSchemaVersion,
@@ -10,7 +10,6 @@ import { axios, mime } from '../external';
10
10
  import { Bundler } from '../types';
11
11
 
12
12
  const { host } = config;
13
- const indexPath = '/index.html';
14
13
 
15
14
  interface Pilet {
16
15
  bundler: Bundler;
@@ -76,6 +75,7 @@ async function loadFeed(feed: string) {
76
75
  export default class PiletInjector implements KrasInjector {
77
76
  public config: PiletInjectorConfig;
78
77
  private piletApi: string;
78
+ private indexPath: string;
79
79
 
80
80
  constructor(options: PiletInjectorConfig, config: KrasConfiguration, core: EventEmitter) {
81
81
  this.config = options;
@@ -84,7 +84,8 @@ export default class PiletInjector implements KrasInjector {
84
84
  ? options.api
85
85
  : `${config.ssl ? 'https' : 'http'}://${host}:${config.port}${options.api}`;
86
86
 
87
- const { pilets, api } = options;
87
+ const { pilets, api, publicUrl } = options;
88
+ this.indexPath = `${publicUrl}index.html`;
88
89
  const cbs = {};
89
90
 
90
91
  core.on('user-connected', (e) => {
@@ -220,15 +221,15 @@ export default class PiletInjector implements KrasInjector {
220
221
  const target = join(app, path);
221
222
 
222
223
  if (existsSync(target) && statSync(target).isFile()) {
223
- if (req.url === indexPath) {
224
+ if (req.url === this.indexPath) {
224
225
  return this.sendIndexFile(target, req.url);
225
226
  } else {
226
227
  return this.sendFile(target, req.url);
227
228
  }
228
- } else if (req.url !== indexPath) {
229
+ } else if (req.url !== this.indexPath) {
229
230
  return this.handle({
230
231
  ...req,
231
- url: indexPath,
232
+ url: this.indexPath,
232
233
  });
233
234
  }
234
235
  }
package/src/messages.ts CHANGED
@@ -549,7 +549,11 @@ export function importMapVersionSpecInvalid_0026(depName: string): QuickMessage
549
549
  * node modules are most likely not installed (correctly).
550
550
  */
551
551
  export function importMapReferenceNotFound_0027(dir: string, reference: string): QuickMessage {
552
- return [LogLevels.error, '0027', `The reference to "${reference}" could not be resolved from "${dir}". Are you sure the file or package exists?`];
552
+ return [
553
+ LogLevels.error,
554
+ '0027',
555
+ `The reference to "${reference}" could not be resolved from "${dir}". Are you sure the file or package exists?`,
556
+ ];
553
557
  }
554
558
 
555
559
  /**
@@ -1,5 +1,5 @@
1
1
  import { PiletRuleContext } from '../types';
2
- import { getPiletsInfo, getSourceFiles, isValidDependency } from '../common';
2
+ import { retrieveExternals, getSourceFiles, isValidDependency } from '../common';
3
3
 
4
4
  export type Options = 'ignore' | 'active' | 'only-used';
5
5
 
@@ -21,7 +21,7 @@ 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 } = getPiletsInfo(context.data.appPackage);
24
+ const externals = await retrieveExternals(context.data.appRoot, context.data.appPackage);
25
25
  const markedPeerDependencies = Object.keys(context.peerDependencies);
26
26
  const markedPeerModules = context.peerModules;
27
27
  const missingExternals = externals
@@ -55,7 +55,8 @@ export interface TemplateFileLocation {
55
55
 
56
56
  export interface PiletsInfo {
57
57
  files: Array<string | TemplateFileLocation>;
58
- externals: Array<string>;
58
+ template: string;
59
+ externals?: Array<string>;
59
60
  devDependencies: Record<string, string | true>;
60
61
  scripts: Record<string, string>;
61
62
  validators: Record<string, any>;
@@ -106,6 +107,7 @@ export interface PiletRuleContext extends RuleContext {
106
107
  }
107
108
 
108
109
  export interface PiralData {
110
+ appRoot: string;
109
111
  appFile: string;
110
112
  appPackage: any;
111
113
  piletPackage: any;
@@ -231,7 +231,7 @@ export type PackageType = 'registry' | 'file' | 'git';
231
231
 
232
232
  export type NpmClientType = 'npm' | 'yarn' | 'pnpm';
233
233
 
234
- export type Framework = 'piral' | 'piral-core' | 'piral-base';
234
+ export type Framework = 'piral-native' | 'piral' | 'piral-core' | 'piral-base';
235
235
 
236
236
  export interface StandardEnvProps {
237
237
  production?: boolean;