From 615212bfc043b4d416655bb1ecce490a3ae7ae96 Mon Sep 17 00:00:00 2001 From: Lorenzo Rapetti Date: Thu, 7 Feb 2019 14:51:21 +0100 Subject: [PATCH 1/5] Migrate jest-changed-files to Typescript --- e2e/__tests__/jestChangedFiles.test.js | 4 +-- packages/jest-changed-files/package.json | 1 + .../jest-changed-files/src/{git.js => git.ts} | 10 +++---- .../jest-changed-files/src/{hg.js => hg.ts} | 10 +++---- .../src/{index.js => index.ts} | 28 +++++++++++-------- packages/jest-changed-files/src/types.ts | 28 +++++++++++++++++++ packages/jest-changed-files/tsconfig.json | 7 +++++ 7 files changed, 62 insertions(+), 26 deletions(-) rename packages/jest-changed-files/src/{git.js => git.ts} (91%) rename packages/jest-changed-files/src/{hg.js => hg.ts} (87%) rename packages/jest-changed-files/src/{index.js => index.ts} (65%) create mode 100644 packages/jest-changed-files/src/types.ts create mode 100644 packages/jest-changed-files/tsconfig.json 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" + } +} From b1f773802d8657b9a6455fe4dfdc63682971d493 Mon Sep 17 00:00:00 2001 From: Lorenzo Rapetti Date: Thu, 7 Feb 2019 14:59:04 +0100 Subject: [PATCH 2/5] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a516378ae79..34b0e950f185 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - `[jest-regex-util]`: Migrate to TypeScript ([#7822](https://github.com/facebook/jest/pull/7822)) - `[jest-diff]`: Migrate to TypeScript ([#7824](https://github.com/facebook/jest/pull/7824)) - `[jest-leak-detector]`: Migrate to TypeScript ([#7825](https://github.com/facebook/jest/pull/7825)) +- `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827)) ### Performance From ce9f636e832e3e2655c0ead33b0279b7d5568d7a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 7 Feb 2019 17:47:09 +0100 Subject: [PATCH 3/5] Update e2e/__tests__/jestChangedFiles.test.js Co-Authored-By: loryman --- e2e/__tests__/jestChangedFiles.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/jestChangedFiles.test.js b/e2e/__tests__/jestChangedFiles.test.js index 8af8e70c1f41..33b5310599f4 100644 --- a/e2e/__tests__/jestChangedFiles.test.js +++ b/e2e/__tests__/jestChangedFiles.test.js @@ -13,7 +13,7 @@ import {wrap} from 'jest-snapshot-serializer-raw'; import { findRepos, getChangedFilesForRoots, -} from '../../packages/jest-changed-files'; +} from 'jest-changed-files'; import {skipSuiteOnWindows} from '../../scripts/ConditionalTest'; import {cleanup, run, writeFiles} from '../Utils'; import runJest from '../runJest'; From 8fbbd6890662835c4cbe36bf1a934432b2a28377 Mon Sep 17 00:00:00 2001 From: Lorenzo Rapetti Date: Thu, 7 Feb 2019 17:50:23 +0100 Subject: [PATCH 4/5] Stricter typings Merge remote-tracking branch 'origin/jest-changed-files-typescript' into jest-changed-files-typescript --- packages/jest-changed-files/src/git.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-changed-files/src/git.ts b/packages/jest-changed-files/src/git.ts index a766bd6b5a3e..27124ef57c33 100644 --- a/packages/jest-changed-files/src/git.ts +++ b/packages/jest-changed-files/src/git.ts @@ -28,7 +28,7 @@ const adapter: SCMAdapter = { cwd: string, options?: Options, ): Promise> => { - const changedSince: string | null | undefined = + const changedSince: string | undefined = options && (options.withAncestor ? 'HEAD^' : options.changedSince); const includePaths: Array = (options && options.includePaths) || []; @@ -70,7 +70,7 @@ const adapter: SCMAdapter = { } }, - getRoot: async (cwd: string): Promise => { + getRoot: async (cwd: string): Promise => { const options = ['rev-parse', '--show-toplevel']; try { From c1bb6076ac8973aaab5e3b304eb084eec8ca0494 Mon Sep 17 00:00:00 2001 From: Lorenzo Rapetti Date: Thu, 7 Feb 2019 17:53:29 +0100 Subject: [PATCH 5/5] Make prettier happy --- e2e/__tests__/jestChangedFiles.test.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/e2e/__tests__/jestChangedFiles.test.js b/e2e/__tests__/jestChangedFiles.test.js index 33b5310599f4..d4efe1838964 100644 --- a/e2e/__tests__/jestChangedFiles.test.js +++ b/e2e/__tests__/jestChangedFiles.test.js @@ -10,10 +10,7 @@ import os from 'os'; import path from 'path'; import {wrap} from 'jest-snapshot-serializer-raw'; -import { - findRepos, - getChangedFilesForRoots, -} from 'jest-changed-files'; +import {findRepos, getChangedFilesForRoots} from 'jest-changed-files'; import {skipSuiteOnWindows} from '../../scripts/ConditionalTest'; import {cleanup, run, writeFiles} from '../Utils'; import runJest from '../runJest';