Skip to content

Commit

Permalink
chore: simplify types in jest-changed-files
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 23, 2019
1 parent e998c92 commit 481334b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/jest-changed-files/package.json
Expand Up @@ -10,6 +10,7 @@
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/types": "^24.1.0",
"execa": "^1.0.0",
"throat": "^4.0.0"
},
Expand Down
17 changes: 8 additions & 9 deletions packages/jest-changed-files/src/git.ts
Expand Up @@ -8,13 +8,14 @@

import path from 'path';
import execa from 'execa';
import {Config} from '@jest/types';

import {Path, Options, SCMAdapter} from './types';
import {SCMAdapter} from './types';

const findChangedFilesUsingCommand = async (
args: Array<string>,
cwd: Path,
): Promise<Array<Path>> => {
cwd: Config.Path,
): Promise<Array<Config.Path>> => {
const result = await execa('git', args, {cwd});

return result.stdout
Expand All @@ -24,14 +25,12 @@ const findChangedFilesUsingCommand = async (
};

const adapter: SCMAdapter = {
findChangedFiles: async (
cwd: string,
options?: Options,
): Promise<Array<Path>> => {
findChangedFiles: async (cwd, options) => {
const changedSince: string | undefined =
options && (options.withAncestor ? 'HEAD^' : options.changedSince);

const includePaths: Array<Path> = (options && options.includePaths) || [];
const includePaths: Array<Config.Path> =
(options && options.includePaths) || [];

if (options && options.lastCommit) {
return findChangedFilesUsingCommand(
Expand Down Expand Up @@ -72,7 +71,7 @@ const adapter: SCMAdapter = {
}
},

getRoot: async (cwd: string): Promise<string | null> => {
getRoot: async cwd => {
const options = ['rev-parse', '--show-toplevel'];

try {
Expand Down
13 changes: 6 additions & 7 deletions packages/jest-changed-files/src/hg.ts
Expand Up @@ -8,17 +8,16 @@

import path from 'path';
import execa from 'execa';
import {Config} from '@jest/types';

import {Path, Options, SCMAdapter} from './types';
import {SCMAdapter} from './types';

const env = {...process.env, HGPLAIN: '1'};

const adapter: SCMAdapter = {
findChangedFiles: async (
cwd: string,
options: Options,
): Promise<Array<Path>> => {
const includePaths: Array<Path> = (options && options.includePaths) || [];
findChangedFiles: async (cwd, options) => {
const includePaths: Array<Config.Path> =
(options && options.includePaths) || [];

const args = ['status', '-amnu'];
if (options && options.withAncestor) {
Expand All @@ -38,7 +37,7 @@ const adapter: SCMAdapter = {
.map(changedPath => path.resolve(cwd, changedPath));
},

getRoot: async (cwd: Path): Promise<Path | null | undefined> => {
getRoot: async cwd => {
try {
const result = await execa('hg', ['root'], {cwd, env});

Expand Down
21 changes: 14 additions & 7 deletions packages/jest-changed-files/src/index.ts
Expand Up @@ -7,11 +7,18 @@
*/

import throat from 'throat';
import {Config} from '@jest/types';

import {Path, ChangedFilesPromise, Options, Repos} from './types';
import {ChangedFilesPromise, Options, Repos, SCMAdapter} from './types';
import git from './git';
import hg from './hg';

type RootPromise = ReturnType<SCMAdapter['getRoot']>;

function notEmpty<T>(value: T | null | undefined): value is T {
return value != null;
}

// This is an arbitrary number. The main goal is to prevent projects with
// many roots (50+) from spawning too many processes at once.
const mutex = throat(5);
Expand All @@ -20,7 +27,7 @@ const findGitRoot = (dir: string) => mutex(() => git.getRoot(dir));
const findHgRoot = (dir: string) => mutex(() => hg.getRoot(dir));

export const getChangedFilesForRoots = async (
roots: Path[],
roots: Config.Path[],
options: Options,
): ChangedFilesPromise => {
const repos = await findRepos(roots);
Expand Down Expand Up @@ -48,22 +55,22 @@ export const getChangedFilesForRoots = async (
return {changedFiles, repos};
};

export const findRepos = async (roots: Path[]): Promise<Repos> => {
export const findRepos = async (roots: Config.Path[]): Promise<Repos> => {
const gitRepos = await Promise.all(
roots.reduce<Promise<string | null | undefined>[]>(
roots.reduce<RootPromise[]>(
(promises, root) => promises.concat(findGitRoot(root)),
[],
),
);
const hgRepos = await Promise.all(
roots.reduce<Promise<string | null | undefined>[]>(
roots.reduce<RootPromise[]>(
(promises, root) => promises.concat(findHgRoot(root)),
[],
),
);

return {
git: new Set(gitRepos.filter(Boolean) as string[]),
hg: new Set(hgRepos.filter(Boolean) as string[]),
git: new Set(gitRepos.filter(notEmpty)),
hg: new Set(hgRepos.filter(notEmpty)),
};
};
16 changes: 9 additions & 7 deletions packages/jest-changed-files/src/types.ts
Expand Up @@ -3,26 +3,28 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

export type Path = string;
import {Config} from '@jest/types';

export type Options = {
lastCommit?: boolean;
withAncestor?: boolean;
changedSince?: string;
includePaths?: Array<Path>;
includePaths?: Array<Config.Path>;
};

export type ChangedFiles = Set<Path>;
export type Repos = {git: Set<Path>; hg: Set<Path>};
type ChangedFiles = Set<Config.Path>;
export type Repos = {git: ChangedFiles; hg: ChangedFiles};
export type ChangedFilesPromise = Promise<{
repos: Repos;
changedFiles: ChangedFiles;
}>;

export type SCMAdapter = {
findChangedFiles: (cwd: Path, options: Options) => Promise<Array<Path>>;
getRoot: (cwd: Path) => Promise<Path | null | undefined>;
findChangedFiles: (
cwd: Config.Path,
options: Options,
) => Promise<Array<Config.Path>>;
getRoot: (cwd: Config.Path) => Promise<Config.Path | null>;
};
5 changes: 4 additions & 1 deletion packages/jest-changed-files/tsconfig.json
Expand Up @@ -3,5 +3,8 @@
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
}
},
"references": [
{"path": "../jest-types"}
]
}

0 comments on commit 481334b

Please sign in to comment.