feat(dev-infra): migrate ts-circular-dependencies tool to use new logging system (#37232)

Migrate the ts-circular-dependencies tool in ng-dev to use new logging system rather
than directly calling console.* to create a better experience
for users.

PR Close #37232
This commit is contained in:
Joey Perrott 2020-05-20 14:52:19 -07:00 committed by Matias Niemelä
parent e28f13a102
commit 7827501d2a
2 changed files with 25 additions and 26 deletions

View File

@ -8,6 +8,8 @@
import {dirname, isAbsolute, resolve} from 'path'; import {dirname, isAbsolute, resolve} from 'path';
import {error} from '../utils/console';
import {ModuleResolver} from './analyzer'; import {ModuleResolver} from './analyzer';
@ -52,8 +54,8 @@ export function loadTestConfig(configPath: string): CircularDependenciesTestConf
} }
return config; return config;
} catch (e) { } catch (e) {
console.error('Could not load test configuration file at: ' + configPath); error('Could not load test configuration file at: ' + configPath);
console.error(`Failed with: ${e.message}`); error(`Failed with: ${e.message}`);
process.exit(1); process.exit(1);
} }
} }

View File

@ -12,7 +12,8 @@ import {sync as globSync} from 'glob';
import {isAbsolute, relative, resolve} from 'path'; import {isAbsolute, relative, resolve} from 'path';
import * as ts from 'typescript'; import * as ts from 'typescript';
import * as yargs from 'yargs'; import * as yargs from 'yargs';
import chalk from 'chalk';
import {green, info, error, red, yellow} from '../utils/console';
import {Analyzer, ReferenceChain} from './analyzer'; import {Analyzer, ReferenceChain} from './analyzer';
import {compareGoldens, convertReferenceChainToGolden, Golden} from './golden'; import {compareGoldens, convertReferenceChainToGolden, Golden} from './golden';
@ -66,15 +67,14 @@ export function main(
const actual = convertReferenceChainToGolden(cycles, baseDir); const actual = convertReferenceChainToGolden(cycles, baseDir);
console.info( info(green(` Current number of cycles: ${yellow(cycles.length.toString())}`));
chalk.green(` Current number of cycles: ${chalk.yellow(cycles.length.toString())}`));
if (approve) { if (approve) {
writeFileSync(goldenFile, JSON.stringify(actual, null, 2)); writeFileSync(goldenFile, JSON.stringify(actual, null, 2));
console.info(chalk.green('✅ Updated golden file.')); info(green('✅ Updated golden file.'));
return 0; return 0;
} else if (!existsSync(goldenFile)) { } else if (!existsSync(goldenFile)) {
console.error(chalk.red(`❌ Could not find golden file: ${goldenFile}`)); error(red(`❌ Could not find golden file: ${goldenFile}`));
return 1; return 1;
} }
@ -84,17 +84,15 @@ export function main(
// it's common that third-party modules are not resolved/visited. Also generated files // it's common that third-party modules are not resolved/visited. Also generated files
// from the View Engine compiler (i.e. factories, summaries) cannot be resolved. // from the View Engine compiler (i.e. factories, summaries) cannot be resolved.
if (printWarnings && warningsCount !== 0) { if (printWarnings && warningsCount !== 0) {
console.info(chalk.yellow('⚠ The following imports could not be resolved:')); info(yellow('⚠ The following imports could not be resolved:'));
Array.from(analyzer.unresolvedModules) Array.from(analyzer.unresolvedModules).sort().forEach(specifier => info(`${specifier}`));
.sort()
.forEach(specifier => console.info(`${specifier}`));
analyzer.unresolvedFiles.forEach((value, key) => { analyzer.unresolvedFiles.forEach((value, key) => {
console.info(`${getRelativePath(baseDir, key)}`); info(`${getRelativePath(baseDir, key)}`);
value.sort().forEach(specifier => console.info(` ${specifier}`)); value.sort().forEach(specifier => info(` ${specifier}`));
}); });
} else { } else {
console.info(chalk.yellow(`${warningsCount} imports could not be resolved.`)); info(yellow(`${warningsCount} imports could not be resolved.`));
console.info(chalk.yellow(` Please rerun with "--warnings" to inspect unresolved imports.`)); info(yellow(` Please rerun with "--warnings" to inspect unresolved imports.`));
} }
const expected: Golden = JSON.parse(readFileSync(goldenFile, 'utf8')); const expected: Golden = JSON.parse(readFileSync(goldenFile, 'utf8'));
@ -102,25 +100,24 @@ export function main(
const isMatching = fixedCircularDeps.length === 0 && newCircularDeps.length === 0; const isMatching = fixedCircularDeps.length === 0 && newCircularDeps.length === 0;
if (isMatching) { if (isMatching) {
console.info(chalk.green('✅ Golden matches current circular dependencies.')); info(green('✅ Golden matches current circular dependencies.'));
return 0; return 0;
} }
console.error(chalk.red('❌ Golden does not match current circular dependencies.')); error(red('❌ Golden does not match current circular dependencies.'));
if (newCircularDeps.length !== 0) { if (newCircularDeps.length !== 0) {
console.error(chalk.yellow(` New circular dependencies which are not allowed:`)); error(yellow(` New circular dependencies which are not allowed:`));
newCircularDeps.forEach(c => console.error(`${convertReferenceChainToString(c)}`)); newCircularDeps.forEach(c => error(`${convertReferenceChainToString(c)}`));
console.error(); error();
} }
if (fixedCircularDeps.length !== 0) { if (fixedCircularDeps.length !== 0) {
console.error( error(yellow(` Fixed circular dependencies that need to be removed from the golden:`));
chalk.yellow(` Fixed circular dependencies that need to be removed from the golden:`)); fixedCircularDeps.forEach(c => error(`${convertReferenceChainToString(c)}`));
fixedCircularDeps.forEach(c => console.error(`${convertReferenceChainToString(c)}`)); error();
console.error();
if (approveCommand) { if (approveCommand) {
console.info(chalk.yellow(` Please approve the new golden with: ${approveCommand}`)); info(yellow(` Please approve the new golden with: ${approveCommand}`));
} else { } else {
console.info(chalk.yellow( info(yellow(
` Please update the golden. The following command can be ` + ` Please update the golden. The following command can be ` +
`run: yarn ts-circular-deps approve ${getRelativePath(process.cwd(), goldenFile)}.`)); `run: yarn ts-circular-deps approve ${getRelativePath(process.cwd(), goldenFile)}.`));
} }