Skip to content

Commit

Permalink
use DEBUG env var instead of verbose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
EladBezalel committed Jun 9, 2024
1 parent d5099d3 commit 48cf1d6
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 153 deletions.
26 changes: 11 additions & 15 deletions libs/core/src/true-affected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ describe('trueAffected', () => {
cwd,
base: 'main',
rootTsConfig: 'tsconfig.json',
verbose: true,
projects: [
{
name: 'angular-component',
Expand Down Expand Up @@ -268,7 +267,6 @@ describe('trueAffected', () => {
cwd,
base: 'main',
__experimentalLockfileCheck: true,
verbose: true,
projects: [
{
name: 'proj1',
Expand Down Expand Up @@ -410,7 +408,6 @@ describe('trueAffected', () => {
},
],
include: filePatterns,
verbose: true,
});

expect(affected).toEqual(expected);
Expand All @@ -425,7 +422,7 @@ describe('trueAffected', () => {
];
jest.spyOn(git, 'getChangedFiles').mockReturnValue(changedFiles);

const log = jest.fn();
const debug = jest.fn();
await trueAffected({
cwd,
base: 'main',
Expand All @@ -449,38 +446,37 @@ describe('trueAffected', () => {
},
],
logger: {
log,
debug,
} as unknown as Console,
verbose: true,
});

expect(log).toHaveBeenCalledWith('Getting affected projects');
expect(log).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith('Getting affected projects');
expect(debug).toHaveBeenCalledWith(
expect.stringContaining('Creating project with root tsconfig from')
);
expect(log).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith(
expect.stringContaining('Adding source files for project proj1')
);
expect(log).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith(
expect.stringContaining('Adding source files for project proj2')
);
expect(log).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith(
expect.stringContaining(
'Could not find a tsconfig for project proj3, adding source files paths'
)
);
expect(log).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith(
`Found ${changedFiles.length} changed files`
);
expect(log).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith(
`Added package proj1 to affected packages for changed line ${changedFiles[0].changedLines[0]} in ${changedFiles[0].filePath}`
);
expect(log).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith(
expect.stringMatching(
new RegExp(`^Found identifier .* in .*${changedFiles[0].filePath}$`)
)
);
expect(log).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith(
'Added package proj2 to affected packages'
);
});
Expand Down
130 changes: 57 additions & 73 deletions libs/core/src/true-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@ const ignoredRootNodeTypes = [

export const DEFAULT_INCLUDE_TEST_FILES = /\.(spec|test)\.(ts|js)x?/;

const DEFAULT_LOGGER = {
...console,
debug: process.env['DEBUG'] === 'true' ? console.debug : () => {},
};

export const trueAffected = async ({
cwd,
rootTsConfig,
base = 'origin/main',
projects,
include = [DEFAULT_INCLUDE_TEST_FILES],
verbose = false,
logger = console,
logger = DEFAULT_LOGGER,
__experimentalLockfileCheck = false,
}: TrueAffected) => {
if (verbose) {
logger.log('Getting affected projects');
if (rootTsConfig != null) {
logger.log(
`Creating project with root tsconfig from ${chalk.bold(
resolve(cwd, rootTsConfig)
)}`
);
}
logger.debug('Getting affected projects');
if (rootTsConfig != null) {
logger.debug(
`Creating project with root tsconfig from ${chalk.bold(
resolve(cwd, rootTsConfig)
)}`
);
}

const project = new Project({
Expand All @@ -60,24 +62,22 @@ export const trueAffected = async ({
const tsConfigPath = resolve(cwd, tsConfig);

if (existsSync(tsConfigPath)) {
if (verbose) {
logger.log(
`Adding source files for project ${chalk.bold(
name
)} from tsconfig at ${chalk.bold(tsConfigPath)}`
);
}
logger.debug(
`Adding source files for project ${chalk.bold(
name
)} from tsconfig at ${chalk.bold(tsConfigPath)}`
);

project.addSourceFilesFromTsConfig(tsConfigPath);
} else {
if (verbose) {
logger.log(
`Could not find a tsconfig for project ${chalk.bold(
name
)}, adding source files paths in ${chalk.bold(
resolve(cwd, sourceRoot)
)}`
);
}
logger.debug(
`Could not find a tsconfig for project ${chalk.bold(
name
)}, adding source files paths in ${chalk.bold(
resolve(cwd, sourceRoot)
)}`
);

project.addSourceFilesAtPaths(
join(resolve(cwd, sourceRoot), '**/*.{ts,js}')
);
Expand All @@ -90,9 +90,7 @@ export const trueAffected = async ({
cwd,
});

if (verbose) {
logger.log(`Found ${chalk.bold(changedFiles.length)} changed files`);
}
logger.debug(`Found ${chalk.bold(changedFiles.length)} changed files`);

const sourceChangedFiles = changedFiles.filter(
({ filePath }) => project.getSourceFile(resolve(cwd, filePath)) != null
Expand All @@ -108,17 +106,15 @@ export const trueAffected = async ({
project.getSourceFile(resolve(cwd, filePath)) == null
)
.flatMap(({ filePath: changedFilePath }) => {
if (verbose) {
logger.log(
`Finding non-source affected files for ${chalk.bold(changedFilePath)}`
);
}
logger.debug(
`Finding non-source affected files for ${chalk.bold(changedFilePath)}`
);

return findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths);
});

if (verbose && nonSourceChangedFiles.length > 0) {
logger.log(
if (nonSourceChangedFiles.length > 0) {
logger.debug(
`Found ${chalk.bold(
nonSourceChangedFiles.length
)} non-source affected files`
Expand All @@ -127,9 +123,7 @@ export const trueAffected = async ({

let changedFilesByLockfile: ChangedFiles[] = [];
if (__experimentalLockfileCheck && hasLockfileChanged(changedFiles)) {
if (verbose) {
logger.log('Lockfile has changed, finding affected files');
}
logger.debug('Lockfile has changed, finding affected files');

changedFilesByLockfile = findAffectedFilesByLockfile(
cwd,
Expand Down Expand Up @@ -157,8 +151,8 @@ export const trueAffected = async ({
.map(({ filePath }) => getPackageNameByPath(filePath, projects))
.filter((v): v is string => v != null);

if (verbose && changedIncludedFilesPackages.length > 0) {
logger.log(
if (changedIncludedFilesPackages.length > 0) {
logger.debug(
`Found ${chalk.bold(
changedIncludedFilesPackages.length
)} affected packages from included files`
Expand Down Expand Up @@ -187,33 +181,25 @@ export const trueAffected = async ({
const identifierName = identifier.getText();
const path = rootNode.getSourceFile().getFilePath();

if (verbose) {
logger.log(
`Found identifier ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
);
}
logger.debug(
`Found identifier ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
);

if (identifierName && path) {
const visited = visitedIdentifiers.get(path) ?? [];
if (visited.includes(identifierName)) {
if (verbose) {
logger.log(
`Already visited ${chalk.bold(identifierName)} in ${chalk.bold(
path
)}`
);
}
logger.debug(
`Already visited ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
);

return;
}

visitedIdentifiers.set(path, [...visited, identifierName]);

if (verbose) {
logger.log(
`Visiting ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
);
}
logger.debug(
`Visiting ${chalk.bold(identifierName)} in ${chalk.bold(path)}`
);
}

refs.forEach((node) => {
Expand All @@ -222,9 +208,8 @@ export const trueAffected = async ({

if (pkg) {
affectedPackages.add(pkg);
if (verbose) {
logger.log(`Added package ${chalk.bold(pkg)} to affected packages`);
}

logger.debug(`Added package ${chalk.bold(pkg)} to affected packages`);
}

findReferencesLibs(node);
Expand All @@ -250,15 +235,14 @@ export const trueAffected = async ({

if (pkg) {
affectedPackages.add(pkg);
if (verbose) {
logger.log(
`Added package ${chalk.bold(
pkg
)} to affected packages for changed line ${chalk.bold(
line
)} in ${chalk.bold(filePath)}`
);
}

logger.debug(
`Added package ${chalk.bold(
pkg
)} to affected packages for changed line ${chalk.bold(
line
)} in ${chalk.bold(filePath)}`
);
}

findReferencesLibs(changedNode);
Expand All @@ -284,8 +268,8 @@ export const trueAffected = async ({
.filter(([, deps]) => deps.includes(pkg))
.map(([name]) => name);

if (verbose && deps.length > 0) {
logger.log(
if (deps.length > 0) {
logger.debug(
`Adding implicit dependencies ${chalk.bold(
deps.join(', ')
)} to ${chalk.bold(pkg)}`
Expand Down
1 change: 0 additions & 1 deletion libs/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface TrueAffectedProject {
}

export interface TrueAffectedLogging {
verbose?: boolean;
logger?: Console;
}

Expand Down
9 changes: 2 additions & 7 deletions libs/nx/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ describe('cli', () => {
tsConfigFilePath: 'tsconfig.base.json',
target: [],
experimentalLockfileCheck: false,
verbose: false,
});
});

Expand All @@ -90,7 +89,6 @@ describe('cli', () => {
'--tsConfigFilePath=tsconfig.json',
`--cwd=${workspaceCwd}`,
'--includeFiles=package.json,jest.setup.js',
'--verbose=true',
]);
expect(affectedActionSpy).toBeCalledWith({
action: 'build',
Expand All @@ -103,7 +101,6 @@ describe('cli', () => {
tsConfigFilePath: 'tsconfig.json',
target: [],
experimentalLockfileCheck: false,
verbose: true,
});
});
});
Expand All @@ -117,7 +114,8 @@ describe('cli', () => {
beforeEach(() => {
getNxTrueAffectedProjectsSpy = jest
.spyOn(nx, 'getNxTrueAffectedProjects')
.mockImplementation();
.mockImplementation()
.mockResolvedValue([]);

logSpy = jest.spyOn(cli, 'log').mockImplementation();
});
Expand All @@ -137,11 +135,9 @@ describe('cli', () => {
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
verbose: true,
});

expect(getNxTrueAffectedProjectsSpy).toBeCalledWith(process.cwd(), {
verbose: true,
logger: expect.any(Object),
});
expect(trafSpy).toHaveBeenCalledWith({
Expand All @@ -150,7 +146,6 @@ describe('cli', () => {
base: 'origin/main',
projects: [],
include: [],
verbose: true,
logger: expect.any(Object),
});
});
Expand Down
Loading

0 comments on commit 48cf1d6

Please sign in to comment.