Skip to content

Add option to adjust generated files via lint fix #6213

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 3 commits into from May 9, 2017
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
1 change: 1 addition & 0 deletions packages/@angular/cli/blueprints/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Blueprint = require('../../ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;

export default Blueprint.extend({
name: 'class',
description: '',
aliases: ['cl'],

Expand Down
2 changes: 2 additions & 0 deletions packages/@angular/cli/blueprints/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function correctCase(options: any) {
}

export default Blueprint.extend({
name: 'component',
description: '',
aliases: ['c'],

Expand Down Expand Up @@ -271,6 +272,7 @@ export default Blueprint.extend({
this._writeStatusToUI(chalk.yellow,
moduleStatus,
path.relative(this.project.root, this.pathToModule));
this.addModifiedFile(this.pathToModule);
}));
}

Expand Down
2 changes: 2 additions & 0 deletions packages/@angular/cli/blueprints/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Blueprint = require('../../ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;

export default Blueprint.extend({
name: 'directive',
description: '',
aliases: ['d'],

Expand Down Expand Up @@ -165,6 +166,7 @@ export default Blueprint.extend({
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
this.addModifiedFile(this.pathToModule);
}

return Promise.all(returns);
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/blueprints/enum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const stringUtils = require('ember-cli-string-utils');
const Blueprint = require('../../ember-cli/lib/models/blueprint');

export default Blueprint.extend({
name: 'enum',
description: '',
aliases: ['e'],

Expand Down
2 changes: 2 additions & 0 deletions packages/@angular/cli/blueprints/guard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const astUtils = require('../../utilities/ast-utils');
const getFiles = Blueprint.prototype.files;

export default Blueprint.extend({
name: 'guard',
description: '',
aliases: ['g'],

Expand Down Expand Up @@ -110,6 +111,7 @@ export default Blueprint.extend({
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
this.addModifiedFile(this.pathToModule);
}

return Promise.all(returns);
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/blueprints/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const stringUtils = require('ember-cli-string-utils');
const Blueprint = require('../../ember-cli/lib/models/blueprint');

export default Blueprint.extend({
name: 'interface',
description: '',
aliases: ['i'],

Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/blueprints/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Blueprint = require('../../ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;

export default Blueprint.extend({
name: 'module',
description: '',
aliases: ['m'],

Expand Down
2 changes: 2 additions & 0 deletions packages/@angular/cli/blueprints/pipe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Blueprint = require('../../ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;

export default Blueprint.extend({
name: 'pipe',
description: '',
aliases: ['p'],

Expand Down Expand Up @@ -143,6 +144,7 @@ export default Blueprint.extend({
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
this.addModifiedFile(this.pathToModule);
}

return Promise.all(returns);
Expand Down
2 changes: 2 additions & 0 deletions packages/@angular/cli/blueprints/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const astUtils = require('../../utilities/ast-utils');
const getFiles = Blueprint.prototype.files;

export default Blueprint.extend({
name: 'service',
description: '',
aliases: ['s'],

Expand Down Expand Up @@ -122,6 +123,7 @@ export default Blueprint.extend({
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
this.addModifiedFile(this.pathToModule);
}

return Promise.all(returns);
Expand Down
160 changes: 117 additions & 43 deletions packages/@angular/cli/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,143 @@
import * as chalk from 'chalk';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as path from 'path';
import { oneLine } from 'common-tags';
import { CliConfig } from '../models/config';

const chalk = require('chalk');
const EmberGenerateCommand = require('../ember-cli/lib/commands/generate');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean ../ember-cli/lib/commands/generate' is no longer necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.
A followup PR for general ember-cli cleanup is planned.

const Command = require('../ember-cli/lib/models/command');
const Blueprint = require('../ember-cli/lib/models/blueprint');
const parseOptions = require('../ember-cli/lib/utilities/parse-options');
const SilentError = require('silent-error');

const blueprintList = fs.readdirSync(path.join(__dirname, '..', 'blueprints'));
const blueprints = blueprintList
.filter(bp => bp.indexOf('-test') === -1)
.filter(bp => bp !== 'ng')
.map(bp => Blueprint.load(path.join(__dirname, '..', 'blueprints', bp)));
function loadBlueprints(): Array<any> {
const blueprintList = fs.readdirSync(path.join(__dirname, '..', 'blueprints'));
const blueprints = blueprintList
.filter(bp => bp.indexOf('-test') === -1)
.filter(bp => bp !== 'ng')
.map(bp => Blueprint.load(path.join(__dirname, '..', 'blueprints', bp)));

return blueprints;
}

const GenerateCommand = EmberGenerateCommand.extend({
export default Command.extend({
name: 'generate',
description: 'Generates and/or modifies files based on a blueprint.',
aliases: ['g'],

availableOptions: [
{
name: 'dry-run',
type: Boolean,
default: false,
aliases: ['d'],
description: 'Run through without making any changes.'
},
{
name: 'lint-fix',
type: Boolean,
aliases: ['lf'],
description: 'Use lint to fix files after generation.'
},
{
name: 'verbose',
type: Boolean,
default: false,
aliases: ['v'],
description: 'Adds more details to output logging.'
}
],

blueprints: blueprints,
anonymousOptions: [
'<blueprint>'
],

beforeRun: function (rawArgs: string[]) {
if (!rawArgs.length) {
return;
}

// map the blueprint name to allow for aliases
rawArgs[0] = mapBlueprintName(rawArgs[0]);
const isHelp = ['--help', '-h'].includes(rawArgs[0]);
if (isHelp) {
return;
}

this.blueprints = loadBlueprints();

const isHelp: boolean = ['--help', '-h'].indexOf(rawArgs[0]) > -1;
if (!isHelp && !fs.existsSync(path.join(__dirname, '..', 'blueprints', rawArgs[0]))) {
const name = rawArgs[0];
const blueprint = this.blueprints.find((bp: any) => bp.name === name
|| (bp.aliases && bp.aliases.includes(name)));

if (!blueprint) {
SilentError.debugOrThrow('@angular/cli/commands/generate',
`Invalid blueprint: ${rawArgs[0]}`);
`Invalid blueprint: ${name}`);
}

if (!isHelp && !rawArgs[1]) {
if (!rawArgs[1]) {
SilentError.debugOrThrow('@angular/cli/commands/generate',
`The \`ng generate ${rawArgs[0]}\` command requires a name to be specified.`);
`The \`ng generate ${name}\` command requires a name to be specified.`);
}

// Override default help to hide ember blueprints
EmberGenerateCommand.prototype.printDetailedHelp = function () {
this.ui.writeLine(chalk.cyan(' Available blueprints'));
this.ui.writeLine(blueprints.map(bp => bp.printBasicHelp(false)).join(os.EOL));
rawArgs[0] = blueprint.name;
this.registerOptions(blueprint);
},

printDetailedHelp: function () {
if (!this.blueprints) {
this.blueprints = loadBlueprints();
}
this.ui.writeLine(chalk.cyan(' Available blueprints'));
this.ui.writeLine(this.blueprints.map((bp: any) => bp.printBasicHelp(false)).join(os.EOL));
},

run: function (commandOptions: any, rawArgs: string[]) {
const name = rawArgs[0];
if (!name) {
return Promise.reject(new SilentError(oneLine`
The "ng generate" command requires a
blueprint name to be specified.
For more details, use "ng help".
`));
}

const blueprint = this.blueprints.find((bp: any) => bp.name === name
|| (bp.aliases && bp.aliases.includes(name)));

const blueprintOptions = {
target: this.project.root,
entity: {
name: rawArgs[1],
options: parseOptions(rawArgs.slice(2))
},
ui: this.ui,
project: this.project,
settings: this.settings,
testing: this.testing,
args: rawArgs,
...commandOptions
};

return EmberGenerateCommand.prototype.beforeRun.apply(this, arguments);
}
});
return blueprint.install(blueprintOptions)
.then(() => {
const lintFix = commandOptions.lintFix !== undefined ?
commandOptions.lintFix : CliConfig.getValue('defaults.lintFix');

function mapBlueprintName(name: string): string {
let mappedName: string = aliasMap[name];
return mappedName ? mappedName : name;
}
if (lintFix && blueprint.modifiedFiles) {
const LintTask = require('../tasks/lint').default;
const lintTask = new LintTask({
ui: this.ui,
project: this.project
});

const aliasMap: { [alias: string]: string } = {
'cl': 'class',
'c': 'component',
'd': 'directive',
'e': 'enum',
'g': 'guard',
'i': 'interface',
'm': 'module',
'p': 'pipe',
'r': 'route',
's': 'service'
};

export default GenerateCommand;
GenerateCommand.overrideCore = true;
return lintTask.run({
fix: true,
force: true,
silent: true,
configs: [{
files: blueprint.modifiedFiles.filter((file: string) => /.ts$/.test(file))
}]
});
}
});
}
});
8 changes: 6 additions & 2 deletions packages/@angular/cli/commands/lint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {oneLine} from 'common-tags';
import { oneLine } from 'common-tags';
import { CliConfig } from '../models/config';

const Command = require('../ember-cli/lib/models/command');

Expand Down Expand Up @@ -52,6 +53,9 @@ export default Command.extend({
project: this.project
});

return lintTask.run(commandOptions);
return lintTask.run({
...commandOptions,
configs: CliConfig.fromProject().config.lint
});
}
});
8 changes: 8 additions & 0 deletions packages/@angular/cli/ember-cli/lib/models/blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ Blueprint.prototype._writeStatusToUI = function(chalkColor, keyword, message) {
}
};

Blueprint.prototype.addModifiedFile = function(file) {
if (!this.modifiedFiles) {
this.modifiedFiles = [];
}
this.modifiedFiles.push(file);
}

/**
@private
@method _writeFile
Expand All @@ -372,6 +379,7 @@ Blueprint.prototype._writeStatusToUI = function(chalkColor, keyword, message) {
*/
Blueprint.prototype._writeFile = function(info) {
if (!this.dryRun) {
this.addModifiedFile(info.outputPath);
return writeFile(info.outputPath, info.render());
}
};
Expand Down
5 changes: 5 additions & 0 deletions packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@
"description": "How often to check for file updates.",
"type": "number"
},
"lintFix": {
"description": "Use lint to fix files after generation",
"type": "boolean",
"default": false
},
"class": {
"description": "Options for generating a class.",
"type": "object",
Expand Down
Loading