Skip to content

Commit 99949d0

Browse files
committed
refactor:[WIP] reduce unsafe command arg usage
1 parent 401e912 commit 99949d0

File tree

6 files changed

+19
-32
lines changed

6 files changed

+19
-32
lines changed

packages/core/packages/PackageManager.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ export class PackageManager {
8888
const config = ProjectConfig.localConfig();
8989
if (!config.packagesInstalled) {
9090
let command: string;
91-
let managerCommand: string;
91+
const managerCommand = this.getManager();
9292

93-
managerCommand = this.getManager();
9493
switch (managerCommand) {
9594
case "npm":
9695
/* passes through */
@@ -281,7 +280,7 @@ export class PackageManager {
281280
}
282281
}
283282

284-
private static getManager(/*config:Config*/): string {
283+
private static getManager(/*config:Config*/): 'npm' {
285284
//stub to potentially swap out managers
286285
return "npm";
287286
}

packages/core/util/Util.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -348,29 +348,17 @@ export class Util {
348348
}
349349

350350
/**
351-
* Execute synchronous command with options using spawnSync
352-
* @param command Command to be executed
353-
* @param args Command arguments
354-
* @param options Command options
355-
* @throws {Error} On non-zero exit code. Error has 'status', 'signal', 'output', 'stdout', 'stderr'
356-
*/
357-
public static spawnSync(command: string, args: string[], options?: SpawnSyncOptions) {
358-
try {
359-
return spawnSync(command, args, options);
360-
} catch (error) {
361-
// Handle potential process interruption
362-
// Check if the error output ends with "^C"
363-
if (error.stderr && error.stderr.toString().endsWith() === "^C") {
364-
return process.exit();
365-
}
366-
367-
// Handle specific exit codes for different signals
368-
if (error.status === 3221225786 || error.status > 128) {
369-
return process.exit();
370-
}
371-
372-
throw error;
373-
}
351+
* Execute synchronous command with options using spawnSync
352+
* @param command Command to be executed
353+
* NOTE: `spawn` without `shell` (unsafe) is **not** equivalent to `exec` & requires `npm.cmd` to run the correct process on win
354+
* do not call with/add commands that are not known binaries without validating first
355+
* @param args Command arguments
356+
* @param options Command options
357+
* @returns {SpawnSyncReturns} object with status and stdout
358+
* @remarks Consuming code MUST handle the result and check for failure status!
359+
*/
360+
public static spawnSync(command: string, args: string[], options?: Omit<SpawnSyncOptions, 'shell'>) {
361+
return spawnSync(command, args, options);
374362
}
375363

376364
/**
@@ -383,7 +371,7 @@ export class Util {
383371
const options: any = { cwd: path.join(parentRoot, projectName), stdio: [process.stdin, "ignore", "ignore"] };
384372
Util.execSync("git init", options);
385373
Util.execSync("git add .", options);
386-
Util.execSync("git commit -m " + "\"Initial commit for project: " + projectName + "\"", options);
374+
Util.execSync("git commit -m " + "\"Initial commit for project\"", options);
387375
Util.log(Util.greenCheck() + " Git Initialized and Project '" + projectName + "' Committed");
388376
} catch (error) {
389377
Util.error("Git initialization failed. Install Git in order to automatically commit the project.", "yellow");

packages/ng-schematics/src/ng-new/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function newProject(options: OptionsSchema): Rule {
174174
}
175175
if (!options.skipGit) {
176176
const gitTask = context.addTask(
177-
new RepositoryInitializerTask(options.name, { message: `Initial commit for project: ${options.name}` }),
177+
new RepositoryInitializerTask(options.name, { message: `Initial commit for project` }),
178178
[...installChain] //copy
179179
);
180180
installChain.push(gitTask);

packages/ng-schematics/src/ng-new/index_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe("Schematics ng-new", () => {
152152
authorEmail: undefined,
153153
authorName: undefined,
154154
commit: true,
155-
message: `Initial commit for project: ${workingDirectory}`
155+
message: `Initial commit for project`
156156
};
157157
const expectedStart: RunSchematicTaskOptions<any> = {
158158
collection: null,
@@ -206,7 +206,7 @@ describe("Schematics ng-new", () => {
206206
authorEmail: undefined,
207207
authorName: undefined,
208208
commit: true,
209-
message: `Initial commit for project: ${workingDirectory}`
209+
message: `Initial commit for project`
210210
};
211211
expect(taskOptions.length).toBe(2);
212212
expect(mockProject.upgradeIgniteUIPackages).toHaveBeenCalled();

spec/acceptance/new-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe("New command", () => {
135135
process.chdir(projectName);
136136
expect(fs.existsSync(".git")).toBeTruthy();
137137
expect(Util.execSync("git log -1 --pretty=format:'%s'").toString())
138-
.toMatch("Initial commit for project: " + projectName);
138+
.toMatch("Initial commit for project");
139139
process.chdir("../");
140140
testFolder = "./angularProj";
141141
});

spec/unit/new-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe("Unit - New command", () => {
288288

289289
expect(Util.execSync).toHaveBeenCalledWith("git init", jasmine.any(Object));
290290
expect(Util.execSync).toHaveBeenCalledWith("git add .", jasmine.any(Object));
291-
expect(Util.execSync).toHaveBeenCalledWith("git commit -m " + "\"Initial commit for project: " + projectName + "\"",
291+
expect(Util.execSync).toHaveBeenCalledWith("git commit -m \"Initial commit for project\"",
292292
jasmine.any(Object));
293293
expect(Util.log).toHaveBeenCalledWith(
294294
jasmine.stringMatching("Git Initialized and Project '" + projectName + "' Committed")

0 commit comments

Comments
 (0)