@travetto/compiler 3.1.0-rc.2 → 3.1.0-rc.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/compiler",
3
- "version": "3.1.0-rc.2",
3
+ "version": "3.1.0-rc.4",
4
4
  "description": "The compiler infrastructure for the Travetto framework",
5
5
  "keywords": [
6
6
  "compiler",
@@ -31,12 +31,12 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@parcel/watcher": "^2.1.0",
34
- "@travetto/manifest": "^3.1.0-rc.1",
35
- "@travetto/terminal": "^3.1.0-rc.0",
36
- "@travetto/transformer": "^3.1.0-rc.2"
34
+ "@travetto/manifest": "^3.1.0-rc.2",
35
+ "@travetto/terminal": "^3.1.0-rc.1",
36
+ "@travetto/transformer": "^3.1.0-rc.3"
37
37
  },
38
38
  "peerDependencies": {
39
- "@travetto/cli": "^3.1.0-rc.3"
39
+ "@travetto/cli": "^3.1.0-rc.6"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@travetto/cli": {
package/src/compiler.ts CHANGED
@@ -72,10 +72,15 @@ export class Compiler {
72
72
  */
73
73
  async * emit(files: string[], emitter: CompileEmitter): AsyncIterable<CompileEmitEvent> {
74
74
  let i = 0;
75
+ let lastSent = Date.now();
75
76
  for (const file of files) {
76
77
  const err = await emitter(file);
77
78
  const imp = file.replace(/.*node_modules\//, '');
78
79
  yield { file: imp, i: i += 1, err, total: files.length };
80
+ if ((Date.now() - lastSent) > 50) { // Limit to 1 every 50ms
81
+ lastSent = Date.now();
82
+ process.send?.({ type: 'status', total: files.length, idx: i });
83
+ }
79
84
  }
80
85
  Log.debug(`Compiled ${i} files`);
81
86
  }
@@ -89,7 +94,9 @@ export class Compiler {
89
94
 
90
95
  Log.debug('Compilation started');
91
96
 
92
- process.on('disconnect', () => process.exit(0));
97
+ if (process.send) {
98
+ process.on('disconnect', () => process.exit(0));
99
+ }
93
100
 
94
101
  const emitter = await this.getCompiler();
95
102
  let failed = false;
@@ -104,6 +111,8 @@ export class Compiler {
104
111
  return { idx: i, total, text: `Compiling [%idx/%total] -- ${file}` };
105
112
  };
106
113
 
114
+ process.send?.({ type: 'start' });
115
+
107
116
  if (this.#dirtyFiles.length) {
108
117
  await GlobalTerminal.trackProgress(this.emit(this.#dirtyFiles, emitter), resolveEmittedFile, { position: 'bottom', minDelay: 50 });
109
118
  if (failed) {
@@ -118,7 +127,7 @@ export class Compiler {
118
127
  await emitter(resolved, true);
119
128
  }
120
129
 
121
- process.send?.('build-complete');
130
+ process.send?.({ type: 'complete' });
122
131
 
123
132
  if (this.#watch) {
124
133
  Log.info('Watch is ready');
@@ -135,7 +144,7 @@ export class Compiler {
135
144
  }
136
145
  }
137
146
  if (!process.exitCode) {
138
- process.send?.('restart');
147
+ process.send?.({ type: 'restart' });
139
148
  }
140
149
  }
141
150
  }
@@ -3,7 +3,7 @@ import path from 'path';
3
3
 
4
4
  import type { ManifestContext } from '@travetto/manifest';
5
5
 
6
- import { TranspileUtil, CompileResult } from './transpile';
6
+ import { TranspileUtil, CompileResult, BuildEvent } from './transpile';
7
7
  import { LockManager } from './lock';
8
8
  import { LogUtil } from './log';
9
9
 
@@ -16,7 +16,7 @@ const importManifest = (ctx: ManifestContext): Promise<typeof import('@travetto/
16
16
  /**
17
17
  * Run the compiler
18
18
  */
19
- async function compile(ctx: ManifestContext, op: 'watch' | 'build' | undefined, onMessage: (msg: unknown) => void): Promise<CompileResult> {
19
+ async function compile(ctx: ManifestContext, op: 'watch' | 'build' | undefined, onMessage: (msg: BuildEvent) => void): Promise<CompileResult> {
20
20
  let changes = 0;
21
21
 
22
22
  await LogUtil.withLogger('precompile', async () => {
@@ -117,7 +117,10 @@ export async function launch(ctx: ManifestContext, root: ManifestContext, op?: '
117
117
  if (op !== 'manifest' && await LockManager.getCompileAction(root, op) === 'build') {
118
118
 
119
119
  // Ready signal
120
- process.send?.('ready');
120
+ if (process.send) {
121
+ process.send('ready');
122
+ process.on('disconnect', () => process.exit(0));
123
+ }
121
124
 
122
125
  await LockManager.withLocks(root, async (acquire, release) => {
123
126
  let action: CompileResult;
@@ -127,8 +130,8 @@ export async function launch(ctx: ManifestContext, root: ManifestContext, op?: '
127
130
  acquire('build');
128
131
  }
129
132
  action = await compile(root, op, msg => {
130
- switch (msg) {
131
- case 'build-complete': {
133
+ switch (msg.type) {
134
+ case 'complete': {
132
135
  release('build');
133
136
  break;
134
137
  }
@@ -138,6 +141,11 @@ export async function launch(ctx: ManifestContext, root: ManifestContext, op?: '
138
141
  });
139
142
  }
140
143
 
144
+ // Disconnect for non-cli operations
145
+ if (op && process.send) {
146
+ process.disconnect();
147
+ }
148
+
141
149
  switch (op) {
142
150
  case 'manifest': return exportManifest(ctx, ...args);
143
151
  case 'build': return LogUtil.log('build', [], 'info', 'Successfully built');
package/support/lock.ts CHANGED
@@ -105,7 +105,7 @@ export class LockManager {
105
105
  handler();
106
106
 
107
107
  remove = (): void => {
108
- clearInterval(timer);
108
+ clearTimeout(timer);
109
109
  unwatchFile(file, handler);
110
110
  };
111
111
  });
@@ -11,11 +11,15 @@ import { LogUtil } from './log';
11
11
 
12
12
  type ModFile = { input: string, output: string, stale: boolean };
13
13
  export type CompileResult = 'restart' | 'complete' | 'skipped';
14
+ export type BuildEvent = { type: 'restart' | 'start' | 'complete' } | { type: 'status', idx: number, total: number };
14
15
 
15
16
  const OPT_CACHE: Record<string, import('typescript').CompilerOptions> = {};
16
17
  const SRC_REQ = createRequire(path.resolve('node_modules'));
17
18
  const RECENT_STAT = (stat: { ctimeMs: number, mtimeMs: number }): number => Math.max(stat.ctimeMs, stat.mtimeMs);
18
19
 
20
+ const isBuildEvent = (ev: unknown): ev is BuildEvent =>
21
+ ev !== undefined && ev !== null && typeof ev === 'object' && 'type' in ev && typeof ev.type === 'string';
22
+
19
23
  /**
20
24
  * Transpile utilities for launching
21
25
  */
@@ -190,7 +194,7 @@ export class TranspileUtil {
190
194
  /**
191
195
  * Run compiler
192
196
  */
193
- static async runCompiler(ctx: ManifestContext, manifest: ManifestRoot, changed: DeltaEvent[], watch: boolean, onMessage: (msg: unknown) => void): Promise<CompileResult> {
197
+ static async runCompiler(ctx: ManifestContext, manifest: ManifestRoot, changed: DeltaEvent[], watch: boolean, onMessage: (msg: BuildEvent) => void): Promise<CompileResult> {
194
198
  const compiler = path.resolve(ctx.workspacePath, ctx.compilerFolder);
195
199
  const main = path.resolve(compiler, 'node_modules', '@travetto/compiler/support/compiler-entry.js');
196
200
  const deltaFile = path.resolve(os.tmpdir(), `manifest-delta.${process.pid}.${process.ppid}.${Date.now()}.json`);
@@ -216,10 +220,14 @@ export class TranspileUtil {
216
220
  .on('message', msg => {
217
221
  if (LogUtil.isLogEvent(msg)) {
218
222
  log(...msg);
219
- } else if (msg === 'restart') {
220
- res(msg);
221
- } else {
222
- onMessage(msg);
223
+ } else if (isBuildEvent(msg)) {
224
+ // Send to parent if exists
225
+ process.send?.(msg);
226
+ if (msg.type === 'restart') {
227
+ res('restart');
228
+ } else {
229
+ onMessage(msg);
230
+ }
223
231
  }
224
232
  })
225
233
  .on('exit', code => (code !== null && code > 0) ? rej(new Error('Failed during compilation')) : res('complete'));
@@ -231,6 +239,8 @@ export class TranspileUtil {
231
239
  await timers.setTimeout(150 + 100 * Math.random());
232
240
  }
233
241
 
242
+ LogUtil.log('compiler-exec', [], 'info', `Result ${result}, exit code: ${proc?.exitCode}`);
243
+
234
244
  return result;
235
245
  } finally {
236
246
  if (proc?.killed === false) { proc.kill('SIGKILL'); }