Skip to content

Commit

Permalink
refactor(build-infrastructure): Make package manager install command …
Browse files Browse the repository at this point in the history
…an array of args (#22918)

The package manager's install command was a string, but an array of args
is better since we were splitting the string anyway.
  • Loading branch information
tylerbutler authored Oct 29, 2024
1 parent 0b1ce2b commit bea83a6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export interface IPackage<J extends PackageJson = PackageJson> extends Installab

// @public
export interface IPackageManager {
installCommand(updateLockfile: boolean): string;
getInstallCommandWithArgs(updateLockfile: boolean): string[];
readonly lockfileName: string;
readonly name: PackageManagerName;
}
Expand Down
22 changes: 12 additions & 10 deletions build-tools/packages/build-infrastructure/src/packageManagers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class PackageManager implements IPackageManager {
public readonly lockfileName: string;

/**
* Instantiates a new package manager object. Prefer the createPackageManager function to calling the constructor
* directly.
* Instantiates a new package manager object. Prefer the {@link createPackageManager} function, which retuns an
* {@link IPackageManager}, to calling the constructor directly.
*/
public constructor(public readonly name: PackageManagerName) {
switch (this.name) {
Expand All @@ -35,22 +35,24 @@ export class PackageManager implements IPackageManager {
}
}

public installCommand(updateLockfile: boolean): string {
/**
* {@inheritdoc IPackageManager.getInstallCommandWithArgs}
*/
public getInstallCommandWithArgs(updateLockfile: boolean): string[] {
const args: string[] = ["install"];
switch (this.name) {
case "npm": {
const command = "install";
const update = updateLockfile ? "--package-lock=true" : "--package-lock=false";
return `${command} ${update}`;
args.push(updateLockfile ? "--package-lock=true" : "--package-lock=false");
return args;
}

case "pnpm": {
const command = "install";
const update = updateLockfile ? "--no-frozen-lockfile" : "--frozen-lockfile";
return `${command} ${update}`;
args.push(updateLockfile ? "--no-frozen-lockfile" : "--frozen-lockfile");
return args;
}

case "yarn": {
return "install";
return args;
}

default: {
Expand Down
14 changes: 10 additions & 4 deletions build-tools/packages/build-infrastructure/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,20 @@ export interface IPackageManager {
readonly lockfileName: string;

/**
* Returns an install command that can be used to install dependencies using this package manager.
* Returns an array of arguments, including the name of the command, e.g. "install", that can be used to install
* dependencies using this package manager.
*
* @param updateLockfile - If `true`, then the returned command will include flags or arguments necessary to update
* the lockfile during install. If `false`, such flags or arguments should be omitted. Note that the command will
* _not_ include the package manager name istself. For example, the `npm` package manager will return the string
* `"install"`, not `"npm install"`.
* _not_ include the package manager name istself. For example, the `npm` package manager will return `["install"]`,
* not `["npm", "install"]`.
*
* @example
*
* For the pnpm package manager, calling `getInstallCommandWithArgs(true)` would return
* `["install", "--no-frozen-lockfile"]`.
*/
installCommand(updateLockfile: boolean): string;
getInstallCommandWithArgs(updateLockfile: boolean): string[];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions build-tools/packages/build-infrastructure/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ export class Workspace implements IWorkspace {
* {@inheritDoc Installable.install}
*/
public async install(updateLockfile: boolean): Promise<boolean> {
const command = this.packageManager.installCommand(updateLockfile);
const commandArgs = this.packageManager.getInstallCommandWithArgs(updateLockfile);

const output = await execa(this.packageManager.name, command.split(" "), {
const output = await execa(this.packageManager.name, commandArgs, {
cwd: this.directory,
});

Expand Down

0 comments on commit bea83a6

Please sign in to comment.