Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #314 from wincent/glh/pretty
Browse files Browse the repository at this point in the history
Prettify codebase
  • Loading branch information
wincent authored May 26, 2017
2 parents e4e42a8 + 1f1f2bd commit f5f37b4
Show file tree
Hide file tree
Showing 12 changed files with 1,247 additions and 859 deletions.
4 changes: 0 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"flowtype/use-flow-type": 2,
"flowtype/semi": 2,

"array-bracket-spacing": [2, "always"],
"arrow-parens": [2, "as-needed"],
"arrow-spacing": 2,
"block-scoped-var": 0,
Expand Down Expand Up @@ -81,7 +80,6 @@
"linebreak-style": 2,
"lines-around-comment": 0,
"max-depth": 0,
"max-len": [2, 80, 4],
"max-nested-callbacks": 0,
"max-params": 0,
"max-statements": 0,
Expand Down Expand Up @@ -193,7 +191,6 @@
"object-shorthand": [2, "always"],
"one-var": [2, "never"],
"operator-assignment": [2, "always"],
"operator-linebreak": [2, "after"],
"padded-blocks": 0,
"prefer-const": 2,
"prefer-reflect": 0,
Expand All @@ -206,7 +203,6 @@
"semi-spacing": [2, {"before": false, "after": true}],
"sort-vars": 0,
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
"space-in-parens": 0,
"space-infix-ops": [2, {"int32Hint": false}],
"space-unary-ops": [2, {"words": true, "nonwords": false}],
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"scripts": {
"prepublish": ". ./resources/prepublish.sh",
"test": "npm run lint && npm run check && npm run testonly",
"test": "npm run lint && npm run pretty-check && npm run check && npm run testonly",
"testonly": "mocha $npm_package_options_mocha",
"lint": "eslint src",
"check": "flow check",
Expand All @@ -57,12 +57,15 @@
"watch": "node resources/watch.js",
"cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
"cover:lcov": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha",
"pretty": "node resources/pretty.js",
"pretty-check": "node resources/pretty.js --check",
"preversion": "npm test"
},
"dependencies": {
"accepts": "^1.3.0",
"content-type": "^1.0.2",
"http-errors": "^1.3.0",
"prettier": "^1.3.1",
"raw-body": "^2.1.0"
},
"devDependencies": {
Expand Down
16 changes: 8 additions & 8 deletions resources/interfaces/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
/* Flow declarations for express requests and responses */
/* eslint-disable no-unused-vars */
declare class Request {
method: String;
body: Object;
query: Object;
method: String,
body: Object,
query: Object,
}

declare class Response {
status: (code: Number) => Response;
set: (field: String, value: String) => Response;
send: (body: String) => void;
end: (body: Buffer) => void;
json: (body: Object) => void;
status: (code: Number) => Response,
set: (field: String, value: String) => Response,
send: (body: String) => void,
end: (body: Buffer) => void,
json: (body: Object) => void,
}
7 changes: 3 additions & 4 deletions resources/mocha-bootload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
*/
/* eslint-disable no-console */


require('babel-register')({
plugins: [ 'transform-async-to-generator', 'transform-runtime' ]
plugins: ['transform-async-to-generator', 'transform-runtime'],
});

process.on('unhandledRejection', function (error) {
process.on('unhandledRejection', function(error) {
console.error('Unhandled Promise Rejection:');
console.error(error && error.stack || error);
console.error((error && error.stack) || error);
});
51 changes: 51 additions & 0 deletions resources/pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env node
/**
* Copyright (c) Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

const { spawnSync } = require('child_process');
const { join } = require('path');

const INVERSE = '\x1b[7m';
const RESET = '\x1b[0m';
const YELLOW = '\x1b[33m';

const options = ['--single-quote', '--trailing-comma=all'];
const glob = '{resources,src}/**/*.js';
const root = join(__dirname, '..');
const executable = join(root, 'node_modules', '.bin', 'prettier');

const check = process.argv.indexOf('--check') !== -1;
const mode = check ? '--list-different' : '--write';
process.chdir(root);

const { stdout, stderr, status, error } = spawnSync(executable, [
...options,
mode,
glob,
]);
const out = stdout.toString().trim();
const err = stdout.toString().trim();

function print(message) {
if (message) {
process.stdout.write(message + '\n');
}
}

if (status) {
print(out);
print(err);
if (check) {
print(`\n${YELLOW}The files listed above are not correctly formatted.`);
print(`Try: ${INVERSE} npm run pretty ${RESET}`);
}
}
if (error) {
print('error', error);
}
process.exit(status != null ? status : 1);
98 changes: 55 additions & 43 deletions resources/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ const { resolve: resolvePath } = require('path');
const { spawn } = require('child_process');
const flowBinPath = require('flow-bin');


process.env.PATH += ':./node_modules/.bin';

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

function exec(command, options) {
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
const child = spawn(command, options, {
cmd,
env: process.env,
stdio: 'inherit'
stdio: 'inherit',
});
child.on('exit', function (code) {
child.on('exit', function(code) {
if (code === 0) {
resolve(true);
} else {
Expand All @@ -36,18 +35,18 @@ function exec(command, options) {
});
}

const flowServer = spawn(flowBinPath, [ 'server' ], {
const flowServer = spawn(flowBinPath, ['server'], {
cmd,
env: process.env
env: process.env,
});

const watcher = sane(srcDir, { glob: [ '**/*.js', '**/*.graphql' ] })
const watcher = sane(srcDir, { glob: ['**/*.js', '**/*.graphql'] })
.on('ready', startWatch)
.on('add', changeFile)
.on('delete', deleteFile)
.on('change', changeFile);

process.on('SIGINT', function () {
process.on('SIGINT', function() {
watcher.close();
flowServer.kill();
console.log(CLEARLINE + yellow(invert('stopped watching')));
Expand Down Expand Up @@ -100,14 +99,17 @@ function checkFiles(filepaths) {

return parseFiles(filepaths)
.then(() => runTests(filepaths))
.then(testSuccess => lintFiles(filepaths)
.then(lintSuccess => typecheckStatus()
.then(typecheckSuccess =>
testSuccess && lintSuccess && typecheckSuccess)))
.then(testSuccess =>
lintFiles(filepaths).then(lintSuccess =>
typecheckStatus().then(
typecheckSuccess => testSuccess && lintSuccess && typecheckSuccess,
),
),
)
.catch(() => false)
.then(success => {
process.stdout.write(
'\n' + (success ? '' : '\x07') + green(invert('watching...'))
'\n' + (success ? '' : '\x07') + green(invert('watching...')),
);
});
}
Expand All @@ -117,50 +119,60 @@ function checkFiles(filepaths) {
function parseFiles(filepaths) {
console.log('Checking Syntax');

return Promise.all(filepaths.map(filepath => {
if (isJS(filepath) && !isTest(filepath)) {
return exec('babel', [
'--optional', 'runtime',
'--out-file', '/dev/null',
srcPath(filepath)
]);
}
}));
return Promise.all(
filepaths.map(filepath => {
if (isJS(filepath) && !isTest(filepath)) {
return exec('babel', [
'--optional',
'runtime',
'--out-file',
'/dev/null',
srcPath(filepath),
]);
}
}),
);
}

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

return exec('mocha', [
'--reporter', 'progress',
'--require', 'resources/mocha-bootload'
].concat(
allTests(filepaths) ? filepaths.map(srcPath) :
[ 'src/**/__tests__/**/*.js' ]
)).catch(() => false);
return exec(
'mocha',
['--reporter', 'progress', '--require', 'resources/mocha-bootload'].concat(
allTests(filepaths)
? filepaths.map(srcPath)
: ['src/**/__tests__/**/*.js'],
),
).catch(() => false);
}

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

return filepaths.reduce((prev, filepath) => prev.then(prevSuccess => {
if (isJS(filepath)) {
process.stdout.write(' ' + filepath + ' ...');
return exec('eslint', [ srcPath(filepath) ])
.catch(() => false)
.then(success => {
const msg = CLEARLINE + ' ' + (success ? CHECK : X) + ' ' + filepath;
console.log(msg);
return prevSuccess && success;
});
}
return prevSuccess;
}), Promise.resolve(true));
return filepaths.reduce(
(prev, filepath) =>
prev.then(prevSuccess => {
if (isJS(filepath)) {
process.stdout.write(' ' + filepath + ' ...');
return exec('eslint', [srcPath(filepath)])
.catch(() => false)
.then(success => {
const msg =
CLEARLINE + ' ' + (success ? CHECK : X) + ' ' + filepath;
console.log(msg);
return prevSuccess && success;
});
}
return prevSuccess;
}),
Promise.resolve(true),
);
}

function typecheckStatus() {
console.log('\nType Checking\n');
return exec(flowBinPath, [ 'status' ]).catch(() => false);
return exec(flowBinPath, ['status']).catch(() => false);
}

// Filepath
Expand Down
Loading

0 comments on commit f5f37b4

Please sign in to comment.