2020-03-31 20:42:07 +08:00
"use strict" ;
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
var _ _generator = ( this && this . _ _generator ) || function ( thisArg , body ) {
var _ = { label : 0 , sent : function ( ) { if ( t [ 0 ] & 1 ) throw t [ 1 ] ; return t [ 1 ] ; } , trys : [ ] , ops : [ ] } , f , y , t , g ;
return g = { next : verb ( 0 ) , "throw" : verb ( 1 ) , "return" : verb ( 2 ) } , typeof Symbol === "function" && ( g [ Symbol . iterator ] = function ( ) { return this ; } ) , g ;
function verb ( n ) { return function ( v ) { return step ( [ n , v ] ) ; } ; }
function step ( op ) {
if ( f ) throw new TypeError ( "Generator is already executing." ) ;
while ( _ ) try {
if ( f = 1 , y && ( t = op [ 0 ] & 2 ? y [ "return" ] : op [ 0 ] ? y [ "throw" ] || ( ( t = y [ "return" ] ) && t . call ( y ) , 0 ) : y . next ) && ! ( t = t . call ( y , op [ 1 ] ) ) . done ) return t ;
if ( y = 0 , t ) op = [ op [ 0 ] & 2 , t . value ] ;
switch ( op [ 0 ] ) {
case 0 : case 1 : t = op ; break ;
case 4 : _ . label ++ ; return { value : op [ 1 ] , done : false } ;
case 5 : _ . label ++ ; y = op [ 1 ] ; op = [ 0 ] ; continue ;
case 7 : op = _ . ops . pop ( ) ; _ . trys . pop ( ) ; continue ;
default :
if ( ! ( t = _ . trys , t = t . length > 0 && t [ t . length - 1 ] ) && ( op [ 0 ] === 6 || op [ 0 ] === 2 ) ) { _ = 0 ; continue ; }
if ( op [ 0 ] === 3 && ( ! t || ( op [ 1 ] > t [ 0 ] && op [ 1 ] < t [ 3 ] ) ) ) { _ . label = op [ 1 ] ; break ; }
if ( op [ 0 ] === 6 && _ . label < t [ 1 ] ) { _ . label = t [ 1 ] ; t = op ; break ; }
if ( t && _ . label < t [ 2 ] ) { _ . label = t [ 2 ] ; _ . ops . push ( op ) ; break ; }
if ( t [ 2 ] ) _ . ops . pop ( ) ;
_ . trys . pop ( ) ; continue ;
}
op = body . call ( thisArg , _ ) ;
} catch ( e ) { op = [ 6 , e ] ; y = 0 ; } finally { f = t = 0 ; }
if ( op [ 0 ] & 5 ) throw op [ 1 ] ; return { value : op [ 0 ] ? op [ 1 ] : void 0 , done : true } ;
}
} ;
var _ _read = ( this && this . _ _read ) || function ( o , n ) {
var m = typeof Symbol === "function" && o [ Symbol . iterator ] ;
if ( ! m ) return o ;
var i = m . call ( o ) , r , ar = [ ] , e ;
try {
while ( ( n === void 0 || n -- > 0 ) && ! ( r = i . next ( ) ) . done ) ar . push ( r . value ) ;
}
catch ( error ) { e = { error : error } ; }
finally {
try {
if ( r && ! r . done && ( m = i [ "return" ] ) ) m . call ( i ) ;
}
finally { if ( e ) throw e . error ; }
}
return ar ;
} ;
var _ _spread = ( this && this . _ _spread ) || function ( ) {
for ( var ar = [ ] , i = 0 ; i < arguments . length ; i ++ ) ar = ar . concat ( _ _read ( arguments [ i ] ) ) ;
return ar ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
var bs _logger _1 = require ( "bs-logger" ) ;
2020-04-30 20:40:07 +08:00
var stableStringify = require ( "fast-json-stable-stringify" ) ;
2020-03-31 20:42:07 +08:00
var fs _1 = require ( "fs" ) ;
var json5 _1 = require ( "json5" ) ;
var path _1 = require ( "path" ) ;
var backports _1 = require ( "../../util/backports" ) ;
var presets _1 = require ( "../helpers/presets" ) ;
exports . run = function ( args ) { return _ _awaiter ( void 0 , void 0 , void 0 , function ( ) {
var nullLogger , file , filePath , footNotes , name , isPackage , actualConfig , migratedConfig , presetName , preset , jsTransformers , jsWithTs , jsWithBabel , presetValue , migratedValue , presetValue , migratedValue , before , after , stringify , prefix ;
return _ _generator ( this , function ( _a ) {
nullLogger = bs _logger _1 . createLogger ( { targets : [ ] } ) ;
file = args . _ [ 0 ] ;
filePath = path _1 . resolve ( process . cwd ( ) , file ) ;
footNotes = [ ] ;
if ( ! fs _1 . existsSync ( filePath ) ) {
throw new Error ( "Configuration file " + file + " does not exists." ) ;
}
name = path _1 . basename ( file ) ;
isPackage = name === 'package.json' ;
if ( ! /\.(js|json)$/ . test ( name ) ) {
throw new TypeError ( "Configuration file " + file + " must be a JavaScript or JSON file." ) ;
}
actualConfig = require ( filePath ) ;
if ( isPackage ) {
actualConfig = actualConfig . jest ;
}
if ( ! actualConfig )
actualConfig = { } ;
migratedConfig = backports _1 . backportJestConfig ( nullLogger , actualConfig ) ;
if ( ! migratedConfig . preset && args . jestPreset ) {
if ( args . js ) {
presetName = args . js === 'babel' ? "ts-jest/presets/js-with-babel" : "ts-jest/presets/js-with-ts" ;
}
else {
jsTransformers = Object . keys ( migratedConfig . transform || { } ) . reduce ( function ( list , pattern ) {
if ( RegExp ( pattern . replace ( /^<rootDir>\/?/ , '/dummy-project/' ) ) . test ( '/dummy-project/src/foo.js' ) ) {
var transformer = migratedConfig . transform [ pattern ] ;
if ( /\bbabel-jest\b/ . test ( transformer ) )
transformer = 'babel-jest' ;
else if ( /\ts-jest\b/ . test ( transformer ) )
transformer = 'ts-jest' ;
return _ _spread ( list , [ transformer ] ) ;
}
return list ;
} , [ ] ) ;
jsWithTs = jsTransformers . includes ( 'ts-jest' ) ;
jsWithBabel = jsTransformers . includes ( 'babel-jest' ) ;
if ( jsWithBabel && ! jsWithTs ) {
presetName = "ts-jest/presets/js-with-babel" ;
}
else if ( jsWithTs && ! jsWithBabel ) {
presetName = "ts-jest/presets/js-with-ts" ;
}
else {
presetName = "ts-jest/presets/default" ;
}
}
presetName = presetName || "ts-jest/presets/default" ;
preset = presets _1 . allPresets [ presetName ] ;
footNotes . push ( "Detected preset '" + preset . label + "' as the best matching preset for your configuration.\nVisit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more information about presets.\n" ) ;
}
else if ( migratedConfig . preset && migratedConfig . preset . startsWith ( 'ts-jest' ) ) {
if ( args . jestPreset === false ) {
delete migratedConfig . preset ;
}
else {
preset = presets _1 . allPresets [ migratedConfig . preset ] || presets _1 . defaults ;
}
}
if ( preset )
migratedConfig . preset = preset . name ;
if ( migratedConfig . moduleFileExtensions && migratedConfig . moduleFileExtensions . length && preset ) {
presetValue = dedupSort ( preset . value . moduleFileExtensions || [ ] ) . join ( '::' ) ;
migratedValue = dedupSort ( migratedConfig . moduleFileExtensions ) . join ( '::' ) ;
if ( presetValue === migratedValue ) {
delete migratedConfig . moduleFileExtensions ;
}
}
if ( migratedConfig . testRegex && preset ) {
migratedConfig . testMatch = null ;
}
else if ( migratedConfig . testMatch && migratedConfig . testMatch . length && preset ) {
presetValue = dedupSort ( preset . value . testMatch || [ ] ) . join ( '::' ) ;
migratedValue = dedupSort ( migratedConfig . testMatch ) . join ( '::' ) ;
if ( presetValue === migratedValue ) {
delete migratedConfig . testMatch ;
}
}
if ( migratedConfig . transform ) {
Object . keys ( migratedConfig . transform ) . forEach ( function ( key ) {
var val = migratedConfig . transform [ key ] ;
if ( typeof val === 'string' && /\/?ts-jest(?:\/preprocessor\.js)?$/ . test ( val ) ) {
;
migratedConfig . transform [ key ] = 'ts-jest' ;
}
} ) ;
}
if ( preset &&
migratedConfig . transform &&
2020-04-30 20:40:07 +08:00
stableStringify ( migratedConfig . transform ) === stableStringify ( preset . value . transform ) ) {
2020-03-31 20:42:07 +08:00
delete migratedConfig . transform ;
}
cleanupConfig ( actualConfig ) ;
cleanupConfig ( migratedConfig ) ;
2020-04-30 20:40:07 +08:00
before = stableStringify ( actualConfig ) ;
after = stableStringify ( migratedConfig ) ;
2020-03-31 20:42:07 +08:00
if ( after === before ) {
process . stderr . write ( "\nNo migration needed for given Jest configuration\n " ) ;
return [ 2 ] ;
}
2020-05-15 05:33:08 +08:00
stringify = file . endsWith ( '.json' ) ? JSON . stringify : json5 _1 . stringify ;
prefix = file . endsWith ( '.json' ) ? '"jest": ' : 'module.exports = ' ;
2020-03-31 20:42:07 +08:00
if ( preset && migratedConfig . transform ) {
footNotes . push ( "\nI couldn't check if your \"transform\" value is the same as mine which is: " + stringify ( preset . value . transform , undefined , ' ' ) + "\nIf it is the case, you can safely remove the \"transform\" from what I've migrated.\n" ) ;
}
process . stderr . write ( "\nMigrated Jest configuration:\n" ) ;
process . stdout . write ( "" + prefix + stringify ( migratedConfig , undefined , ' ' ) + "\n" ) ;
if ( footNotes . length ) {
process . stderr . write ( "\n" + footNotes . join ( '\n' ) + "\n" ) ;
}
return [ 2 ] ;
} ) ;
} ) ; } ;
function cleanupConfig ( config ) {
if ( config . globals ) {
if ( config . globals [ 'ts-jest' ] && Object . keys ( config . globals [ 'ts-jest' ] ) . length === 0 ) {
delete config . globals [ 'ts-jest' ] ;
}
if ( Object . keys ( config . globals ) . length === 0 ) {
delete config . globals ;
}
}
if ( config . transform && Object . keys ( config . transform ) . length === 0 ) {
delete config . transform ;
}
if ( config . moduleFileExtensions ) {
config . moduleFileExtensions = dedupSort ( config . moduleFileExtensions ) ;
if ( config . moduleFileExtensions . length === 0 )
delete config . moduleFileExtensions ;
}
if ( config . testMatch ) {
config . testMatch = dedupSort ( config . testMatch ) ;
if ( config . testMatch . length === 0 )
delete config . testMatch ;
}
if ( config . preset === "ts-jest/presets/default" )
config . preset = presets _1 . defaults . name ;
}
function dedupSort ( arr ) {
return arr
. filter ( function ( s , i , a ) { return a . findIndex ( function ( e ) { return s . toString ( ) === e . toString ( ) ; } ) === i ; } )
. sort ( function ( a , b ) { return ( a . toString ( ) > b . toString ( ) ? 1 : a . toString ( ) < b . toString ( ) ? - 1 : 0 ) ; } ) ;
}
exports . help = function ( ) { return _ _awaiter ( void 0 , void 0 , void 0 , function ( ) {
return _ _generator ( this , function ( _a ) {
process . stdout . write ( "\nUsage:\n ts-jest config:migrate [options] <config-file>\n\nArguments:\n <config-file> Can be a js or json Jest config file. If it is a\n package.json file, the configuration will be read from\n the \"jest\" property.\n\nOptions:\n --js ts|babel Process .js files with ts-jest if 'ts' or with\n babel-jest if 'babel'\n --no-jest-preset Disable the use of Jest presets\n" ) ;
return [ 2 ] ;
} ) ;
} ) ; } ;