piral-cli 0.15.0-alpha.3905 → 0.15.0-alpha.4005

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 (41) hide show
  1. package/lib/apps/build-pilet.d.ts +4 -0
  2. package/lib/apps/build-pilet.js +4 -3
  3. package/lib/apps/build-pilet.js.map +1 -1
  4. package/lib/apps/debug-pilet.d.ts +4 -0
  5. package/lib/apps/debug-pilet.js +4 -3
  6. package/lib/apps/debug-pilet.js.map +1 -1
  7. package/lib/apps/publish-pilet.d.ts +9 -1
  8. package/lib/apps/publish-pilet.js +4 -2
  9. package/lib/apps/publish-pilet.js.map +1 -1
  10. package/lib/commands.js +16 -0
  11. package/lib/commands.js.map +1 -1
  12. package/lib/common/http.d.ts +2 -1
  13. package/lib/common/http.js +18 -4
  14. package/lib/common/http.js.map +1 -1
  15. package/lib/common/index.d.ts +1 -0
  16. package/lib/common/index.js +1 -0
  17. package/lib/common/index.js.map +1 -1
  18. package/lib/common/info.d.ts +1 -0
  19. package/lib/common/info.js +3 -1
  20. package/lib/common/info.js.map +1 -1
  21. package/lib/common/parallel.d.ts +1 -0
  22. package/lib/common/parallel.js +29 -0
  23. package/lib/common/parallel.js.map +1 -0
  24. package/lib/external/index.js +142 -75
  25. package/lib/helpers.d.ts +2 -1
  26. package/lib/helpers.js +2 -1
  27. package/lib/helpers.js.map +1 -1
  28. package/lib/types/public.d.ts +1 -0
  29. package/package.json +2 -2
  30. package/src/apps/build-pilet.ts +105 -99
  31. package/src/apps/debug-pilet.ts +61 -54
  32. package/src/apps/publish-pilet.ts +16 -2
  33. package/src/commands.ts +18 -0
  34. package/src/common/http.test.ts +7 -7
  35. package/src/common/http.ts +21 -3
  36. package/src/common/index.ts +1 -0
  37. package/src/common/info.ts +3 -0
  38. package/src/common/parallel.test.ts +28 -0
  39. package/src/common/parallel.ts +21 -0
  40. package/src/helpers.ts +2 -0
  41. package/src/types/public.ts +2 -0
@@ -16,6 +16,8 @@ import {
16
16
  fail,
17
17
  log,
18
18
  logDone,
19
+ cpuCount,
20
+ concurrentWorkers,
19
21
  } from '../common';
20
22
 
21
23
  export interface DebugPiletOptions {
@@ -57,6 +59,11 @@ export interface DebugPiletOptions {
57
59
  */
58
60
  port?: number;
59
61
 
62
+ /**
63
+ * Sets the maximum number of parallel build processes.
64
+ */
65
+ concurrency?: number;
66
+
60
67
  /**
61
68
  * Defines if hot module reloading (HMR) should be integrated for faster debugging.
62
69
  */
@@ -112,6 +119,7 @@ export const debugPiletDefaults: DebugPiletOptions = {
112
119
  hmr: true,
113
120
  optimizeModules: false,
114
121
  schemaVersion: config.schemaVersion,
122
+ concurrency: cpuCount,
115
123
  };
116
124
 
117
125
  const injectorName = resolve(__dirname, '../injectors/pilet.js');
@@ -168,6 +176,7 @@ export async function debugPilet(baseDir = process.cwd(), options: DebugPiletOpt
168
176
  open = debugPiletDefaults.open,
169
177
  hmr = debugPiletDefaults.hmr,
170
178
  logLevel = debugPiletDefaults.logLevel,
179
+ concurrency = debugPiletDefaults.concurrency,
171
180
  optimizeModules = debugPiletDefaults.optimizeModules,
172
181
  schemaVersion = debugPiletDefaults.schemaVersion,
173
182
  _ = {},
@@ -204,63 +213,61 @@ export async function debugPilet(baseDir = process.cwd(), options: DebugPiletOpt
204
213
  process.stdout.setMaxListeners(maxListeners);
205
214
  process.stdin.setMaxListeners(maxListeners);
206
215
 
207
- const pilets = await Promise.all(
208
- allEntries.map(async (entryModule) => {
209
- const targetDir = dirname(entryModule);
210
- const { peerDependencies, peerModules, root, appPackage, appFile, ignored, emulator, importmap } =
211
- await retrievePiletData(targetDir, app);
212
- const externals = [...Object.keys(peerDependencies), ...peerModules];
213
- const mocks = join(targetDir, 'mocks');
214
- const dest = resolve(root, target);
215
- const outDir = dirname(dest);
216
- const outFile = basename(dest);
217
- const exists = await checkExistingDirectory(mocks);
218
-
219
- if (exists) {
220
- if (krasConfig.directory === undefined) {
221
- krasConfig.directory = mocks;
222
- }
223
-
224
- krasConfig.sources.push(mocks);
216
+ const pilets = await concurrentWorkers(allEntries, concurrency, async (entryModule) => {
217
+ const targetDir = dirname(entryModule);
218
+ const { peerDependencies, peerModules, root, appPackage, appFile, ignored, emulator, importmap } =
219
+ await retrievePiletData(targetDir, app);
220
+ const externals = [...Object.keys(peerDependencies), ...peerModules];
221
+ const mocks = join(targetDir, 'mocks');
222
+ const dest = resolve(root, target);
223
+ const outDir = dirname(dest);
224
+ const outFile = basename(dest);
225
+ const exists = await checkExistingDirectory(mocks);
226
+
227
+ if (exists) {
228
+ if (krasConfig.directory === undefined) {
229
+ krasConfig.directory = mocks;
225
230
  }
226
231
 
227
- await hooks.beforeBuild?.({ root, importmap, entryModule, schemaVersion });
228
-
229
- const bundler = await callPiletDebug(
230
- {
231
- root,
232
- piral: appPackage.name,
233
- optimizeModules,
234
- hmr,
235
- externals,
236
- targetDir,
237
- importmap,
238
- outFile,
239
- outDir,
240
- entryModule: `./${relative(root, entryModule)}`,
241
- logLevel,
242
- version: schemaVersion,
243
- ignored,
244
- _,
245
- },
246
- bundlerName,
247
- );
248
-
249
- bundler.on((args) => {
250
- hooks.afterBuild?.({ ...args, root, importmap, entryModule, schemaVersion, bundler, outFile, outDir });
251
- });
252
-
253
- return {
254
- emulator,
255
- appFile,
256
- appVersion: appPackage.version,
257
- externals,
258
- piral: appPackage.name,
259
- bundler,
232
+ krasConfig.sources.push(mocks);
233
+ }
234
+
235
+ await hooks.beforeBuild?.({ root, importmap, entryModule, schemaVersion });
236
+
237
+ const bundler = await callPiletDebug(
238
+ {
260
239
  root,
261
- };
262
- }),
263
- );
240
+ piral: appPackage.name,
241
+ optimizeModules,
242
+ hmr,
243
+ externals,
244
+ targetDir,
245
+ importmap,
246
+ outFile,
247
+ outDir,
248
+ entryModule: `./${relative(root, entryModule)}`,
249
+ logLevel,
250
+ version: schemaVersion,
251
+ ignored,
252
+ _,
253
+ },
254
+ bundlerName,
255
+ );
256
+
257
+ bundler.on((args) => {
258
+ hooks.afterBuild?.({ ...args, root, importmap, entryModule, schemaVersion, bundler, outFile, outDir });
259
+ });
260
+
261
+ return {
262
+ emulator,
263
+ appFile,
264
+ appVersion: appPackage.version,
265
+ externals,
266
+ piral: appPackage.name,
267
+ bundler,
268
+ root,
269
+ };
270
+ });
264
271
 
265
272
  // sanity check see #250
266
273
  checkSanity(pilets);
@@ -1,6 +1,6 @@
1
1
  import { relative, dirname, basename, resolve } from 'path';
2
2
  import { callPiletBuild } from '../bundler';
3
- import { LogLevels, PiletSchemaVersion, PiletPublishSource } from '../types';
3
+ import { LogLevels, PiletSchemaVersion, PiletPublishSource, PiletPublishScheme } from '../types';
4
4
  import {
5
5
  postFile,
6
6
  readBinary,
@@ -72,11 +72,21 @@ export interface PublishPiletOptions {
72
72
  */
73
73
  fields?: Record<string, string>;
74
74
 
75
+ /**
76
+ * Places additional headers that should be posted to the feed service.
77
+ */
78
+ headers?: Record<string, string>;
79
+
75
80
  /**
76
81
  * Sets the bundler to use for building, if any specific.
77
82
  */
78
83
  bundlerName?: string;
79
84
 
85
+ /**
86
+ * Sets the authorization scheme to use.
87
+ */
88
+ mode?: PiletPublishScheme;
89
+
80
90
  /**
81
91
  * Additional arguments for a specific bundler.
82
92
  */
@@ -90,8 +100,10 @@ export const publishPiletDefaults: PublishPiletOptions = {
90
100
  cert: undefined,
91
101
  logLevel: LogLevels.info,
92
102
  schemaVersion: config.schemaVersion,
103
+ mode: 'basic',
93
104
  from: 'local',
94
105
  fields: {},
106
+ headers: {},
95
107
  };
96
108
 
97
109
  async function getFiles(
@@ -200,6 +212,8 @@ export async function publishPilet(baseDir = process.cwd(), options: PublishPile
200
212
  schemaVersion = publishPiletDefaults.schemaVersion,
201
213
  cert = config.cert ?? publishPiletDefaults.cert,
202
214
  fields = publishPiletDefaults.fields,
215
+ headers = publishPiletDefaults.headers,
216
+ mode = publishPiletDefaults.mode,
203
217
  _ = {},
204
218
  bundlerName,
205
219
  } = options;
@@ -240,7 +254,7 @@ export async function publishPilet(baseDir = process.cwd(), options: PublishPile
240
254
 
241
255
  if (content) {
242
256
  progress(`Publishing "%s" ...`, file, url);
243
- const result = await postFile(url, apiKey, content, fields, ca);
257
+ const result = await postFile(url, mode, apiKey, content, fields, headers, ca);
244
258
 
245
259
  if (result.success) {
246
260
  successfulUploads.push(file);
package/src/commands.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  fromKeys,
16
16
  bundlerKeys,
17
17
  piralBuildTypeKeys,
18
+ publishModeKeys,
18
19
  } from './helpers';
19
20
  import {
20
21
  ToolCommand,
@@ -25,6 +26,7 @@ import {
25
26
  PiletPublishSource,
26
27
  PiletSchemaVersion,
27
28
  PiletBuildType,
29
+ PiletPublishScheme,
28
30
  } from './types';
29
31
 
30
32
  function specializeCommand(commands: Array<ToolCommand<any>>, command: ToolCommand<any>, suffix: string) {
@@ -414,6 +416,9 @@ const allCommands: Array<ToolCommand<any>> = [
414
416
  .number('log-level')
415
417
  .describe('log-level', 'Sets the log level to use (1-5).')
416
418
  .default('log-level', apps.debugPiletDefaults.logLevel)
419
+ .number('concurrency')
420
+ .describe('concurrency', 'Sets the maximum number of concurrent build jobs.')
421
+ .default('concurrency', apps.debugPiletDefaults.concurrency)
417
422
  .boolean('open')
418
423
  .describe('open', 'Opens the pilet directly in the browser.')
419
424
  .default('open', apps.debugPiletDefaults.open)
@@ -452,6 +457,7 @@ const allCommands: Array<ToolCommand<any>> = [
452
457
  logLevel: args['log-level'] as LogLevels,
453
458
  open: args.open as boolean,
454
459
  schemaVersion: args.schema as PiletSchemaVersion,
460
+ concurrency: args.concurrency as number,
455
461
  feed: args.feed as string,
456
462
  hooks: args.hooks as object,
457
463
  _: args,
@@ -477,6 +483,9 @@ const allCommands: Array<ToolCommand<any>> = [
477
483
  .number('log-level')
478
484
  .describe('log-level', 'Sets the log level to use (1-5).')
479
485
  .default('log-level', apps.buildPiletDefaults.logLevel)
486
+ .number('concurrency')
487
+ .describe('concurrency', 'Sets the maximum number of concurrent build jobs.')
488
+ .default('concurrency', apps.buildPiletDefaults.concurrency)
480
489
  .boolean('source-maps')
481
490
  .describe('source-maps', 'Creates source maps for the bundles.')
482
491
  .default('source-maps', apps.buildPiletDefaults.sourceMaps)
@@ -524,6 +533,7 @@ const allCommands: Array<ToolCommand<any>> = [
524
533
  fresh: args.fresh as boolean,
525
534
  logLevel: args['log-level'] as LogLevels,
526
535
  schemaVersion: args.schema as PiletSchemaVersion,
536
+ concurrency: args.concurrency as number,
527
537
  app: args.app as string,
528
538
  hooks: args.hooks as object,
529
539
  _: args,
@@ -590,6 +600,9 @@ const allCommands: Array<ToolCommand<any>> = [
590
600
  .choices('schema', schemaKeys)
591
601
  .describe('schema', 'Sets the schema to be used when making a fresh build of the pilet.')
592
602
  .default('schema', apps.publishPiletDefaults.schemaVersion)
603
+ .choices('mode', publishModeKeys)
604
+ .describe('mode', 'Sets the authorization mode to use.')
605
+ .default('mode', apps.publishPiletDefaults.mode)
593
606
  .choices('bundler', availableBundlers)
594
607
  .describe('bundler', 'Sets the bundler to use.')
595
608
  .default('bundler', availableBundlers[0])
@@ -599,6 +612,9 @@ const allCommands: Array<ToolCommand<any>> = [
599
612
  .option('fields', undefined)
600
613
  .describe('fields', 'Sets additional fields to be included in the feed service request.')
601
614
  .default('fields', apps.publishPiletDefaults.fields)
615
+ .option('headers', undefined)
616
+ .describe('headers', 'Sets additional headers to be included in the feed service request.')
617
+ .default('headers', apps.publishPiletDefaults.headers)
602
618
  .string('base')
603
619
  .default('base', process.cwd())
604
620
  .describe('base', 'Sets the base directory. By default the current directory is used.');
@@ -615,6 +631,8 @@ const allCommands: Array<ToolCommand<any>> = [
615
631
  from: args.from as PiletPublishSource,
616
632
  schemaVersion: args.schema as PiletSchemaVersion,
617
633
  fields: args.fields as Record<string, string>,
634
+ headers: args.headers as Record<string, string>,
635
+ mode: args.mode as PiletPublishScheme,
618
636
  _: args,
619
637
  });
620
638
  },
@@ -71,7 +71,7 @@ jest.mock('axios', () => ({
71
71
 
72
72
  describe('HTTP Module', () => {
73
73
  it('postFile form posts a file successfully should be ok', async () => {
74
- const result = await postFile(apiUrl, '123', Buffer.from('example'));
74
+ const result = await postFile(apiUrl, 'basic', '123', Buffer.from('example'));
75
75
  expect(result).toEqual({
76
76
  response: undefined,
77
77
  status: 200,
@@ -80,7 +80,7 @@ describe('HTTP Module', () => {
80
80
  });
81
81
 
82
82
  it('postFile form fails to post file should be false', async () => {
83
- const result = await postFile(apiUrl, '124', Buffer.from('example'));
83
+ const result = await postFile(apiUrl, 'basic', '124', Buffer.from('example'));
84
84
  expect(result).toEqual({
85
85
  response: '',
86
86
  status: 401,
@@ -89,7 +89,7 @@ describe('HTTP Module', () => {
89
89
  });
90
90
 
91
91
  it('postFile form not found to post file should be false', async () => {
92
- const result = await postFile('http://sample.com/', '', Buffer.from('example'));
92
+ const result = await postFile('http://sample.com/', 'basic', '', Buffer.from('example'));
93
93
  expect(result).toEqual({
94
94
  response: '',
95
95
  status: 404,
@@ -99,7 +99,7 @@ describe('HTTP Module', () => {
99
99
 
100
100
  it('postFile call results in error request', async () => {
101
101
  errorRequest = true;
102
- const result = await postFile('http://sample.com/', '', Buffer.from('example'));
102
+ const result = await postFile('http://sample.com/', 'basic', '', Buffer.from('example'));
103
103
  expect(result).toEqual({
104
104
  response: undefined,
105
105
  status: 500,
@@ -110,7 +110,7 @@ describe('HTTP Module', () => {
110
110
 
111
111
  it('postFile call results in error other', async () => {
112
112
  errorOther = true;
113
- const result = await postFile('http://sample.com/', '', Buffer.from('example'));
113
+ const result = await postFile('http://sample.com/', 'basic', '', Buffer.from('example'));
114
114
  expect(result).toEqual({
115
115
  response: undefined,
116
116
  status: 500,
@@ -121,7 +121,7 @@ describe('HTTP Module', () => {
121
121
 
122
122
  it('postFile call results in error response', async () => {
123
123
  errorResponse = true;
124
- let result = await postFile('http://sample.com/', '', Buffer.from('example'));
124
+ let result = await postFile('http://sample.com/', 'basic', '', Buffer.from('example'));
125
125
  expect(result).toEqual({
126
126
  response: 'This component is not available anymore.',
127
127
  status: 410,
@@ -129,7 +129,7 @@ describe('HTTP Module', () => {
129
129
  });
130
130
  errorResponse = false;
131
131
  errorResponse2 = true;
132
- result = await postFile('http://sample.com/', '', Buffer.from('example'));
132
+ result = await postFile('http://sample.com/', 'basic', '', Buffer.from('example'));
133
133
  expect(result).toEqual({
134
134
  response: 'This component is not available anymore.',
135
135
  status: 410,
@@ -5,6 +5,7 @@ import { platform, tmpdir } from 'os';
5
5
  import { createWriteStream } from 'fs';
6
6
  import { log } from './log';
7
7
  import { axios, FormData } from '../external';
8
+ import { PiletPublishScheme } from '../types';
8
9
 
9
10
  const os = platform();
10
11
  const standardHeaders = {
@@ -67,25 +68,42 @@ export interface PostFileResult {
67
68
 
68
69
  export function postFile(
69
70
  target: string,
71
+ scheme: PiletPublishScheme,
70
72
  key: string,
71
73
  file: Buffer,
72
- fields: Record<string, string> = {},
74
+ customFields: Record<string, string> = {},
75
+ customHeaders: Record<string, string> = {},
73
76
  ca?: Buffer,
74
77
  ): Promise<PostFileResult> {
75
78
  const form = new FormData();
76
79
  const httpsAgent = ca ? new Agent({ ca }) : undefined;
77
80
 
78
- Object.keys(fields).forEach((key) => form.append(key, fields[key]));
81
+ Object.keys(customFields).forEach((key) => form.append(key, customFields[key]));
79
82
 
80
83
  form.append('file', file, 'pilet.tgz');
81
84
 
82
85
  const headers: Record<string, string> = {
83
86
  ...form.getHeaders(),
84
87
  ...standardHeaders,
88
+ ...customHeaders,
85
89
  };
86
90
 
87
91
  if (key) {
88
- headers.authorization = `Basic ${key}`;
92
+ switch (scheme) {
93
+ case 'basic':
94
+ headers.authorization = `Basic ${key}`;
95
+ break;
96
+ case 'bearer':
97
+ headers.authorization = `Bearer ${key}`;
98
+ break;
99
+ case 'digest':
100
+ headers.authorization = `Digest ${key}`;
101
+ break;
102
+ case 'none':
103
+ default:
104
+ headers.authorization = key;
105
+ break;
106
+ }
89
107
  }
90
108
 
91
109
  return axios.default
@@ -20,6 +20,7 @@ export * from './merge';
20
20
  export * from './npm';
21
21
  export * from './pack';
22
22
  export * from './package';
23
+ export * from './parallel';
23
24
  export * from './patcher';
24
25
  export * from './patches';
25
26
  export * from './port';
@@ -1,3 +1,5 @@
1
+ import { cpus } from 'os';
2
+
1
3
  const info = require('../../package.json');
2
4
 
3
5
  export function findCompatVersion(version: string) {
@@ -18,3 +20,4 @@ export const compatVersion = findCompatVersion(cliVersion);
18
20
  export const repositoryUrl = info.repository.url;
19
21
  export const isWindows = process.platform === 'win32';
20
22
  export const pathSeparator = isWindows ? ';' : ':';
23
+ export const cpuCount = cpus().length;
@@ -0,0 +1,28 @@
1
+ import { concurrentWorkers } from './parallel';
2
+
3
+ describe('Concurrent Workers', () => {
4
+ it('Can run against no entries', async () => {
5
+ const result = await concurrentWorkers([], 10, () => Promise.resolve('foo'));
6
+ expect(result).toEqual([]);
7
+ });
8
+
9
+ it('Can run against single entry', async () => {
10
+ const result = await concurrentWorkers(['bar'], 10, (item) => Promise.resolve('foo' + item));
11
+ expect(result).toEqual(['foobar']);
12
+ });
13
+
14
+ it('Can run against less entries than concurrency', async () => {
15
+ const result = await concurrentWorkers(['bar', 'rba', 'abr', 'rab', 'arb', 'bra'], 10, (item) => Promise.resolve('foo' + item));
16
+ expect(result).toEqual(['foobar', 'foorba', 'fooabr', 'foorab', 'fooarb', 'foobra']);
17
+ });
18
+
19
+ it('Can run against more entries than concurrency', async () => {
20
+ const result = await concurrentWorkers(['bar', 'rba', 'abr', 'rab', 'arb', 'bra'], 2, (item) => Promise.resolve('foo' + item));
21
+ expect(result).toEqual(['foobar', 'foorba', 'fooabr', 'foorab', 'fooarb', 'foobra']);
22
+ });
23
+
24
+ it('Can run against equal entries than concurrency', async () => {
25
+ const result = await concurrentWorkers(['bar', 'rba', 'abr', 'rab', 'arb', 'bra'], 6, (item) => Promise.resolve('foo' + item));
26
+ expect(result).toEqual(['foobar', 'foorba', 'fooabr', 'foorab', 'fooarb', 'foobra']);
27
+ });
28
+ });
@@ -0,0 +1,21 @@
1
+ export async function concurrentWorkers<T, R>(
2
+ items: Array<T>,
3
+ concurrency: number,
4
+ worker: (item: T) => Promise<R>,
5
+ ): Promise<Array<R>> {
6
+ const maxItems = items.length;
7
+ const results: Array<R> = new Array(maxItems);
8
+ let offset = 0;
9
+
10
+ await Promise.all(
11
+ items.slice(0, concurrency).map(async () => {
12
+ while (offset < maxItems) {
13
+ const i = offset++;
14
+ const item = items[i];
15
+ results[i] = await worker(item);
16
+ }
17
+ }),
18
+ );
19
+
20
+ return results;
21
+ }
package/src/helpers.ts CHANGED
@@ -7,9 +7,11 @@ import {
7
7
  PiletPublishSource,
8
8
  PiralBuildType,
9
9
  PiletBuildType,
10
+ PiletPublishScheme,
10
11
  } from './types';
11
12
 
12
13
  export const schemaKeys: Array<PiletSchemaVersion> = ['v0', 'v1', 'v2', 'none'];
14
+ export const publishModeKeys: Array<PiletPublishScheme> = ['none', 'basic', 'bearer', 'digest'];
13
15
  export const fromKeys: Array<PiletPublishSource> = ['local', 'remote', 'npm'];
14
16
  export const piralBuildTypeKeys: Array<PiralBuildType> = ['all', 'release', 'emulator', 'emulator-sources'];
15
17
  export const piletBuildTypeKeys: Array<PiletBuildType> = ['default', 'standalone', 'manifest'];
@@ -218,6 +218,8 @@ export interface BundlerDefinition {
218
218
 
219
219
  export type PiletSchemaVersion = 'none' | 'v0' | 'v1' | 'v2';
220
220
 
221
+ export type PiletPublishScheme = 'none' | 'digest' | 'bearer' | 'basic';
222
+
221
223
  export type PiletPublishSource = 'local' | 'npm' | 'remote';
222
224
 
223
225
  export type PiralBuildType = 'all' | 'release' | 'emulator' | 'emulator-sources';