diff --git a/e2e/__tests__/jestChangedFiles.test.js b/e2e/__tests__/jestChangedFiles.test.js index 6020514f44f3..8af8e70c1f41 100644 --- a/e2e/__tests__/jestChangedFiles.test.js +++ b/e2e/__tests__/jestChangedFiles.test.js @@ -9,14 +9,14 @@ import os from 'os'; import path from 'path'; +import {wrap} from 'jest-snapshot-serializer-raw'; import { findRepos, getChangedFilesForRoots, -} from '../../packages/jest-changed-files/src'; +} from '../../packages/jest-changed-files'; import {skipSuiteOnWindows} from '../../scripts/ConditionalTest'; import {cleanup, run, writeFiles} from '../Utils'; import runJest from '../runJest'; -import {wrap} from 'jest-snapshot-serializer-raw'; skipSuiteOnWindows(); diff --git a/packages/jest-changed-files/package.json b/packages/jest-changed-files/package.json index de3d1e78442a..6aafc1373de5 100644 --- a/packages/jest-changed-files/package.json +++ b/packages/jest-changed-files/package.json @@ -8,6 +8,7 @@ }, "license": "MIT", "main": "build/index.js", + "types": "build/index.d.ts", "dependencies": { "execa": "^1.0.0", "throat": "^4.0.0" diff --git a/packages/jest-changed-files/src/git.js b/packages/jest-changed-files/src/git.ts similarity index 91% rename from packages/jest-changed-files/src/git.js rename to packages/jest-changed-files/src/git.ts index 8d405ce593a4..a766bd6b5a3e 100644 --- a/packages/jest-changed-files/src/git.js +++ b/packages/jest-changed-files/src/git.ts @@ -4,15 +4,13 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ -import type {Path} from 'types/Config'; -import type {Options, SCMAdapter} from 'types/ChangedFiles'; - import path from 'path'; import execa from 'execa'; +import {Path, Options, SCMAdapter} from './types'; + const findChangedFilesUsingCommand = async ( args: Array, cwd: Path, @@ -30,7 +28,7 @@ const adapter: SCMAdapter = { cwd: string, options?: Options, ): Promise> => { - const changedSince: ?string = + const changedSince: string | null | undefined = options && (options.withAncestor ? 'HEAD^' : options.changedSince); const includePaths: Array = (options && options.includePaths) || []; @@ -72,7 +70,7 @@ const adapter: SCMAdapter = { } }, - getRoot: async (cwd: string): Promise => { + getRoot: async (cwd: string): Promise => { const options = ['rev-parse', '--show-toplevel']; try { diff --git a/packages/jest-changed-files/src/hg.js b/packages/jest-changed-files/src/hg.ts similarity index 87% rename from packages/jest-changed-files/src/hg.js rename to packages/jest-changed-files/src/hg.ts index 9459619fcc6c..0b3b3ecf7963 100644 --- a/packages/jest-changed-files/src/hg.js +++ b/packages/jest-changed-files/src/hg.ts @@ -4,16 +4,14 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ -import type {Path} from 'types/Config'; -import type {Options, SCMAdapter} from 'types/ChangedFiles'; - import path from 'path'; import execa from 'execa'; -const env = {...process.env, HGPLAIN: 1}; +import {Path, Options, SCMAdapter} from './types'; + +const env = {...process.env, HGPLAIN: '1'}; const ANCESTORS = [ // Parent commit to this one. @@ -51,7 +49,7 @@ const adapter: SCMAdapter = { .map(changedPath => path.resolve(cwd, changedPath)); }, - getRoot: async (cwd: Path): Promise => { + getRoot: async (cwd: Path): Promise => { try { const result = await execa('hg', ['root'], {cwd, env}); diff --git a/packages/jest-changed-files/src/index.js b/packages/jest-changed-files/src/index.ts similarity index 65% rename from packages/jest-changed-files/src/index.js rename to packages/jest-changed-files/src/index.ts index f7ecf3d8fb1d..d3c160e683ee 100644 --- a/packages/jest-changed-files/src/index.js +++ b/packages/jest-changed-files/src/index.ts @@ -4,25 +4,23 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ -import type {Path} from 'types/Config'; -import type {ChangedFilesPromise, Options, Repos} from 'types/ChangedFiles'; +import throat from 'throat'; +import {Path, ChangedFilesPromise, Options, Repos} from './types'; import git from './git'; import hg from './hg'; -import throat from 'throat'; // 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); -const findGitRoot = dir => mutex(() => git.getRoot(dir)); -const findHgRoot = dir => mutex(() => hg.getRoot(dir)); +const findGitRoot = (dir: string) => mutex(() => git.getRoot(dir)); +const findHgRoot = (dir: string) => mutex(() => hg.getRoot(dir)); export const getChangedFilesForRoots = async ( - roots: Array, + roots: Path[], options: Options, ): ChangedFilesPromise => { const repos = await findRepos(roots); @@ -50,16 +48,22 @@ export const getChangedFilesForRoots = async ( return {changedFiles, repos}; }; -export const findRepos = async (roots: Array): Promise => { +export const findRepos = async (roots: Path[]): Promise => { const gitRepos = await Promise.all( - roots.reduce((promises, root) => promises.concat(findGitRoot(root)), []), + roots.reduce[]>( + (promises, root) => promises.concat(findGitRoot(root)), + [], + ), ); const hgRepos = await Promise.all( - roots.reduce((promises, root) => promises.concat(findHgRoot(root)), []), + roots.reduce[]>( + (promises, root) => promises.concat(findHgRoot(root)), + [], + ), ); return { - git: new Set(gitRepos.filter(Boolean)), - hg: new Set(hgRepos.filter(Boolean)), + git: new Set(gitRepos.filter(Boolean) as string[]), + hg: new Set(hgRepos.filter(Boolean) as string[]), }; }; diff --git a/packages/jest-changed-files/src/types.ts b/packages/jest-changed-files/src/types.ts new file mode 100644 index 000000000000..36bde410b70b --- /dev/null +++ b/packages/jest-changed-files/src/types.ts @@ -0,0 +1,28 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * 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; + +export type Options = { + lastCommit?: boolean; + withAncestor?: boolean; + changedSince?: string; + includePaths?: Array; +}; + +export type ChangedFiles = Set; +export type Repos = {git: Set; hg: Set}; +export type ChangedFilesPromise = Promise<{ + repos: Repos; + changedFiles: ChangedFiles; +}>; + +export type SCMAdapter = { + findChangedFiles: (cwd: Path, options: Options) => Promise>; + getRoot: (cwd: Path) => Promise; +}; diff --git a/packages/jest-changed-files/tsconfig.json b/packages/jest-changed-files/tsconfig.json new file mode 100644 index 000000000000..7bb06bce6d20 --- /dev/null +++ b/packages/jest-changed-files/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + } +}