Skip to content

Fix 11034 #11648

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 4 commits into from
Jul 26, 2018
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
53 changes: 46 additions & 7 deletions packages/angular/cli/models/architect-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

// tslint:disable:no-global-tslint-disable no-any
import {
Architect,
BuildEvent,
BuilderDescription,
TargetSpecifier,
} from '@angular-devkit/architect';
import { JsonObject, experimental, schema, strings } from '@angular-devkit/core';
import {
JsonObject,
UnknownException,
experimental,
schema,
strings,
tags,
} from '@angular-devkit/core';
import { NodeJsSyncHost, createConsoleLogger } from '@angular-devkit/core/node';
import { of } from 'rxjs';
import { from } from 'rxjs';
Expand Down Expand Up @@ -90,19 +95,53 @@ export abstract class ArchitectCommand extends Command<ArchitectCommandOptions>
const projectNames = this.getProjectNamesByTarget(this.target);
const { overrides } = this._makeTargetSpecifier(options);
if (projectNames.length > 1 && Object.keys(overrides || {}).length > 0) {
throw new Error('Architect commands with multiple targets cannot specify overrides.'
+ `'${this.target}' would be run on the following projects: ${projectNames.join()}`);
// Verify that all builders are the same, otherwise error out (since the meaning of an
// option could vary from builder to builder).

const builders: string[] = [];
for (const projectName of projectNames) {
const targetSpec: TargetSpecifier = this._makeTargetSpecifier(options);
const targetDesc = this._architect.getBuilderConfiguration({
project: projectName,
target: targetSpec.target,
});

if (builders.indexOf(targetDesc.builder) == -1) {
builders.push(targetDesc.builder);
}
}

if (builders.length > 1) {
throw new Error(tags.oneLine`
Architect commands with command line overrides cannot target different builders. The
'${this.target}' target would run on projects ${projectNames.join()} which have the
following builders: ${'\n ' + builders.join('\n ')}
`);
}
}
}

return true;
}

protected mapArchitectOptions(schema: any) {
protected mapArchitectOptions(schema: JsonObject) {
const properties = schema.properties;
if (typeof properties != 'object' || properties === null || Array.isArray(properties)) {
throw new UnknownException('Invalid schema.');
}
const keys = Object.keys(properties);
keys
.map(key => ({ ...properties[key], ...{ name: strings.dasherize(key) } }))
.map(key => {
const value = properties[key];
if (typeof value != 'object') {
throw new UnknownException('Invalid schema.');
}

return {
...value,
name: strings.dasherize(key),
} as any; // tslint:disable-line:no-any
})
.map(opt => {
let type;
const schematicType = opt.type;
Expand Down
24 changes: 18 additions & 6 deletions packages/angular_devkit/schematics/tasks/tslint-fix/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ function _loadConfiguration(
if (options.tslintConfig) {
return Configuration.parseConfigFile(options.tslintConfig, root);
} else if (options.tslintPath) {
const tslintPath = path.join(root, options.tslintPath);

return Configuration.findConfiguration(tslintPath, file && path.join(root, file)).results;
return Configuration.findConfiguration(path.join(root, options.tslintPath)).results;
} else if (file) {
return Configuration.findConfiguration(null, file).results;
} else {
throw new Error('Executor must specify a tslint configuration.');
}
Expand Down Expand Up @@ -103,13 +103,18 @@ export default function(): TaskExecutor<TslintFixTaskOptions> {
? options.includes
: (options.includes ? [options.includes] : [])
);
const files = (
Array.isArray(options.files)
? options.files
: (options.files ? [options.files] : [])
);

const Linter = tslint.Linter as LinterT;
const Configuration = tslint.Configuration as ConfigurationT;
let program: ts.Program | undefined = undefined;
let filesToLint: string[] = [];
let filesToLint: string[] = files;

if (options.tsConfigPath) {
if (options.tsConfigPath && files.length == 0) {
const tsConfigPath = path.join(process.cwd(), options.tsConfigPath);

if (!fs.existsSync(tsConfigPath)) {
Expand Down Expand Up @@ -148,9 +153,16 @@ export default function(): TaskExecutor<TslintFixTaskOptions> {
};

const linter = new Linter(lintOptions, program);
const config = _loadConfiguration(Configuration, options, root);
// If directory doesn't change, we
let lastDirectory: string | null = null;
let config;

for (const file of filesToLint) {
const dir = path.dirname(file);
if (lastDirectory !== dir) {
lastDirectory = dir;
config = _loadConfiguration(Configuration, options, root, file);
}
const content = _getFileContent(file, options, program);

if (!content) {
Expand Down
14 changes: 4 additions & 10 deletions packages/angular_devkit/schematics/tasks/tslint-fix/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@ export interface TslintFixTaskOptionsBase {
ignoreErrors?: boolean;

includes?: string | string[];
}

export interface TslintFixTaskOptionsPath extends TslintFixTaskOptionsBase {
tslintPath: string;
tslintConfig?: never;
}
files?: string | string[];

export interface TslintFixTaskOptionsConfig extends TslintFixTaskOptionsBase {
tslintPath?: never;
tslintConfig: JsonObject;
tslintPath?: string;
tslintConfig?: JsonObject;
}

export type TslintFixTaskOptions = TslintFixTaskOptionsPath | TslintFixTaskOptionsConfig;
export type TslintFixTaskOptions = TslintFixTaskOptionsBase;
27 changes: 21 additions & 6 deletions packages/angular_devkit/schematics/tasks/tslint-fix/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,34 @@ import { TslintFixName, TslintFixTaskOptions, TslintFixTaskOptionsBase } from '.


export class TslintFixTask implements TaskConfigurationGenerator<TslintFixTaskOptions> {
protected _configOrPath: null | string | JsonObject;
protected _options: TslintFixTaskOptionsBase;

constructor(config: JsonObject, options: TslintFixTaskOptionsBase);
constructor(options: TslintFixTaskOptionsBase);
constructor(path: string, options: TslintFixTaskOptionsBase);
constructor(
protected _configOrPath: string | JsonObject,
protected _options: TslintFixTaskOptionsBase,
) {}
configOrPath: string | JsonObject | TslintFixTaskOptionsBase,
options?: TslintFixTaskOptionsBase,
) {
if (options) {
this._configOrPath = configOrPath as string | JsonObject;
this._options = options;
} else {
this._options = configOrPath as TslintFixTaskOptionsBase;
this._configOrPath = null;
}
}

toConfiguration(): TaskConfiguration<TslintFixTaskOptions> {
const path = typeof this._configOrPath == 'string' ? { tslintPath: this._configOrPath } : {};
const config = typeof this._configOrPath == 'object' && this._configOrPath !== null
? { tslintConfig: this._configOrPath }
: {};
const options = {
...this._options,
...((typeof this._configOrPath == 'string'
? { tslintPath: this._configOrPath }
: { tslintConfig: this._configOrPath })),
...path,
...config,
};

return { name: TslintFixName, options };
Expand Down
2 changes: 2 additions & 0 deletions packages/schematics/angular/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import { InsertChange } from '../utility/change';
import { getWorkspace } from '../utility/config';
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
import { applyLintFix } from '../utility/lint-fix';
import { parseName } from '../utility/parse-name';
import { buildDefaultPath } from '../utility/project';
import { validateHtmlSelector, validateName } from '../utility/validation';
Expand Down Expand Up @@ -164,6 +165,7 @@ export default function(options: ComponentOptions): Rule {
addDeclarationToNgModule(options),
mergeWith(templateSource),
])),
options.lintFix ? applyLintFix(options.path) : noop(),
]);
};
}
4 changes: 4 additions & 0 deletions packages/schematics/angular/component/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ export interface Schema {
* Specifies if the component is an entry component of declaring module.
*/
entryComponent?: boolean;
/**
* Specifies whether to apply lint fixes after generating the component.
*/
lintFix?: boolean;
}
5 changes: 5 additions & 0 deletions packages/schematics/angular/component/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
"type": "boolean",
"default": false,
"description": "Specifies if the component is an entry component of declaring module."
},
"lintFix": {
"type": "boolean",
"default": false,
"description": "Specifies whether to apply lint fixes after generating the component."
}
},
"required": [],
Expand Down
2 changes: 2 additions & 0 deletions packages/schematics/angular/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { addDeclarationToModule, addExportToModule } from '../utility/ast-utils'
import { InsertChange } from '../utility/change';
import { getWorkspace } from '../utility/config';
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
import { applyLintFix } from '../utility/lint-fix';
import { parseName } from '../utility/parse-name';
import { buildDefaultPath } from '../utility/project';
import { validateHtmlSelector } from '../utility/validation';
Expand Down Expand Up @@ -137,6 +138,7 @@ export default function (options: DirectiveOptions): Rule {
addDeclarationToNgModule(options),
mergeWith(templateSource),
])),
options.lintFix ? applyLintFix(options.path) : noop(),
]);
};
}
4 changes: 4 additions & 0 deletions packages/schematics/angular/directive/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ export interface Schema {
* Specifies if declaring module exports the directive.
*/
export?: boolean;
/**
* Specifies whether to apply lint fixes after generating the component.
*/
lintFix?: boolean;
}
5 changes: 5 additions & 0 deletions packages/schematics/angular/directive/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
"type": "boolean",
"default": false,
"description": "Specifies if declaring module exports the directive."
},
"lintFix": {
"type": "boolean",
"default": false,
"description": "Specifies whether to apply lint fixes after generating the directive."
}
},
"required": [],
Expand Down
3 changes: 3 additions & 0 deletions packages/schematics/angular/enum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import {
chain,
mergeWith,
move,
noop,
template,
url,
} from '@angular-devkit/schematics';
import { getWorkspace } from '../utility/config';
import { applyLintFix } from '../utility/lint-fix';
import { parseName } from '../utility/parse-name';
import { buildDefaultPath } from '../utility/project';
import { Schema as EnumOptions } from './schema';
Expand Down Expand Up @@ -53,6 +55,7 @@ export default function (options: EnumOptions): Rule {
branchAndMerge(chain([
mergeWith(templateSource),
])),
options.lintFix ? applyLintFix(options.path) : noop(),
]);
};
}
4 changes: 4 additions & 0 deletions packages/schematics/angular/enum/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ export interface Schema {
* The name of the project.
*/
project?: string;
/**
* Specifies whether to apply lint fixes after generating the component.
*/
lintFix?: boolean;
}
5 changes: 5 additions & 0 deletions packages/schematics/angular/enum/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"$default": {
"$source": "projectName"
}
},
"lintFix": {
"type": "boolean",
"default": false,
"description": "Specifies whether to apply lint fixes after generating the enum."
}
},
"required": [],
Expand Down
2 changes: 2 additions & 0 deletions packages/schematics/angular/guard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
url,
} from '@angular-devkit/schematics';
import { getWorkspace } from '../utility/config';
import { applyLintFix } from '../utility/lint-fix';
import { parseName } from '../utility/parse-name';
import { buildDefaultPath } from '../utility/project';
import { Schema as GuardOptions } from './schema';
Expand Down Expand Up @@ -55,6 +56,7 @@ export default function (options: GuardOptions): Rule {
branchAndMerge(chain([
mergeWith(templateSource),
])),
options.lintFix ? applyLintFix(options.path) : noop(),
]);
};
}
4 changes: 4 additions & 0 deletions packages/schematics/angular/guard/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export interface Schema {
* The name of the project.
*/
project?: string;
/**
* Specifies whether to apply lint fixes after generating the component.
*/
lintFix?: boolean;
}
5 changes: 5 additions & 0 deletions packages/schematics/angular/guard/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"$default": {
"$source": "projectName"
}
},
"lintFix": {
"type": "boolean",
"default": false,
"description": "Specifies whether to apply lint fixes after generating the guard."
}
},
"required": [],
Expand Down
3 changes: 3 additions & 0 deletions packages/schematics/angular/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import {
chain,
mergeWith,
move,
noop,
template,
url,
} from '@angular-devkit/schematics';
import { getWorkspace } from '../utility/config';
import { applyLintFix } from '../utility/lint-fix';
import { parseName } from '../utility/parse-name';
import { buildDefaultPath } from '../utility/project';
import { Schema as InterfaceOptions } from './schema';
Expand Down Expand Up @@ -55,6 +57,7 @@ export default function (options: InterfaceOptions): Rule {
branchAndMerge(chain([
mergeWith(templateSource),
])),
options.lintFix ? applyLintFix(options.path) : noop(),
]);
};
}
4 changes: 4 additions & 0 deletions packages/schematics/angular/interface/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export interface Schema {
* Specifies the type of interface.
*/
type?: string;
/**
* Specifies whether to apply lint fixes after generating the component.
*/
lintFix?: boolean;
}
5 changes: 5 additions & 0 deletions packages/schematics/angular/interface/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"$source": "argv",
"index": 1
}
},
"lintFix": {
"type": "boolean",
"default": false,
"description": "Specifies whether to apply lint fixes after generating the directive."
}
},
"required": [],
Expand Down
Loading