Skip to content

Commit

Permalink
chore(core): add type for hasher filter to avoid typos (#9287)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Mar 11, 2022
1 parent 5db394e commit ca38be7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
10 changes: 7 additions & 3 deletions packages/cypress/src/executors/cypress/hasher.ts
Expand Up @@ -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,
Expand All @@ -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);
}
10 changes: 7 additions & 3 deletions packages/jest/src/executors/jest/hasher.ts
Expand Up @@ -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,
Expand All @@ -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);
}
32 changes: 16 additions & 16 deletions packages/workspace/src/core/hasher/hasher.ts
Expand Up @@ -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<ImplicitHashResult>;
Expand All @@ -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<Hash> {
const command = this.hashCommand(task);

Expand Down Expand Up @@ -141,7 +145,7 @@ export class Hasher {
async hashSource(task: Task): Promise<string> {
return this.projectHashes.hashProjectNodeSource(
task.target.project,
'all-files'
HashFilter.AllFiles
);
}

Expand Down Expand Up @@ -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<ProjectHashResult> {
return Promise.resolve().then(async () => {
const deps = this.projectGraph.dependencies[projectName] ?? [];
Expand All @@ -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
Expand All @@ -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) => {
Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit ca38be7

Please sign in to comment.