-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
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)) | ||
}] | ||
}); | ||
} | ||
}); | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.