Skip to content

Commit 8b3cada

Browse files
clydinBrocco
authored andcommitted
refactor(@angular/cli): remove silent-error dependency
1 parent c211ca6 commit 8b3cada

File tree

9 files changed

+2034
-2098
lines changed

9 files changed

+2034
-2098
lines changed

package-lock.json

Lines changed: 1950 additions & 1933 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"resolve": "^1.1.7",
5252
"rxjs": "^6.0.0",
5353
"semver": "^5.3.0",
54-
"silent-error": "^1.0.0",
5554
"symbol-observable": "^1.2.0",
5655
"yargs-parser": "^10.0.0"
5756
},

packages/@angular/cli/commands/add.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { getPackageManager } from '../utilities/config';
66
import { SchematicCommand } from '../models/schematic-command';
77
import { NpmInstall } from '../tasks/npm-install';
88

9-
const SilentError = require('silent-error');
10-
119

1210
export default class AddCommand extends SchematicCommand {
1311
readonly name = 'add';
@@ -33,10 +31,12 @@ export default class AddCommand extends SchematicCommand {
3331
const collectionName = options._[0];
3432

3533
if (!collectionName) {
36-
throw new SilentError(
34+
this.logger.fatal(
3735
`The "ng ${this.name}" command requires a name argument to be specified eg. `
3836
+ `${terminal.yellow('ng add [name] ')}. For more details, use "ng help".`
3937
);
38+
39+
return false;
4040
}
4141

4242
return true;
@@ -46,10 +46,12 @@ export default class AddCommand extends SchematicCommand {
4646
const firstArg = options._[0];
4747

4848
if (!firstArg) {
49-
throw new SilentError(
49+
this.logger.fatal(
5050
`The "ng ${this.name}" command requires a name argument to be specified eg. `
5151
+ `${terminal.yellow('ng add [name] ')}. For more details, use "ng help".`
5252
);
53+
54+
return 1;
5355
}
5456

5557
const packageManager = getPackageManager();
@@ -93,10 +95,12 @@ export default class AddCommand extends SchematicCommand {
9395
return await this.runSchematic(runOptions);
9496
} catch (e) {
9597
if (e instanceof NodePackageDoesNotSupportSchematics) {
96-
throw new SilentError(tags.oneLine`
98+
this.logger.error(tags.oneLine`
9799
The package that you are trying to add does not support schematics. You can try using
98100
a different version of the package or contact the package author to add ng-add support.
99101
`);
102+
103+
return 1;
100104
}
101105

102106
throw e;

packages/@angular/cli/commands/config.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import {
1616
tags,
1717
} from '@angular-devkit/core';
1818

19-
const SilentError = require('silent-error');
20-
2119

2220
export interface ConfigOptions {
2321
jsonPath: string;
@@ -188,20 +186,24 @@ export default class ConfigCommand extends Command {
188186

189187
if (options.value == undefined) {
190188
if (!config) {
191-
throw new SilentError('No config found.');
189+
this.logger.error('No config found.');
190+
191+
return 1;
192192
}
193193

194-
this.get(config._workspace, options);
194+
return this.get(config._workspace, options);
195195
} else {
196-
this.set(options);
196+
return this.set(options);
197197
}
198198
}
199199

200200
private get(config: experimental.workspace.WorkspaceSchema, options: ConfigOptions) {
201201
const value = options.jsonPath ? getValueFromPath(config as any, options.jsonPath) : config;
202202

203203
if (value === undefined) {
204-
throw new SilentError('Value cannot be found.');
204+
this.logger.error('Value cannot be found.');
205+
206+
return 1;
205207
} else if (typeof value == 'object') {
206208
this.logger.info(JSON.stringify(value, null, 2));
207209
} else {
@@ -228,14 +230,17 @@ export default class ConfigCommand extends Command {
228230
const result = setValueFromPath(configValue, options.jsonPath, value);
229231

230232
if (result === undefined) {
231-
throw new SilentError('Value cannot be found.');
233+
this.logger.error('Value cannot be found.');
234+
235+
return 1;
232236
}
233237

234238
try {
235239
validateWorkspace(configValue);
236240
} catch (error) {
237-
this.logger.error(error.message);
238-
throw new SilentError();
241+
this.logger.fatal(error.message);
242+
243+
return 1;
239244
}
240245

241246
const output = JSON.stringify(configValue, null, 2);

packages/@angular/cli/models/command-runner.ts

Lines changed: 49 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import {
22
Option,
33
CommandContext,
4-
Command,
54
CommandConstructor,
65
CommandScope,
76
ArgumentStrategy
87
} from '../models/command';
9-
import { logging, normalize, tags } from '@angular-devkit/core';
8+
import { logging, tags } from '@angular-devkit/core';
109
import { camelize } from '@angular-devkit/core/src/utils/strings';
11-
import { findUp } from '../utilities/find-up';
1210
import { insideProject } from '../utilities/project';
1311

1412
import * as yargsParser from 'yargs-parser';
15-
import * as fs from 'fs';
16-
import { join } from 'path';
1713

18-
const SilentError = require('silent-error');
1914

2015
export interface CommandMap {
2116
[key: string]: CommandConstructor;
@@ -114,18 +109,57 @@ export async function runCommand(commandMap: CommandMap,
114109
}
115110

116111
if (options.help) {
117-
return await runHelp(command, options);
112+
return command.printHelp(options);
118113
} else {
119-
verifyCommandInScope(command, executionScope);
120-
verifyWorkspace(
121-
command,
122-
executionScope,
123-
context.project.root,
124-
command.allowMissingWorkspace ? logger : null,
125-
);
114+
if (command.scope !== undefined && command.scope !== CommandScope.everywhere) {
115+
if (command.scope !== executionScope) {
116+
let errorMessage;
117+
if (command.scope === CommandScope.inProject) {
118+
errorMessage = `This command can only be run inside of a CLI project.`;
119+
} else {
120+
errorMessage = `This command can not be run inside of a CLI project.`;
121+
}
122+
logger.fatal(errorMessage);
123+
124+
return 1;
125+
}
126+
127+
if (command.scope === CommandScope.inProject) {
128+
if (!context.project.configFile) {
129+
logger.fatal('Invalid project: missing workspace file.');
130+
131+
return 1;
132+
}
133+
134+
if (['.angular-cli.json', 'angular-cli.json'].includes(context.project.configFile)) {
135+
// --------------------------------------------------------------------------------
136+
// If changing this message, please update the same message in
137+
// `packages/@angular/cli/bin/ng-update-message.js`
138+
const message = tags.stripIndent`
139+
The Angular CLI configuration format has been changed, and your existing configuration
140+
can be updated automatically by running the following command:
141+
142+
ng update @angular/cli
143+
`;
144+
145+
logger.warn(message);
146+
147+
return 1;
148+
}
149+
}
150+
}
151+
126152
delete options.h;
127153
delete options.help;
128-
return await validateAndRunCommand(command, options);
154+
155+
const isValid = await command.validate(options);
156+
if (!isValid) {
157+
logger.fatal(`Validation error. Invalid command`);
158+
159+
return 1;
160+
}
161+
162+
return await command.run(options);
129163
}
130164
}
131165

@@ -258,86 +292,3 @@ function listAllCommandNames(map: CommandMap): string[] {
258292
}, [] as string[]),
259293
);
260294
}
261-
262-
263-
function verifyCommandInScope(command: Command, scope = CommandScope.everywhere): void {
264-
if (!command) {
265-
return;
266-
}
267-
if (command.scope !== CommandScope.everywhere) {
268-
if (command.scope !== scope) {
269-
let errorMessage: string;
270-
if (command.scope === CommandScope.inProject) {
271-
errorMessage = `This command can only be run inside of a CLI project.`;
272-
} else {
273-
errorMessage = `This command can not be run inside of a CLI project.`;
274-
}
275-
throw new SilentError(errorMessage);
276-
}
277-
}
278-
}
279-
280-
function verifyWorkspace(
281-
command: Command,
282-
executionScope: CommandScope,
283-
root: string,
284-
logger: logging.Logger | null = null,
285-
): void {
286-
if (command.scope === CommandScope.everywhere) {
287-
return;
288-
}
289-
if (executionScope === CommandScope.inProject) {
290-
if (fs.existsSync(join(root, 'angular.json'))) {
291-
return;
292-
}
293-
if (fs.existsSync(join(root, '.angular.json'))) {
294-
return;
295-
}
296-
297-
// Check if there's an old config file meaning that the project has not been updated
298-
const oldConfigFileNames = [
299-
normalize('.angular-cli.json'),
300-
normalize('angular-cli.json'),
301-
];
302-
const oldConfigFilePath = (root && findUp(oldConfigFileNames, root))
303-
|| findUp(oldConfigFileNames, process.cwd())
304-
|| findUp(oldConfigFileNames, __dirname);
305-
306-
// If an old configuration file is found, throw an exception.
307-
if (oldConfigFilePath) {
308-
// ------------------------------------------------------------------------------------------
309-
// If changing this message, please update the same message in
310-
// `packages/@angular/cli/bin/ng-update-message.js`
311-
const message = tags.stripIndent`
312-
The Angular CLI configuration format has been changed, and your existing configuration can
313-
be updated automatically by running the following command:
314-
315-
ng update @angular/cli
316-
`;
317-
318-
if (!logger) {
319-
throw new SilentError(message);
320-
} else {
321-
logger.warn(message);
322-
return;
323-
}
324-
}
325-
326-
// If no configuration file is found (old or new), throw an exception.
327-
throw new SilentError('Invalid project: missing workspace file.');
328-
}
329-
}
330-
331-
// Execute a command's `printHelp`.
332-
async function runHelp<T>(command: Command<T>, options: T): Promise<void> {
333-
return await command.printHelp(options);
334-
}
335-
336-
// Validate and run a command.
337-
async function validateAndRunCommand<T>(command: Command<T>, options: T): Promise<number | void> {
338-
const isValid = await command.validate(options);
339-
if (isValid !== undefined && !isValid) {
340-
throw new SilentError(`Validation error. Invalid command`);
341-
}
342-
return await command.run(options);
343-
}

packages/@angular/cli/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"resolve": "^1.1.7",
4040
"rxjs": "^6.0.0",
4141
"semver": "^5.1.0",
42-
"silent-error": "^1.0.0",
4342
"symbol-observable": "^1.2.0",
4443
"yargs-parser": "^10.0.0"
4544
},

packages/@angular/cli/tasks/npm-install.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { ModuleNotFoundException, resolve } from '@angular-devkit/core/node';
22
import { spawn } from 'child_process';
33
import { logging, terminal } from '@angular-devkit/core';
44

5-
const SilentError = require('silent-error');
6-
75

86
export type NpmInstall = (packageName: string,
97
logger: logging.Logger,
@@ -16,12 +14,6 @@ export default async function (packageName: string,
1614
packageManager: string,
1715
projectRoot: string,
1816
save = true) {
19-
if (packageManager === 'default') {
20-
packageManager = 'npm';
21-
}
22-
23-
logger.info(terminal.green(`Installing packages for tooling via ${packageManager}.`));
24-
2517
const installArgs: string[] = [];
2618
switch (packageManager) {
2719
case 'cnpm':
@@ -34,9 +26,13 @@ export default async function (packageName: string,
3426
break;
3527

3628
default:
37-
throw new SilentError(`Invalid package manager: ${JSON.stringify(packageManager)}.`);
29+
packageManager = 'npm';
30+
installArgs.push('install', '--quiet');
31+
break;
3832
}
3933

34+
logger.info(terminal.green(`Installing packages for tooling via ${packageManager}.`));
35+
4036
if (packageName) {
4137
try {
4238
// Verify if we need to install the package (it might already be there).

packages/@angular/cli/utilities/schematics.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import {
2020
validateOptionsWithSchema
2121
} from '@angular-devkit/schematics/tools';
2222

23-
const SilentError = require('silent-error');
23+
export class UnknownCollectionError extends Error {
24+
constructor(collectionName: string) {
25+
super(`Invalid collection (${collectionName}).`);
26+
}
27+
}
2428

2529
const engineHost = new NodeModulesEngineHost();
2630
const engine: Engine<FileSystemCollectionDesc, FileSystemSchematicDesc>
@@ -43,8 +47,9 @@ export function getCollection(collectionName: string): Collection<any, any> {
4347
const collection = engine.createCollection(collectionName);
4448

4549
if (collection === null) {
46-
throw new SilentError(`Invalid collection (${collectionName}).`);
50+
throw new UnknownCollectionError(collectionName);
4751
}
52+
4853
return collection;
4954
}
5055

0 commit comments

Comments
 (0)