forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschematic-workflow.ts
More file actions
89 lines (79 loc) · 2.52 KB
/
schematic-workflow.ts
File metadata and controls
89 lines (79 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { logging } from '@angular-devkit/core';
import { NodeWorkflow } from '@angular-devkit/schematics/tools';
import { colors } from '../../utilities/color';
function removeLeadingSlash(value: string): string {
return value[0] === '/' ? value.slice(1) : value;
}
export function subscribeToWorkflow(
workflow: NodeWorkflow,
logger: logging.LoggerApi,
): {
files: Set<string>;
error: boolean;
unsubscribe: () => void;
} {
const files = new Set<string>();
let error = false;
let logs: string[] = [];
const reporterSubscription = workflow.reporter.subscribe((event) => {
// Strip leading slash to prevent confusion.
const eventPath = removeLeadingSlash(event.path);
switch (event.kind) {
case 'error':
error = true;
logger.error(
`ERROR! ${eventPath} ${event.description == 'alreadyExist' ? 'already exists' : 'does not exist'}.`,
);
break;
case 'update':
logs.push(
// TODO: `as unknown` was necessary during TS 5.9 update. Figure out a long-term solution.
`${colors.cyan('UPDATE')} ${eventPath} (${(event.content as unknown as Buffer).length} bytes)`,
);
files.add(eventPath);
break;
case 'create':
logs.push(
// TODO: `as unknown` was necessary during TS 5.9 update. Figure out a long-term solution.
`${colors.green('CREATE')} ${eventPath} (${(event.content as unknown as Buffer).length} bytes)`,
);
files.add(eventPath);
break;
case 'delete':
logs.push(`${colors.yellow('DELETE')} ${eventPath}`);
files.add(eventPath);
break;
case 'rename': {
const newFilename = removeLeadingSlash(event.to);
logs.push(`${colors.blue('RENAME')} ${eventPath} => ${newFilename}`);
files.add(newFilename);
break;
}
}
});
const lifecycleSubscription = workflow.lifeCycle.subscribe((event) => {
if (event.kind == 'end' || event.kind == 'post-tasks-start') {
if (!error) {
// Output the logging queue, no error happened.
logs.forEach((log) => logger.info(log));
}
logs = [];
error = false;
}
});
return {
files,
error,
unsubscribe: () => {
reporterSubscription.unsubscribe();
lifecycleSubscription.unsubscribe();
},
};
}