Skip to content

Commit 25152ed

Browse files
authored
style(prettier): Use prettier for code formatting (#710)
* style(prettier): Use prettier for code formatting This saves us style issues. Also adds husky and lint-staged for pre-commit testing Refs #709
1 parent e2915dc commit 25152ed

File tree

146 files changed

+5796
-3919
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+5796
-3919
lines changed

lib/commands/build.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* @flow */
22
'use strict';
33

4-
54
var streamArray = require('stream-array'),
65
sharedOptions = require('./shared_options'),
76
path = require('path'),
@@ -21,17 +20,20 @@ module.exports.describe = 'build documentation';
2120
* @returns {Object} yargs with options
2221
* @private
2322
*/
24-
module.exports.builder = extend({},
23+
module.exports.builder = extend(
24+
{},
2525
sharedOptions.sharedOutputOptions,
26-
sharedOptions.sharedInputOptions, {
26+
sharedOptions.sharedInputOptions,
27+
{
2728
example: 'documentation build foo.js -f md > API.md',
2829
output: {
2930
describe: 'output location. omit for stdout, otherwise is a filename ' +
30-
'for single-file outputs and a directory name for multi-file outputs like html',
31+
'for single-file outputs and a directory name for multi-file outputs like html',
3132
default: 'stdout',
3233
alias: 'o'
3334
}
34-
});
35+
}
36+
);
3537

3638
/*
3739
* The `build` command. Requires either `--output` or the `callback` argument.
@@ -41,27 +43,35 @@ module.exports.builder = extend({},
4143
* The former case, with the callback, is used by the `serve` command, which is
4244
* just a thin wrapper around this one.
4345
*/
44-
module.exports.handler = function build(argv/*: Object*/) {
46+
module.exports.handler = function build(argv /*: Object*/) {
4547
var watcher;
4648
argv._handled = true;
4749

4850
if (!argv.input.length) {
4951
try {
50-
argv.input = [JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')).main || 'index.js'];
52+
argv.input = [
53+
JSON.parse(
54+
fs.readFileSync(path.resolve('package.json'), 'utf8')
55+
).main || 'index.js'
56+
];
5157
} catch (e) {
52-
throw new Error('documentation was given no files and was not run in a module directory');
58+
throw new Error(
59+
'documentation was given no files and was not run in a module directory'
60+
);
5361
}
5462
}
5563

5664
if (argv.f === 'html' && argv.o === 'stdout') {
57-
throw new Error('The HTML output mode requires a destination directory set with -o');
65+
throw new Error(
66+
'The HTML output mode requires a destination directory set with -o'
67+
);
5868
}
5969

6070
function generator() {
61-
return documentation.build(argv.input, argv)
71+
return documentation
72+
.build(argv.input, argv)
6273
.then(comments =>
63-
documentation.formats[argv.format](comments, argv)
64-
.then(onFormatted))
74+
documentation.formats[argv.format](comments, argv).then(onFormatted))
6575
.catch(err => {
6676
/* eslint no-console: 0 */
6777
if (err instanceof Error) {
@@ -97,9 +107,12 @@ module.exports.handler = function build(argv/*: Object*/) {
97107
watcher = chokidar.watch(argv.input);
98108
watcher.on('all', debounce(generator, 300));
99109
}
100-
documentation.expandInputs(argv.input, argv).then(files =>
101-
watcher.add(files.map(data =>
102-
typeof data === 'string' ? data : data.file)));
110+
documentation
111+
.expandInputs(argv.input, argv)
112+
.then(files =>
113+
watcher.add(
114+
files.map(data => typeof data === 'string' ? data : data.file)
115+
));
103116
}
104117

105118
return generator();

lib/commands/lint.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,34 @@ module.exports.builder = {};
1919
* @returns {undefined} has side-effects
2020
* @private
2121
*/
22-
module.exports.handler = function (argv/*: Object*/) {
22+
module.exports.handler = function(argv /*: Object*/) {
2323
argv._handled = true;
2424
if (!argv.input.length) {
2525
try {
26-
argv.input = [JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')).main || 'index.js'];
26+
argv.input = [
27+
JSON.parse(
28+
fs.readFileSync(path.resolve('package.json'), 'utf8')
29+
).main || 'index.js'
30+
];
2731
} catch (e) {
28-
throw new Error('documentation was given no files and was not run in a module directory');
32+
throw new Error(
33+
'documentation was given no files and was not run in a module directory'
34+
);
2935
}
3036
}
31-
documentation.lint(argv.input, argv).then(lintOutput => {
32-
if (lintOutput) {
33-
console.log(lintOutput);
37+
documentation
38+
.lint(argv.input, argv)
39+
.then(lintOutput => {
40+
if (lintOutput) {
41+
console.log(lintOutput);
42+
process.exit(1);
43+
} else {
44+
process.exit(0);
45+
}
46+
})
47+
.catch(err => {
48+
/* eslint no-console: 0 */
49+
console.error(err);
3450
process.exit(1);
35-
} else {
36-
process.exit(0);
37-
}
38-
}).catch(err => {
39-
/* eslint no-console: 0 */
40-
console.error(err);
41-
process.exit(1);
42-
});
51+
});
4352
};

lib/commands/readme.js

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* @flow */
22
'use strict';
33

4-
54
var fs = require('fs');
65
var remark = require('remark');
76
var path = require('path');
@@ -26,7 +25,7 @@ module.exports.builder = {
2625
describe: 'The markdown file into which to inject documentation',
2726
default: 'README.md'
2827
},
29-
'section': {
28+
section: {
3029
alias: 's',
3130
describe: 'The section heading after which to inject generated documentation',
3231
required: true
@@ -37,7 +36,7 @@ module.exports.builder = {
3736
' just check if its contents match, exiting nonzero if not.',
3837
default: false
3938
},
40-
'quiet': {
39+
quiet: {
4140
alias: 'q',
4241
describe: 'Quiet mode: do not print messages or README diff to stdout.',
4342
default: false
@@ -52,52 +51,66 @@ function noop() {}
5251
* @param {Object} argv args from the CLI option parser
5352
* @return {undefined} has the side-effect of writing a file or printing to stdout
5453
*/
55-
module.exports.handler = function readme(argv/*: Object*/) {
54+
module.exports.handler = function readme(argv /*: Object*/) {
5655
argv._handled = true;
5756

5857
if (!argv.input.length) {
5958
try {
60-
argv.input = [JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')).main || 'index.js'];
59+
argv.input = [
60+
JSON.parse(
61+
fs.readFileSync(path.resolve('package.json'), 'utf8')
62+
).main || 'index.js'
63+
];
6164
} catch (e) {
62-
throw new Error('documentation was given no files and was not run in a module directory');
65+
throw new Error(
66+
'documentation was given no files and was not run in a module directory'
67+
);
6368
}
6469
}
6570

6671
argv.format = 'remark';
6772
/* eslint no-console: 0 */
68-
var log = argv.q ? noop : console.log.bind(console, '[documentation-readme] ');
73+
var log = argv.q
74+
? noop
75+
: console.log.bind(console, '[documentation-readme] ');
6976

7077
var readmeContent = fs.readFileSync(argv.readmeFile, 'utf8');
7178

72-
documentation.build(argv.input, argv)
73-
.then(comments =>
74-
documentation.formats.remark(comments, argv))
75-
.then(docsAst => remark().use(plugin, {
76-
section: argv.section,
77-
toInject: JSON.parse(docsAst)
78-
}).process(readmeContent))
79-
.then(file => {
80-
var diffOutput = disparity.unified(readmeContent, file.contents, {
81-
paths: [argv.readmeFile, argv.readmeFile]
82-
});
83-
if (!diffOutput.length) {
84-
log(`${argv.readmeFile} is up to date.`);
85-
process.exit(0);
86-
}
79+
documentation
80+
.build(argv.input, argv)
81+
.then(comments => documentation.formats.remark(comments, argv))
82+
.then(docsAst =>
83+
remark()
84+
.use(plugin, {
85+
section: argv.section,
86+
toInject: JSON.parse(docsAst)
87+
})
88+
.process(readmeContent))
89+
.then(file => {
90+
var diffOutput = disparity.unified(readmeContent, file.contents, {
91+
paths: [argv.readmeFile, argv.readmeFile]
92+
});
93+
if (!diffOutput.length) {
94+
log(`${argv.readmeFile} is up to date.`);
95+
process.exit(0);
96+
}
8797

88-
if (argv.d) {
89-
log(chalk.bold(`${argv.readmeFile} needs the following updates:`), `\n${diffOutput}`);
90-
process.exit(1);
91-
} else {
92-
log(chalk.bold(`Updating ${argv.readmeFile}`), `\n${diffOutput}`);
93-
}
98+
if (argv.d) {
99+
log(
100+
chalk.bold(`${argv.readmeFile} needs the following updates:`),
101+
`\n${diffOutput}`
102+
);
103+
process.exit(1);
104+
} else {
105+
log(chalk.bold(`Updating ${argv.readmeFile}`), `\n${diffOutput}`);
106+
}
94107

95-
fs.writeFileSync(argv.readmeFile, file.contents);
96-
})
97-
.catch(err => {
98-
console.error(err);
99-
process.exit(1);
100-
});
108+
fs.writeFileSync(argv.readmeFile, file.contents);
109+
})
110+
.catch(err => {
111+
console.error(err);
112+
process.exit(1);
113+
});
101114
};
102115

103116
// wrap the inject utility as an remark plugin

lib/commands/serve.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module.exports.builder = extend(
2929
type: 'number',
3030
default: 4001
3131
}
32-
});
32+
}
33+
);
3334

3435
/**
3536
* Wrap the documentation build command along with a server, making it possible
@@ -38,21 +39,27 @@ module.exports.builder = extend(
3839
* @param {Object} argv cli input
3940
* @returns {undefined} has side effects
4041
*/
41-
module.exports.handler = function serve(argv/*: Object*/) {
42+
module.exports.handler = function serve(argv /*: Object*/) {
4243
argv._handled = true;
4344

4445
if (!argv.input.length) {
4546
try {
46-
argv.input = [JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')).main || 'index.js'];
47+
argv.input = [
48+
JSON.parse(
49+
fs.readFileSync(path.resolve('package.json'), 'utf8')
50+
).main || 'index.js'
51+
];
4752
} catch (e) {
48-
throw new Error('documentation was given no files and was not run in a module directory');
53+
throw new Error(
54+
'documentation was given no files and was not run in a module directory'
55+
);
4956
}
5057
}
5158

5259
var server = new Server(argv.port);
5360
var watcher;
5461

55-
server.on('listening', function () {
62+
server.on('listening', function() {
5663
process.stdout.write(`documentation.js serving on port ${argv.port}\n`);
5764
});
5865

@@ -62,28 +69,32 @@ module.exports.handler = function serve(argv/*: Object*/) {
6269
watcher.on('all', debounce(updateServer, 300));
6370
}
6471

65-
documentation.expandInputs(argv.input, argv)
72+
documentation
73+
.expandInputs(argv.input, argv)
6674
.then(files => {
67-
watcher.add(files.map(data =>
68-
typeof data === 'string' ? data : data.file));
69-
}).catch(err => {
75+
watcher.add(
76+
files.map(data => typeof data === 'string' ? data : data.file)
77+
);
78+
})
79+
.catch(err => {
7080
/* eslint no-console: 0 */
7181
return server.setFiles([errorPage(err)]).start();
7282
});
7383
}
7484

7585
function updateServer() {
76-
documentation.build(argv.input, argv)
77-
.then(comments =>
78-
documentation.formats.html(comments, argv))
79-
.then(files => {
80-
if (argv.watch) {
81-
updateWatcher();
82-
}
83-
server.setFiles(files).start();
84-
}).catch(err => {
85-
return server.setFiles([errorPage(err)]).start();
86-
});
86+
documentation
87+
.build(argv.input, argv)
88+
.then(comments => documentation.formats.html(comments, argv))
89+
.then(files => {
90+
if (argv.watch) {
91+
updateWatcher();
92+
}
93+
server.setFiles(files).start();
94+
})
95+
.catch(err => {
96+
return server.setFiles([errorPage(err)]).start();
97+
});
8798
}
8899

89100
updateServer();

0 commit comments

Comments
 (0)