Skip to content

Commit 3a64b67

Browse files
committed
revert: partially revert changes swapping exec with spawn
Refs: 083bb25, 1f86df3
1 parent 0157241 commit 3a64b67

File tree

6 files changed

+104
-203
lines changed

6 files changed

+104
-203
lines changed

packages/cli/lib/commands/start.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import { PositionalArgs, StartCommandType } from "./types";
77
import { ArgumentsCamelCase } from "yargs";
88

99
const execSyncNpmStart = (port: number, options: ExecSyncOptions): void => {
10-
const args = ['start'];
1110
if (port) {
1211
// Validate port is a number to prevent command injection
1312
if (!Number.isInteger(port) || port < 0 || port > 65535) {
1413
Util.error(`Invalid port number: ${port}`, "red");
1514
return;
1615
}
17-
args.push('--', `--port=${port}`);
16+
Util.execSync(`npm start -- --port=${port}`, options);
17+
return;
1818
}
19-
Util.spawnSync('npm', args, options);
19+
Util.execSync(`npm start`, options);
2020
};
2121

2222
const command: StartCommandType = {

packages/core/packages/PackageManager.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawn } from "child_process";
1+
import { exec } from "child_process";
22
import * as path from "path";
33
import { TemplateManager } from "../../cli/lib/TemplateManager";
44
import { Config, FS_TOKEN, IFileSystem, ProjectTemplate } from "../types";
@@ -123,18 +123,18 @@ export class PackageManager {
123123
}
124124

125125
public static removePackage(packageName: string, verbose: boolean = false): boolean {
126+
let command: string;
126127
const managerCommand = this.getManager();
127-
let args: string[];
128128
switch (managerCommand) {
129129
case "npm":
130130
/* passes through */
131131
default:
132-
args = ['uninstall', packageName, '--quiet', '--save'];
132+
command = `${managerCommand} uninstall ${packageName} --quiet --save`;
133133
break;
134134
}
135135
try {
136136
// tslint:disable-next-line:object-literal-sort-keys
137-
Util.spawnSync(managerCommand, args, { stdio: "pipe", encoding: "utf8" });
137+
Util.execSync(command, { stdio: "pipe", encoding: "utf8" });
138138
} catch (error) {
139139
Util.log(`Error uninstalling package ${packageName} with ${managerCommand}`);
140140
if (verbose) {
@@ -149,10 +149,10 @@ export class PackageManager {
149149

150150
public static addPackage(packageName: string, verbose: boolean = false): boolean {
151151
const managerCommand = this.getManager();
152-
const args = this.getInstallArgs(packageName);
152+
const command = this.getInstallCommand(managerCommand, packageName);
153153
try {
154154
// tslint:disable-next-line:object-literal-sort-keys
155-
Util.spawnSync(managerCommand, args, { stdio: "pipe", encoding: "utf8" });
155+
Util.execSync(command, { stdio: "pipe", encoding: "utf8" });
156156
} catch (error) {
157157
Util.log(`Error installing package ${packageName} with ${managerCommand}`);
158158
if (verbose) {
@@ -165,7 +165,7 @@ export class PackageManager {
165165
}
166166

167167
public static async queuePackage(packageName: string, verbose = false) {
168-
const args = this.getInstallArgs(packageName).map(arg => arg === '--save' ? '--no-save' : arg);
168+
const command = this.getInstallCommand(this.getManager(), packageName).replace("--save", "--no-save");
169169
const [packName, version] = packageName.split(/@(?=[^\/]+$)/);
170170
const packageJSON = this.getPackageJSON();
171171
if (!packageJSON.dependencies) {
@@ -190,24 +190,13 @@ export class PackageManager {
190190
// D.P. Concurrent install runs should be supported
191191
// https://github.com/npm/npm/issues/5948
192192
// https://github.com/npm/npm/issues/2500
193-
const managerCommand = this.getManager();
194193
const task = new Promise<{ packageName, error, stdout, stderr }>((resolve, reject) => {
195-
const child = spawn(managerCommand, args);
196-
let stdout = '';
197-
let stderr = '';
198-
child.stdout?.on('data', (data) => {
199-
stdout += data.toString();
200-
});
201-
child.stderr?.on('data', (data) => {
202-
stderr += data.toString();
203-
});
204-
child.on('close', (code) => {
205-
const error = code !== 0 ? new Error(`Process exited with code ${code}`) : null;
206-
resolve({ packageName, error, stdout, stderr });
207-
});
208-
child.on('error', (err) => {
209-
resolve({ packageName, error: err, stdout, stderr });
210-
});
194+
const child = exec(
195+
command, {},
196+
(error, stdout, stderr) => {
197+
resolve({ packageName, error, stdout, stderr });
198+
}
199+
);
211200
});
212201
task["packageName"] = packName;
213202
this.installQueue.push(task);
@@ -236,7 +225,7 @@ export class PackageManager {
236225
const fullPackageRegistry = config.igPackageRegistry;
237226
try {
238227
// tslint:disable-next-line:object-literal-sort-keys
239-
Util.spawnSync('npm', ['whoami', `--registry=${fullPackageRegistry}`], { stdio: 'pipe', encoding: 'utf8' });
228+
Util.execSync(`npm whoami --registry=${fullPackageRegistry}`, { stdio: "pipe", encoding: "utf8" });
240229
} catch (error) {
241230
// try registering the user:
242231
Util.log(
@@ -265,7 +254,7 @@ export class PackageManager {
265254
if (login?.status === 0) {
266255
//make sure scope is configured:
267256
try {
268-
Util.spawnSync('npm', ['config', 'set', `@infragistics:registry`, fullPackageRegistry]);
257+
Util.execSync(`npm config set @infragistics:registry ${fullPackageRegistry}`);
269258
return true;
270259
} catch (error) {
271260
return false;
@@ -292,10 +281,6 @@ export class PackageManager {
292281
}
293282
}
294283

295-
private static getInstallArgs(packageName: string): string[] {
296-
return ['install', packageName, '--quiet', '--save'];
297-
}
298-
299284
private static getManager(/*config:Config*/): string {
300285
//stub to potentially swap out managers
301286
return "npm";

packages/core/util/Util.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,6 @@ export class Util {
283283
}
284284

285285
for (const key of Object.keys(source)) {
286-
// Skip keys that can affect the prototype of objects to avoid prototype pollution vulnerabilities
287-
if (key === "__proto__" || key === "constructor") {
288-
continue;
289-
}
290-
291286
const sourceKeyIsArray = Array.isArray(source[key]);
292287
const targetHasThisKey = target.hasOwnProperty(key);
293288

@@ -388,8 +383,7 @@ export class Util {
388383
const options: any = { cwd: path.join(parentRoot, projectName), stdio: [process.stdin, "ignore", "ignore"] };
389384
Util.execSync("git init", options);
390385
Util.execSync("git add .", options);
391-
const commitMessage = `"Initial commit for project: ${projectName}"`;
392-
Util.spawnSync('git', ['commit', '-m', commitMessage], options);
386+
Util.execSync("git commit -m " + "\"Initial commit for project: " + projectName + "\"", options);
393387
Util.log(Util.greenCheck() + " Git Initialized and Project '" + projectName + "' Committed");
394388
} catch (error) {
395389
Util.error("Git initialization failed. Install Git in order to automatically commit the project.", "yellow");

spec/unit/new-spec.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,12 @@ describe("Unit - New command", () => {
284284
getProjectLibrary: mockProjLib
285285
});
286286

287-
spyOn(Util, "spawnSync").and.returnValues({
288-
status: 0,
289-
pid: 0,
290-
output: [],
291-
stdout: "",
292-
stderr: "",
293-
signal: "SIGABRT"
294-
});
295-
296287
await newCmd.handler({ name: projectName, framework: "jq", _: ["new"], $0: "new" });
297288

298289
expect(Util.execSync).toHaveBeenCalledWith("git init", jasmine.any(Object));
299290
expect(Util.execSync).toHaveBeenCalledWith("git add .", jasmine.any(Object));
300-
expect(Util.spawnSync).toHaveBeenCalledWith(
301-
"git",
302-
['commit', '-m', `"Initial commit for project: ${projectName}"`],
303-
{ cwd: path.join(process.cwd(), projectName), stdio: [process.stdin, "ignore", "ignore"] }
304-
);
291+
expect(Util.execSync).toHaveBeenCalledWith("git commit -m " + "\"Initial commit for project: " + projectName + "\"",
292+
jasmine.any(Object));
305293
expect(Util.log).toHaveBeenCalledWith(
306294
jasmine.stringMatching("Git Initialized and Project '" + projectName + "' Committed")
307295
);

0 commit comments

Comments
 (0)