Skip to content

Commit

Permalink
Extract getConfigs method from jest-cli index file
Browse files Browse the repository at this point in the history
  • Loading branch information
rubennorte committed Oct 3, 2018
1 parent 78e0893 commit 1d465a5
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 136 deletions.
149 changes: 149 additions & 0 deletions packages/jest-cli/src/cli/getConfigs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/

import type {Argv} from 'types/Argv';
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';

import chalk from 'chalk';
import exit from 'exit';
import fs from 'fs';
import logDebugMessages from '../lib/log_debug_messages';
import {readConfig} from 'jest-config';

const printDebugInfoAndExitIfNeeded = (
argv,
globalConfig,
configs,
outputStream,
) => {
if (argv.debug) {
logDebugMessages(globalConfig, configs, outputStream);
return;
}

if (argv.showConfig) {
logDebugMessages(globalConfig, configs, process.stdout);
exit(0);
}
};

const ensureNoDuplicateConfigs = (parsedConfigs, projects, rootConfigPath) => {
const configPathMap = new Map();

for (const config of parsedConfigs) {
const {configPath} = config;
if (configPathMap.has(configPath)) {
const message = `Whoops! Two projects resolved to the same config path: ${chalk.bold(
String(configPath),
)}:
Project 1: ${chalk.bold(projects[parsedConfigs.findIndex(x => x === config)])}
Project 2: ${chalk.bold(
projects[parsedConfigs.findIndex(x => x === configPathMap.get(configPath))],
)}
This usually means that your ${chalk.bold(
'"projects"',
)} config includes a directory that doesn't have any configuration recognizable by Jest. Please fix it.
`;

throw new Error(message);
}
if (configPath !== null) {
configPathMap.set(configPath, config);
}
}
};

// Possible scenarios:
// 1. jest --config config.json
// 2. jest --projects p1 p2
// 3. jest --projects p1 p2 --config config.json
// 4. jest --projects p1
// 5. jest
//
// If no projects are specified, process.cwd() will be used as the default
// (and only) project.
export default function getConfigs(
projectsFromCLIArgs: Array<Path>,
argv: Argv,
outputStream: stream$Writable | tty$WriteStream,
): {
globalConfig: GlobalConfig,
configs: Array<ProjectConfig>,
hasDeprecationWarnings: boolean,
} {
let globalConfig;
let hasDeprecationWarnings;
let configs: Array<ProjectConfig> = [];
let projects = projectsFromCLIArgs;
let configPath: ?Path;

if (projectsFromCLIArgs.length === 1) {
const parsedConfig = readConfig(argv, projects[0]);
configPath = parsedConfig.configPath;

if (parsedConfig.globalConfig.projects) {
// If this was a single project, and its config has `projects`
// settings, use that value instead.
projects = parsedConfig.globalConfig.projects;
}

hasDeprecationWarnings = parsedConfig.hasDeprecationWarnings;
globalConfig = parsedConfig.globalConfig;
configs = [parsedConfig.projectConfig];
if (globalConfig.projects && globalConfig.projects.length) {
// Even though we had one project in CLI args, there might be more
// projects defined in the config.
projects = globalConfig.projects;
}
}

if (projects.length > 1) {
const parsedConfigs = projects
.filter(root => {
// Ignore globbed files that cannot be `require`d.
if (
fs.existsSync(root) &&
!fs.lstatSync(root).isDirectory() &&
!root.endsWith('.js') &&
!root.endsWith('.json')
) {
return false;
}

return true;
})
.map(root => readConfig(argv, root, true, configPath));

ensureNoDuplicateConfigs(parsedConfigs, projects, configPath);
configs = parsedConfigs.map(({projectConfig}) => projectConfig);
if (!hasDeprecationWarnings) {
hasDeprecationWarnings = parsedConfigs.some(
({hasDeprecationWarnings}) => !!hasDeprecationWarnings,
);
}
// If no config was passed initially, use the one from the first project
if (!globalConfig) {
globalConfig = parsedConfigs[0].globalConfig;
}
}

if (!globalConfig || !configs.length) {
throw new Error('jest: No configuration found for any project.');
}

printDebugInfoAndExitIfNeeded(argv, globalConfig, configs, outputStream);

return {
configs,
globalConfig,
hasDeprecationWarnings: !!hasDeprecationWarnings,
};
}
139 changes: 3 additions & 136 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@

import type {AggregatedResult} from 'types/TestResult';
import type {Argv} from 'types/Argv';
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
import type {GlobalConfig, Path} from 'types/Config';

import {Console, clearLine, createDirectory} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {readConfig, deprecationEntries} from 'jest-config';
import {deprecationEntries} from 'jest-config';
import getConfigs from './getConfigs';
import * as args from './args';
import chalk from 'chalk';
import createContext from '../lib/create_context';
import exit from 'exit';
import getChangedFilesPromise from '../getChangedFilesPromise';
import {formatHandleErrors} from '../collectHandles';
import fs from 'fs';
import handleDeprecationWarnings from '../lib/handle_deprecation_warnings';
import logDebugMessages from '../lib/log_debug_messages';
import {print as preRunMessagePrint} from '../preRunMessage';
import runJest from '../runJest';
import Runtime from 'jest-runtime';
Expand Down Expand Up @@ -201,138 +200,6 @@ const getProjectListFromCLIArgs = (argv, project: ?Path) => {
return projects;
};

const printDebugInfoAndExitIfNeeded = (
argv,
globalConfig,
configs,
outputStream,
) => {
if (argv.debug) {
logDebugMessages(globalConfig, configs, outputStream);
return;
}

if (argv.showConfig) {
logDebugMessages(globalConfig, configs, process.stdout);
exit(0);
}
};

const ensureNoDuplicateConfigs = (parsedConfigs, projects, rootConfigPath) => {
const configPathMap = new Map();

for (const config of parsedConfigs) {
const {configPath} = config;
if (configPathMap.has(configPath)) {
const message = `Whoops! Two projects resolved to the same config path: ${chalk.bold(
String(configPath),
)}:
Project 1: ${chalk.bold(projects[parsedConfigs.findIndex(x => x === config)])}
Project 2: ${chalk.bold(
projects[parsedConfigs.findIndex(x => x === configPathMap.get(configPath))],
)}
This usually means that your ${chalk.bold(
'"projects"',
)} config includes a directory that doesn't have any configuration recognizable by Jest. Please fix it.
`;

throw new Error(message);
}
if (configPath !== null) {
configPathMap.set(configPath, config);
}
}
};

// Possible scenarios:
// 1. jest --config config.json
// 2. jest --projects p1 p2
// 3. jest --projects p1 p2 --config config.json
// 4. jest --projects p1
// 5. jest
//
// If no projects are specified, process.cwd() will be used as the default
// (and only) project.
const getConfigs = (
projectsFromCLIArgs: Array<Path>,
argv: Argv,
outputStream,
): {
globalConfig: GlobalConfig,
configs: Array<ProjectConfig>,
hasDeprecationWarnings: boolean,
} => {
let globalConfig;
let hasDeprecationWarnings;
let configs: Array<ProjectConfig> = [];
let projects = projectsFromCLIArgs;
let configPath: ?Path;

if (projectsFromCLIArgs.length === 1) {
const parsedConfig = readConfig(argv, projects[0]);
configPath = parsedConfig.configPath;

if (parsedConfig.globalConfig.projects) {
// If this was a single project, and its config has `projects`
// settings, use that value instead.
projects = parsedConfig.globalConfig.projects;
}

hasDeprecationWarnings = parsedConfig.hasDeprecationWarnings;
globalConfig = parsedConfig.globalConfig;
configs = [parsedConfig.projectConfig];
if (globalConfig.projects && globalConfig.projects.length) {
// Even though we had one project in CLI args, there might be more
// projects defined in the config.
projects = globalConfig.projects;
}
}

if (projects.length > 1) {
const parsedConfigs = projects
.filter(root => {
// Ignore globbed files that cannot be `require`d.
if (
fs.existsSync(root) &&
!fs.lstatSync(root).isDirectory() &&
!root.endsWith('.js') &&
!root.endsWith('.json')
) {
return false;
}

return true;
})
.map(root => readConfig(argv, root, true, configPath));

ensureNoDuplicateConfigs(parsedConfigs, projects, configPath);
configs = parsedConfigs.map(({projectConfig}) => projectConfig);
if (!hasDeprecationWarnings) {
hasDeprecationWarnings = parsedConfigs.some(
({hasDeprecationWarnings}) => !!hasDeprecationWarnings,
);
}
// If no config was passed initially, use the one from the first project
if (!globalConfig) {
globalConfig = parsedConfigs[0].globalConfig;
}
}

if (!globalConfig || !configs.length) {
throw new Error('jest: No configuration found for any project.');
}

printDebugInfoAndExitIfNeeded(argv, globalConfig, configs, outputStream);

return {
configs,
globalConfig,
hasDeprecationWarnings: !!hasDeprecationWarnings,
};
};

const buildContextsAndHasteMaps = async (
configs,
globalConfig,
Expand Down

0 comments on commit 1d465a5

Please sign in to comment.