Skip to content

Commit 85eef76

Browse files
committed
refactor: move all CLI-specific exceptions to different options
We made too many shortcuts for passing data in and custom schematics could not work properly. This is temporary as we will likely move some more logic into schematics tooling to be able to pass only the raw args and the CLI config, but for now this is enough to unblock AngularMix.
1 parent eed13cf commit 85eef76

10 files changed

Lines changed: 855 additions & 74 deletions

File tree

package-lock.json

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

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
},
4242
"homepage": "https://github.com/angular/angular-cli",
4343
"dependencies": {
44-
"@angular-devkit/build-optimizer": "~0.0.23",
45-
"@angular-devkit/schematics": "~0.0.25",
46-
"@schematics/angular": "~0.0.38",
44+
"@angular-devkit/build-optimizer": "~0.0.26",
45+
"@angular-devkit/core": "~0.0.18",
46+
"@angular-devkit/schematics": "~0.0.31",
47+
"@schematics/angular": "~0.0.42",
4748
"autoprefixer": "^6.5.3",
4849
"chalk": "^2.0.1",
4950
"circular-dependency-plugin": "^3.0.0",

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

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { cyan, yellow } from 'chalk';
2-
const stringUtils = require('ember-cli-string-utils');
32
import { oneLine } from 'common-tags';
43
import { CliConfig } from '../models/config';
54

@@ -11,14 +10,11 @@ import {
1110
} from '../utilities/schematics';
1211
import { DynamicPathOptions, dynamicPathParser } from '../utilities/dynamic-path-parser';
1312
import { getAppFromConfig } from '../utilities/app-utils';
14-
import * as path from 'path';
1513
import { SchematicAvailableOptions } from '../tasks/schematic-get-options';
1614

1715
const Command = require('../ember-cli/lib/models/command');
1816
const SilentError = require('silent-error');
1917

20-
const separatorRegEx = /[\/\\]/g;
21-
2218

2319
export default Command.extend({
2420
name: 'generate',
@@ -77,16 +73,15 @@ export default Command.extend({
7773

7874
beforeRun: function(rawArgs: string[]) {
7975

80-
const isHelp = ['--help', '-h'].includes(rawArgs[0]);
76+
const isHelp = ['--help', '-h'].indexOf(rawArgs[0]) != -1;
8177
if (isHelp) {
8278
return;
8379
}
8480

8581
const schematicName = rawArgs[0];
8682
if (!schematicName) {
8783
return Promise.reject(new SilentError(oneLine`
88-
The "ng generate" command requires a
89-
schematic name to be specified.
84+
The "ng generate" command requires a schematic name to be specified.
9085
For more details, use "ng help".
9186
`));
9287
}
@@ -127,7 +122,6 @@ export default Command.extend({
127122
}
128123

129124
const entityName = rawArgs[1];
130-
commandOptions.name = stringUtils.dasherize(entityName.split(separatorRegEx).pop());
131125

132126
const appConfig = getAppFromConfig(commandOptions.app);
133127
const dynamicPathOptions: DynamicPathOptions = {
@@ -137,34 +131,16 @@ export default Command.extend({
137131
dryRun: commandOptions.dryRun
138132
};
139133
const parsedPath = dynamicPathParser(dynamicPathOptions);
140-
commandOptions.sourceDir = appConfig.root;
141-
const root = appConfig.root + path.sep;
142-
commandOptions.appRoot = parsedPath.appRoot === appConfig.root ? '' :
143-
parsedPath.appRoot.startsWith(root)
144-
? parsedPath.appRoot.substr(root.length)
145-
: parsedPath.appRoot;
146-
147-
commandOptions.path = parsedPath.dir.replace(separatorRegEx, '/');
148-
commandOptions.path = parsedPath.dir === appConfig.root ? '' :
149-
parsedPath.dir.startsWith(root)
150-
? commandOptions.path.substr(root.length)
151-
: commandOptions.path;
134+
135+
// Do not pass the schematics name itself, that's not useful.
136+
commandOptions._ = rawArgs.slice(1);
137+
commandOptions._angularCliConfig = (CliConfig.fromProject() || CliConfig.fromGlobal()).config;
138+
commandOptions._angularCliAppConfig = appConfig;
139+
commandOptions._angularCliParsedPath = parsedPath;
152140

153141
const cwd = this.project.root;
154142
const schematicName = rawArgs[0];
155143

156-
if (['component', 'c', 'directive', 'd'].indexOf(schematicName) !== -1) {
157-
if (commandOptions.prefix === undefined) {
158-
commandOptions.prefix = appConfig.prefix;
159-
}
160-
161-
if (schematicName === 'component' || schematicName === 'c') {
162-
if (commandOptions.styleext === undefined) {
163-
commandOptions.styleext = CliConfig.getValue('defaults.styleExt');
164-
}
165-
}
166-
}
167-
168144
const SchematicRunTask = require('../tasks/schematic-run').default;
169145
const schematicRunTask = new SchematicRunTask({
170146
ui: this.ui,
@@ -173,10 +149,6 @@ export default Command.extend({
173149
const collectionName = commandOptions.collection ||
174150
CliConfig.getValue('defaults.schematics.collection');
175151

176-
if (collectionName === '@schematics/angular' && schematicName === 'interface' && rawArgs[2]) {
177-
commandOptions.type = rawArgs[2];
178-
}
179-
180152
return schematicRunTask.run({
181153
taskOptions: commandOptions,
182154
workingDir: cwd,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ const NewCommand = Command.extend({
111111
},
112112

113113
run: function (commandOptions: any, rawArgs: string[]) {
114+
commandOptions._ = [...rawArgs];
115+
commandOptions._angularCliConfig = (CliConfig.fromProject() || CliConfig.fromGlobal()).config;
116+
114117
const packageName = rawArgs.shift();
115118

116119
if (!packageName) {
@@ -121,7 +124,7 @@ const NewCommand = Command.extend({
121124
}
122125

123126
validateProjectName(packageName);
124-
commandOptions.name = packageName;
127+
125128
if (commandOptions.dryRun) {
126129
commandOptions.skipGit = true;
127130
}

packages/@angular/cli/lib/cli/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Prevent the dependency validation from tripping because we don't import these. We need
22
// it as a peer dependency of @angular/core.
33
// require('zone.js')
4+
// require('@angular-devkit/core')
45

56
import * as path from 'path';
67

packages/@angular/cli/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
},
2828
"homepage": "https://github.com/angular/angular-cli",
2929
"dependencies": {
30-
"@angular-devkit/build-optimizer": "~0.0.18",
31-
"@angular-devkit/schematics": "~0.0.25",
30+
"@angular-devkit/build-optimizer": "~0.0.26",
31+
"@angular-devkit/core": "~0.0.18",
32+
"@angular-devkit/schematics": "~0.0.31",
3233
"@ngtools/json-schema": "1.1.0",
3334
"@ngtools/webpack": "1.8.0-beta.4",
34-
"@schematics/angular": "~0.0.38",
35+
"@schematics/angular": "~0.0.42",
3536
"autoprefixer": "^6.5.3",
3637
"chalk": "^2.0.1",
3738
"circular-dependency-plugin": "^3.0.0",

packages/@angular/cli/tasks/init.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as chalk from 'chalk';
22
import LinkCli from '../tasks/link-cli';
33
import NpmInstall from '../tasks/npm-install';
4-
import { validateProjectName } from '../utilities/validate-project-name';
54
import {checkYarnOrCNPM} from '../utilities/check-package-manager';
65
import {CliConfig} from '../models/config';
76

87
const Task = require('../ember-cli/lib/models/task');
9-
const SilentError = require('silent-error');
108
const GitInit = require('../tasks/git-init');
119
const packageJson = require('../package.json');
1210

@@ -48,23 +46,10 @@ export default Task.extend({
4846
});
4947
}
5048

51-
const project = this.project;
52-
const packageName = commandOptions.name !== '.' && commandOptions.name || project.name();
53-
5449
if (commandOptions.style === undefined) {
5550
commandOptions.style = CliConfig.fromGlobal().get('defaults.styleExt');
5651
}
5752

58-
if (!packageName) {
59-
const message = 'The `ng ' + this.name + '` command requires a ' +
60-
'package.json in current folder with name attribute or a specified name via arguments. ' +
61-
'For more details, use `ng help`.';
62-
63-
return Promise.reject(new SilentError(message));
64-
}
65-
66-
validateProjectName(packageName);
67-
6853
const SchematicRunTask = require('../tasks/schematic-run').default;
6954
const schematicRunTask = new SchematicRunTask({
7055
ui: this.ui,
@@ -106,7 +91,7 @@ export default Task.extend({
10691
})
10792
.then(() => {
10893
if (!commandOptions.dryRun) {
109-
this.ui.writeLine(chalk.green(`Project '${packageName}' successfully created.`));
94+
this.ui.writeLine(chalk.green(`Project '${commandOptions.name}' successfully created.`));
11095
}
11196
});
11297
}

packages/@angular/cli/tasks/schematic-get-options.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export default Task.extend({
3737
case 'boolean':
3838
type = Boolean;
3939
break;
40+
case undefined:
41+
return null;
4042
}
4143
let aliases: string[] = [];
4244
if (opt.alias) {
@@ -52,7 +54,7 @@ export default Task.extend({
5254
type,
5355
default: undefined // do not carry over schematics defaults
5456
};
55-
});
57+
}).filter(x => x);
5658

5759
return Promise.resolve(availableOptions);
5860
}

packages/@ngtools/json-schema/src/schema-tree.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ export abstract class NonLeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
168168
case 'enum': Klass = EnumSchemaTreeNode; break;
169169
case 'oneOf': Klass = OneOfSchemaTreeNode; break;
170170

171+
case undefined: Klass = AnySchemaTreeNode; break;
172+
171173
default:
172174
throw new InvalidSchema('Type ' + type + ' not understood by SchemaClassFactory.');
173175
}
@@ -452,6 +454,24 @@ export abstract class LeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
452454
}
453455

454456

457+
class AnySchemaTreeNode extends LeafSchemaTreeNode<any> {
458+
serialize(serializer: Serializer) {
459+
switch (typeof this.value) {
460+
case 'string': serializer.outputString(this); break;
461+
case 'number': serializer.outputNumber(this); break;
462+
case 'boolean': serializer.outputBoolean(this); break;
463+
464+
default: serializer.outputValue(this);
465+
}
466+
}
467+
468+
isCompatible(_: any) { return true; }
469+
convert(v: any) { return v; }
470+
get type() { return 'any'; }
471+
get tsType(): null { return null; }
472+
}
473+
474+
455475
/** Basic primitives for JSON Schema. */
456476
class StringSchemaTreeNode extends LeafSchemaTreeNode<string> {
457477
serialize(serializer: Serializer) { serializer.outputString(this); }

tests/collections/@custom/application/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ const s = require('@angular-devkit/schematics');
22

33
exports.default = function(options) {
44
return s.chain([s.mergeWith(s.apply(
5-
s.url('./files'), [s.template({}), s.move(options.name)]))]);
5+
s.url('./files'), [s.template({}), s.move(options._[0])]))]);
66
};

0 commit comments

Comments
 (0)