Skip to content

Commit ba4e963

Browse files
lorenzorapettiSimenB
authored andcommitted
Migrate jest-changed-files to Typescript (#7827)
* Migrate jest-changed-files to Typescript * Add changelog entry * Update e2e/__tests__/jestChangedFiles.test.js Co-Authored-By: loryman <[email protected]> * Stricter typings Merge remote-tracking branch 'origin/jest-changed-files-typescript' into jest-changed-files-typescript * Make prettier happy
1 parent 605e199 commit ba4e963

File tree

8 files changed

+63
-29
lines changed

8 files changed

+63
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- `[jest-regex-util]`: Migrate to TypeScript ([#7822](https://github.com/facebook/jest/pull/7822))
1616
- `[jest-diff]`: Migrate to TypeScript ([#7824](https://github.com/facebook/jest/pull/7824))
1717
- `[jest-leak-detector]`: Migrate to TypeScript ([#7825](https://github.com/facebook/jest/pull/7825))
18+
- `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827))
1819

1920
### Performance
2021

e2e/__tests__/jestChangedFiles.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99

1010
import os from 'os';
1111
import path from 'path';
12-
import {
13-
findRepos,
14-
getChangedFilesForRoots,
15-
} from '../../packages/jest-changed-files/src';
12+
import {wrap} from 'jest-snapshot-serializer-raw';
13+
import {findRepos, getChangedFilesForRoots} from 'jest-changed-files';
1614
import {skipSuiteOnWindows} from '../../scripts/ConditionalTest';
1715
import {cleanup, run, writeFiles} from '../Utils';
1816
import runJest from '../runJest';
19-
import {wrap} from 'jest-snapshot-serializer-raw';
2017

2118
skipSuiteOnWindows();
2219

packages/jest-changed-files/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"license": "MIT",
1010
"main": "build/index.js",
11+
"types": "build/index.d.ts",
1112
"dependencies": {
1213
"execa": "^1.0.0",
1314
"throat": "^4.0.0"

packages/jest-changed-files/src/git.js renamed to packages/jest-changed-files/src/git.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
87
*/
98

10-
import type {Path} from 'types/Config';
11-
import type {Options, SCMAdapter} from 'types/ChangedFiles';
12-
139
import path from 'path';
1410
import execa from 'execa';
1511

12+
import {Path, Options, SCMAdapter} from './types';
13+
1614
const findChangedFilesUsingCommand = async (
1715
args: Array<string>,
1816
cwd: Path,
@@ -30,7 +28,7 @@ const adapter: SCMAdapter = {
3028
cwd: string,
3129
options?: Options,
3230
): Promise<Array<Path>> => {
33-
const changedSince: ?string =
31+
const changedSince: string | undefined =
3432
options && (options.withAncestor ? 'HEAD^' : options.changedSince);
3533

3634
const includePaths: Array<Path> = (options && options.includePaths) || [];
@@ -72,7 +70,7 @@ const adapter: SCMAdapter = {
7270
}
7371
},
7472

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

7876
try {

packages/jest-changed-files/src/hg.js renamed to packages/jest-changed-files/src/hg.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
87
*/
98

10-
import type {Path} from 'types/Config';
11-
import type {Options, SCMAdapter} from 'types/ChangedFiles';
12-
139
import path from 'path';
1410
import execa from 'execa';
1511

16-
const env = {...process.env, HGPLAIN: 1};
12+
import {Path, Options, SCMAdapter} from './types';
13+
14+
const env = {...process.env, HGPLAIN: '1'};
1715

1816
const ANCESTORS = [
1917
// Parent commit to this one.
@@ -51,7 +49,7 @@ const adapter: SCMAdapter = {
5149
.map(changedPath => path.resolve(cwd, changedPath));
5250
},
5351

54-
getRoot: async (cwd: Path): Promise<?Path> => {
52+
getRoot: async (cwd: Path): Promise<Path | null | undefined> => {
5553
try {
5654
const result = await execa('hg', ['root'], {cwd, env});
5755

packages/jest-changed-files/src/index.js renamed to packages/jest-changed-files/src/index.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
87
*/
98

10-
import type {Path} from 'types/Config';
11-
import type {ChangedFilesPromise, Options, Repos} from 'types/ChangedFiles';
9+
import throat from 'throat';
1210

11+
import {Path, ChangedFilesPromise, Options, Repos} from './types';
1312
import git from './git';
1413
import hg from './hg';
15-
import throat from 'throat';
1614

1715
// This is an arbitrary number. The main goal is to prevent projects with
1816
// many roots (50+) from spawning too many processes at once.
1917
const mutex = throat(5);
2018

21-
const findGitRoot = dir => mutex(() => git.getRoot(dir));
22-
const findHgRoot = dir => mutex(() => hg.getRoot(dir));
19+
const findGitRoot = (dir: string) => mutex(() => git.getRoot(dir));
20+
const findHgRoot = (dir: string) => mutex(() => hg.getRoot(dir));
2321

2422
export const getChangedFilesForRoots = async (
25-
roots: Array<Path>,
23+
roots: Path[],
2624
options: Options,
2725
): ChangedFilesPromise => {
2826
const repos = await findRepos(roots);
@@ -50,16 +48,22 @@ export const getChangedFilesForRoots = async (
5048
return {changedFiles, repos};
5149
};
5250

53-
export const findRepos = async (roots: Array<Path>): Promise<Repos> => {
51+
export const findRepos = async (roots: Path[]): Promise<Repos> => {
5452
const gitRepos = await Promise.all(
55-
roots.reduce((promises, root) => promises.concat(findGitRoot(root)), []),
53+
roots.reduce<Promise<string | null | undefined>[]>(
54+
(promises, root) => promises.concat(findGitRoot(root)),
55+
[],
56+
),
5657
);
5758
const hgRepos = await Promise.all(
58-
roots.reduce((promises, root) => promises.concat(findHgRoot(root)), []),
59+
roots.reduce<Promise<string | null | undefined>[]>(
60+
(promises, root) => promises.concat(findHgRoot(root)),
61+
[],
62+
),
5963
);
6064

6165
return {
62-
git: new Set(gitRepos.filter(Boolean)),
63-
hg: new Set(hgRepos.filter(Boolean)),
66+
git: new Set(gitRepos.filter(Boolean) as string[]),
67+
hg: new Set(hgRepos.filter(Boolean) as string[]),
6468
};
6569
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
export type Path = string;
10+
11+
export type Options = {
12+
lastCommit?: boolean;
13+
withAncestor?: boolean;
14+
changedSince?: string;
15+
includePaths?: Array<Path>;
16+
};
17+
18+
export type ChangedFiles = Set<Path>;
19+
export type Repos = {git: Set<Path>; hg: Set<Path>};
20+
export type ChangedFilesPromise = Promise<{
21+
repos: Repos;
22+
changedFiles: ChangedFiles;
23+
}>;
24+
25+
export type SCMAdapter = {
26+
findChangedFiles: (cwd: Path, options: Options) => Promise<Array<Path>>;
27+
getRoot: (cwd: Path) => Promise<Path | null | undefined>;
28+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
}
7+
}

0 commit comments

Comments
 (0)