-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
278 additions
and
107 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 |
---|---|---|
@@ -1,94 +1,99 @@ | ||
import { platform } from 'os' | ||
import { Runner, ProjectWorkspace } from 'jest-editor-support' | ||
import { WatchMode } from '../Jest' | ||
|
||
export class JestProcess { | ||
static readonly keepAliveLimit = 5 | ||
private runner: Runner | ||
private projectWorkspace: ProjectWorkspace | ||
private onExitCallback: Function | ||
private jestSupportEvents: Map<string, (...args: any[]) => void> | ||
private resolve: Function | ||
private keepAliveCounter: number | ||
public keepAlive: boolean | ||
public stopRequested: boolean | ||
watchMode: WatchMode | ||
|
||
private startRunner() { | ||
this.stopRequested = false | ||
let exited = false | ||
|
||
const options = { | ||
noColor: true, | ||
shell: platform() === 'win32', | ||
} | ||
this.runner = new Runner(this.projectWorkspace, options) | ||
|
||
this.restoreJestEvents() | ||
|
||
this.runner.start(this.watchMode !== WatchMode.None, this.watchMode === WatchMode.WatchAll) | ||
|
||
this.runner.on('debuggerProcessExit', () => { | ||
if (!exited) { | ||
exited = true | ||
if (--this.keepAliveCounter > 0) { | ||
this.runner.removeAllListeners() | ||
this.startRunner() | ||
} else if (this.onExitCallback) { | ||
this.onExitCallback(this) | ||
if (this.stopRequested) { | ||
this.resolve() | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
private restoreJestEvents() { | ||
for (const [event, callback] of this.jestSupportEvents.entries()) { | ||
this.runner.on(event, callback) | ||
} | ||
} | ||
|
||
constructor({ | ||
projectWorkspace, | ||
watchMode = WatchMode.None, | ||
keepAlive = false, | ||
}: { | ||
projectWorkspace: ProjectWorkspace | ||
watchMode?: WatchMode | ||
keepAlive?: boolean | ||
}) { | ||
this.keepAlive = keepAlive | ||
this.watchMode = watchMode | ||
this.projectWorkspace = projectWorkspace | ||
this.keepAliveCounter = keepAlive ? JestProcess.keepAliveLimit : 1 | ||
this.jestSupportEvents = new Map() | ||
|
||
this.startRunner() | ||
} | ||
|
||
public onExit(callback: Function) { | ||
this.onExitCallback = callback | ||
} | ||
|
||
public onJestEditorSupportEvent(event, callback) { | ||
this.jestSupportEvents.set(event, callback) | ||
this.runner.on(event, callback) | ||
return this | ||
} | ||
|
||
public stop() { | ||
this.stopRequested = true | ||
this.keepAliveCounter = 1 | ||
this.jestSupportEvents.clear() | ||
this.runner.closeProcess() | ||
return new Promise(resolve => { | ||
this.resolve = resolve | ||
}) | ||
} | ||
|
||
public runJestWithUpdateForSnapshots(callback) { | ||
this.runner.runJestWithUpdateForSnapshots(callback) | ||
} | ||
} | ||
import { platform } from 'os' | ||
import { Runner, ProjectWorkspace, Options } from 'jest-editor-support' | ||
import { WatchMode } from '../Jest' | ||
import { createProcessInWSL } from './WslProcess' | ||
|
||
export class JestProcess { | ||
static readonly keepAliveLimit = 5 | ||
private runner: Runner | ||
private projectWorkspace: ProjectWorkspace | ||
private onExitCallback: Function | ||
private jestSupportEvents: Map<string, (...args: any[]) => void> | ||
private resolve: Function | ||
private keepAliveCounter: number | ||
public keepAlive: boolean | ||
public stopRequested: boolean | ||
watchMode: WatchMode | ||
|
||
private startRunner() { | ||
this.stopRequested = false | ||
let exited = false | ||
|
||
const options: Options = { | ||
noColor: true, | ||
shell: platform() === 'win32', | ||
} | ||
// If Windows Subsystem for Linux is used, spawn in wsl | ||
if (this.projectWorkspace.pathToJest && this.projectWorkspace.pathToJest.startsWith('wsl')) { | ||
options.createProcess = createProcessInWSL | ||
} | ||
this.runner = new Runner(this.projectWorkspace, options) | ||
|
||
this.restoreJestEvents() | ||
|
||
this.runner.start(this.watchMode !== WatchMode.None, this.watchMode === WatchMode.WatchAll) | ||
|
||
this.runner.on('debuggerProcessExit', () => { | ||
if (!exited) { | ||
exited = true | ||
if (--this.keepAliveCounter > 0) { | ||
this.runner.removeAllListeners() | ||
this.startRunner() | ||
} else if (this.onExitCallback) { | ||
this.onExitCallback(this) | ||
if (this.stopRequested) { | ||
this.resolve() | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
private restoreJestEvents() { | ||
for (const [event, callback] of this.jestSupportEvents.entries()) { | ||
this.runner.on(event, callback) | ||
} | ||
} | ||
|
||
constructor({ | ||
projectWorkspace, | ||
watchMode = WatchMode.None, | ||
keepAlive = false, | ||
}: { | ||
projectWorkspace: ProjectWorkspace | ||
watchMode?: WatchMode | ||
keepAlive?: boolean | ||
}) { | ||
this.keepAlive = keepAlive | ||
this.watchMode = watchMode | ||
this.projectWorkspace = projectWorkspace | ||
this.keepAliveCounter = keepAlive ? JestProcess.keepAliveLimit : 1 | ||
this.jestSupportEvents = new Map() | ||
|
||
this.startRunner() | ||
} | ||
|
||
public onExit(callback: Function) { | ||
this.onExitCallback = callback | ||
} | ||
|
||
public onJestEditorSupportEvent(event, callback) { | ||
this.jestSupportEvents.set(event, callback) | ||
this.runner.on(event, callback) | ||
return this | ||
} | ||
|
||
public stop() { | ||
this.stopRequested = true | ||
this.keepAliveCounter = 1 | ||
this.jestSupportEvents.clear() | ||
this.runner.closeProcess() | ||
return new Promise(resolve => { | ||
this.resolve = resolve | ||
}) | ||
} | ||
|
||
public runJestWithUpdateForSnapshots(callback) { | ||
this.runner.runJestWithUpdateForSnapshots(callback) | ||
} | ||
} |
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,70 @@ | ||
'use strict' | ||
import * as child_process from 'child_process' | ||
|
||
import { ProjectWorkspace, Options } from '../../node_modules/jest-editor-support' | ||
|
||
/** | ||
* Spawns and returns a Jest process with specific args | ||
* | ||
* @param {string[]} args | ||
* @returns {ChildProcess} | ||
*/ | ||
/** | ||
* 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. | ||
* | ||
* | ||
*/ | ||
import { win32, posix } from 'path' | ||
|
||
export const createProcessInWSL = (workspace: ProjectWorkspace, args, options: Options = { shell: true }) => { | ||
// A command could look like `npm run test`, which we cannot use as a command | ||
// as they can only be the first command, so take out the command, and add | ||
// any other bits into the args | ||
const runtimeExecutable = workspace.pathToJest | ||
const parameters = runtimeExecutable.split(' ') | ||
const command = parameters[0] | ||
const initialArgs = parameters.slice(1) | ||
let runtimeArgs = [].concat(initialArgs, args) | ||
|
||
// If a path to configuration file was defined, push it to runtimeArgs | ||
if (workspace.pathToConfig) { | ||
runtimeArgs.push('--config') | ||
runtimeArgs.push(workspace.pathToConfig) | ||
} | ||
|
||
runtimeArgs = runtimeArgs.map(windowsPathToWSL) | ||
|
||
// To use our own commands in create-react, we need to tell the command that | ||
// we're in a CI environment, or it will always append --watch | ||
const env = process.env | ||
env['CI'] = 'true' | ||
|
||
const spawnOptions = { | ||
cwd: workspace.rootPath, | ||
env: env, | ||
shell: options.shell, | ||
} | ||
|
||
if (workspace.debug) { | ||
console.log(`spawning process with command=${command}, args=${runtimeArgs.toString()}`) | ||
} | ||
|
||
return child_process.spawn(command, runtimeArgs, spawnOptions) | ||
} | ||
|
||
function windowsPathToWSL(maybePath: string): string { | ||
const isPath = win32.parse(maybePath).dir | ||
if (!isPath) { | ||
return maybePath | ||
} | ||
const windowsPath = maybePath[0].toLocaleLowerCase() + maybePath.substr(1) | ||
const path = windowsPath | ||
.split(win32.sep) | ||
.join(posix.sep) | ||
.replace(/^(\w):/, '/mnt/$1') | ||
|
||
return path | ||
} |
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
Oops, something went wrong.