piral-cli 0.15.0-alpha.4029 → 0.15.0-alpha.4041

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 (38) hide show
  1. package/lib/apps/build-pilet.d.ts +4 -0
  2. package/lib/apps/build-pilet.js +8 -6
  3. package/lib/apps/build-pilet.js.map +1 -1
  4. package/lib/apps/debug-pilet.d.ts +5 -0
  5. package/lib/apps/debug-pilet.js +10 -7
  6. package/lib/apps/debug-pilet.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/run-debug-mono-piral.js +4 -4
  10. package/lib/build/run-debug-mono-piral.js.map +1 -1
  11. package/lib/commands.js +8 -0
  12. package/lib/commands.js.map +1 -1
  13. package/lib/common/importmap.js +20 -1
  14. package/lib/common/importmap.js.map +1 -1
  15. package/lib/common/package.d.ts +7 -0
  16. package/lib/common/package.js +30 -9
  17. package/lib/common/package.js.map +1 -1
  18. package/lib/injectors/pilet.d.ts +1 -0
  19. package/lib/injectors/pilet.js +17 -13
  20. package/lib/injectors/pilet.js.map +1 -1
  21. package/lib/rules/pilet-has-externals-as-peers.js +1 -1
  22. package/lib/rules/pilet-has-externals-as-peers.js.map +1 -1
  23. package/lib/types/common.d.ts +3 -1
  24. package/lib/types/public.d.ts +1 -0
  25. package/package.json +2 -2
  26. package/src/apps/build-pilet.ts +16 -7
  27. package/src/apps/debug-pilet.ts +18 -5
  28. package/src/apps/new-pilet.ts +4 -3
  29. package/src/build/run-debug-mono-piral.ts +4 -2
  30. package/src/commands.ts +8 -0
  31. package/src/common/importmap.ts +25 -1
  32. package/src/common/package.test.ts +2 -2
  33. package/src/common/package.ts +31 -11
  34. package/src/injectors/pilet.test.ts +6 -0
  35. package/src/injectors/pilet.ts +20 -14
  36. package/src/rules/pilet-has-externals-as-peers.ts +2 -2
  37. package/src/types/common.ts +3 -1
  38. package/src/types/public.ts +1 -0
package/src/commands.ts CHANGED
@@ -410,6 +410,9 @@ const allCommands: Array<ToolCommand<any>> = [
410
410
  .string('target')
411
411
  .describe('target', 'Sets the target directory or file of bundling.')
412
412
  .default('target', apps.debugPiletDefaults.target)
413
+ .string('public-url')
414
+ .describe('public-url', 'Sets the public URL (path) of the application.')
415
+ .default('public-url', apps.debugPiletDefaults.publicUrl)
413
416
  .number('port')
414
417
  .describe('port', 'Sets the port of the local development server.')
415
418
  .default('port', apps.debugPiletDefaults.port)
@@ -448,6 +451,7 @@ const allCommands: Array<ToolCommand<any>> = [
448
451
  return apps.debugPilet(args.base as string, {
449
452
  entry: args.source as string,
450
453
  target: args.target as string,
454
+ publicUrl: args['public-url'] as string,
451
455
  port: args.port as number,
452
456
  hmr: args.hmr as boolean,
453
457
  bundlerName: args.bundler as string,
@@ -480,6 +484,9 @@ const allCommands: Array<ToolCommand<any>> = [
480
484
  .string('target')
481
485
  .describe('target', 'Sets the target file of bundling.')
482
486
  .default('target', apps.buildPiletDefaults.target)
487
+ .string('public-url')
488
+ .describe('public-url', 'Sets the public URL (path) of the application.')
489
+ .default('public-url', apps.buildPiletDefaults.publicUrl)
483
490
  .number('log-level')
484
491
  .describe('log-level', 'Sets the log level to use (1-5).')
485
492
  .default('log-level', apps.buildPiletDefaults.logLevel)
@@ -523,6 +530,7 @@ const allCommands: Array<ToolCommand<any>> = [
523
530
  return apps.buildPilet(args.base as string, {
524
531
  entry: args.source as string,
525
532
  target: args.target as string,
533
+ publicUrl: args['public-url'] as string,
526
534
  minify: args.minify as boolean,
527
535
  contentHash: args['content-hash'] as boolean,
528
536
  bundlerName: args.bundler as string,
@@ -7,6 +7,7 @@ import { SharedDependency } from '../types';
7
7
 
8
8
  interface Importmap {
9
9
  imports: Record<string, string>;
10
+ inherit: Array<string>;
10
11
  }
11
12
 
12
13
  function tryResolve(baseDir: string, name: string) {
@@ -53,6 +54,7 @@ function getLocalDependencyVersion(
53
54
  async function resolveImportmap(dir: string, importmap: Importmap) {
54
55
  const dependencies: Array<SharedDependency> = [];
55
56
  const sharedImports = importmap?.imports;
57
+ const inheritedImports = importmap?.inherit;
56
58
 
57
59
  if (typeof sharedImports === 'object' && sharedImports) {
58
60
  for (const depName of Object.keys(sharedImports)) {
@@ -96,7 +98,9 @@ async function resolveImportmap(dir: string, importmap: Importmap) {
96
98
 
97
99
  if (exists) {
98
100
  const isDirectory = await checkIsDirectory(entry);
99
- const packageJson = isDirectory ? resolve(entry, 'package.json') : await findFile(dirname(entry), 'package.json');
101
+ const packageJson = isDirectory
102
+ ? resolve(entry, 'package.json')
103
+ : await findFile(dirname(entry), 'package.json');
100
104
  const packageJsonExists = await checkExists(packageJson);
101
105
 
102
106
  if (packageJsonExists) {
@@ -131,6 +135,26 @@ async function resolveImportmap(dir: string, importmap: Importmap) {
131
135
  }
132
136
  }
133
137
 
138
+ if (Array.isArray(inheritedImports)) {
139
+ for (const inheritedImport of inheritedImports) {
140
+ const packageJson = tryResolve(dir, `${inheritedImport}/package.json`);
141
+
142
+ if (packageJson) {
143
+ const packageDir = dirname(packageJson);
144
+ const packageDetails = require(packageJson);
145
+ const otherDependencies = await readImportmap(packageDir, packageDetails);
146
+
147
+ for (const dependency of otherDependencies) {
148
+ const entry = dependencies.find((dep) => dep.name === dependency.name);
149
+
150
+ if (!entry) {
151
+ dependencies.push(dependency);
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+
134
158
  return dependencies;
135
159
  }
136
160
 
@@ -29,7 +29,7 @@ describe('CLI package module', () => {
29
29
  it('getPiletsInfo returns pilets information about provided piralInfo', () => {
30
30
  const emptyPiletsInfo = {
31
31
  files: [],
32
- externals: [],
32
+ template: 'default',
33
33
  scripts: {},
34
34
  validators: {},
35
35
  devDependencies: {},
@@ -45,7 +45,7 @@ describe('CLI package module', () => {
45
45
  const piralInfo = {
46
46
  pilets: {
47
47
  files: ['foo.tgz', 'foo2.tgz'],
48
- externals: [],
48
+ template: 'default',
49
49
  scripts: {},
50
50
  validators: {},
51
51
  devDependencies: {},
@@ -150,8 +150,10 @@ function findPackage(pck: string | Array<string>, baseDir: string) {
150
150
  if (path) {
151
151
  log('generalDebug_0003', `Following the app package in "${path}" ...`);
152
152
  const appPackage = require(path);
153
+ const root = dirname(path);
153
154
  const relPath = appPackage && appPackage.app;
154
- appPackage.app = relPath && resolve(dirname(path), relPath);
155
+ appPackage.app = relPath && resolve(root, relPath);
156
+ appPackage.root = root;
155
157
  return appPackage;
156
158
  }
157
159
  }
@@ -192,6 +194,10 @@ export function getPiralPackage(
192
194
  start: 'piral debug',
193
195
  build: 'piral build',
194
196
  },
197
+ importmap: {
198
+ imports: {},
199
+ inherit: ['piral-base', framework !== 'piral-base' && 'piral-core'].filter(Boolean),
200
+ },
195
201
  pilets: getPiletsInfo({}),
196
202
  dependencies,
197
203
  devDependencies,
@@ -345,8 +351,8 @@ export async function copyPiralFiles(
345
351
  export function getPiletsInfo(piralInfo: any): PiletsInfo {
346
352
  const {
347
353
  files = [],
348
- externals = [],
349
354
  scripts = {},
355
+ template = 'default',
350
356
  validators = {},
351
357
  devDependencies = {},
352
358
  preScaffold = '',
@@ -358,8 +364,8 @@ export function getPiletsInfo(piralInfo: any): PiletsInfo {
358
364
 
359
365
  return {
360
366
  files,
361
- externals,
362
367
  scripts,
368
+ template,
363
369
  validators,
364
370
  devDependencies,
365
371
  preScaffold,
@@ -442,6 +448,21 @@ export async function findPackageVersion(rootPath: string, packageName: string):
442
448
  }
443
449
  }
444
450
 
451
+ export async function retrieveExternals(root: string, packageInfo: any) {
452
+ const sharedDependencies = await readImportmap(root, packageInfo);
453
+
454
+ if (sharedDependencies.length === 0) {
455
+ const allDeps = {
456
+ ...packageInfo.devDependencies,
457
+ ...packageInfo.dependencies,
458
+ };
459
+ const deps = packageInfo.pilets?.externals;
460
+ return makeExternals(allDeps, deps);
461
+ }
462
+
463
+ return sharedDependencies.map((m) => m.name);
464
+ }
465
+
445
466
  export async function retrievePiletsInfo(entryFile: string) {
446
467
  const exists = await checkExists(entryFile);
447
468
 
@@ -456,12 +477,9 @@ export async function retrievePiletsInfo(entryFile: string) {
456
477
  }
457
478
 
458
479
  const packageInfo = require(packageJson);
459
- const allDeps = {
460
- ...packageInfo.devDependencies,
461
- ...packageInfo.dependencies,
462
- };
463
480
  const info = getPiletsInfo(packageInfo);
464
- const externals = makeExternals(allDeps, info.externals);
481
+ const root = dirname(packageJson);
482
+ const externals = await retrieveExternals(root, packageInfo);
465
483
 
466
484
  return {
467
485
  ...info,
@@ -475,7 +493,7 @@ export async function retrievePiletsInfo(entryFile: string) {
475
493
  },
476
494
  scripts: packageInfo.scripts,
477
495
  ignored: checkArrayOrUndefined(packageInfo, 'preservedDependencies'),
478
- root: dirname(packageJson),
496
+ root,
479
497
  };
480
498
  }
481
499
 
@@ -599,9 +617,10 @@ export async function retrievePiletData(target: string, app?: string) {
599
617
  app || (piletPackage.piral && piletPackage.piral.name) || Object.keys(piletPackage.devDependencies),
600
618
  target,
601
619
  );
602
- const appFile: string = appPackage && appPackage.app;
620
+ const appFile: string = appPackage?.app;
621
+ const appRoot: string = appPackage?.root;
603
622
 
604
- if (!appFile) {
623
+ if (!appFile || !appRoot) {
605
624
  fail('appInstanceInvalid_0011');
606
625
  }
607
626
 
@@ -616,6 +635,7 @@ export async function retrievePiletData(target: string, app?: string) {
616
635
  ignored: checkArrayOrUndefined(piletPackage, 'preservedDependencies'),
617
636
  importmap,
618
637
  appFile,
638
+ appRoot,
619
639
  piletPackage,
620
640
  appPackage,
621
641
  emulator,
@@ -4,6 +4,8 @@ import PiletInjector from './pilet';
4
4
 
5
5
  const optionsMock = {
6
6
  pilets: [],
7
+ publicUrl: '/',
8
+ meta: 'debug-meta.json',
7
9
  api: '',
8
10
  app: '',
9
11
  active: true,
@@ -83,6 +85,8 @@ describe('Piral-CLI piral injector', () => {
83
85
  // Arrange
84
86
  const optionsMock = {
85
87
  pilets: [],
88
+ meta: 'debug-meta.json',
89
+ publicUrl: '/',
86
90
  api: 'http://someFakeApi:1234',
87
91
  app: '',
88
92
  active: true,
@@ -110,6 +114,8 @@ describe('Piral-CLI piral injector', () => {
110
114
  // Arrange
111
115
  const optionsMock = {
112
116
  pilets: [],
117
+ meta: 'debug-meta.json',
118
+ publicUrl: '/',
113
119
  api: 'http://someFakeApi:1234',
114
120
  app: '',
115
121
  active: true,
@@ -10,6 +10,7 @@ import { axios, mime } from '../external';
10
10
  import { Bundler } from '../types';
11
11
 
12
12
  const { host } = config;
13
+ const indexPath = '/index.html';
13
14
 
14
15
  interface Pilet {
15
16
  bundler: Bundler;
@@ -19,6 +20,7 @@ interface Pilet {
19
20
 
20
21
  export interface PiletInjectorConfig extends KrasInjectorConfig {
21
22
  pilets: Array<Pilet>;
23
+ publicUrl: string;
22
24
  meta: string;
23
25
  api: string;
24
26
  app: string;
@@ -210,26 +212,30 @@ export default class PiletInjector implements KrasInjector {
210
212
  }
211
213
 
212
214
  handle(req: KrasRequest): KrasResponse {
213
- const { app, api } = this.config;
214
- const path = req.url.substring(1).split('?')[0];
215
+ const { app, api, publicUrl } = this.config;
215
216
 
216
217
  if (!req.target) {
217
- const target = join(app, path);
218
+ if (req.url.startsWith(publicUrl)) {
219
+ const path = req.url.substring(publicUrl.length).split('?')[0];
220
+ const target = join(app, path);
218
221
 
219
- if (existsSync(target) && statSync(target).isFile()) {
220
- if (req.url === '/index.html') {
221
- return this.sendIndexFile(target, req.url);
222
+ if (existsSync(target) && statSync(target).isFile()) {
223
+ if (req.url === indexPath) {
224
+ return this.sendIndexFile(target, req.url);
225
+ } else {
226
+ return this.sendFile(target, req.url);
227
+ }
228
+ } else if (req.url !== indexPath) {
229
+ return this.handle({
230
+ ...req,
231
+ url: indexPath,
232
+ });
222
233
  }
223
- return this.sendFile(target, req.url);
224
- } else if (req.url !== '/index.html') {
225
- return this.handle({
226
- ...req,
227
- url: '/index.html',
228
- });
229
- } else {
230
- return undefined;
231
234
  }
235
+
236
+ return undefined;
232
237
  } else if (req.target === api) {
238
+ const path = req.url.substring(1).split('?')[0];
233
239
  return this.sendResponse(path, req.url);
234
240
  }
235
241
  }
@@ -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;
@@ -80,6 +80,7 @@ export interface DebugPiralParameters extends BaseBundleParameters {
80
80
  export interface WatchPiralParameters extends BaseBundleParameters {
81
81
  piral: string;
82
82
  externals: Array<string>;
83
+ publicUrl: string;
83
84
  entryFiles: string;
84
85
  logLevel: LogLevels;
85
86
  }