MCPcopy Create free account
hub / github.com/angular/dev-infra / captureLogOutputForCommand

Function captureLogOutputForCommand

ng-dev/utils/logging.ts:124–174  ·  view source on GitHub ↗
(argv: Arguments)

Source from the content-addressed store, hash-verified

122 * middleware of yargs to enable the file logging before the rest of the command parsing and
123 * response is executed.
124 */
125export async function captureLogOutputForCommand(argv: Arguments) {
126 // TODO(josephperrott): remove this guard against running multiple times after
127 // https://github.com/yargs/yargs/issues/2223 is fixed
128 if (logStream !== undefined) {
129 return;
130 }
131 const repoDir = determineRepoBaseDirFromCwd();
132 const logFilePath = join(repoDir, '.ng-dev.log');
133 if (existsSync(logFilePath) && lstatSync(logFilePath).isSymbolicLink()) {
134 throw new Error(
135 'Security Violation: .ng-dev.log is a symbolic link. ' +
136 'To prevent arbitrary file write, execution is aborted.',
137 );
138 }
139 logStream = createWriteStream(logFilePath, {encoding: 'utf-8'});
140
141 /** The date time used for timestamping when the command was invoked. */
142 const now = new Date();
143 /** Header line to separate command runs in log files. */
144 const headerLine = Array(100).fill('#').join('');
145 appendToLogFile(
146 undefined,
147 `${headerLine}\nCommand: ${argv.$0} ${argv._.join(' ')}\nRan at: ${now}\n`,
148 );
149
150 // When the command is completed, write the logged output to the appropriate log files
151 registerCompletedFunction(async (error) => {
152 if (error instanceof Error) {
153 appendToLogFile(LogLevel.ERROR, error.message);
154 const stack = error.stack?.split('\n') ?? [];
155 for (const line of stack) {
156 appendToLogFile(LogLevel.ERROR, line);
157 }
158 }
159 appendToLogFile(
160 undefined,
161 `\n\nCommand ran in ${new Date().getTime() - now.getTime()}ms\nExit Code: ${process.exitCode}\n`,
162 );
163 logStream!.end(() => {
164 // For failure codes greater than 1, the new logged lines should be written to a specific log
165 // file for the command run failure.
166 if (process.exitCode !== 0) {
167 const errorLogFileName = `.ng-dev.err-${now.getTime()}.log`;
168 console.error(
169 `Exit code: ${process.exitCode}.\nWriting full log to ${join(repoDir, errorLogFileName)}`,
170 );
171 copyFileSync(logFilePath, join(repoDir, errorLogFileName));
172 }
173 });
174 });
175}
176
177/** Write the provided text to the log file, prepending each line with the log level. */

Callers

nothing calls this directly

Calls 5

appendToLogFileFunction · 0.85
endMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected