From 481334b645348fc7471baa8b1a41d99c8e1d6017 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 23 Feb 2019 13:30:46 +0100 Subject: [PATCH] chore: simplify types in jest-changed-files --- packages/jest-changed-files/package.json | 1 + packages/jest-changed-files/src/git.ts | 17 ++++++++--------- packages/jest-changed-files/src/hg.ts | 13 ++++++------- packages/jest-changed-files/src/index.ts | 21 ++++++++++++++------- packages/jest-changed-files/src/types.ts | 16 +++++++++------- packages/jest-changed-files/tsconfig.json | 5 ++++- 6 files changed, 42 insertions(+), 31 deletions(-) diff --git a/packages/jest-changed-files/package.json b/packages/jest-changed-files/package.json index 6aafc1373de5..26af22275c41 100644 --- a/packages/jest-changed-files/package.json +++ b/packages/jest-changed-files/package.json @@ -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" }, diff --git a/packages/jest-changed-files/src/git.ts b/packages/jest-changed-files/src/git.ts index 463ad88647da..ed0d4a9b90a1 100644 --- a/packages/jest-changed-files/src/git.ts +++ b/packages/jest-changed-files/src/git.ts @@ -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, - cwd: Path, -): Promise> => { + cwd: Config.Path, +): Promise> => { const result = await execa('git', args, {cwd}); return result.stdout @@ -24,14 +25,12 @@ const findChangedFilesUsingCommand = async ( }; const adapter: SCMAdapter = { - findChangedFiles: async ( - cwd: string, - options?: Options, - ): Promise> => { + findChangedFiles: async (cwd, options) => { const changedSince: string | undefined = options && (options.withAncestor ? 'HEAD^' : options.changedSince); - const includePaths: Array = (options && options.includePaths) || []; + const includePaths: Array = + (options && options.includePaths) || []; if (options && options.lastCommit) { return findChangedFilesUsingCommand( @@ -72,7 +71,7 @@ const adapter: SCMAdapter = { } }, - getRoot: async (cwd: string): Promise => { + getRoot: async cwd => { const options = ['rev-parse', '--show-toplevel']; try { diff --git a/packages/jest-changed-files/src/hg.ts b/packages/jest-changed-files/src/hg.ts index 96cb347a5882..89d4aea508b2 100644 --- a/packages/jest-changed-files/src/hg.ts +++ b/packages/jest-changed-files/src/hg.ts @@ -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> => { - const includePaths: Array = (options && options.includePaths) || []; + findChangedFiles: async (cwd, options) => { + const includePaths: Array = + (options && options.includePaths) || []; const args = ['status', '-amnu']; if (options && options.withAncestor) { @@ -38,7 +37,7 @@ const adapter: SCMAdapter = { .map(changedPath => path.resolve(cwd, changedPath)); }, - getRoot: async (cwd: Path): Promise => { + getRoot: async cwd => { try { const result = await execa('hg', ['root'], {cwd, env}); diff --git a/packages/jest-changed-files/src/index.ts b/packages/jest-changed-files/src/index.ts index d3c160e683ee..b53f80db208f 100644 --- a/packages/jest-changed-files/src/index.ts +++ b/packages/jest-changed-files/src/index.ts @@ -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; + +function notEmpty(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); @@ -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); @@ -48,22 +55,22 @@ export const getChangedFilesForRoots = async ( return {changedFiles, repos}; }; -export const findRepos = async (roots: Path[]): Promise => { +export const findRepos = async (roots: Config.Path[]): Promise => { const gitRepos = await Promise.all( - roots.reduce[]>( + roots.reduce( (promises, root) => promises.concat(findGitRoot(root)), [], ), ); const hgRepos = await Promise.all( - roots.reduce[]>( + roots.reduce( (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)), }; }; diff --git a/packages/jest-changed-files/src/types.ts b/packages/jest-changed-files/src/types.ts index 36bde410b70b..3d040a6c49dd 100644 --- a/packages/jest-changed-files/src/types.ts +++ b/packages/jest-changed-files/src/types.ts @@ -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; + includePaths?: Array; }; -export type ChangedFiles = Set; -export type Repos = {git: Set; hg: Set}; +type ChangedFiles = Set; +export type Repos = {git: ChangedFiles; hg: ChangedFiles}; export type ChangedFilesPromise = Promise<{ repos: Repos; changedFiles: ChangedFiles; }>; export type SCMAdapter = { - findChangedFiles: (cwd: Path, options: Options) => Promise>; - getRoot: (cwd: Path) => Promise; + findChangedFiles: ( + cwd: Config.Path, + options: Options, + ) => Promise>; + getRoot: (cwd: Config.Path) => Promise; }; diff --git a/packages/jest-changed-files/tsconfig.json b/packages/jest-changed-files/tsconfig.json index 7bb06bce6d20..25e34a0f4511 100644 --- a/packages/jest-changed-files/tsconfig.json +++ b/packages/jest-changed-files/tsconfig.json @@ -3,5 +3,8 @@ "compilerOptions": { "rootDir": "src", "outDir": "build" - } + }, + "references": [ + {"path": "../jest-types"} + ] }