chore(core): add type for hasher filter to avoid typos (#9287)

This commit is contained in:
Miroslav Jonaš 2022-03-11 14:54:21 +01:00 committed by GitHub
parent 5db394ecd8
commit ca38be735d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 22 deletions

View File

@ -5,7 +5,11 @@ import {
TaskGraph, TaskGraph,
WorkspaceJsonConfiguration, WorkspaceJsonConfiguration,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { Hash, Hasher } from '@nrwl/workspace/src/core/hasher/hasher'; import {
Hash,
Hasher,
HashFilter,
} from '@nrwl/workspace/src/core/hasher/hasher';
export default async function run( export default async function run(
task: Task, task: Task,
@ -21,7 +25,7 @@ export default async function run(
: undefined; : undefined;
const filter = const filter =
cypressPluginConfig && cypressPluginConfig.hashingExcludesTestsOfDeps cypressPluginConfig && cypressPluginConfig.hashingExcludesTestsOfDeps
? 'exclude-tests-of-deps' ? HashFilter.ExcludeTestsOfDeps
: 'all-files'; : HashFilter.AllFiles;
return context.hasher.hashTaskWithDepsAndContext(task, filter); return context.hasher.hashTaskWithDepsAndContext(task, filter);
} }

View File

@ -5,7 +5,11 @@ import {
TaskGraph, TaskGraph,
WorkspaceJsonConfiguration, WorkspaceJsonConfiguration,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { Hash, Hasher } from '@nrwl/workspace/src/core/hasher/hasher'; import {
Hash,
Hasher,
HashFilter,
} from '@nrwl/workspace/src/core/hasher/hasher';
export default async function run( export default async function run(
task: Task, task: Task,
@ -21,7 +25,7 @@ export default async function run(
: undefined; : undefined;
const filter = const filter =
jestPluginConfig && jestPluginConfig.hashingExcludesTestsOfDeps jestPluginConfig && jestPluginConfig.hashingExcludesTestsOfDeps
? 'exclude-tests-of-deps' ? HashFilter.ExcludeTestsOfDeps
: 'all-files'; : HashFilter.AllFiles;
return context.hasher.hashTaskWithDepsAndContext(task, filter); return context.hasher.hashTaskWithDepsAndContext(task, filter);
} }

View File

@ -49,6 +49,13 @@ interface TsconfigJsonConfiguration {
compilerOptions: CompilerOptions; compilerOptions: CompilerOptions;
} }
export const HashFilter = {
AllFiles: 'all-files',
ExcludeTestsOfAll: 'exclude-tests-of-all',
ExcludeTestsOfDeps: 'exclude-tests-of-deps',
};
export type HashFilter = typeof HashFilter[keyof typeof HashFilter];
export class Hasher { export class Hasher {
static version = '2.0'; static version = '2.0';
private implicitDependencies: Promise<ImplicitHashResult>; private implicitDependencies: Promise<ImplicitHashResult>;
@ -75,10 +82,7 @@ export class Hasher {
async hashTaskWithDepsAndContext( async hashTaskWithDepsAndContext(
task: Task, task: Task,
filter: filter: HashFilter = HashFilter.AllFiles
| 'all-files'
| 'exclude-tests-of-all'
| 'exclude-tests-of-deps' = 'all-files'
): Promise<Hash> { ): Promise<Hash> {
const command = this.hashCommand(task); const command = this.hashCommand(task);
@ -141,7 +145,7 @@ export class Hasher {
async hashSource(task: Task): Promise<string> { async hashSource(task: Task): Promise<string> {
return this.projectHashes.hashProjectNodeSource( return this.projectHashes.hashProjectNodeSource(
task.target.project, task.target.project,
'all-files' HashFilter.AllFiles
); );
} }
@ -318,7 +322,7 @@ class ProjectHasher {
async hashProject( async hashProject(
projectName: string, projectName: string,
visited: string[], visited: string[],
filter: 'all-files' | 'exclude-tests-of-all' | 'exclude-tests-of-deps' filter: HashFilter
): Promise<ProjectHashResult> { ): Promise<ProjectHashResult> {
return Promise.resolve().then(async () => { return Promise.resolve().then(async () => {
const deps = this.projectGraph.dependencies[projectName] ?? []; const deps = this.projectGraph.dependencies[projectName] ?? [];
@ -335,11 +339,10 @@ class ProjectHasher {
) )
).filter((r) => !!r); ).filter((r) => !!r);
const filterForProject = const filterForProject =
filter === 'all-files' filter === HashFilter.AllFiles ||
? 'all-files' (filter === HashFilter.ExcludeTestsOfDeps && visited[0] === projectName)
: filter === 'exclude-tests-of-deps' && visited[0] === projectName ? HashFilter.AllFiles
? 'all-files' : HashFilter.ExcludeTestsOfAll;
: 'exclude-tests';
const projectHash = await this.hashProjectNodeSource( const projectHash = await this.hashProjectNodeSource(
projectName, projectName,
filterForProject filterForProject
@ -358,10 +361,7 @@ class ProjectHasher {
}); });
} }
async hashProjectNodeSource( async hashProjectNodeSource(projectName: string, filter: HashFilter) {
projectName: string,
filter: 'all-files' | 'exclude-tests'
) {
const mapKey = `${projectName}-${filter}`; const mapKey = `${projectName}-${filter}`;
if (!this.sourceHashes[mapKey]) { if (!this.sourceHashes[mapKey]) {
this.sourceHashes[mapKey] = new Promise(async (res) => { this.sourceHashes[mapKey] = new Promise(async (res) => {
@ -392,7 +392,7 @@ class ProjectHasher {
} }
const filteredFiles = const filteredFiles =
filter === 'all-files' filter === HashFilter.AllFiles
? p.data.files ? p.data.files
: p.data.files.filter((f) => !this.isSpec(f.file)); : p.data.files.filter((f) => !this.isSpec(f.file));