From 0857c4db26f0574ebd6c96b0f93bfd290abc71d0 Mon Sep 17 00:00:00 2001 From: extr0py Date: Sun, 30 Apr 2017 11:15:12 -0700 Subject: [PATCH] Fix #422 - Get tslint working in release builds (#425) * Add execNodeScript command and leverage in typescript * Create execNodeScript and use with tslint * Clean up whitespace --- browser/src/NeovimInstance.ts | 1 + browser/src/Plugins/Api/Oni.ts | 40 +++++++++++++++++++ vim/core/oni-plugin-tslint/lib/index.js | 10 ++--- .../src/TypeScriptServerHost.ts | 8 +--- vim/core/oni-plugin-typescript/src/index.ts | 2 +- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/browser/src/NeovimInstance.ts b/browser/src/NeovimInstance.ts index 9c938896c7..891e2d25b5 100644 --- a/browser/src/NeovimInstance.ts +++ b/browser/src/NeovimInstance.ts @@ -2,6 +2,7 @@ import * as cp from "child_process" import { remote } from "electron" import { EventEmitter } from "events" import * as path from "path" + import * as Q from "q" import * as Actions from "./actions" import * as Config from "./Config" diff --git a/browser/src/Plugins/Api/Oni.ts b/browser/src/Plugins/Api/Oni.ts index dae2091448..6c62d59dbc 100644 --- a/browser/src/Plugins/Api/Oni.ts +++ b/browser/src/Plugins/Api/Oni.ts @@ -1,3 +1,4 @@ +import * as ChildProcess from "child_process" import { EventEmitter } from "events" import { IPluginChannel } from "./Channel" @@ -51,6 +52,44 @@ export class Oni extends EventEmitter implements Oni.Plugin.Api { this._languageService = new DebouncedLanguageService(languageService) } + public execNodeScript(scriptPath: string, args: string[] = [], options: ChildProcess.ExecOptions = {}, callback: (err: any, stdout: string, stderr: string) => void): ChildProcess.ChildProcess { + const requiredOptions = { + env: { + ELECTRON_RUN_AS_NODE: 1, + }, + } + + const opts = { + ...options, + ...requiredOptions, + } + + const execOptions = [process.execPath, scriptPath].concat(args) + const execString = execOptions.map((s) => `"${s}"`).join(" ") + + return ChildProcess.exec(execString, opts, callback) + } + + /** + * Wrapper around `child_process.exec` to run using electron as opposed to node + */ + public spawnNodeScript(scriptPath: string, args: string[] = [], options: ChildProcess.SpawnOptions = {}): ChildProcess.ChildProcess { + const requiredOptions = { + env: { + ELECTRON_RUN_AS_NODE: 1, + }, + } + + const opts = { + ...options, + ...requiredOptions, + } + + const allArgs = [scriptPath].concat(args) + + return ChildProcess.spawn(process.execPath, allArgs, opts) + } + public setHighlights(file: string, key: string, highlights: Oni.Plugin.SyntaxHighlight[]) { this._channel.send("set-syntax-highlights", null, { file, @@ -65,6 +104,7 @@ export class Oni extends EventEmitter implements Oni.Plugin.Api { key, }) } + private _handleNotification(arg: any): void { if (arg.type === "buffer-update") { this.emit("buffer-update", arg.payload) diff --git a/vim/core/oni-plugin-tslint/lib/index.js b/vim/core/oni-plugin-tslint/lib/index.js index ccce5f3a6d..35412dd256 100644 --- a/vim/core/oni-plugin-tslint/lib/index.js +++ b/vim/core/oni-plugin-tslint/lib/index.js @@ -5,9 +5,7 @@ const exec = require("child_process").exec const findParentDir = require("find-parent-dir") -const isWindows = os.platform() === "win32" -const tslintExecutable = isWindows ? "tslint.cmd" : "tslint" -const tslintPath = path.join(__dirname, "..", "..", "..", "..", "node_modules", ".bin", tslintExecutable) +const tslintPath = path.join(__dirname, "..", "..", "..", "..", "node_modules", "tslint", "lib", "tslint-cli.js") let lastErrors = {} let lastArgs = null @@ -97,12 +95,12 @@ const activate = (Oni) => { processArgs = processArgs.concat(["--fix"]) } - processArgs = processArgs.concat(["--force", "--format json"]) + processArgs = processArgs.concat(["--force", "--format", "json"]) - processArgs.push("--config", path.join(configPath, "tslint.json")) + processArgs = processArgs.concat(["--config", path.join(configPath, "tslint.json")]) processArgs = processArgs.concat(args) - return Q.nfcall(exec, tslintPath + " " + processArgs.join(" "), { cwd: workingDirectory }) + return Q.nfcall(Oni.execNodeScript, tslintPath, processArgs, { cwd: workingDirectory }) .then((stdout, stderr) => { const errorOutput = stdout.join(os.EOL).trim() diff --git a/vim/core/oni-plugin-typescript/src/TypeScriptServerHost.ts b/vim/core/oni-plugin-typescript/src/TypeScriptServerHost.ts index 4dfbbd53fc..73d256c045 100644 --- a/vim/core/oni-plugin-typescript/src/TypeScriptServerHost.ts +++ b/vim/core/oni-plugin-typescript/src/TypeScriptServerHost.ts @@ -24,7 +24,7 @@ export class TypeScriptServerHost extends events.EventEmitter { return this._tssProcess.pid } - constructor() { + constructor(Oni: any) { super() // Other tries for creating process: @@ -39,11 +39,7 @@ export class TypeScriptServerHost extends events.EventEmitter { // This has some info on using eventPort: https://github.com/Microsoft/TypeScript/blob/master/src/server/server.ts // which might be more reliable // Can create the port using this here: https://github.com/Microsoft/TypeScript/blob/master/src/server/server.ts - this._tssProcess = childProcess.exec(`node "${tssPath}"`, { maxBuffer: 500 * 1024 * 1024 }, (err) => { - if (err) { - console.error(err) - } - }) + this._tssProcess = Oni.spawnNodeScript(tssPath) console.log("Process ID: " + this._tssProcess.pid) // tslint:disable-line no-console this._rl = readline.createInterface({ diff --git a/vim/core/oni-plugin-typescript/src/index.ts b/vim/core/oni-plugin-typescript/src/index.ts index dcd8e3a581..138f8c61d8 100644 --- a/vim/core/oni-plugin-typescript/src/index.ts +++ b/vim/core/oni-plugin-typescript/src/index.ts @@ -24,7 +24,7 @@ export interface IDisplayPart { export const activate = (Oni) => { - const host = new TypeScriptServerHost() + const host = new TypeScriptServerHost(Oni) const quickInfo = new QuickInfo(Oni, host) const lastOpenFile = null