Skip to content

Commit 7734943

Browse files
committed
Add eslint with eslint-config-airbnb. Fix all eslint errors.
1 parent e2e7f0a commit 7734943

File tree

10 files changed

+102
-32
lines changed

10 files changed

+102
-32
lines changed

.eslintrc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"rules": {
3+
"indent": [
4+
2,
5+
"tab",
6+
{
7+
"SwitchCase": 1
8+
}
9+
],
10+
"quotes": [
11+
2,
12+
"single"
13+
],
14+
/*"linebreak-style": [
15+
2,
16+
"windows"
17+
],*/
18+
"semi": [
19+
2,
20+
"always"
21+
],
22+
"space-before-function-paren": [
23+
2,
24+
{
25+
"anonymous": "always",
26+
"named": "never"
27+
}
28+
],
29+
"no-use-before-define": [
30+
2,
31+
"nofunc"
32+
],
33+
// TODO: turning off for now
34+
"comma-dangle": [
35+
0
36+
],
37+
"one-var": [
38+
0
39+
],
40+
"func-names": [
41+
0
42+
],
43+
"no-else-return": [
44+
0
45+
]
46+
},
47+
"env": {
48+
"node": true,
49+
"mocha": true
50+
},
51+
"extends": [
52+
"eslint:recommended",
53+
"airbnb/base"
54+
]
55+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Overview
66

7-
This is a boilerplate for building REST APIs with ES6 and Express. Helps you stay productive by following best practices. Follows [Airbnb's Javascript style guide](https://github.com/airbnb/javascript).
7+
This is a starter kit for building REST APIs with ES6 and Express. Helps you stay productive by following best practices. Follows [Airbnb's Javascript style guide](https://github.com/airbnb/javascript).
88

99
### Features
1010

@@ -13,7 +13,7 @@ This is a boilerplate for building REST APIs with ES6 and Express. Helps you sta
1313
| ES2015 via Babel | ES2015 support using [Babel](https://babeljs.io/). |
1414
| Code Linting | JavaScript code linting is done using [ESLint](http://eslint.org) - a pluggable linter tool for identifying and reporting on patterns in JavaScript. Uses ESLint with [eslint-config-airbnb](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb), which tries to follow the Airbnb JavaScript style guide. |
1515
| Auto server restart | Restart the server using [nodemon](https://github.com/remy/nodemon) in real-time anytime an edit is made, with babel compilation. |
16-
| Environment variables | Supports setting env variable using .env file at the root. Uses [dot-env](https://www.npmjs.com/package/dotenv) to enable this feature.) |
16+
| Environment variables | Supports setting env variable using .env file at the root. Uses [dot-env](https://www.npmjs.com/package/dotenv) to enable this feature. |
1717

1818
## Getting Started
1919

config/env.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ const config = require('dotenv').config() || {};
66
// root path of server
77
config.rootPath = path.join(__dirname, '../');
88

9-
console.log('loaded env');
109
export default config;

config/express.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import express from 'express';
2-
import path from 'path';
32
import logger from 'morgan';
43
import bodyParser from 'body-parser';
54
import compress from 'compression';
@@ -26,23 +25,23 @@ app.use(cors());
2625
app.use('/api', routes);
2726

2827
// catch different types of error
29-
app.use(function (err, req, res, next) {
28+
app.use((err, req, res, next) => {
3029
const error = utilityService.createError(err.message, err.status, err.isPublic);
3130
return next(error);
3231
});
3332

3433
// catch 404 and forward to error handler
35-
app.use(function(req, res, next) {
34+
app.use((req, res, next) => {
3635
const err = utilityService.createError('API not found', httpStatus.NOT_FOUND);
3736
return next(err);
3837
});
3938

4039
// error handler
41-
app.use(function(err, req, res, next) {
42-
return res.status(err.status).json({
40+
app.use((err, req, res, next) => // eslint-disable-line no-unused-vars
41+
res.status(err.status).json({
4342
message: err.isPublic ? err.message : httpStatus[err.status],
4443
error: process.env.NODE_ENV === 'development' ? err : {}
45-
});
46-
});
44+
})
45+
);
4746

4847
export default app;

gulpfile.babel.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const paths = {
1111
};
1212

1313
// Clean up dist files
14-
gulp.task('clean', () => {
15-
return del(['dist/**', 'coverage/**', '!dist', '!coverage']);
16-
});
14+
gulp.task('clean', () =>
15+
del(['dist/**', 'coverage/**', '!dist', '!coverage'])
16+
);
1717

1818
// Compile ES6 to ES5 and copy to dist
19-
gulp.task('babel', () => {
20-
return gulp.src([...paths.js, '!gulpfile.babel.js'], {base: '.'})
19+
gulp.task('babel', () =>
20+
gulp.src([...paths.js, '!gulpfile.babel.js'], { base: '.' })
2121
.pipe(plugins.newer('dist'))
2222
.pipe(plugins.sourcemaps.init())
2323
.pipe(plugins.babel())
@@ -27,22 +27,36 @@ gulp.task('babel', () => {
2727
})
2828
.pipe(plugins.sourcemaps.write('.', {
2929
includeContent: false,
30-
sourceRoot: function (file) {
30+
sourceRoot(file) {
3131
return path.relative(file.path, __dirname);
3232
}
3333
}))
34-
.pipe(gulp.dest('dist'));
35-
});
34+
.pipe(gulp.dest('dist'))
35+
);
36+
37+
// Lint Javascript
38+
gulp.task('eslint', () =>
39+
gulp.src(paths.js)
40+
// eslint() attaches the lint output to the "eslint" property
41+
// of the file object so it can be used by other modules.
42+
.pipe(plugins.eslint())
43+
// eslint.format() outputs the lint results to the console.
44+
// Alternatively use eslint.formatEach() (see Docs).
45+
.pipe(plugins.eslint.format())
46+
// To have the process exit with an error code (1) on
47+
// lint error, return the stream and pipe to failAfterError last.
48+
.pipe(plugins.eslint.failAfterError())
49+
);
3650

3751
// Start server with restart on file changes
38-
gulp.task('nodemon', ['babel'], () => {
52+
gulp.task('nodemon', ['babel'], () =>
3953
plugins.nodemon({
4054
script: path.join('dist', 'index.js'),
4155
ext: 'js',
4256
ignore: ['node_modules/**/*.js', 'dist/**/*.js'],
4357
tasks: ['babel']
44-
});
45-
});
58+
})
59+
);
4660

4761
gulp.task('serve', ['clean'], () => {
4862
runSequence('nodemon');
@@ -51,4 +65,4 @@ gulp.task('serve', ['clean'], () => {
5165
// clean and compile files, the default task
5266
gulp.task('default', ['clean'], () => {
5367
runSequence('babel');
54-
});
68+
});

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ const debug = require('debug')('express-es6-rest-api-starter:index');
66
// listen on port HTTP_PORT
77
app.listen(config.HTTP_PORT, () => {
88
debug(`started server on port ${config.HTTP_PORT} (${config.NODE_ENV})`);
9-
});
9+
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727
"devDependencies": {
2828
"babel": "^5.8.23",
2929
"del": "^2.2.0",
30+
"eslint": "^1.10.3",
31+
"eslint-config-airbnb": "^4.0.0",
3032
"gulp": "^3.9.0",
3133
"gulp-babel": "^5.2.1",
34+
"gulp-eslint": "^1.1.1",
3235
"gulp-load-plugins": "^1.2.0",
3336
"gulp-newer": "^1.1.0",
3437
"gulp-nodemon": "^2.0.6",

server/routes/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import express from 'express';
22
import userRoutes from './users';
33

4-
const router = express.Router();
4+
const router = express.Router(); // eslint-disable-line new-cap
55

66
/* GET home page. */
7-
router.get('/health-check', function(req, res, next) {
8-
return res.send('OK');
9-
});
7+
router.get('/health-check', (req, res) =>
8+
res.send('OK')
9+
);
1010

1111
router.use('/users', userRoutes);
1212

server/routes/users.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import express from 'express';
22

3-
const router = express.Router();
3+
const router = express.Router(); // eslint-disable-line new-cap
44

55
/* GET users listing. */
6-
router.get('/', function(req, res, next) {
7-
res.send('respond with a resource');
8-
});
6+
router.get('/', (req, res) =>
7+
res.send('respond with a resource')
8+
);
99

1010
export default router;

server/services/utility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export function createError(message, status = httpStatus.INTERNAL_SERVER_ERROR,
55
error.status = status;
66
error.isPublic = isPublic;
77
return error;
8-
}
8+
}

0 commit comments

Comments
 (0)