Skip to content

Commit

Permalink
Migrate jest-changed-files to Typescript (#7827)
Browse files Browse the repository at this point in the history
* Migrate jest-changed-files to Typescript

* Add changelog entry

* Update e2e/__tests__/jestChangedFiles.test.js

Co-Authored-By: loryman <lorenzo.rapetti.94@gmail.com>

* Stricter typings


Merge remote-tracking branch 'origin/jest-changed-files-typescript' into jest-changed-files-typescript

* Make prettier happy
  • Loading branch information
lorenzorapetti authored and SimenB committed Feb 7, 2019
1 parent 605e199 commit ba4e963
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
7 changes: 2 additions & 5 deletions e2e/__tests__/jestChangedFiles.test.js
Expand Up @@ -9,14 +9,11 @@

import os from 'os';
import path from 'path';
import {
findRepos,
getChangedFilesForRoots,
} from '../../packages/jest-changed-files/src';
import {wrap} from 'jest-snapshot-serializer-raw';
import {findRepos, getChangedFilesForRoots} from '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();

Expand Down
1 change: 1 addition & 0 deletions packages/jest-changed-files/package.json
Expand Up @@ -8,6 +8,7 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"execa": "^1.0.0",
"throat": "^4.0.0"
Expand Down
Expand Up @@ -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<string>,
cwd: Path,
Expand All @@ -30,7 +28,7 @@ const adapter: SCMAdapter = {
cwd: string,
options?: Options,
): Promise<Array<Path>> => {
const changedSince: ?string =
const changedSince: string | undefined =
options && (options.withAncestor ? 'HEAD^' : options.changedSince);

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

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

try {
Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -51,7 +49,7 @@ const adapter: SCMAdapter = {
.map(changedPath => path.resolve(cwd, changedPath));
},

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

Expand Down
Expand Up @@ -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<Path>,
roots: Path[],
options: Options,
): ChangedFilesPromise => {
const repos = await findRepos(roots);
Expand Down Expand Up @@ -50,16 +48,22 @@ export const getChangedFilesForRoots = async (
return {changedFiles, repos};
};

export const findRepos = async (roots: Array<Path>): Promise<Repos> => {
export const findRepos = async (roots: Path[]): Promise<Repos> => {
const gitRepos = await Promise.all(
roots.reduce((promises, root) => promises.concat(findGitRoot(root)), []),
roots.reduce<Promise<string | null | undefined>[]>(
(promises, root) => promises.concat(findGitRoot(root)),
[],
),
);
const hgRepos = await Promise.all(
roots.reduce((promises, root) => promises.concat(findHgRoot(root)), []),
roots.reduce<Promise<string | null | undefined>[]>(
(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[]),
};
};
28 changes: 28 additions & 0 deletions 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<Path>;
};

export type ChangedFiles = Set<Path>;
export type Repos = {git: Set<Path>; hg: Set<Path>};
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>;
};
7 changes: 7 additions & 0 deletions packages/jest-changed-files/tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
}
}

0 comments on commit ba4e963

Please sign in to comment.