-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from kulshekhar/cleanup
Cleanup
- Loading branch information
Showing
29 changed files
with
721 additions
and
2,707 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
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
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
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
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 |
---|---|---|
@@ -1,91 +1,82 @@ | ||
import * as crypto from 'crypto'; | ||
import * as fs from 'fs-extra'; | ||
import * as nodepath from 'path'; | ||
import * as tsc from 'typescript'; | ||
import { JestConfig, Path, TransformOptions } from './jest-types'; | ||
import { getPostProcessHook } from './postprocess'; | ||
import { getTSConfig, getTSJestConfig } from './utils'; | ||
import { | ||
cacheFile, | ||
getTSConfig, | ||
getTSJestConfig, | ||
injectSourcemapHook, | ||
} from './utils'; | ||
|
||
export function process( | ||
src: string, | ||
path: Path, | ||
config: JestConfig, | ||
filePath: Path, | ||
jestConfig: JestConfig, | ||
transformOptions: TransformOptions = { instrument: false }, | ||
) { | ||
// transformOptions.instrument is a proxy for collectCoverage | ||
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902 | ||
const compilerOptions = getTSConfig( | ||
config.globals, | ||
jestConfig.globals, | ||
transformOptions.instrument, | ||
); | ||
const tsJestConfig = getTSJestConfig(config.globals); | ||
|
||
const isTsFile = path.endsWith('.ts') || path.endsWith('.tsx'); | ||
const isJsFile = path.endsWith('.js') || path.endsWith('.jsx'); | ||
const isHtmlFile = path.endsWith('.html'); | ||
|
||
const postHook = getPostProcessHook(compilerOptions, config, tsJestConfig); | ||
const isTsFile = /\.tsx?$/.test(filePath); | ||
const isJsFile = /\.jsx?$/.test(filePath); | ||
const isHtmlFile = /\.html$/.test(filePath); | ||
|
||
if (isHtmlFile && config.globals.__TRANSFORM_HTML__) { | ||
// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145 | ||
if (isHtmlFile && jestConfig.globals.__TRANSFORM_HTML__) { | ||
src = 'module.exports=`' + src + '`;'; | ||
} | ||
|
||
const processFile = | ||
compilerOptions.allowJs === true ? isTsFile || isJsFile : isTsFile; | ||
|
||
if (processFile) { | ||
const tsTranspiled = tsc.transpileModule(src, { | ||
compilerOptions, | ||
fileName: path, | ||
}); | ||
|
||
const outputText = postHook( | ||
tsTranspiled.outputText, | ||
path, | ||
config, | ||
transformOptions, | ||
); | ||
if (!processFile) { | ||
return src; | ||
} | ||
|
||
// store transpiled code contains source map into cache, except test cases | ||
if (!config.testRegex || !path.match(config.testRegex)) { | ||
const outputFilePath = nodepath.join( | ||
config.cacheDirectory, | ||
'/ts-jest/', | ||
crypto | ||
.createHash('md5') | ||
.update(path) | ||
.digest('hex'), | ||
); | ||
const tsTranspiled = tsc.transpileModule(src, { | ||
compilerOptions, | ||
fileName: filePath, | ||
}); | ||
|
||
fs.outputFileSync(outputFilePath, outputText); | ||
} | ||
const postHook = getPostProcessHook( | ||
compilerOptions, | ||
jestConfig, | ||
getTSJestConfig(jestConfig.globals), | ||
); | ||
|
||
const start = outputText.length > 12 ? outputText.substr(1, 10) : ''; | ||
const outputText = postHook( | ||
tsTranspiled.outputText, | ||
filePath, | ||
jestConfig, | ||
transformOptions, | ||
); | ||
|
||
const modified = | ||
start === 'use strict' | ||
? `'use strict';require('ts-jest').install();${outputText}` | ||
: `require('ts-jest').install();${outputText}`; | ||
const modified = injectSourcemapHook(outputText); | ||
|
||
return modified; | ||
} | ||
cacheFile(jestConfig, filePath, modified); | ||
|
||
return src; | ||
return modified; | ||
} | ||
|
||
export function getCacheKey( | ||
fileData: string, | ||
filePath: Path, | ||
configStr: string, | ||
options: TransformOptions = { instrument: false }, | ||
jestConfigStr: string, | ||
transformOptions: TransformOptions = { instrument: false }, | ||
): string { | ||
const jestConfig: JestConfig = JSON.parse(configStr); | ||
const tsConfig = getTSConfig(jestConfig.globals, options.instrument); | ||
const jestConfig: JestConfig = JSON.parse(jestConfigStr); | ||
|
||
const tsConfig = getTSConfig(jestConfig.globals, transformOptions.instrument); | ||
|
||
return crypto | ||
.createHash('md5') | ||
.update(JSON.stringify(tsConfig), 'utf8') | ||
.update(JSON.stringify(options), 'utf8') | ||
.update(fileData + filePath + configStr, 'utf8') | ||
.update(JSON.stringify(transformOptions), 'utf8') | ||
.update(fileData + filePath + jestConfigStr, 'utf8') | ||
.digest('hex'); | ||
} |
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,66 @@ | ||
import * as fs from 'fs'; | ||
import { normalize } from 'jest-config'; | ||
import * as setFromArgv from 'jest-config/build/set_from_argv'; | ||
import * as path from 'path'; | ||
|
||
function readRawConfig(argv, root) { | ||
const rawConfig = parseConfig(argv); | ||
|
||
if (typeof rawConfig === 'string') { | ||
return loadJestConfigFromFile(path.resolve(process.cwd(), rawConfig), argv); | ||
} | ||
|
||
if (typeof rawConfig === 'object') { | ||
const config = Object.assign({}, rawConfig); | ||
config.rootDir = config.rootDir || root; | ||
return normalize(config, argv); | ||
} | ||
|
||
const packageConfig = loadJestConfigFromPackage( | ||
path.join(root, 'package.json'), | ||
argv, | ||
); | ||
return packageConfig || normalize({ rootDir: root }, argv); | ||
} | ||
|
||
function loadJestConfigFromPackage(filePath, argv) { | ||
/* tslint:disable */ | ||
const R_OK = (fs.constants && fs.constants.R_OK) || (fs['R_OK'] as number); | ||
/* tslint:enable */ | ||
try { | ||
fs.accessSync(filePath, R_OK); | ||
} catch (e) { | ||
return null; | ||
} | ||
|
||
const packageData = require(filePath); | ||
const config = packageData.jest || {}; | ||
const root = path.dirname(filePath); | ||
config.rootDir = config.rootDir ? path.resolve(root, config.rootDir) : root; | ||
return normalize(config, argv); | ||
} | ||
|
||
function parseConfig(argv) { | ||
if (argv.config && typeof argv.config === 'string') { | ||
// If the passed in value looks like JSON, treat it as an object. | ||
if (argv.config[0] === '{' && argv.config[argv.config.length - 1] === '}') { | ||
return JSON.parse(argv.config); | ||
} | ||
} | ||
return argv.config; | ||
} | ||
|
||
function loadJestConfigFromFile(filePath, argv) { | ||
const config = JSON.parse(fs.readFileSync(filePath, 'utf-8')); | ||
config.rootDir = config.rootDir | ||
? path.resolve(path.dirname(filePath), config.rootDir) | ||
: process.cwd(); | ||
return normalize(config, argv); | ||
} | ||
|
||
export function getJestConfig(root) { | ||
const yargs = require('yargs'); | ||
const argv = yargs(process.argv.slice(2)).argv; | ||
const rawConfig = readRawConfig(argv, root); | ||
return Object.freeze(setFromArgv.default(rawConfig, argv)); | ||
} |
Oops, something went wrong.