Skip to content

Commit 1f89a2b

Browse files
committed
feat(format): add code formatting
Close angular#143
1 parent 8741403 commit 1f89a2b

File tree

6 files changed

+64
-7
lines changed

6 files changed

+64
-7
lines changed

README.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,21 @@ ng github-pages:deploy
176176

177177
Checkout [angular-cli-github-pages addon](https://github.com/IgorMinar/angular-cli-github-pages) docs for more info.
178178

179-
### Linting code
179+
### Linting and formatting code
180180

181-
You can lint your app code by running `ng lint`.
182-
This will use the `lint` npm script that in generated projects uses `tslint`.
181+
You can lint or format your app code by running `ng lint` or `ng format` respectively.
182+
This will use the `lint`/`format` npm script that in generated projects uses `tslint`/`clang-format`.
183183

184-
You can modify the `lint` script in `package.json` to run whatever linting tool
185-
you prefer and `ng lint` will still run it.
184+
You can modify the these scripts in `package.json` to run whatever tool you prefer.
185+
186+
187+
### Formatting code
188+
189+
You can format your app code by running `ng format`.
190+
This will use the `format` npm script that in generated projects uses `clang-format`.
191+
192+
You can modify the `format` script in `package.json` to run whatever formatting tool
193+
you prefer and `ng format` will still run it.
186194

187195

188196
## Known issues
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Language: JavaScript
2+
BasedOnStyle: Google
3+
ColumnLimit: 100

addon/ng2/blueprints/ng2/files/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
"scripts": {
1212
"start": "ng server",
1313
"postinstall": "typings install --ambient",
14-
"lint": "tslint src/**/*.ts"
14+
"lint": "tslint src/**/*.ts",
15+
"format": "clang-format -i -style=file --glob=src/**/*.ts"
1516
},
1617
"private": true,
1718
"dependencies": {
1819
"angular2": "2.0.0-beta.6",
20+
"clang-format": "^1.0.35",
1921
"es6-promise": "^3.0.2",
2022
"es6-shim": "^0.33.3",
2123
"reflect-metadata": "0.1.2",

addon/ng2/commands/format.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* jshint node: true */
2+
'use strict';
3+
4+
var Command = require('ember-cli/lib/models/command');
5+
var FormatTask = require('../tasks/format');
6+
7+
module.exports = Command.extend({
8+
name: 'format',
9+
description: 'Formats code in existing project',
10+
works: 'insideProject',
11+
run: function() {
12+
var formatTask = new FormatTask({
13+
ui: this.ui,
14+
analytics: this.analytics,
15+
project: this.project
16+
});
17+
18+
return formatTask.run();
19+
}
20+
});

addon/ng2/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module.exports = {
1010
'install' : require('./commands/install'),
1111
'uninstall' : require('./commands/uninstall'),
1212
'test' : require('./commands/test'),
13-
'lint' : require('./commands/lint')
13+
'lint' : require('./commands/lint'),
14+
'format' : require('./commands/format')
1415
};
1516
}
1617
};

addon/ng2/tasks/format.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* jshint node: true */
2+
'use strict';
3+
4+
var Promise = require('ember-cli/lib/ext/promise');
5+
var Task = require('ember-cli/lib/models/task');
6+
var exec = Promise.denodeify(require('child_process').exec);
7+
8+
module.exports = Task.extend({
9+
run: function() {
10+
var chalk = require('chalk');
11+
var ui = this.ui;
12+
13+
return exec('npm run format')
14+
.then(function() {
15+
ui.writeLine(chalk.green('Successfully formatted files.'));
16+
})
17+
.catch(function( /*error*/ ) {
18+
ui.writeLine(chalk.red(
19+
'Couldn\'t do \'npm run format\'. Please check this script exists in your package.json.'
20+
));
21+
});
22+
}
23+
});

0 commit comments

Comments
 (0)