Skip to content

Commit

Permalink
Migrate jest-changed-files to Typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzorapetti committed Feb 7, 2019
1 parent e67c818 commit 45a9d90
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 26 deletions.
4 changes: 2 additions & 2 deletions e2e/__tests__/jestChangedFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions packages/jest-changed-files/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 | null | 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 | undefined> => {
const options = ['rev-parse', '--show-toplevel'];

try {
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
}
}

0 comments on commit 45a9d90

Please sign in to comment.