Skip to content

Commit

Permalink
remove executeCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
chunyu3 committed Dec 9, 2024
1 parent a14b47e commit 5534466
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 151 deletions.
6 changes: 3 additions & 3 deletions packages/typespec-vscode/src/emit/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Executable } from "vscode-languageclient/node.js";
import logger from "../log/logger.js";
import { InstallationAction, NpmUtil } from "../npm-utils.js";
import { getMainTspFile, resolveTypeSpecCli, toError, toOutput } from "../typespec-utils.js";
import { ExecOutput, executeCommand, isFile, promisifyExec } from "../utils.js";
import { ExecOutput, isFile, promisifySpawn } from "../utils.js";
import { EmitQuickPickItem } from "./emit-quick-pick-item.js";
import { clientEmitters, Emitter } from "./emitter.js";
export async function doEmit(
Expand Down Expand Up @@ -403,7 +403,7 @@ export async function compile(
args.push("--option", `${emitter}.${key}=${value}`);
}

return await promisifyExec(
return await promisifySpawn(
cli.command,
args,
{
Expand All @@ -429,7 +429,7 @@ export async function check(startFile: string): Promise<{
args.push("compile");
args.push(startFile);
args.push("--no-emit");
await executeCommand(cli.command, args, {
await promisifySpawn(cli.command, args, {
cwd: dirname(startFile),
});
logger.info(`complete runtime check.`);
Expand Down
54 changes: 0 additions & 54 deletions packages/typespec-vscode/src/emit/emitters.ts

This file was deleted.

12 changes: 2 additions & 10 deletions packages/typespec-vscode/src/npm-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import path from "path";
import logger from "./log/logger.js";
import { ExecOutput, executionEvents, loadModule, promisifyExec } from "./utils.js";
import { ExecOutput, executionEvents, loadModule, promisifySpawn } from "./utils.js";

export enum InstallationAction {
Install = "Install",
Expand Down Expand Up @@ -30,15 +30,7 @@ export class NpmUtil {
options: any = {},
on?: executionEvents,
): Promise<ExecOutput> {
let command;
if (packages.length > 0) {
command = `npm install ${packages.join(" ")}`;
} else {
command = `npm install`;
}

return await promisifyExec(command, [], { ...options, cwd: this.cwd }, on);
//return spawnExecution("npm", ["install", ...packages], { ...options, cwd: this.cwd });
return promisifySpawn("npm", ["install", ...packages], { cwd: this.cwd }, on);
}

public async ensureNpmPackageInstall(
Expand Down
147 changes: 63 additions & 84 deletions packages/typespec-vscode/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ModuleResolutionResult, ResolveModuleHost } from "@typespec/compiler";
import { exec, spawn, SpawnOptions } from "child_process";
import { spawn, SpawnOptions } from "child_process";
import { readFile, realpath, stat } from "fs/promises";
import { dirname, normalize, resolve } from "path";
import { Executable } from "vscode-languageclient/node.js";
Expand Down Expand Up @@ -119,38 +119,36 @@ export interface executionEvents {
onExit?: (code: number | null, stdout: string, stderror: string) => void;
}

export async function executeCommand(
export async function spawnExecution(
command: string,
args: string[],
options: any,
on?: executionEvents,
): Promise<ExecOutput> {
let stdout: string = "";
let errMessage: string = "";
let stdout = "";
let stderr = "";
let retCode = 0;
if (args.length > 0) {
command = `${command} ${args.join(" ")}`;
}

const child = exec(command, options);
child.stdout?.on("data", (data) => {
const child = spawn(command, args, options);

child.stdout.on("data", (data) => {
stdout += data.toString();
on?.onStdioOut?.(data.toString());
});

child.stderr?.on("data", (data) => {
errMessage += data.toString();
child.stderr.on("data", (data) => {
stderr += data.toString();
on?.onStdioError?.(data.toString());
});

if (on && on.onError) {
child.on("error", (error: any) => {
on.onError!(error, stdout, errMessage);
on.onError!(error, stdout, stderr);
});
}
if (on && on.onExit) {
child.on("exit", (code) => {
on.onExit!(code, stdout, errMessage);
on.onExit!(code, stdout, stderr);
});
}

Expand All @@ -164,37 +162,64 @@ export async function executeCommand(

return {
stdout: stdout,
stderr: errMessage,
stderr: stderr,
exitCode: retCode,
error: errMessage,
error: stderr,
spawnOptions: options,
};
}

export async function promisifyExec(
export async function promisifySpawn(
command: string,
args: string[],
options: any,
options: SpawnOptions,
on?: executionEvents,
): Promise<ExecOutput> {
let stdout: string = "";
let stderr: string = "";
const shell = process.platform === "win32";
const cmd = shell && command.includes(" ") ? `"${command}"` : command;
let stdout = "";
let stderr = "";
let retCode = 0;
if (args.length > 0) {
command = `${command} ${args.join(" ")}`;
}
return await new Promise((resolve, reject) => {
const child = exec(command, options);
child.stdout?.on("data", (data) => {
stdout += data.toString();
on?.onStdioOut?.(data.toString());
});

const spawnOptions: SpawnOptions = {
shell,
stdio: "pipe",
windowsHide: true,
...options,
};
const child = spawn(cmd, args, spawnOptions);

child.stdout?.on("data", (data) => {
stdout += data.toString();
if (on && on.onStdioOut) {
on.onStdioOut(data.toString());
}
});

child.stderr?.on("data", (data) => {
stderr += data.toString();
if (on && on.onStdioError) {
on.onStdioError(data.toString());
}
});

child.on("error", (error) => {
if (on && on.onError) {
on.onError(error, stdout, stderr);
}
stderr += error.message;
});

return new Promise((resolve, reject) => {
child.on("error", (error) => {
stderr += error.message;
});
child.stderr?.on("data", (data) => {
stderr += data.toString();
on?.onStdioError?.(data.toString());
resolve({
stdout: stdout,
stderr: stderr,
exitCode: 0x1212,
error: stderr,
spawnOptions: spawnOptions,
});
});
child.on("close", (code) => {
retCode = code ?? 0;
Expand All @@ -203,68 +228,22 @@ export async function promisifyExec(
stderr: stderr,
exitCode: retCode,
error: stderr,
spawnOptions: options,
spawnOptions: spawnOptions,
});
});

child.on("exit", (code) => {
retCode = code ?? 0;
if (on && on.onExit) {
on.onExit(code, stdout, stderr);
}
resolve({
stdout: stdout,
stderr: stderr,
exitCode: retCode,
error: stderr,
spawnOptions: options,
spawnOptions: spawnOptions,
});
});
});
}

export async function spawnExecution(
command: string,
args: string[],
options: any,
on?: executionEvents,
): Promise<ExecOutput> {
let stdout = "";
let stderr = "";
let retCode = 0;

const child = spawn(command, args, options);

child.stdout.on("data", (data) => {
stdout += data.toString();
on?.onStdioOut?.(data.toString());
});

child.stderr.on("data", (data) => {
stderr += data.toString();
on?.onStdioError?.(data.toString());
});

if (on && on.onError) {
child.on("error", (error: any) => {
on.onError!(error, stdout, stderr);
});
}
if (on && on.onExit) {
child.on("exit", (code) => {
on.onExit!(code, stdout, stderr);
});
}

child.on("close", (code) => {
retCode = code ?? 0;
});

child.on("exit", (code) => {
retCode = code ?? 0;
});

return {
stdout: stdout,
stderr: stderr,
exitCode: retCode,
error: stderr,
spawnOptions: options,
};
}

0 comments on commit 5534466

Please sign in to comment.