Skip to content

style(prettier): Use prettier for code formatting #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions lib/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* @flow */
'use strict';


var streamArray = require('stream-array'),
sharedOptions = require('./shared_options'),
path = require('path'),
Expand All @@ -21,17 +20,20 @@ module.exports.describe = 'build documentation';
* @returns {Object} yargs with options
* @private
*/
module.exports.builder = extend({},
module.exports.builder = extend(
{},
sharedOptions.sharedOutputOptions,
sharedOptions.sharedInputOptions, {
sharedOptions.sharedInputOptions,
{
example: 'documentation build foo.js -f md > API.md',
output: {
describe: 'output location. omit for stdout, otherwise is a filename ' +
'for single-file outputs and a directory name for multi-file outputs like html',
'for single-file outputs and a directory name for multi-file outputs like html',
default: 'stdout',
alias: 'o'
}
});
}
);

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

if (!argv.input.length) {
try {
argv.input = [JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')).main || 'index.js'];
argv.input = [
JSON.parse(
fs.readFileSync(path.resolve('package.json'), 'utf8')
).main || 'index.js'
];
} catch (e) {
throw new Error('documentation was given no files and was not run in a module directory');
throw new Error(
'documentation was given no files and was not run in a module directory'
);
}
}

if (argv.f === 'html' && argv.o === 'stdout') {
throw new Error('The HTML output mode requires a destination directory set with -o');
throw new Error(
'The HTML output mode requires a destination directory set with -o'
);
}

function generator() {
return documentation.build(argv.input, argv)
return documentation
.build(argv.input, argv)
.then(comments =>
documentation.formats[argv.format](comments, argv)
.then(onFormatted))
documentation.formats[argv.format](comments, argv).then(onFormatted))
.catch(err => {
/* eslint no-console: 0 */
if (err instanceof Error) {
Expand Down Expand Up @@ -97,9 +107,12 @@ module.exports.handler = function build(argv/*: Object*/) {
watcher = chokidar.watch(argv.input);
watcher.on('all', debounce(generator, 300));
}
documentation.expandInputs(argv.input, argv).then(files =>
watcher.add(files.map(data =>
typeof data === 'string' ? data : data.file)));
documentation
.expandInputs(argv.input, argv)
.then(files =>
watcher.add(
files.map(data => typeof data === 'string' ? data : data.file)
));
}

return generator();
Expand Down
37 changes: 23 additions & 14 deletions lib/commands/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,34 @@ module.exports.builder = {};
* @returns {undefined} has side-effects
* @private
*/
module.exports.handler = function (argv/*: Object*/) {
module.exports.handler = function(argv /*: Object*/) {
argv._handled = true;
if (!argv.input.length) {
try {
argv.input = [JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')).main || 'index.js'];
argv.input = [
JSON.parse(
fs.readFileSync(path.resolve('package.json'), 'utf8')
).main || 'index.js'
];
} catch (e) {
throw new Error('documentation was given no files and was not run in a module directory');
throw new Error(
'documentation was given no files and was not run in a module directory'
);
}
}
documentation.lint(argv.input, argv).then(lintOutput => {
if (lintOutput) {
console.log(lintOutput);
documentation
.lint(argv.input, argv)
.then(lintOutput => {
if (lintOutput) {
console.log(lintOutput);
process.exit(1);
} else {
process.exit(0);
}
})
.catch(err => {
/* eslint no-console: 0 */
console.error(err);
process.exit(1);
} else {
process.exit(0);
}
}).catch(err => {
/* eslint no-console: 0 */
console.error(err);
process.exit(1);
});
});
};
81 changes: 47 additions & 34 deletions lib/commands/readme.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* @flow */
'use strict';


var fs = require('fs');
var remark = require('remark');
var path = require('path');
Expand All @@ -26,7 +25,7 @@ module.exports.builder = {
describe: 'The markdown file into which to inject documentation',
default: 'README.md'
},
'section': {
section: {
alias: 's',
describe: 'The section heading after which to inject generated documentation',
required: true
Expand All @@ -37,7 +36,7 @@ module.exports.builder = {
' just check if its contents match, exiting nonzero if not.',
default: false
},
'quiet': {
quiet: {
alias: 'q',
describe: 'Quiet mode: do not print messages or README diff to stdout.',
default: false
Expand All @@ -52,52 +51,66 @@ function noop() {}
* @param {Object} argv args from the CLI option parser
* @return {undefined} has the side-effect of writing a file or printing to stdout
*/
module.exports.handler = function readme(argv/*: Object*/) {
module.exports.handler = function readme(argv /*: Object*/) {
argv._handled = true;

if (!argv.input.length) {
try {
argv.input = [JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')).main || 'index.js'];
argv.input = [
JSON.parse(
fs.readFileSync(path.resolve('package.json'), 'utf8')
).main || 'index.js'
];
} catch (e) {
throw new Error('documentation was given no files and was not run in a module directory');
throw new Error(
'documentation was given no files and was not run in a module directory'
);
}
}

argv.format = 'remark';
/* eslint no-console: 0 */
var log = argv.q ? noop : console.log.bind(console, '[documentation-readme] ');
var log = argv.q
? noop
: console.log.bind(console, '[documentation-readme] ');

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

documentation.build(argv.input, argv)
.then(comments =>
documentation.formats.remark(comments, argv))
.then(docsAst => remark().use(plugin, {
section: argv.section,
toInject: JSON.parse(docsAst)
}).process(readmeContent))
.then(file => {
var diffOutput = disparity.unified(readmeContent, file.contents, {
paths: [argv.readmeFile, argv.readmeFile]
});
if (!diffOutput.length) {
log(`${argv.readmeFile} is up to date.`);
process.exit(0);
}
documentation
.build(argv.input, argv)
.then(comments => documentation.formats.remark(comments, argv))
.then(docsAst =>
remark()
.use(plugin, {
section: argv.section,
toInject: JSON.parse(docsAst)
})
.process(readmeContent))
.then(file => {
var diffOutput = disparity.unified(readmeContent, file.contents, {
paths: [argv.readmeFile, argv.readmeFile]
});
if (!diffOutput.length) {
log(`${argv.readmeFile} is up to date.`);
process.exit(0);
}

if (argv.d) {
log(chalk.bold(`${argv.readmeFile} needs the following updates:`), `\n${diffOutput}`);
process.exit(1);
} else {
log(chalk.bold(`Updating ${argv.readmeFile}`), `\n${diffOutput}`);
}
if (argv.d) {
log(
chalk.bold(`${argv.readmeFile} needs the following updates:`),
`\n${diffOutput}`
);
process.exit(1);
} else {
log(chalk.bold(`Updating ${argv.readmeFile}`), `\n${diffOutput}`);
}

fs.writeFileSync(argv.readmeFile, file.contents);
})
.catch(err => {
console.error(err);
process.exit(1);
});
fs.writeFileSync(argv.readmeFile, file.contents);
})
.catch(err => {
console.error(err);
process.exit(1);
});
};

// wrap the inject utility as an remark plugin
Expand Down
51 changes: 31 additions & 20 deletions lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports.builder = extend(
type: 'number',
default: 4001
}
});
}
);

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

if (!argv.input.length) {
try {
argv.input = [JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8')).main || 'index.js'];
argv.input = [
JSON.parse(
fs.readFileSync(path.resolve('package.json'), 'utf8')
).main || 'index.js'
];
} catch (e) {
throw new Error('documentation was given no files and was not run in a module directory');
throw new Error(
'documentation was given no files and was not run in a module directory'
);
}
}

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

server.on('listening', function () {
server.on('listening', function() {
process.stdout.write(`documentation.js serving on port ${argv.port}\n`);
});

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

documentation.expandInputs(argv.input, argv)
documentation
.expandInputs(argv.input, argv)
.then(files => {
watcher.add(files.map(data =>
typeof data === 'string' ? data : data.file));
}).catch(err => {
watcher.add(
files.map(data => typeof data === 'string' ? data : data.file)
);
})
.catch(err => {
/* eslint no-console: 0 */
return server.setFiles([errorPage(err)]).start();
});
}

function updateServer() {
documentation.build(argv.input, argv)
.then(comments =>
documentation.formats.html(comments, argv))
.then(files => {
if (argv.watch) {
updateWatcher();
}
server.setFiles(files).start();
}).catch(err => {
return server.setFiles([errorPage(err)]).start();
});
documentation
.build(argv.input, argv)
.then(comments => documentation.formats.html(comments, argv))
.then(files => {
if (argv.watch) {
updateWatcher();
}
server.setFiles(files).start();
})
.catch(err => {
return server.setFiles([errorPage(err)]).start();
});
}

updateServer();
Expand Down
Loading