-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
index.js
101 lines (89 loc) · 2.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* 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 {EnvironmentClass} from 'types/Environment';
import chalk from 'chalk';
import os from 'os';
import path from 'path';
import yargs from 'yargs';
import {Console, setGlobal} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {readConfig, deprecationEntries} from 'jest-config';
// eslint-disable-next-line import/default
import Runtime from '../';
import * as args from './args';
const VERSION = (require('../../package.json').version: string);
export function run(cliArgv?: Argv, cliInfo?: Array<string>) {
const realFs = require('fs');
const fs = require('graceful-fs');
fs.gracefulify(realFs);
let argv;
if (cliArgv) {
argv = cliArgv;
} else {
argv = yargs
.usage(args.usage)
.help(false)
.version(false)
.options(args.options).argv;
validateCLIOptions(
argv,
Object.assign({}, args.options, {deprecationEntries}),
);
}
if (argv.help) {
yargs.showHelp();
process.on('exit', () => (process.exitCode = 1));
return;
}
if (argv.version) {
console.log(`v${VERSION}\n`);
return;
}
if (!argv._.length) {
console.log('Please provide a path to a script. (See --help for details)');
process.on('exit', () => (process.exitCode = 1));
return;
}
const root = process.cwd();
const filePath = path.resolve(root, argv._[0]);
if (argv.debug) {
const info = cliInfo ? ', ' + cliInfo.join(', ') : '';
console.log(`Using Jest Runtime v${VERSION}${info}`);
}
const options = readConfig(argv, root);
const globalConfig = options.globalConfig;
// Always disable automocking in scripts.
const config = Object.assign({}, options.projectConfig, {
automock: false,
unmockedModulePathPatterns: null,
});
Runtime.createContext(config, {
maxWorkers: os.cpus().length - 1,
watchman: globalConfig.watchman,
})
.then(hasteMap => {
/* $FlowFixMe */
const Environment = (require(config.testEnvironment): EnvironmentClass);
const environment = new Environment(config);
setGlobal(
environment.global,
'console',
new Console(process.stdout, process.stderr),
);
environment.global.jestProjectConfig = config;
environment.global.jestGlobalConfig = globalConfig;
const runtime = new Runtime(config, environment, hasteMap.resolver);
runtime.requireModule(filePath);
})
.catch(e => {
console.error(chalk.red(e.stack || e));
process.on('exit', () => (process.exitCode = 1));
});
}