Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions ng-dev/ts-circular-dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,6 @@ export function main(

Log.info(green(` Current number of cycles: ${yellow(cycles.length.toString())}`));

if (goldenFile && approve) {
writeFileSync(goldenFile, JSON.stringify(actual, null, 2));
Log.info(green('✔ Updated golden file.'));
return 0;
} else if (!goldenFile) {
Log.error(`x Circular dependency goldens are not allowed.`);
return 1;
} else if (!existsSync(goldenFile)) {
Log.error(`x Could not find golden file: ${goldenFile}`);
return 1;
}

const warningsCount = analyzer.unresolvedFiles.size + analyzer.unresolvedModules.size;

// By default, warnings for unresolved files or modules are not printed. This is because
Expand All @@ -119,6 +107,34 @@ export function main(
Log.warn(` Please rerun with "--warnings" to inspect unresolved imports.`);
}

if (goldenFile) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIt: I'd prefer if we guard by approve at the first condition, as that would make it easier to follow IMO.

(maybe that's just me though)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that going based on the golden makes sense because its essentially shortcutting the rest of the logic below when no golden exists.

I reordered some things that I think end sup making it a bit better, let me know what you think.

// Golden file exists
if (approve) {
writeFileSync(goldenFile, JSON.stringify(actual, null, 2));
Log.info(green('✔ Updated golden file.'));
return 0;
}
if (!existsSync(goldenFile)) {
Log.error(`x Could not find golden file: ${goldenFile}`);
return 1;
}
} else {
if (approve) {
Log.error(
`x Cannot approve circular depdencies within this repository as no golden file exists.`,
);
return 1;
}
// No golden file exists
if (cycles.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be outside? What if there is a goldenFile and cycles?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is within the goldenFile being undefined so there is no golden file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But shouldn't this fail? if there are cycles and goldenFile being !== undefined?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it will happen automatically below with the comparison. That's the thing I missed. Very confusing :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the case where there is no golden file, and there are cycles here. This fails as it returns a 1 as the exit code.

For cases where there is a golden file to do the comparison to later, we check the specifics.

Log.error(`x No circular dependencies are allow within this repository.`);
return 1;
}

Log.info(green('✔ No circular dependencies found in this repository.'));
return 0;
}

const expected = goldenFile ? (JSON.parse(readFileSync(goldenFile, 'utf8')) as Golden) : [];
const {fixedCircularDeps, newCircularDeps} = compareGoldens(actual, expected);
const isMatching = fixedCircularDeps.length === 0 && newCircularDeps.length === 0;
Expand Down
Loading