Skip to content
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

feat: better instructions on package manager installation failure #1504

Merged
merged 1 commit into from
Feb 14, 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
16 changes: 12 additions & 4 deletions lib/package-managers/abstract.package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export abstract class AbstractPackageManager {
});
spinner.start();
try {
const commandArguments = `${this.cli.install} --silent`;
const commandArgs = `${this.cli.install} ${this.cli.silentFlag}`;
const collect = true;
const dasherizedDirectory: string = dasherize(directory);
const dasherizedDirectory = dasherize(directory);
await this.runner.run(
commandArguments,
commandArgs,
collect,
join(process.cwd(), dasherizedDirectory),
);
Expand All @@ -39,7 +39,15 @@ export abstract class AbstractPackageManager {
console.info();
} catch {
spinner.fail();
console.error(chalk.red(MESSAGES.PACKAGE_MANAGER_INSTALLATION_FAILED));
const commandArgs = this.cli.install;
const commandToRun = this.runner.rawFullCommand(commandArgs);
console.error(
chalk.red(
MESSAGES.PACKAGE_MANAGER_INSTALLATION_FAILED(
chalk.bold(commandToRun),
),
),
);
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/package-managers/npm.package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class NpmPackageManager extends AbstractPackageManager {
remove: 'uninstall',
saveFlag: '--save',
saveDevFlag: '--save-dev',
silentFlag: '--silent',
};
}
}
1 change: 1 addition & 0 deletions lib/package-managers/package-manager-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface PackageManagerCommands {
remove: string;
saveFlag: string;
saveDevFlag: string;
silentFlag: string;
}
1 change: 1 addition & 0 deletions lib/package-managers/pnpm.package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class PnpmPackageManager extends AbstractPackageManager {
remove: 'uninstall',
saveFlag: '--save',
saveDevFlag: '--save-dev',
silentFlag: '--reporter=silent',
};
}
}
1 change: 1 addition & 0 deletions lib/package-managers/yarn.package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class YarnPackageManager extends AbstractPackageManager {
remove: 'remove',
saveFlag: '',
saveDevFlag: '-D',
silentFlag: '--silent',
};
}
}
9 changes: 9 additions & 0 deletions lib/runners/abstract.runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ export class AbstractRunner {
});
});
}

/**
* @param command
* @returns The entire command that will be ran when calling `run(command)`.
*/
public rawFullCommand(command: string): string {
const commandArgs: string[] = [...this.args, command];
return `${this.binary} ${commandArgs.join(' ')}`;
}
}
3 changes: 2 additions & 1 deletion lib/ui/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const MESSAGES = {
GET_STARTED_INFORMATION: `${EMOJIS.POINT_RIGHT} Get started with the following commands:`,
CHANGE_DIR_COMMAND: (name: string) => `$ cd ${name}`,
START_COMMAND: (name: string) => `$ ${name} run start`,
PACKAGE_MANAGER_INSTALLATION_FAILED: `${EMOJIS.SCREAM} Packages installation failed, see above`,
PACKAGE_MANAGER_INSTALLATION_FAILED: (commandToRunManually: string) =>
`${EMOJIS.SCREAM} Packages installation failed!\nIn case you don't see any errors above, consider manually running the failed command ${commandToRunManually} to see more details on why it errored out.`,
// tslint:disable-next-line:max-line-length
NEST_INFORMATION_PACKAGE_MANAGER_FAILED: `${EMOJIS.SMIRK} cannot read your project package.json file, are you inside your project directory?`,
LIBRARY_INSTALLATION_FAILED_BAD_PACKAGE: (name: string) =>
Expand Down
1 change: 1 addition & 0 deletions test/lib/package-managers/npm.package-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('NpmPackageManager', () => {
remove: 'uninstall',
saveFlag: '--save',
saveDevFlag: '--save-dev',
silentFlag: '--silent',
};
expect(packageManager.cli).toMatchObject(expectedValues);
});
Expand Down
21 changes: 11 additions & 10 deletions test/lib/package-managers/pnpm.package-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PnpmRunner } from '../../../lib/runners/pnpm.runner';

jest.mock('../../../lib/runners/pnpm.runner');

describe('NpmPackageManager', () => {
describe('PnpmPackageManager', () => {
let packageManager: PnpmPackageManager;
beforeEach(() => {
(PnpmRunner as any).mockClear();
Expand All @@ -29,6 +29,7 @@ describe('NpmPackageManager', () => {
remove: 'uninstall',
saveFlag: '--save',
saveDevFlag: '--save-dev',
silentFlag: '--reporter=silent',
};
expect(packageManager.cli).toMatchObject(expectedValues);
});
Expand All @@ -38,7 +39,7 @@ describe('NpmPackageManager', () => {
const dirName = '/tmp';
const testDir = join(process.cwd(), dirName);
packageManager.install(dirName, 'pnpm');
expect(spy).toBeCalledWith('install --silent', true, testDir);
expect(spy).toBeCalledWith('install --reporter=silent', true, testDir);
});
});
describe('addProduction', () => {
Expand All @@ -47,8 +48,8 @@ describe('NpmPackageManager', () => {
const dependencies = ['@nestjs/common', '@nestjs/core'];
const tag = '5.0.0';
const command = `install --save ${dependencies
.map((dependency) => `${dependency}@${tag}`)
.join(' ')}`;
.map((dependency) => `${dependency}@${tag}`)
.join(' ')}`;
packageManager.addProduction(dependencies, tag);
expect(spy).toBeCalledWith(command, true);
});
Expand All @@ -59,8 +60,8 @@ describe('NpmPackageManager', () => {
const dependencies = ['@nestjs/common', '@nestjs/core'];
const tag = '5.0.0';
const command = `install --save-dev ${dependencies
.map((dependency) => `${dependency}@${tag}`)
.join(' ')}`;
.map((dependency) => `${dependency}@${tag}`)
.join(' ')}`;
packageManager.addDevelopment(dependencies, tag);
expect(spy).toBeCalledWith(command, true);
});
Expand Down Expand Up @@ -91,8 +92,8 @@ describe('NpmPackageManager', () => {
const uninstallCommand = `uninstall --save ${dependencies.join(' ')}`;

const installCommand = `install --save ${dependencies
.map((dependency) => `${dependency}@${tag}`)
.join(' ')}`;
.map((dependency) => `${dependency}@${tag}`)
.join(' ')}`;

return packageManager.upgradeProduction(dependencies, tag).then(() => {
expect(spy.mock.calls).toEqual([
Expand All @@ -110,8 +111,8 @@ describe('NpmPackageManager', () => {
const uninstallCommand = `uninstall --save-dev ${dependencies.join(' ')}`;

const installCommand = `install --save-dev ${dependencies
.map((dependency) => `${dependency}@${tag}`)
.join(' ')}`;
.map((dependency) => `${dependency}@${tag}`)
.join(' ')}`;

return packageManager.upgradeDevelopment(dependencies, tag).then(() => {
expect(spy.mock.calls).toEqual([
Expand Down
1 change: 1 addition & 0 deletions test/lib/package-managers/yarn.package-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('YarnPackageManager', () => {
remove: 'remove',
saveFlag: '',
saveDevFlag: '-D',
silentFlag: '--silent',
};
expect(packageManager.cli).toMatchObject(expectedValues);
});
Expand Down