diff --git a/packages/cypress/src/executors/cypress/hasher.ts b/packages/cypress/src/executors/cypress/hasher.ts index 60aeeaa377..84adaf52b7 100644 --- a/packages/cypress/src/executors/cypress/hasher.ts +++ b/packages/cypress/src/executors/cypress/hasher.ts @@ -5,7 +5,11 @@ import { TaskGraph, WorkspaceJsonConfiguration, } 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( task: Task, @@ -21,7 +25,7 @@ export default async function run( : undefined; const filter = cypressPluginConfig && cypressPluginConfig.hashingExcludesTestsOfDeps - ? 'exclude-tests-of-deps' - : 'all-files'; + ? HashFilter.ExcludeTestsOfDeps + : HashFilter.AllFiles; return context.hasher.hashTaskWithDepsAndContext(task, filter); } diff --git a/packages/jest/src/executors/jest/hasher.ts b/packages/jest/src/executors/jest/hasher.ts index 10ea47ef77..292da60b61 100644 --- a/packages/jest/src/executors/jest/hasher.ts +++ b/packages/jest/src/executors/jest/hasher.ts @@ -5,7 +5,11 @@ import { TaskGraph, WorkspaceJsonConfiguration, } 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( task: Task, @@ -21,7 +25,7 @@ export default async function run( : undefined; const filter = jestPluginConfig && jestPluginConfig.hashingExcludesTestsOfDeps - ? 'exclude-tests-of-deps' - : 'all-files'; + ? HashFilter.ExcludeTestsOfDeps + : HashFilter.AllFiles; return context.hasher.hashTaskWithDepsAndContext(task, filter); } diff --git a/packages/workspace/src/core/hasher/hasher.ts b/packages/workspace/src/core/hasher/hasher.ts index a7d3a77bdb..66204e217e 100644 --- a/packages/workspace/src/core/hasher/hasher.ts +++ b/packages/workspace/src/core/hasher/hasher.ts @@ -49,6 +49,13 @@ interface TsconfigJsonConfiguration { 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 { static version = '2.0'; private implicitDependencies: Promise; @@ -75,10 +82,7 @@ export class Hasher { async hashTaskWithDepsAndContext( task: Task, - filter: - | 'all-files' - | 'exclude-tests-of-all' - | 'exclude-tests-of-deps' = 'all-files' + filter: HashFilter = HashFilter.AllFiles ): Promise { const command = this.hashCommand(task); @@ -141,7 +145,7 @@ export class Hasher { async hashSource(task: Task): Promise { return this.projectHashes.hashProjectNodeSource( task.target.project, - 'all-files' + HashFilter.AllFiles ); } @@ -318,7 +322,7 @@ class ProjectHasher { async hashProject( projectName: string, visited: string[], - filter: 'all-files' | 'exclude-tests-of-all' | 'exclude-tests-of-deps' + filter: HashFilter ): Promise { return Promise.resolve().then(async () => { const deps = this.projectGraph.dependencies[projectName] ?? []; @@ -335,11 +339,10 @@ class ProjectHasher { ) ).filter((r) => !!r); const filterForProject = - filter === 'all-files' - ? 'all-files' - : filter === 'exclude-tests-of-deps' && visited[0] === projectName - ? 'all-files' - : 'exclude-tests'; + filter === HashFilter.AllFiles || + (filter === HashFilter.ExcludeTestsOfDeps && visited[0] === projectName) + ? HashFilter.AllFiles + : HashFilter.ExcludeTestsOfAll; const projectHash = await this.hashProjectNodeSource( projectName, filterForProject @@ -358,10 +361,7 @@ class ProjectHasher { }); } - async hashProjectNodeSource( - projectName: string, - filter: 'all-files' | 'exclude-tests' - ) { + async hashProjectNodeSource(projectName: string, filter: HashFilter) { const mapKey = `${projectName}-${filter}`; if (!this.sourceHashes[mapKey]) { this.sourceHashes[mapKey] = new Promise(async (res) => { @@ -392,7 +392,7 @@ class ProjectHasher { } const filteredFiles = - filter === 'all-files' + filter === HashFilter.AllFiles ? p.data.files : p.data.files.filter((f) => !this.isSpec(f.file));