From 1f89a2be0d9547e37e4a972ab6f0cb9616f57619 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Fri, 12 Feb 2016 08:57:45 +0000 Subject: [PATCH] feat(format): add code formatting Close #143 --- README.md | 18 ++++++++++----- addon/ng2/blueprints/ng2/files/.clang-format | 3 +++ addon/ng2/blueprints/ng2/files/package.json | 4 +++- addon/ng2/commands/format.js | 20 +++++++++++++++++ addon/ng2/index.js | 3 ++- addon/ng2/tasks/format.js | 23 ++++++++++++++++++++ 6 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 addon/ng2/blueprints/ng2/files/.clang-format create mode 100644 addon/ng2/commands/format.js create mode 100644 addon/ng2/tasks/format.js diff --git a/README.md b/README.md index f2f100dec688..ccf857b94f3b 100644 --- a/README.md +++ b/README.md @@ -176,13 +176,21 @@ ng github-pages:deploy Checkout [angular-cli-github-pages addon](https://github.com/IgorMinar/angular-cli-github-pages) docs for more info. -### Linting code +### Linting and formatting 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 lint or format your app code by running `ng lint` or `ng format` respectively. +This will use the `lint`/`format` npm script that in generated projects uses `tslint`/`clang-format`. -You can modify the `lint` script in `package.json` to run whatever linting tool -you prefer and `ng lint` will still run it. +You can modify the these scripts in `package.json` to run whatever tool you prefer. + + +### Formatting code + +You can format your app code by running `ng format`. +This will use the `format` npm script that in generated projects uses `clang-format`. + +You can modify the `format` script in `package.json` to run whatever formatting tool +you prefer and `ng format` will still run it. ## Known issues diff --git a/addon/ng2/blueprints/ng2/files/.clang-format b/addon/ng2/blueprints/ng2/files/.clang-format new file mode 100644 index 000000000000..8d1c3c310286 --- /dev/null +++ b/addon/ng2/blueprints/ng2/files/.clang-format @@ -0,0 +1,3 @@ +Language: JavaScript +BasedOnStyle: Google +ColumnLimit: 100 diff --git a/addon/ng2/blueprints/ng2/files/package.json b/addon/ng2/blueprints/ng2/files/package.json index 10e00ff0d99a..3c2779f7b510 100644 --- a/addon/ng2/blueprints/ng2/files/package.json +++ b/addon/ng2/blueprints/ng2/files/package.json @@ -11,11 +11,13 @@ "scripts": { "start": "ng server", "postinstall": "typings install --ambient", - "lint": "tslint src/**/*.ts" + "lint": "tslint src/**/*.ts", + "format": "clang-format -i -style=file --glob=src/**/*.ts" }, "private": true, "dependencies": { "angular2": "2.0.0-beta.6", + "clang-format": "^1.0.35", "es6-promise": "^3.0.2", "es6-shim": "^0.33.3", "reflect-metadata": "0.1.2", diff --git a/addon/ng2/commands/format.js b/addon/ng2/commands/format.js new file mode 100644 index 000000000000..29aeebab949c --- /dev/null +++ b/addon/ng2/commands/format.js @@ -0,0 +1,20 @@ +/* jshint node: true */ +'use strict'; + +var Command = require('ember-cli/lib/models/command'); +var FormatTask = require('../tasks/format'); + +module.exports = Command.extend({ + name: 'format', + description: 'Formats code in existing project', + works: 'insideProject', + run: function() { + var formatTask = new FormatTask({ + ui: this.ui, + analytics: this.analytics, + project: this.project + }); + + return formatTask.run(); + } +}); diff --git a/addon/ng2/index.js b/addon/ng2/index.js index 23ff7b422edd..59d3bacb6e1e 100644 --- a/addon/ng2/index.js +++ b/addon/ng2/index.js @@ -10,7 +10,8 @@ module.exports = { 'install' : require('./commands/install'), 'uninstall' : require('./commands/uninstall'), 'test' : require('./commands/test'), - 'lint' : require('./commands/lint') + 'lint' : require('./commands/lint'), + 'format' : require('./commands/format') }; } }; diff --git a/addon/ng2/tasks/format.js b/addon/ng2/tasks/format.js new file mode 100644 index 000000000000..06dcbd07802e --- /dev/null +++ b/addon/ng2/tasks/format.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 format') + .then(function() { + ui.writeLine(chalk.green('Successfully formatted files.')); + }) + .catch(function( /*error*/ ) { + ui.writeLine(chalk.red( + 'Couldn\'t do \'npm run format\'. Please check this script exists in your package.json.' + )); + }); + } +});