Skip to content

Commit

Permalink
Use eslint and prettier (GoogleCloudPlatform#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
fhinkel authored Nov 9, 2018
1 parent 092728c commit b939a87
Show file tree
Hide file tree
Showing 137 changed files with 1,966 additions and 1,894 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/node_modules/*

19 changes: 19 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
extends:
- 'eslint:recommended'
- 'plugin:node/recommended'
- prettier
env:
mocha: true
plugins:
- node
- prettier
rules:
prettier/prettier: error
block-scoped-var: error
eqeqeq: error
no-warning-comments: warn
no-console: off
node/no-missing-require: off
node/no-unpublished-require: off

3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/*
samples/node_modules/*
src/**/doc/*
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
bracketSpacing: false
printWidth: 80
semi: true
singleQuote: true
tabWidth: 2
trailingComma: es5
useTabs: false
28 changes: 0 additions & 28 deletions 1-hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,6 @@
}
}
},
"semistandard": {
"globals": [
"after",
"afterEach",
"before",
"beforeEach",
"describe",
"it"
]
},
"contributors": [
"Ace Nassri <anassri@google.com>",
"Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>",
"Allen Day <allenday@users.noreply.github.com>",
"André Cipriani Bandarra <andreban@gmail.com>",
"Dominik Staskiewicz <stadominik@gmail.com>",
"F. Hinkelmann <fhinkel@vt.edu>",
"Jason Dobry <jdobry@google.com>",
"Jon Wayne Parrott <jonwayne@google.com>",
"Justin Beckwith <justin.beckwith@gmail.com>",
"Michael McDonald <mcdonald@firebase.com>",
"Sean McBreen <seanmcb@google.com>",
"Steren <steren.giannini@gmail.com>",
"Steve Perry <steveperry-53@users.noreply.github.com>",
"chenyumic <chenyumic@google.com>",
"renovate[bot] <renovate[bot]@users.noreply.github.com>",
"shollyman <shollyman@google.com>"
],
"scripts": {
"start": "node app.js",
"e2e": "repo-tools test deploy",
Expand Down
2 changes: 1 addition & 1 deletion 2-structured-data/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ app.use((req, res) => {
});

// Basic error handler
app.use((err, req, res, next) => {
app.use((err, req, res) => {
/* jshint unused:false */
console.error(err);
// If our routes specified a specific response, then send that. Otherwise,
Expand Down
8 changes: 4 additions & 4 deletions 2-structured-data/books/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
const express = require('express');
const bodyParser = require('body-parser');

function getModel () {
function getModel() {
return require(`./model-${require('../config').get('DATA_BACKEND')}`);
}

Expand All @@ -38,7 +38,7 @@ router.get('/', (req, res, next) => {
}
res.json({
items: entities,
nextPageToken: cursor
nextPageToken: cursor,
});
});
});
Expand Down Expand Up @@ -94,7 +94,7 @@ router.put('/:book', (req, res, next) => {
* Delete a book.
*/
router.delete('/:book', (req, res, next) => {
getModel().delete(req.params.book, (err) => {
getModel().delete(req.params.book, err => {
if (err) {
next(err);
return;
Expand All @@ -111,7 +111,7 @@ router.use((err, req, res, next) => {
// responding to the request
err.response = {
message: err.message,
internalCode: err.code
internalCode: err.code,
};
next(err);
});
Expand Down
14 changes: 7 additions & 7 deletions 2-structured-data/books/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
const express = require('express');
const bodyParser = require('body-parser');

function getModel () {
function getModel() {
return require(`./model-${require('../config').get('DATA_BACKEND')}`);
}

const router = express.Router();

// Automatically parse request body as form data
router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.urlencoded({extended: false}));

// Set Content-Type for all responses for these routes
router.use((req, res, next) => {
Expand All @@ -44,7 +44,7 @@ router.get('/', (req, res, next) => {
}
res.render('books/list.pug', {
books: entities,
nextPageToken: cursor
nextPageToken: cursor,
});
});
});
Expand All @@ -58,7 +58,7 @@ router.get('/', (req, res, next) => {
router.get('/add', (req, res) => {
res.render('books/form.pug', {
book: {},
action: 'Add'
action: 'Add',
});
});
// [END add_get]
Expand Down Expand Up @@ -96,7 +96,7 @@ router.get('/:book/edit', (req, res, next) => {
}
res.render('books/form.pug', {
book: entity,
action: 'Edit'
action: 'Edit',
});
});
});
Expand Down Expand Up @@ -130,7 +130,7 @@ router.get('/:book', (req, res, next) => {
return;
}
res.render('books/view.pug', {
book: entity
book: entity,
});
});
});
Expand All @@ -141,7 +141,7 @@ router.get('/:book', (req, res, next) => {
* Delete a book.
*/
router.get('/:book/delete', (req, res, next) => {
getModel().delete(req.params.book, (err) => {
getModel().delete(req.params.book, err => {
if (err) {
next(err);
return;
Expand Down
64 changes: 38 additions & 26 deletions 2-structured-data/books/model-cloudsql.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ const config = require('../config');
const options = {
user: config.get('MYSQL_USER'),
password: config.get('MYSQL_PASSWORD'),
database: 'bookshelf'
database: 'bookshelf',
};

if (config.get('INSTANCE_CONNECTION_NAME') && config.get('NODE_ENV') === 'production') {
if (
config.get('INSTANCE_CONNECTION_NAME') &&
config.get('NODE_ENV') === 'production'
) {
options.socketPath = `/cloudsql/${config.get('INSTANCE_CONNECTION_NAME')}`;
}

const connection = mysql.createConnection(options);

// [START list]
function list (limit, token, cb) {
function list(limit, token, cb) {
token = token ? parseInt(token, 10) : 0;
connection.query(
'SELECT * FROM `books` LIMIT ? OFFSET ?', [limit, token],
'SELECT * FROM `books` LIMIT ? OFFSET ?',
[limit, token],
(err, results) => {
if (err) {
cb(err);
Expand All @@ -47,7 +51,7 @@ function list (limit, token, cb) {
// [END list]

// [START create]
function create (data, cb) {
function create(data, cb) {
connection.query('INSERT INTO `books` SET ?', data, (err, res) => {
if (err) {
cb(err);
Expand All @@ -58,37 +62,39 @@ function create (data, cb) {
}
// [END create]

function read (id, cb) {
function read(id, cb) {
connection.query(
'SELECT * FROM `books` WHERE `id` = ?', id, (err, results) => {
'SELECT * FROM `books` WHERE `id` = ?',
id,
(err, results) => {
if (!err && !results.length) {
err = {
code: 404,
message: 'Not found'
message: 'Not found',
};
}
if (err) {
cb(err);
return;
}
cb(null, results[0]);
});
}
);
}

// [START update]
function update (id, data, cb) {
connection.query(
'UPDATE `books` SET ? WHERE `id` = ?', [data, id], (err) => {
if (err) {
cb(err);
return;
}
read(id, cb);
});
function update(id, data, cb) {
connection.query('UPDATE `books` SET ? WHERE `id` = ?', [data, id], err => {
if (err) {
cb(err);
return;
}
read(id, cb);
});
}
// [END update]

function _delete (id, cb) {
function _delete(id, cb) {
connection.query('DELETE FROM `books` WHERE `id` = ?', id, cb);
}

Expand All @@ -98,7 +104,7 @@ module.exports = {
create: create,
read: read,
update: update,
delete: _delete
delete: _delete,
};

if (module === require.main) {
Expand All @@ -107,7 +113,8 @@ if (module === require.main) {

console.log(
`Running this script directly will allow you to initialize your mysql database.
This script will not modify any existing tables.`);
This script will not modify any existing tables.`
);

prompt.get(['user', 'password'], (err, result) => {
if (err) {
Expand All @@ -117,10 +124,15 @@ if (module === require.main) {
});
}

function createSchema (config) {
const connection = mysql.createConnection(extend({
multipleStatements: true
}, config));
function createSchema(config) {
const connection = mysql.createConnection(
extend(
{
multipleStatements: true,
},
config
)
);

connection.query(
`CREATE DATABASE IF NOT EXISTS \`bookshelf\`
Expand All @@ -137,7 +149,7 @@ function createSchema (config) {
\`createdBy\` VARCHAR(255) NULL,
\`createdById\` VARCHAR(255) NULL,
PRIMARY KEY (\`id\`));`,
(err) => {
err => {
if (err) {
throw err;
}
Expand Down
Loading

0 comments on commit b939a87

Please sign in to comment.