-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract getConfigs method from jest-cli index file
- Loading branch information
1 parent
78e0893
commit 1d465a5
Showing
2 changed files
with
152 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters