Skip to content

Commit f5f37b4

Browse files
authored
Merge pull request graphql#314 from wincent/glh/pretty
Prettify codebase
2 parents e4e42a8 + 1f1f2bd commit f5f37b4

File tree

12 files changed

+1247
-859
lines changed

12 files changed

+1247
-859
lines changed

.eslintrc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"flowtype/use-flow-type": 2,
4848
"flowtype/semi": 2,
4949

50-
"array-bracket-spacing": [2, "always"],
5150
"arrow-parens": [2, "as-needed"],
5251
"arrow-spacing": 2,
5352
"block-scoped-var": 0,
@@ -81,7 +80,6 @@
8180
"linebreak-style": 2,
8281
"lines-around-comment": 0,
8382
"max-depth": 0,
84-
"max-len": [2, 80, 4],
8583
"max-nested-callbacks": 0,
8684
"max-params": 0,
8785
"max-statements": 0,
@@ -193,7 +191,6 @@
193191
"object-shorthand": [2, "always"],
194192
"one-var": [2, "never"],
195193
"operator-assignment": [2, "always"],
196-
"operator-linebreak": [2, "after"],
197194
"padded-blocks": 0,
198195
"prefer-const": 2,
199196
"prefer-reflect": 0,
@@ -206,7 +203,6 @@
206203
"semi-spacing": [2, {"before": false, "after": true}],
207204
"sort-vars": 0,
208205
"space-before-blocks": [2, "always"],
209-
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
210206
"space-in-parens": 0,
211207
"space-infix-ops": [2, {"int32Hint": false}],
212208
"space-unary-ops": [2, {"words": true, "nonwords": false}],

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"scripts": {
5050
"prepublish": ". ./resources/prepublish.sh",
51-
"test": "npm run lint && npm run check && npm run testonly",
51+
"test": "npm run lint && npm run pretty-check && npm run check && npm run testonly",
5252
"testonly": "mocha $npm_package_options_mocha",
5353
"lint": "eslint src",
5454
"check": "flow check",
@@ -57,12 +57,15 @@
5757
"watch": "node resources/watch.js",
5858
"cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
5959
"cover:lcov": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha",
60+
"pretty": "node resources/pretty.js",
61+
"pretty-check": "node resources/pretty.js --check",
6062
"preversion": "npm test"
6163
},
6264
"dependencies": {
6365
"accepts": "^1.3.0",
6466
"content-type": "^1.0.2",
6567
"http-errors": "^1.3.0",
68+
"prettier": "^1.3.1",
6669
"raw-body": "^2.1.0"
6770
},
6871
"devDependencies": {

resources/interfaces/express.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
/* Flow declarations for express requests and responses */
33
/* eslint-disable no-unused-vars */
44
declare class Request {
5-
method: String;
6-
body: Object;
7-
query: Object;
5+
method: String,
6+
body: Object,
7+
query: Object,
88
}
99

1010
declare class Response {
11-
status: (code: Number) => Response;
12-
set: (field: String, value: String) => Response;
13-
send: (body: String) => void;
14-
end: (body: Buffer) => void;
15-
json: (body: Object) => void;
11+
status: (code: Number) => Response,
12+
set: (field: String, value: String) => Response,
13+
send: (body: String) => void,
14+
end: (body: Buffer) => void,
15+
json: (body: Object) => void,
1616
}

resources/mocha-bootload.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
*/
99
/* eslint-disable no-console */
1010

11-
1211
require('babel-register')({
13-
plugins: [ 'transform-async-to-generator', 'transform-runtime' ]
12+
plugins: ['transform-async-to-generator', 'transform-runtime'],
1413
});
1514

16-
process.on('unhandledRejection', function (error) {
15+
process.on('unhandledRejection', function(error) {
1716
console.error('Unhandled Promise Rejection:');
18-
console.error(error && error.stack || error);
17+
console.error((error && error.stack) || error);
1918
});

resources/pretty.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Copyright (c) Facebook, Inc.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the license found in the
7+
* LICENSE file in the root directory of this source tree.
8+
*/
9+
10+
const { spawnSync } = require('child_process');
11+
const { join } = require('path');
12+
13+
const INVERSE = '\x1b[7m';
14+
const RESET = '\x1b[0m';
15+
const YELLOW = '\x1b[33m';
16+
17+
const options = ['--single-quote', '--trailing-comma=all'];
18+
const glob = '{resources,src}/**/*.js';
19+
const root = join(__dirname, '..');
20+
const executable = join(root, 'node_modules', '.bin', 'prettier');
21+
22+
const check = process.argv.indexOf('--check') !== -1;
23+
const mode = check ? '--list-different' : '--write';
24+
process.chdir(root);
25+
26+
const { stdout, stderr, status, error } = spawnSync(executable, [
27+
...options,
28+
mode,
29+
glob,
30+
]);
31+
const out = stdout.toString().trim();
32+
const err = stdout.toString().trim();
33+
34+
function print(message) {
35+
if (message) {
36+
process.stdout.write(message + '\n');
37+
}
38+
}
39+
40+
if (status) {
41+
print(out);
42+
print(err);
43+
if (check) {
44+
print(`\n${YELLOW}The files listed above are not correctly formatted.`);
45+
print(`Try: ${INVERSE} npm run pretty ${RESET}`);
46+
}
47+
}
48+
if (error) {
49+
print('error', error);
50+
}
51+
process.exit(status != null ? status : 1);

resources/watch.js

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@ const { resolve: resolvePath } = require('path');
1313
const { spawn } = require('child_process');
1414
const flowBinPath = require('flow-bin');
1515

16-
1716
process.env.PATH += ':./node_modules/.bin';
1817

1918
const cmd = resolvePath(__dirname);
2019
const srcDir = resolvePath(cmd, '../src');
2120

2221
function exec(command, options) {
23-
return new Promise(function (resolve, reject) {
22+
return new Promise(function(resolve, reject) {
2423
const child = spawn(command, options, {
2524
cmd,
2625
env: process.env,
27-
stdio: 'inherit'
26+
stdio: 'inherit',
2827
});
29-
child.on('exit', function (code) {
28+
child.on('exit', function(code) {
3029
if (code === 0) {
3130
resolve(true);
3231
} else {
@@ -36,18 +35,18 @@ function exec(command, options) {
3635
});
3736
}
3837

39-
const flowServer = spawn(flowBinPath, [ 'server' ], {
38+
const flowServer = spawn(flowBinPath, ['server'], {
4039
cmd,
41-
env: process.env
40+
env: process.env,
4241
});
4342

44-
const watcher = sane(srcDir, { glob: [ '**/*.js', '**/*.graphql' ] })
43+
const watcher = sane(srcDir, { glob: ['**/*.js', '**/*.graphql'] })
4544
.on('ready', startWatch)
4645
.on('add', changeFile)
4746
.on('delete', deleteFile)
4847
.on('change', changeFile);
4948

50-
process.on('SIGINT', function () {
49+
process.on('SIGINT', function() {
5150
watcher.close();
5251
flowServer.kill();
5352
console.log(CLEARLINE + yellow(invert('stopped watching')));
@@ -100,14 +99,17 @@ function checkFiles(filepaths) {
10099

101100
return parseFiles(filepaths)
102101
.then(() => runTests(filepaths))
103-
.then(testSuccess => lintFiles(filepaths)
104-
.then(lintSuccess => typecheckStatus()
105-
.then(typecheckSuccess =>
106-
testSuccess && lintSuccess && typecheckSuccess)))
102+
.then(testSuccess =>
103+
lintFiles(filepaths).then(lintSuccess =>
104+
typecheckStatus().then(
105+
typecheckSuccess => testSuccess && lintSuccess && typecheckSuccess,
106+
),
107+
),
108+
)
107109
.catch(() => false)
108110
.then(success => {
109111
process.stdout.write(
110-
'\n' + (success ? '' : '\x07') + green(invert('watching...'))
112+
'\n' + (success ? '' : '\x07') + green(invert('watching...')),
111113
);
112114
});
113115
}
@@ -117,50 +119,60 @@ function checkFiles(filepaths) {
117119
function parseFiles(filepaths) {
118120
console.log('Checking Syntax');
119121

120-
return Promise.all(filepaths.map(filepath => {
121-
if (isJS(filepath) && !isTest(filepath)) {
122-
return exec('babel', [
123-
'--optional', 'runtime',
124-
'--out-file', '/dev/null',
125-
srcPath(filepath)
126-
]);
127-
}
128-
}));
122+
return Promise.all(
123+
filepaths.map(filepath => {
124+
if (isJS(filepath) && !isTest(filepath)) {
125+
return exec('babel', [
126+
'--optional',
127+
'runtime',
128+
'--out-file',
129+
'/dev/null',
130+
srcPath(filepath),
131+
]);
132+
}
133+
}),
134+
);
129135
}
130136

131137
function runTests(filepaths) {
132138
console.log('\nRunning Tests');
133139

134-
return exec('mocha', [
135-
'--reporter', 'progress',
136-
'--require', 'resources/mocha-bootload'
137-
].concat(
138-
allTests(filepaths) ? filepaths.map(srcPath) :
139-
[ 'src/**/__tests__/**/*.js' ]
140-
)).catch(() => false);
140+
return exec(
141+
'mocha',
142+
['--reporter', 'progress', '--require', 'resources/mocha-bootload'].concat(
143+
allTests(filepaths)
144+
? filepaths.map(srcPath)
145+
: ['src/**/__tests__/**/*.js'],
146+
),
147+
).catch(() => false);
141148
}
142149

143150
function lintFiles(filepaths) {
144151
console.log('Linting Code\n');
145152

146-
return filepaths.reduce((prev, filepath) => prev.then(prevSuccess => {
147-
if (isJS(filepath)) {
148-
process.stdout.write(' ' + filepath + ' ...');
149-
return exec('eslint', [ srcPath(filepath) ])
150-
.catch(() => false)
151-
.then(success => {
152-
const msg = CLEARLINE + ' ' + (success ? CHECK : X) + ' ' + filepath;
153-
console.log(msg);
154-
return prevSuccess && success;
155-
});
156-
}
157-
return prevSuccess;
158-
}), Promise.resolve(true));
153+
return filepaths.reduce(
154+
(prev, filepath) =>
155+
prev.then(prevSuccess => {
156+
if (isJS(filepath)) {
157+
process.stdout.write(' ' + filepath + ' ...');
158+
return exec('eslint', [srcPath(filepath)])
159+
.catch(() => false)
160+
.then(success => {
161+
const msg =
162+
CLEARLINE + ' ' + (success ? CHECK : X) + ' ' + filepath;
163+
console.log(msg);
164+
return prevSuccess && success;
165+
});
166+
}
167+
return prevSuccess;
168+
}),
169+
Promise.resolve(true),
170+
);
159171
}
160172

161173
function typecheckStatus() {
162174
console.log('\nType Checking\n');
163-
return exec(flowBinPath, [ 'status' ]).catch(() => false);
175+
return exec(flowBinPath, ['status']).catch(() => false);
164176
}
165177

166178
// Filepath

0 commit comments

Comments
 (0)