piral-cli 0.15.3 → 0.15.4-beta.5013

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 (48) hide show
  1. package/lib/apps/build-pilet.d.ts +4 -0
  2. package/lib/apps/build-pilet.js +5 -1
  3. package/lib/apps/build-pilet.js.map +1 -1
  4. package/lib/apps/build-piral.d.ts +4 -0
  5. package/lib/apps/build-piral.js +4 -1
  6. package/lib/apps/build-piral.js.map +1 -1
  7. package/lib/apps/debug-piral.js +15 -5
  8. package/lib/apps/debug-piral.js.map +1 -1
  9. package/lib/apps/publish-pilet.js +1 -0
  10. package/lib/apps/publish-pilet.js.map +1 -1
  11. package/lib/apps/run-emulator-piral.js +1 -1
  12. package/lib/apps/run-emulator-piral.js.map +1 -1
  13. package/lib/build/run-build-pilet.js +3 -3
  14. package/lib/build/run-build-pilet.js.map +1 -1
  15. package/lib/build/run-build-piral.js +3 -3
  16. package/lib/build/run-build-piral.js.map +1 -1
  17. package/lib/commands.js +8 -0
  18. package/lib/commands.js.map +1 -1
  19. package/lib/common/envs.js +2 -1
  20. package/lib/common/envs.js.map +1 -1
  21. package/lib/common/importmap.js +33 -27
  22. package/lib/common/importmap.js.map +1 -1
  23. package/lib/common/npm.d.ts +2 -2
  24. package/lib/common/npm.js +44 -28
  25. package/lib/common/npm.js.map +1 -1
  26. package/lib/common/package.d.ts +2 -2
  27. package/lib/common/package.js +41 -35
  28. package/lib/common/package.js.map +1 -1
  29. package/lib/common/watcher.d.ts +9 -1
  30. package/lib/common/watcher.js +69 -12
  31. package/lib/common/watcher.js.map +1 -1
  32. package/lib/types/public.d.ts +2 -0
  33. package/package.json +2 -2
  34. package/src/apps/build-pilet.ts +10 -0
  35. package/src/apps/build-piral.ts +9 -0
  36. package/src/apps/debug-piral.ts +20 -4
  37. package/src/apps/publish-pilet.ts +1 -0
  38. package/src/apps/run-emulator-piral.ts +1 -1
  39. package/src/build/run-build-pilet.ts +3 -1
  40. package/src/build/run-build-piral.ts +3 -1
  41. package/src/commands.ts +8 -0
  42. package/src/common/envs.ts +2 -1
  43. package/src/common/importmap.ts +14 -12
  44. package/src/common/npm.test.ts +48 -29
  45. package/src/common/npm.ts +20 -12
  46. package/src/common/package.ts +20 -12
  47. package/src/common/watcher.ts +82 -17
  48. package/src/types/public.ts +2 -0
@@ -1,20 +1,77 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.watcherTask = void 0;
13
+ const fs_1 = require("fs");
4
14
  function watcherTask(cb) {
5
- return new Promise((resolve, reject) => {
6
- const disposers = [];
7
- const context = {
8
- onClean(dispose) {
9
- disposers.push(dispose);
10
- },
11
- watch(file) { },
12
- };
13
- cb(context).then(() => {
14
- resolve();
15
- }, (err) => {
16
- reject(err);
15
+ let running = Promise.resolve();
16
+ let pending = false;
17
+ let notify = () => { };
18
+ const disposers = [];
19
+ const triggers = [];
20
+ const end = new Promise(resolve => {
21
+ notify = resolve;
22
+ });
23
+ const ref = {
24
+ data: undefined,
25
+ onTrigger(cb) {
26
+ triggers.push(cb);
27
+ },
28
+ end,
29
+ };
30
+ const reRun = () => __awaiter(this, void 0, void 0, function* () {
31
+ if (!pending) {
32
+ pending = true;
33
+ yield running;
34
+ disposers.splice(0, disposers.length).forEach((dispose) => {
35
+ dispose();
36
+ });
37
+ pending = false;
38
+ context.status = 'reoccuring';
39
+ yield run();
40
+ triggers.splice(0, triggers.length).forEach((trigger) => {
41
+ trigger();
42
+ });
43
+ }
44
+ });
45
+ const context = {
46
+ onClean(dispose) {
47
+ disposers.push(dispose);
48
+ },
49
+ watch(file) {
50
+ if ((0, fs_1.existsSync)(file)) {
51
+ const watcher = (0, fs_1.watch)(file, {
52
+ persistent: false,
53
+ }, reRun);
54
+ disposers.push(() => watcher.close());
55
+ }
56
+ },
57
+ dependOn(anotherRef) {
58
+ anotherRef.onTrigger(reRun);
59
+ },
60
+ close() {
61
+ cb = () => Promise.resolve(undefined);
62
+ reRun().then(notify);
63
+ },
64
+ status: 'initial',
65
+ };
66
+ const run = () => __awaiter(this, void 0, void 0, function* () {
67
+ running = cb(context).then((data) => {
68
+ ref.data = data;
17
69
  });
70
+ yield running;
71
+ return ref;
72
+ });
73
+ return new Promise((resolve, reject) => {
74
+ run().then(resolve, reject);
18
75
  });
19
76
  }
20
77
  exports.watcherTask = watcherTask;
@@ -1 +1 @@
1
- {"version":3,"file":"watcher.js","sourceRoot":"","sources":["../../src/common/watcher.ts"],"names":[],"mappings":";;;AAKA,SAAgB,WAAW,CAAC,EAAqD;IAC/E,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,MAAM,SAAS,GAAsB,EAAE,CAAC;QACxC,MAAM,OAAO,GAAmB;YAC9B,OAAO,CAAC,OAAO;gBACb,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC1B,CAAC;YACD,KAAK,CAAC,IAAI,IAAG,CAAC;SACf,CAAC;QACF,EAAE,CAAC,OAAO,CAAC,CAAC,IAAI,CACd,GAAG,EAAE;YACH,OAAO,EAAE,CAAC;QACZ,CAAC,EACD,CAAC,GAAG,EAAE,EAAE;YACN,MAAM,CAAC,GAAG,CAAC,CAAC;QACd,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAlBD,kCAkBC"}
1
+ {"version":3,"file":"watcher.js","sourceRoot":"","sources":["../../src/common/watcher.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2BAAuC;AAgBvC,SAAgB,WAAW,CAAW,EAAkD;IACtF,IAAI,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAChC,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,IAAI,MAAM,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;IAEtB,MAAM,SAAS,GAAsB,EAAE,CAAC;IACxC,MAAM,QAAQ,GAAsB,EAAE,CAAC;IACvC,MAAM,GAAG,GAAG,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE;QACtC,MAAM,GAAG,OAAO,CAAC;IACnB,CAAC,CAAC,CAAC;IACH,MAAM,GAAG,GAAkB;QACzB,IAAI,EAAE,SAAS;QACf,SAAS,CAAC,EAAE;YACV,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACpB,CAAC;QACD,GAAG;KACJ,CAAC;IAEF,MAAM,KAAK,GAAG,GAAS,EAAE;QACvB,IAAI,CAAC,OAAO,EAAE;YACZ,OAAO,GAAG,IAAI,CAAC;YACf,MAAM,OAAO,CAAC;YACd,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBACxD,OAAO,EAAE,CAAC;YACZ,CAAC,CAAC,CAAC;YACH,OAAO,GAAG,KAAK,CAAC;YAChB,OAAO,CAAC,MAAM,GAAG,YAAY,CAAC;YAC9B,MAAM,GAAG,EAAE,CAAC;YAEZ,QAAQ,CAAC,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBACtD,OAAO,EAAE,CAAC;YACZ,CAAC,CAAC,CAAC;SACJ;IACH,CAAC,CAAA,CAAC;IAEF,MAAM,OAAO,GAAmB;QAC9B,OAAO,CAAC,OAAO;YACb,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC1B,CAAC;QACD,KAAK,CAAC,IAAI;YACR,IAAI,IAAA,eAAU,EAAC,IAAI,CAAC,EAAE;gBACpB,MAAM,OAAO,GAAG,IAAA,UAAK,EACnB,IAAI,EACJ;oBACE,UAAU,EAAE,KAAK;iBAClB,EACD,KAAK,CACN,CAAC;gBACF,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;aACvC;QACH,CAAC;QACD,QAAQ,CAAC,UAAU;YACjB,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;QACD,KAAK;YACH,EAAE,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACtC,KAAK,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACvB,CAAC;QACD,MAAM,EAAE,SAAS;KAClB,CAAC;IAEF,MAAM,GAAG,GAAG,GAAS,EAAE;QACrB,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;YAClC,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;QAClB,CAAC,CAAC,CAAC;QACH,MAAM,OAAO,CAAC;QACd,OAAO,GAAG,CAAC;IACb,CAAC,CAAA,CAAC;IAEF,OAAO,IAAI,OAAO,CAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACpD,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;AACL,CAAC;AAxED,kCAwEC"}
@@ -76,6 +76,7 @@ export interface BuildPiralParameters extends BaseBundleParameters {
76
76
  emulator: boolean;
77
77
  standalone: boolean;
78
78
  sourceMaps: boolean;
79
+ watch: boolean;
79
80
  contentHash: boolean;
80
81
  minify: boolean;
81
82
  externals: Array<string>;
@@ -100,6 +101,7 @@ export interface DebugPiletParameters extends BaseBundleParameters {
100
101
  export interface BuildPiletParameters extends BaseBundleParameters {
101
102
  piralInstances: Array<string>;
102
103
  sourceMaps: boolean;
104
+ watch: boolean;
103
105
  contentHash: boolean;
104
106
  minify: boolean;
105
107
  externals: Array<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "piral-cli",
3
- "version": "0.15.3",
3
+ "version": "0.15.4-beta.5013",
4
4
  "description": "The standard CLI for creating and building a Piral instance or a Pilet.",
5
5
  "keywords": [
6
6
  "portal",
@@ -80,5 +80,5 @@
80
80
  "typescript": "^4.0.0",
81
81
  "yargs": "^15.0.0"
82
82
  },
83
- "gitHead": "14b0f1a34c86dd85bd66b66ebbca9c1dc5eb7f59"
83
+ "gitHead": "58a5f43b7d8f4ed1bf34b8cf79ac6f38de998419"
84
84
  }
@@ -112,6 +112,11 @@ export interface BuildPiletOptions {
112
112
  */
113
113
  sourceMaps?: boolean;
114
114
 
115
+ /**
116
+ * States if the build should run continuously and re-build when files change.
117
+ */
118
+ watch?: boolean;
119
+
115
120
  /**
116
121
  * Sets the bundler to use for building, if any specific.
117
122
  */
@@ -165,6 +170,7 @@ export const buildPiletDefaults: BuildPiletOptions = {
165
170
  type: 'default',
166
171
  fresh: false,
167
172
  sourceMaps: true,
173
+ watch: false,
168
174
  contentHash: true,
169
175
  optimizeModules: false,
170
176
  schemaVersion: config.schemaVersion,
@@ -180,6 +186,7 @@ export async function buildPilet(baseDir = process.cwd(), options: BuildPiletOpt
180
186
  logLevel = buildPiletDefaults.logLevel,
181
187
  minify = buildPiletDefaults.minify,
182
188
  sourceMaps = buildPiletDefaults.sourceMaps,
189
+ watch = buildPiletDefaults.watch,
183
190
  contentHash = buildPiletDefaults.contentHash,
184
191
  fresh = buildPiletDefaults.fresh,
185
192
  concurrency = buildPiletDefaults.concurrency,
@@ -233,6 +240,7 @@ export async function buildPilet(baseDir = process.cwd(), options: BuildPiletOpt
233
240
  piralInstances,
234
241
  optimizeModules,
235
242
  sourceMaps,
243
+ watch,
236
244
  contentHash,
237
245
  minify,
238
246
  externals,
@@ -313,6 +321,7 @@ export async function buildPilet(baseDir = process.cwd(), options: BuildPiletOpt
313
321
  standalone: true,
314
322
  optimizeModules: false,
315
323
  sourceMaps,
324
+ watch: false,
316
325
  contentHash,
317
326
  minify,
318
327
  externals: [],
@@ -338,6 +347,7 @@ export async function buildPilet(baseDir = process.cwd(), options: BuildPiletOpt
338
347
  standalone: true,
339
348
  optimizeModules: false,
340
349
  sourceMaps,
350
+ watch: false,
341
351
  contentHash,
342
352
  minify,
343
353
  externals: externals.map(m => m.name),
@@ -69,6 +69,11 @@ export interface BuildPiralOptions {
69
69
  */
70
70
  sourceMaps?: boolean;
71
71
 
72
+ /**
73
+ * States if the build should run continuously and re-build when files change.
74
+ */
75
+ watch?: boolean;
76
+
72
77
  /**
73
78
  * Sets the bundler to use for building, if any specific.
74
79
  */
@@ -114,6 +119,7 @@ export const buildPiralDefaults: BuildPiralOptions = {
114
119
  type: 'all',
115
120
  subdir: true,
116
121
  sourceMaps: true,
122
+ watch: false,
117
123
  contentHash: true,
118
124
  optimizeModules: false,
119
125
  };
@@ -138,6 +144,7 @@ export async function buildPiral(baseDir = process.cwd(), options: BuildPiralOpt
138
144
  logLevel = buildPiralDefaults.logLevel,
139
145
  minify = buildPiralDefaults.minify,
140
146
  sourceMaps = buildPiralDefaults.sourceMaps,
147
+ watch = buildPiralDefaults.watch,
141
148
  contentHash = buildPiralDefaults.contentHash,
142
149
  subdir = buildPiralDefaults.subdir,
143
150
  fresh = buildPiralDefaults.fresh,
@@ -190,6 +197,7 @@ export async function buildPiral(baseDir = process.cwd(), options: BuildPiralOpt
190
197
  standalone: false,
191
198
  optimizeModules,
192
199
  sourceMaps,
200
+ watch,
193
201
  contentHash,
194
202
  minify: false,
195
203
  externals: externals.map(m => m.name),
@@ -261,6 +269,7 @@ export async function buildPiral(baseDir = process.cwd(), options: BuildPiralOpt
261
269
  standalone: false,
262
270
  optimizeModules,
263
271
  sourceMaps,
272
+ watch,
264
273
  contentHash,
265
274
  minify,
266
275
  externals: externals.map(m => m.name),
@@ -124,14 +124,16 @@ export async function debugPiral(baseDir = process.cwd(), options: DebugPiralOpt
124
124
 
125
125
  await hooks.onBegin?.({ options, fullBase });
126
126
 
127
- await watcherTask(async (watcherContext) => {
128
- progress('Reading configuration ...');
127
+ progress('Reading configuration ...');
128
+
129
+ const buildRef = await watcherTask(async (watcherContext) => {
129
130
  const entryFiles = await retrievePiralRoot(fullBase, entry);
130
- const targetDir = dirname(entryFiles);
131
131
  const { externals, name, root, ignored } = await retrievePiletsInfo(entryFiles);
132
132
  const piralInstances = [name];
133
133
  const dest = getDestination(entryFiles, resolve(fullBase, target));
134
134
 
135
+ watcherContext.watch(join(root, 'package.json'));
136
+
135
137
  await checkCliCompatibility(root);
136
138
 
137
139
  await hooks.beforeBuild?.({ root, publicUrl, externals, entryFiles, piralInstances });
@@ -160,7 +162,14 @@ export async function debugPiral(baseDir = process.cwd(), options: DebugPiralOpt
160
162
  });
161
163
 
162
164
  bundler.start();
165
+ return { bundler, entryFiles, root };
166
+ });
167
+
168
+ const serverRef = await watcherTask(async (watcherContext) => {
169
+ watcherContext.dependOn(buildRef);
163
170
 
171
+ const { bundler, entryFiles, root } = buildRef.data;
172
+ const targetDir = dirname(entryFiles);
164
173
  const krasBaseConfig = resolve(fullBase, krasrc);
165
174
  const krasRootConfig = resolve(root, krasrc);
166
175
  const mocks = join(targetDir, 'mocks');
@@ -182,6 +191,10 @@ export async function debugPiral(baseDir = process.cwd(), options: DebugPiralOpt
182
191
  },
183
192
  },
184
193
  };
194
+
195
+ watcherContext.watch(krasBaseConfig);
196
+ watcherContext.watch(krasRootConfig);
197
+
185
198
  const krasConfig = readKrasConfig({ port, initial, required }, krasBaseConfig, krasRootConfig);
186
199
 
187
200
  log('generalVerbose_0004', `Using kras with configuration: ${JSON.stringify(krasConfig, undefined, 2)}`);
@@ -195,8 +208,11 @@ export async function debugPiral(baseDir = process.cwd(), options: DebugPiralOpt
195
208
  await krasServer.start();
196
209
  openBrowser(open, port, publicUrl, !!krasConfig.ssl);
197
210
  await hooks.afterOnline?.({ krasServer, krasConfig, open, port, publicUrl });
198
- await new Promise((resolve) => krasServer.on('close', resolve));
211
+
212
+ watcherContext.onClean(() => krasServer.stop());
199
213
  });
200
214
 
215
+ await Promise.all([buildRef.end, serverRef.end]);
216
+
201
217
  await hooks.onEnd?.({});
202
218
  }
@@ -159,6 +159,7 @@ async function getFiles(
159
159
  piralInstances,
160
160
  optimizeModules: false,
161
161
  sourceMaps: true,
162
+ watch: false,
162
163
  contentHash: true,
163
164
  minify: true,
164
165
  externals,
@@ -117,7 +117,7 @@ always-auth=true`,
117
117
 
118
118
  const npmClient = await determineNpmClient(appRoot, defaultNpmClient);
119
119
  const [packageName] = await installPiralInstance(app, fullBase, appRoot, npmClient);
120
- const piral = findPiralInstance(packageName, appRoot, originalPort);
120
+ const piral = await findPiralInstance(packageName, appRoot, originalPort);
121
121
  const port = await getAvailablePort(piral.port);
122
122
 
123
123
  const krasBaseConfig = resolve(fullBase, krasrc);
@@ -7,6 +7,7 @@ function run(
7
7
  root: string,
8
8
  piralInstances: Array<string>,
9
9
  sourceMaps: boolean,
10
+ watch: boolean,
10
11
  contentHash: boolean,
11
12
  minify: boolean,
12
13
  externals: Array<string>,
@@ -40,7 +41,7 @@ function run(
40
41
  contentHash,
41
42
  minify,
42
43
  logLevel,
43
- watch: false,
44
+ watch,
44
45
  args,
45
46
  });
46
47
  }
@@ -56,6 +57,7 @@ process.on('message', async (msg) => {
56
57
  process.cwd(),
57
58
  msg.piralInstances,
58
59
  msg.sourceMaps,
60
+ msg.watch,
59
61
  msg.contentHash,
60
62
  msg.minify,
61
63
  msg.externals,
@@ -9,6 +9,7 @@ function run(
9
9
  emulator: boolean,
10
10
  standalone: boolean,
11
11
  sourceMaps: boolean,
12
+ watch: boolean,
12
13
  contentHash: boolean,
13
14
  minify: boolean,
14
15
  externals: Array<string>,
@@ -42,7 +43,7 @@ function run(
42
43
  publicUrl,
43
44
  hmr: false,
44
45
  logLevel,
45
- watch: false,
46
+ watch,
46
47
  args,
47
48
  });
48
49
  }
@@ -60,6 +61,7 @@ process.on('message', async (msg) => {
60
61
  msg.emulator,
61
62
  msg.standalone,
62
63
  msg.sourceMaps,
64
+ msg.watch,
63
65
  msg.contentHash,
64
66
  msg.minify,
65
67
  msg.externals,
package/src/commands.ts CHANGED
@@ -142,6 +142,9 @@ const allCommands: Array<ToolCommand<any>> = [
142
142
  .boolean('source-maps')
143
143
  .describe('source-maps', 'Create associated source maps for the bundles.')
144
144
  .default('source-maps', apps.buildPiralDefaults.sourceMaps)
145
+ .boolean('watch')
146
+ .describe('watch', 'Continuously re-builds while watching the source files.')
147
+ .default('watch', apps.buildPiralDefaults.watch)
145
148
  .boolean('subdir')
146
149
  .describe(
147
150
  'subdir',
@@ -175,6 +178,7 @@ const allCommands: Array<ToolCommand<any>> = [
175
178
  subdir: args.subdir as boolean,
176
179
  contentHash: args['content-hash'] as boolean,
177
180
  sourceMaps: args['source-maps'] as boolean,
181
+ watch: args.watch as boolean,
178
182
  optimizeModules: args['optimize-modules'] as boolean,
179
183
  logLevel: args['log-level'] as LogLevels,
180
184
  type: args.type as PiralBuildType,
@@ -507,6 +511,9 @@ const allCommands: Array<ToolCommand<any>> = [
507
511
  .boolean('source-maps')
508
512
  .describe('source-maps', 'Creates source maps for the bundles.')
509
513
  .default('source-maps', apps.buildPiletDefaults.sourceMaps)
514
+ .boolean('watch')
515
+ .describe('watch', 'Continuously re-builds while watching the source files.')
516
+ .default('watch', apps.buildPiletDefaults.watch)
510
517
  .boolean('fresh')
511
518
  .describe('fresh', 'Performs a fresh build by removing the target directory first.')
512
519
  .default('fresh', apps.buildPiletDefaults.fresh)
@@ -547,6 +554,7 @@ const allCommands: Array<ToolCommand<any>> = [
547
554
  bundlerName: args.bundler as string,
548
555
  declaration: args.declaration as boolean,
549
556
  sourceMaps: args['source-maps'] as boolean,
557
+ watch: args.watch as boolean,
550
558
  optimizeModules: args['optimize-modules'] as boolean,
551
559
  type: args.type as PiletBuildType,
552
560
  fresh: args.fresh as boolean,
@@ -1,4 +1,5 @@
1
1
  import { join } from 'path';
2
+ import { readFileSync } from 'fs';
2
3
  import { log } from './log';
3
4
  import { frameworkLibs } from './constants';
4
5
  import { pathSeparator, cliVersion, compatVersion } from './info';
@@ -11,7 +12,7 @@ function hasPath(path: string) {
11
12
 
12
13
  export function setStandardEnvs(options: StandardEnvProps) {
13
14
  log('generalDebug_0003', `Setting environment variables in "${options.root}" ...`);
14
- const packageJson = require(join(options.root, 'package.json'));
15
+ const packageJson = JSON.parse(readFileSync(join(options.root, 'package.json'), 'utf8'));
15
16
  const binDir = join(options.root, 'node_modules', '.bin');
16
17
 
17
18
  process.env.BUILD_TIME = new Date().toDateString();
@@ -1,4 +1,4 @@
1
- import { resolve, dirname, isAbsolute } from 'path';
1
+ import { resolve, dirname, isAbsolute, basename } from 'path';
2
2
  import { log, fail } from './log';
3
3
  import { satisfies, validate } from './version';
4
4
  import { computeHash } from './hash';
@@ -16,12 +16,14 @@ function getDependencyDetails(depName: string): [assetName: string, identifier:
16
16
  return [assetName, id, version];
17
17
  }
18
18
 
19
- function getLocalDependencyVersion(
19
+ async function getLocalDependencyVersion(
20
20
  packageJson: string,
21
21
  depName: string,
22
22
  versionSpec: string,
23
- ): [offeredVersion: string, requiredVersion: string] {
24
- const details = require(packageJson);
23
+ ): Promise<[offeredVersion: string, requiredVersion: string]> {
24
+ const packageDir = dirname(packageJson);
25
+ const packageFile = basename(packageJson);
26
+ const details = await readJson(packageDir, packageFile);
25
27
 
26
28
  if (versionSpec) {
27
29
  if (!validate(versionSpec)) {
@@ -38,24 +40,24 @@ function getLocalDependencyVersion(
38
40
  return [details.version, details.version];
39
41
  }
40
42
 
41
- function getInheritedDependencies(inheritedImport: string, dir: string): Promise<Array<SharedDependency>> {
43
+ async function getInheritedDependencies(inheritedImport: string, dir: string): Promise<Array<SharedDependency>> {
42
44
  const packageJson = tryResolvePackage(`${inheritedImport}/package.json`, dir);
43
45
 
44
46
  if (packageJson) {
45
47
  const packageDir = dirname(packageJson);
46
- const packageDetails = require(packageJson);
48
+ const packageDetails = await readJson(packageDir, 'package.json');
47
49
  return readImportmap(packageDir, packageDetails, true);
48
50
  } else {
49
51
  const directImportmap = tryResolvePackage(inheritedImport, dir);
50
52
 
51
53
  if (directImportmap) {
52
- const content = require(directImportmap);
53
54
  const baseDir = dirname(directImportmap);
54
- return resolveImportmap(baseDir, content);
55
+ const content = await readJson(baseDir, basename(directImportmap));
56
+ return await resolveImportmap(baseDir, content);
55
57
  }
56
58
  }
57
59
 
58
- return Promise.resolve([]);
60
+ return [];
59
61
  }
60
62
 
61
63
  async function resolveImportmap(dir: string, importmap: Importmap): Promise<Array<SharedDependency>> {
@@ -86,7 +88,7 @@ async function resolveImportmap(dir: string, importmap: Importmap): Promise<Arra
86
88
 
87
89
  if (entry) {
88
90
  const packageJson = await findFile(dirname(entry), 'package.json');
89
- const [version, requireVersion] = getLocalDependencyVersion(packageJson, depName, versionSpec);
91
+ const [version, requireVersion] = await getLocalDependencyVersion(packageJson, depName, versionSpec);
90
92
 
91
93
  dependencies.push({
92
94
  id: `${identifier}@${version}`,
@@ -104,7 +106,7 @@ async function resolveImportmap(dir: string, importmap: Importmap): Promise<Arra
104
106
 
105
107
  if (entry) {
106
108
  const packageJson = await findFile(dirname(entry), 'package.json');
107
- const [version, requireVersion] = getLocalDependencyVersion(packageJson, depName, versionSpec);
109
+ const [version, requireVersion] = await getLocalDependencyVersion(packageJson, depName, versionSpec);
108
110
 
109
111
  dependencies.push({
110
112
  id: `${identifier}@${version}`,
@@ -129,7 +131,7 @@ async function resolveImportmap(dir: string, importmap: Importmap): Promise<Arra
129
131
  const packageJsonExists = await checkExists(packageJson);
130
132
 
131
133
  if (packageJsonExists) {
132
- const [version, requireVersion] = getLocalDependencyVersion(packageJson, depName, versionSpec);
134
+ const [version, requireVersion] = await getLocalDependencyVersion(packageJson, depName, versionSpec);
133
135
 
134
136
  dependencies.push({
135
137
  id: `${identifier}@${version}`,
@@ -27,9 +27,23 @@ import {
27
27
  jest.mock('child_process');
28
28
 
29
29
  jest.mock('../external', () => ({
30
+ ...jest.requireActual('../external'),
30
31
  rc() {},
31
32
  ora() {
32
- return {};
33
+ return {
34
+ succeed(msg) {
35
+ console.warn(msg);
36
+ },
37
+ warn(msg) {
38
+ console.warn(msg);
39
+ },
40
+ info(msg) {
41
+ console.info(msg);
42
+ },
43
+ fail(msg) {
44
+ console.error(msg);
45
+ },
46
+ };
33
47
  },
34
48
  getModulePath(root, moduleName) {
35
49
  return require.resolve(moduleName, {
@@ -54,9 +68,7 @@ jest.mock('./scripts', () => ({
54
68
  }));
55
69
 
56
70
  jest.mock('fs', () => ({
57
- createReadStream() {
58
- return undefined;
59
- },
71
+ ...jest.requireActual('fs'),
60
72
  exists: (file: string, cb: (status: boolean) => void) =>
61
73
  cb(
62
74
  shouldFind &&
@@ -67,6 +79,12 @@ jest.mock('fs', () => ({
67
79
  return true;
68
80
  },
69
81
  readFile: (file: string, type: string, callback: (err: NodeJS.ErrnoException | undefined, data: string) => void) => {
82
+ const fs = jest.requireActual('fs');
83
+
84
+ if (fs.existsSync(file)) {
85
+ return fs.readFile(file, type, callback);
86
+ }
87
+
70
88
  return callback(undefined, '');
71
89
  },
72
90
  realpathSync: () => ({}),
@@ -384,11 +402,12 @@ describe('npm Module', () => {
384
402
  });
385
403
 
386
404
  it('verify whether package is linked', () => {
387
- let result = isLinkedPackage('foo', 'registry', false);
405
+ const target = process.cwd();
406
+ let result = isLinkedPackage('foo', 'registry', false, target);
388
407
  expect(result).toBeFalsy();
389
- result = isLinkedPackage('foo', 'file', false);
408
+ result = isLinkedPackage('foo', 'file', false, target);
390
409
  expect(result).toBeFalsy();
391
- result = isLinkedPackage('foo', 'git', false);
410
+ result = isLinkedPackage('foo', 'git', false, target);
392
411
  expect(result).toBeFalsy();
393
412
  });
394
413
 
@@ -403,16 +422,16 @@ describe('npm Module', () => {
403
422
  expect(result).toBe('foo');
404
423
  });
405
424
 
406
- it('retrieve package name', () => {
425
+ it('retrieve package name', async () => {
407
426
  let result = getPackageName('./', 'foo', 'file');
408
427
  expect(result).toBeInstanceOf(Promise);
409
- result.then((message) => expect(message).toBeUndefined());
428
+ await result.then((message) => expect(message).toBeUndefined());
410
429
  result = getPackageName('./', 'foo', 'git');
411
430
  expect(result).toBeInstanceOf(Promise);
412
- result.then((message) => expect(message).toBeUndefined());
431
+ await result.then((message) => expect(message).toBeUndefined());
413
432
  result = getPackageName('./', 'foo', 'registry');
414
433
  expect(result).toBeInstanceOf(Promise);
415
- result.then((name) => expect(name).toEqual('foo'));
434
+ await result.then((name) => expect(name).toEqual('foo'));
416
435
  });
417
436
 
418
437
  it('gets path to file package', () => {
@@ -436,39 +455,39 @@ describe('npm Module', () => {
436
455
  expect(result).toEqual('git+https://.foo.git');
437
456
  });
438
457
 
439
- it('gets path to git package', () => {
458
+ it('gets path to git package', async () => {
440
459
  const result = getCurrentPackageDetails('./', './foo.tgz', '', 'file://foo.tgz', './');
441
- result.then(([path, version]) => {
460
+ await result.then(([path, version]) => {
442
461
  expect(path).not.toBeUndefined();
443
462
  });
444
463
  const result2 = getCurrentPackageDetails('./', './foo.tgz', '', 'git+https://.foo.git', './');
445
- result2.then(([path, version]) => {
464
+ await result2.then(([path, version]) => {
446
465
  expect(path).not.toBeUndefined();
447
466
  });
448
467
  const result3 = getCurrentPackageDetails('./', './foo.tgz', '1.0.0', 'latest', './');
449
- result3.then(([path, version]) => {
468
+ await result3.then(([path, version]) => {
450
469
  expect(path).toEqual('./foo.tgz@latest');
451
470
  expect(version).toEqual('latest');
452
471
  });
453
472
  });
454
473
 
455
- it('makeExternals without externals returns coreExternals', () => {
456
- const externals = makeExternals(process.cwd(), { piral: '*' });
474
+ it('makeExternals without externals returns coreExternals', async () => {
475
+ const externals = await makeExternals(process.cwd(), { piral: '*' }, []);
457
476
  expect(externals).toEqual(['react', 'react-dom', 'react-router', 'react-router-dom', 'tslib']);
458
477
  });
459
478
 
460
- it('makeExternals with no externals returns coreExternals', () => {
461
- const externals = makeExternals(process.cwd(), { piral: '*' }, []);
479
+ it('makeExternals with no externals returns coreExternals', async () => {
480
+ const externals = await makeExternals(process.cwd(), { piral: '*' }, []);
462
481
  expect(externals).toEqual(['react', 'react-dom', 'react-router', 'react-router-dom', 'tslib']);
463
482
  });
464
483
 
465
- it('makeExternals with exclude coreExternals returns empty set', () => {
466
- const externals = makeExternals(process.cwd(), { piral: '*' }, ['!*']);
484
+ it('makeExternals with exclude coreExternals returns empty set', async () => {
485
+ const externals = await makeExternals(process.cwd(), { piral: '*' }, ['!*']);
467
486
  expect(externals).toEqual([]);
468
487
  });
469
488
 
470
- it('makeExternals with externals concats coreExternals', () => {
471
- const externals = makeExternals(process.cwd(), { piral: '*' }, ['foo', 'bar']);
489
+ it('makeExternals with externals concats coreExternals', async () => {
490
+ const externals = await makeExternals(process.cwd(), { piral: '*' }, ['foo', 'bar']);
472
491
  expect(externals).toEqual([
473
492
  'foo',
474
493
  'bar',
@@ -480,18 +499,18 @@ describe('npm Module', () => {
480
499
  ]);
481
500
  });
482
501
 
483
- it('makeExternals with external duplicate only reflects coreExternals', () => {
484
- const externals = makeExternals(process.cwd(), { piral: '*' }, ['react', 'foo']);
502
+ it('makeExternals with external duplicate only reflects coreExternals', async () => {
503
+ const externals = await makeExternals(process.cwd(), { piral: '*' }, ['react', 'foo']);
485
504
  expect(externals).toEqual(['react', 'foo', 'react-dom', 'react-router', 'react-router-dom', 'tslib']);
486
505
  });
487
506
 
488
- it('makeExternals with explicit include and exclude', () => {
489
- const externals = makeExternals(process.cwd(), { piral: '*' }, ['react', 'react-calendar', '!tslib']);
507
+ it('makeExternals with explicit include and exclude', async () => {
508
+ const externals = await makeExternals(process.cwd(), { piral: '*' }, ['react', 'react-calendar', '!tslib']);
490
509
  expect(externals).toEqual(['react', 'react-calendar', 'react-dom', 'react-router', 'react-router-dom']);
491
510
  });
492
511
 
493
- it('makeExternals with all exclude and explicit include', () => {
494
- const externals = makeExternals(process.cwd(), { piral: '*' }, ['react', 'react-router-dom', '!*']);
512
+ it('makeExternals with all exclude and explicit include', async () => {
513
+ const externals = await makeExternals(process.cwd(), { piral: '*' }, ['react', 'react-router-dom', '!*']);
495
514
  expect(externals).toEqual(['react', 'react-router-dom']);
496
515
  });
497
516
  });