Skip to content

fix(@angular/cli): support silent package installs with Yarn 2+ #23242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions packages/angular/cli/src/utilities/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { memoize } from './memoize';
import { Spinner } from './spinner';

interface PackageManagerOptions {
silent: string;
saveDev: string;
install: string;
installAll?: string;
Expand Down Expand Up @@ -79,28 +78,24 @@ export class PackageManagerUtils {
cwd?: string,
): Promise<boolean> {
const packageManagerArgs = this.getArguments();
const installArgs: string[] = [
packageManagerArgs.install,
packageName,
packageManagerArgs.silent,
];
const installArgs: string[] = [packageManagerArgs.install, packageName];

if (save === 'devDependencies') {
installArgs.push(packageManagerArgs.saveDev);
}

return this.run([...installArgs, ...extraArgs], cwd);
return this.run([...installArgs, ...extraArgs], { cwd, silent: true });
}

/** Install all packages. */
async installAll(extraArgs: string[] = [], cwd?: string): Promise<boolean> {
const packageManagerArgs = this.getArguments();
const installArgs: string[] = [packageManagerArgs.silent];
const installArgs: string[] = [];
if (packageManagerArgs.installAll) {
installArgs.push(packageManagerArgs.installAll);
}

return this.run([...installArgs, ...extraArgs], cwd);
return this.run([...installArgs, ...extraArgs], { cwd, silent: true });
}

/** Install a single package temporary. */
Expand Down Expand Up @@ -160,15 +155,13 @@ export class PackageManagerUtils {
switch (this.name) {
case PackageManager.Yarn:
return {
silent: '--silent',
saveDev: '--dev',
install: 'add',
prefix: '--modules-folder',
noLockfile: '--no-lockfile',
};
case PackageManager.Pnpm:
return {
silent: '--silent',
saveDev: '--save-dev',
install: 'add',
installAll: 'install',
Expand All @@ -177,7 +170,6 @@ export class PackageManagerUtils {
};
default:
return {
silent: '--quiet',
saveDev: '--save-dev',
install: 'install',
installAll: 'install',
Expand All @@ -187,15 +179,21 @@ export class PackageManagerUtils {
}
}

private async run(args: string[], cwd = process.cwd()): Promise<boolean> {
private async run(
args: string[],
options: { cwd?: string; silent?: boolean } = {},
): Promise<boolean> {
const { cwd = process.cwd(), silent = false } = options;

const spinner = new Spinner();
spinner.start('Installing packages...');

return new Promise((resolve) => {
const bufferedOutput: { stream: NodeJS.WriteStream; data: Buffer }[] = [];

const childProcess = spawn(this.name, args, {
stdio: 'pipe',
// Always pipe stderr to allow for failures to be reported
stdio: silent ? ['ignore', 'ignore', 'pipe'] : 'pipe',
shell: true,
cwd,
}).on('close', (code: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { TaskExecutor, UnsuccessfulWorkflowExecution } from '../../src';
import { NodePackageTaskFactoryOptions, NodePackageTaskOptions } from './options';

interface PackageManagerProfile {
quietArgument?: string;
commands: {
installAll?: string;
installPackage: string;
Expand All @@ -24,7 +23,6 @@ interface PackageManagerProfile {

const packageManagers: { [name: string]: PackageManagerProfile } = {
'npm': {
quietArgument: '--quiet',
commands: {
installAll: 'install',
installPackage: 'install',
Expand All @@ -37,13 +35,11 @@ const packageManagers: { [name: string]: PackageManagerProfile } = {
},
},
'yarn': {
quietArgument: '--silent',
commands: {
installPackage: 'add',
},
},
'pnpm': {
quietArgument: '--silent',
commands: {
installAll: 'install',
installPackage: 'install',
Expand Down Expand Up @@ -81,10 +77,15 @@ export default function (

const bufferedOutput: { stream: NodeJS.WriteStream; data: Buffer }[] = [];
const spawnOptions: SpawnOptions = {
stdio: options.hideOutput ? 'pipe' : 'inherit',
shell: true,
cwd: path.join(rootDirectory, options.workingDirectory || ''),
};
if (options.hideOutput) {
spawnOptions.stdio = options.quiet ? ['ignore', 'ignore', 'pipe'] : 'pipe';
} else {
spawnOptions.stdio = options.quiet ? ['ignore', 'ignore', 'inherit'] : 'inherit';
}

const args: string[] = [];

if (options.packageName) {
Expand All @@ -96,10 +97,6 @@ export default function (
args.push(taskPackageManagerProfile.commands.installAll);
}

if (options.quiet && taskPackageManagerProfile.quietArgument) {
args.push(taskPackageManagerProfile.quietArgument);
}

if (!options.allowScripts) {
args.push('--ignore-scripts');
}
Expand Down