diff --git a/README.md b/README.md index 1b15d80db7ca..f2f100dec688 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ ng serve ``` Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. -You can configure the default HTTP port and the one used by the LiveReload server with two command-line options : +You can configure the default HTTP port and the one used by the LiveReload server with two command-line options : ```bash ng serve --port 4201 --live-reload-port 49153 @@ -176,6 +176,14 @@ ng github-pages:deploy Checkout [angular-cli-github-pages addon](https://github.com/IgorMinar/angular-cli-github-pages) docs for more info. +### Linting code + +You can lint your app code by running `ng lint`. +This will use the `lint` npm script that in generated projects uses `tslint`. + +You can modify the `lint` script in `package.json` to run whatever linting tool +you prefer and `ng lint` will still run it. + ## Known issues diff --git a/addon/ng2/blueprints/ng2/files/package.json b/addon/ng2/blueprints/ng2/files/package.json index 7e3fefd0cf74..10e00ff0d99a 100644 --- a/addon/ng2/blueprints/ng2/files/package.json +++ b/addon/ng2/blueprints/ng2/files/package.json @@ -10,7 +10,8 @@ }, "scripts": { "start": "ng server", - "postinstall": "typings install --ambient" + "postinstall": "typings install --ambient", + "lint": "tslint src/**/*.ts" }, "private": true, "dependencies": { @@ -31,6 +32,7 @@ "karma-chrome-launcher": "^0.2.1", "karma-jasmine": "^0.3.6", "protractor": "^3.0.0", + "tslint": "^3.3.0", "typescript": "^1.7.3", "typings": "^0.6.6" } diff --git a/addon/ng2/blueprints/ng2/files/tslint.json b/addon/ng2/blueprints/ng2/files/tslint.json new file mode 100644 index 000000000000..c9cd1dd37911 --- /dev/null +++ b/addon/ng2/blueprints/ng2/files/tslint.json @@ -0,0 +1,63 @@ +{ + "rules": { + "max-line-length": [true, 100], + "no-inferrable-types": true, + "class-name": true, + "comment-format": [ + true, + "check-space" + ], + "indent": [ + true, + "spaces" + ], + "eofline": true, + "no-duplicate-variable": true, + "no-eval": true, + "no-arg": true, + "no-internal-module": true, + "no-trailing-whitespace": true, + "no-bitwise": true, + "no-shadowed-variable": true, + "no-unused-expression": true, + "no-unused-variable": true, + "one-line": [ + true, + "check-catch", + "check-else", + "check-open-brace", + "check-whitespace" + ], + "quotemark": [ + true, + "single", + "avoid-escape" + ], + "semicolon": true, + "typedef-whitespace": [ + true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + } + ], + "curly": true, + "variable-name": [ + true, + "ban-keywords", + "check-format", + "allow-trailing-underscore" + ], + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-separator", + "check-type" + ] + } +} diff --git a/addon/ng2/commands/lint.js b/addon/ng2/commands/lint.js new file mode 100644 index 000000000000..459d3ec39fc9 --- /dev/null +++ b/addon/ng2/commands/lint.js @@ -0,0 +1,20 @@ +/* jshint node: true */ +'use strict'; + +var Command = require('ember-cli/lib/models/command'); +var LintTask = require('../tasks/lint'); + +module.exports = Command.extend({ + name: 'lint', + description: 'Lints code in existing project', + works: 'insideProject', + run: function() { + var lintTask = new LintTask({ + ui: this.ui, + analytics: this.analytics, + project: this.project + }); + + return lintTask.run(); + } +}); diff --git a/addon/ng2/index.js b/addon/ng2/index.js index d05efd8e9458..23ff7b422edd 100644 --- a/addon/ng2/index.js +++ b/addon/ng2/index.js @@ -9,7 +9,8 @@ module.exports = { 'init' : require('./commands/init'), 'install' : require('./commands/install'), 'uninstall' : require('./commands/uninstall'), - 'test' : require('./commands/test') + 'test' : require('./commands/test'), + 'lint' : require('./commands/lint') }; } }; diff --git a/addon/ng2/tasks/lint.js b/addon/ng2/tasks/lint.js new file mode 100644 index 000000000000..bd24dcb438ea --- /dev/null +++ b/addon/ng2/tasks/lint.js @@ -0,0 +1,23 @@ +/* jshint node: true */ +'use strict'; + +var Promise = require('ember-cli/lib/ext/promise'); +var Task = require('ember-cli/lib/models/task'); +var exec = Promise.denodeify(require('child_process').exec); + +module.exports = Task.extend({ + run: function() { + var chalk = require('chalk'); + var ui = this.ui; + + return exec('npm run lint') + .then(function() { + ui.writeLine(chalk.green('Successfully linted files.')); + }) + .catch(function( /*error*/ ) { + ui.writeLine(chalk.red( + 'Couldn\'t do \'npm run lint\'. Please check this script exists in your package.json.' + )); + }); + } +});