44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
66 *
7- * @flow
87 */
98
10- 'use strict' ;
11-
12- import type { Path } from 'types/Config' ;
13-
14- import { sync as spawnSync } from 'execa' ;
159import fs from 'fs' ;
1610import path from 'path' ;
11+ import { Config } from '@jest/types' ;
12+
13+ import { sync as spawnSync , ExecaReturns } from 'execa' ;
1714import { createDirectory } from 'jest-util' ;
1815import rimraf from 'rimraf' ;
1916
20- export const run = ( cmd : string , cwd ? : Path ) => {
17+ export type RunResult = ExecaReturns & {
18+ status : number ;
19+ error : Error ;
20+ } ;
21+ export const run = ( cmd : string , cwd ?: Config . Path ) : RunResult => {
2122 const args = cmd . split ( / \s / ) . slice ( 1 ) ;
2223 const spawnOptions = { cwd, reject : false } ;
23- const result = spawnSync ( cmd . split ( / \s / ) [ 0 ] , args , spawnOptions ) ;
24+ const result = spawnSync ( cmd . split ( / \s / ) [ 0 ] , args , spawnOptions ) as RunResult ;
2425
2526 // For compat with cross-spawn
2627 result . status = result . code ;
@@ -39,7 +40,7 @@ export const run = (cmd: string, cwd?: Path) => {
3940 return result ;
4041} ;
4142
42- export const linkJestPackage = ( packageName : string , cwd : Path ) => {
43+ export const linkJestPackage = ( packageName : string , cwd : Config . Path ) => {
4344 const packagesDir = path . resolve ( __dirname , '../packages' ) ;
4445 const packagePath = path . resolve ( packagesDir , packageName ) ;
4546 const destination = path . resolve ( cwd , 'node_modules/' , packageName ) ;
@@ -50,8 +51,8 @@ export const linkJestPackage = (packageName: string, cwd: Path) => {
5051
5152export const makeTemplate = (
5253 str : string ,
53- ) : ( ( values ?: Array < any > ) => string ) => ( values : ? Array < any > ) =>
54- str . replace ( / \$ ( \d + ) / g, ( match , number ) => {
54+ ) : ( ( values ?: Array < any > ) => string ) => ( values ?: Array < any > ) =>
55+ str . replace ( / \$ ( \d + ) / g, ( _match , number ) => {
5556 if ( ! Array . isArray ( values ) ) {
5657 throw new Error ( 'Array of values must be passed to the template.' ) ;
5758 }
@@ -76,14 +77,13 @@ export const writeFiles = (
7677) => {
7778 createDirectory ( directory ) ;
7879 Object . keys ( files ) . forEach ( fileOrPath => {
79- const filePath = fileOrPath . split ( '/' ) ; // ['tmp', 'a.js']
80- const filename = filePath . pop ( ) ; // filepath becomes dirPath (no filename)
80+ const dirname = path . dirname ( fileOrPath ) ;
8181
82- if ( filePath . length ) {
83- createDirectory ( path . join . apply ( path , [ directory ] . concat ( filePath ) ) ) ;
82+ if ( dirname !== '/' ) {
83+ createDirectory ( path . join ( directory , dirname ) ) ;
8484 }
8585 fs . writeFileSync (
86- path . resolve . apply ( path , [ directory ] . concat ( filePath , [ filename ] ) ) ,
86+ path . resolve ( directory , ... fileOrPath . split ( '/' ) ) ,
8787 files [ fileOrPath ] ,
8888 ) ;
8989 } ) ;
@@ -118,7 +118,7 @@ export const sortLines = (output: string) =>
118118 . join ( '\n' ) ;
119119
120120export const createEmptyPackage = (
121- directory : Path ,
121+ directory : Config . Path ,
122122 packageJson ?: { [ keys : string ] : any } ,
123123) => {
124124 const DEFAULT_PACKAGE_JSON = {
@@ -168,7 +168,7 @@ export const extractSummary = (stdout: string) => {
168168const sortTests = ( stdout : string ) =>
169169 stdout
170170 . split ( '\n' )
171- . reduce ( ( tests , line , i ) => {
171+ . reduce ( ( tests : Array < Array < string > > , line ) => {
172172 if ( [ 'RUNS' , 'PASS' , 'FAIL' ] . includes ( line . slice ( 0 , 4 ) ) ) {
173173 tests . push ( [ line . trimRight ( ) ] ) ;
174174 } else if ( line ) {
@@ -194,11 +194,11 @@ export const extractSortedSummary = (stdout: string) => {
194194
195195export const extractSummaries = (
196196 stdout : string ,
197- ) : Array < { rest : string , summary : string } > => {
197+ ) : Array < { rest : string ; summary : string } > => {
198198 const regex = / T e s t S u i t e s : .* \n T e s t s .* \n S n a p s h o t s .* \n T i m e .* ( \n R a n a l l t e s t s u i t e s ) * .* \n * $ / gm;
199199
200200 let match = regex . exec ( stdout ) ;
201- const matches = [ ] ;
201+ const matches : Array < RegExpExecArray > = [ ] ;
202202
203203 while ( match ) {
204204 matches . push ( match ) ;
0 commit comments