Skip to content

Commit 699e748

Browse files
committed
style: Use await/async instead of Promises
1 parent ff7a32d commit 699e748

File tree

7 files changed

+128
-141
lines changed

7 files changed

+128
-141
lines changed

src/commands/build.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports.builder = _.assign(
2525
{
2626
example: 'documentation build foo.js -f md > API.md',
2727
output: {
28-
describe: 'output location. omit for stdout, otherwise is a filename ' +
28+
describe:
29+
'output location. omit for stdout, otherwise is a filename ' +
2930
'for single-file outputs and a directory name for multi-file outputs like html',
3031
default: 'stdout',
3132
alias: 'o'
@@ -64,21 +65,23 @@ module.exports.handler = function build(argv: Object) {
6465
);
6566
}
6667

67-
function generator() {
68-
return documentation
69-
.build(argv.input, argv)
70-
.then(comments =>
71-
documentation.formats[argv.format](comments, argv).then(onFormatted)
72-
)
73-
.catch(err => {
74-
/* eslint no-console: 0 */
75-
if (err instanceof Error) {
76-
console.error(err.stack);
77-
} else {
78-
console.error(err);
79-
}
80-
process.exit(1);
81-
});
68+
async function generator() {
69+
try {
70+
const comments = await documentation.build(argv.input, argv);
71+
const formatted = await documentation.formats[argv.format](
72+
comments,
73+
argv
74+
);
75+
onFormatted(formatted);
76+
} catch (err) {
77+
/* eslint no-console: 0 */
78+
if (err instanceof Error) {
79+
console.error(err.stack);
80+
} else {
81+
console.error(err);
82+
}
83+
process.exit(1);
84+
}
8285
}
8386

8487
function onFormatted(output) {
@@ -100,18 +103,15 @@ module.exports.handler = function build(argv: Object) {
100103
}
101104
}
102105

103-
function updateWatcher() {
106+
async function updateWatcher() {
104107
if (!watcher) {
105108
watcher = chokidar.watch(argv.input);
106109
watcher.on('all', _.debounce(generator, 300));
107110
}
108-
documentation
109-
.expandInputs(argv.input, argv)
110-
.then(files =>
111-
watcher.add(
112-
files.map(data => (typeof data === 'string' ? data : data.file))
113-
)
114-
);
111+
const files = await documentation.expandInputs(argv.input, argv);
112+
watcher.add(
113+
files.map(data => (typeof data === 'string' ? data : data.file))
114+
);
115115
}
116116

117117
return generator();

src/commands/lint.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports.builder = {};
1818
* @returns {undefined} has side-effects
1919
* @private
2020
*/
21-
module.exports.handler = function(argv: Object) {
21+
module.exports.handler = async function(argv: Object) {
2222
argv._handled = true;
2323
if (!argv.input.length) {
2424
try {
@@ -32,19 +32,17 @@ module.exports.handler = function(argv: Object) {
3232
);
3333
}
3434
}
35-
documentation
36-
.lint(argv.input, argv)
37-
.then(lintOutput => {
38-
if (lintOutput) {
39-
console.log(lintOutput);
40-
process.exit(1);
41-
} else {
42-
process.exit(0);
43-
}
44-
})
45-
.catch(err => {
46-
/* eslint no-console: 0 */
47-
console.error(err);
35+
try {
36+
const lintOutput = await documentation.lint(argv.input, argv);
37+
if (lintOutput) {
38+
console.log(lintOutput);
4839
process.exit(1);
49-
});
40+
} else {
41+
process.exit(0);
42+
}
43+
} catch (err) {
44+
/* eslint no-console: 0 */
45+
console.error(err);
46+
process.exit(1);
47+
}
5048
};

src/commands/readme.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports.builder = {
5151
* @param {Object} argv args from the CLI option parser
5252
* @return {undefined} has the side-effect of writing a file or printing to stdout
5353
*/
54-
module.exports.handler = function readme(argv: Object) {
54+
module.exports.handler = async function readme(argv: Object) {
5555
argv._handled = true;
5656

5757
if (!argv.input.length) {
@@ -75,44 +75,39 @@ module.exports.handler = function readme(argv: Object) {
7575
}
7676
};
7777

78-
var readmeContent = fs.readFileSync(argv.readmeFile, 'utf8');
79-
80-
documentation
81-
.build(argv.input, argv)
82-
.then(comments => documentation.formats.remark(comments, argv))
83-
.then(docsAst =>
84-
remark()
85-
.use(plugin, {
86-
section: argv.section,
87-
toInject: JSON.parse(docsAst)
88-
})
89-
.process(readmeContent)
90-
)
91-
.then(file => {
92-
var diffOutput = disparity.unified(readmeContent, file.contents, {
93-
paths: [argv.readmeFile, argv.readmeFile]
94-
});
95-
if (!diffOutput.length) {
96-
log(`${argv.readmeFile} is up to date.`);
97-
process.exit(0);
98-
}
99-
100-
if (argv.d) {
101-
log(
102-
chalk.bold(`${argv.readmeFile} needs the following updates:`),
103-
`\n${diffOutput}`
104-
);
105-
process.exit(1);
106-
} else {
107-
log(chalk.bold(`Updating ${argv.readmeFile}`), `\n${diffOutput}`);
108-
}
78+
try {
79+
var readmeContent = fs.readFileSync(argv.readmeFile, 'utf8');
80+
const comments = await documentation.build(argv.input, argv);
81+
const docsAst = await documentation.formats.remark(comments, argv);
82+
const file = await remark()
83+
.use(plugin, {
84+
section: argv.section,
85+
toInject: JSON.parse(docsAst)
86+
})
87+
.process(readmeContent);
88+
var diffOutput = disparity.unified(readmeContent, file.contents, {
89+
paths: [argv.readmeFile, argv.readmeFile]
90+
});
91+
if (!diffOutput.length) {
92+
log(`${argv.readmeFile} is up to date.`);
93+
process.exit(0);
94+
}
10995

110-
fs.writeFileSync(argv.readmeFile, file.contents);
111-
})
112-
.catch(err => {
113-
console.error(err);
96+
if (argv.d) {
97+
log(
98+
chalk.bold(`${argv.readmeFile} needs the following updates:`),
99+
`\n${diffOutput}`
100+
);
114101
process.exit(1);
115-
});
102+
} else {
103+
log(chalk.bold(`Updating ${argv.readmeFile}`), `\n${diffOutput}`);
104+
}
105+
106+
fs.writeFileSync(argv.readmeFile, file.contents);
107+
} catch (err) {
108+
console.error(err);
109+
process.exit(1);
110+
}
116111
};
117112

118113
// wrap the inject utility as an remark plugin

src/commands/serve.js

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports.builder = _.assign(
3838
* @param {Object} argv cli input
3939
* @returns {undefined} has side effects
4040
*/
41-
module.exports.handler = function serve(argv: Object) {
41+
module.exports.handler = async function serve(argv: Object) {
4242
argv._handled = true;
4343

4444
if (!argv.input.length) {
@@ -54,48 +54,43 @@ module.exports.handler = function serve(argv: Object) {
5454
}
5555
}
5656

57-
getPort(argv.port).then(port => {
58-
var server = new Server(port);
59-
var watcher;
57+
const port = await getPort(argv.port);
58+
var server = new Server(port);
59+
var watcher;
6060

61-
server.on('listening', function() {
62-
process.stdout.write(`documentation.js serving on port ${port}\n`);
63-
});
61+
server.on('listening', function() {
62+
process.stdout.write(`documentation.js serving on port ${port}\n`);
63+
});
6464

65-
function updateWatcher() {
66-
if (!watcher) {
67-
watcher = chokidar.watch(argv.input);
68-
watcher.on('all', _.debounce(updateServer, 300));
69-
}
65+
async function updateWatcher() {
66+
if (!watcher) {
67+
watcher = chokidar.watch(argv.input);
68+
watcher.on('all', _.debounce(updateServer, 300));
69+
}
7070

71-
documentation
72-
.expandInputs(argv.input, argv)
73-
.then(files => {
74-
watcher.add(
75-
files.map(data => (typeof data === 'string' ? data : data.file))
76-
);
77-
})
78-
.catch(err => {
79-
/* eslint no-console: 0 */
80-
return server.setFiles([errorPage(err)]).start();
81-
});
71+
try {
72+
const files = await documentation.expandInputs(argv.input, argv);
73+
watcher.add(
74+
files.map(data => (typeof data === 'string' ? data : data.file))
75+
);
76+
} catch (err) {
77+
/* eslint no-console: 0 */
78+
return server.setFiles([errorPage(err)]).start();
8279
}
80+
}
8381

84-
function updateServer() {
85-
documentation
86-
.build(argv.input, argv)
87-
.then(comments => documentation.formats.html(comments, argv))
88-
.then(files => {
89-
if (argv.watch) {
90-
updateWatcher();
91-
}
92-
server.setFiles(files).start();
93-
})
94-
.catch(err => {
95-
return server.setFiles([errorPage(err)]).start();
96-
});
82+
async function updateServer() {
83+
try {
84+
const comments = await documentation.build(argv.input, argv);
85+
const files = await documentation.formats.html(comments, argv);
86+
if (argv.watch) {
87+
updateWatcher();
88+
}
89+
server.setFiles(files).start();
90+
} catch (err) {
91+
return server.setFiles([errorPage(err)]).start();
9792
}
93+
}
9894

99-
updateServer();
100-
});
95+
updateServer();
10196
};

src/merge_config.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ function processToc(config: DocumentationConfig, absFilePath: string) {
3131
* @returns {Promise<Object>} configuration with inferred parameters
3232
* @throws {Error} if the file cannot be read.
3333
*/
34-
function mergePackage(config: Object): Promise<Object> {
34+
async function mergePackage(config: Object): Promise<Object> {
3535
if (config.noPackage) {
3636
return Promise.resolve(config);
3737
}
38-
return (
39-
readPkgUp()
40-
.then(pkg => {
41-
['name', 'homepage', 'version'].forEach(key => {
42-
config[`project-${key}`] = config[`project-${key}`] || pkg.pkg[key];
43-
});
44-
return config;
45-
})
46-
// Allow this to fail: this inference is not required.
47-
.catch(() => config)
48-
);
38+
try {
39+
const pkg = await readPkgUp();
40+
['name', 'homepage', 'version'].forEach(key => {
41+
config[`project-${key}`] = config[`project-${key}`] || pkg.pkg[key];
42+
});
43+
} catch (_) {
44+
return config;
45+
}
46+
return config;
4947
}
5048

5149
/**
@@ -56,25 +54,24 @@ function mergePackage(config: Object): Promise<Object> {
5654
* @returns {Promise<Object>} configuration, if it can be parsed
5755
* @throws {Error} if the file cannot be read.
5856
*/
59-
function mergeConfigFile(config): Promise<Object> {
57+
async function mergeConfigFile(config): Promise<Object> {
6058
if (config && typeof config.config === 'string') {
6159
var filePath = config.config;
6260
var ext = path.extname(filePath);
6361
var absFilePath = path.resolve(process.cwd(), filePath);
64-
return pify(fs).readFile(absFilePath, 'utf8').then(rawFile => {
65-
if (ext === '.json') {
66-
return Object.assign(
67-
{},
68-
config,
69-
processToc(JSON.parse(stripComments(rawFile)), absFilePath)
70-
);
71-
}
62+
const rawFile = await pify(fs).readFile(absFilePath, 'utf8');
63+
if (ext === '.json') {
7264
return Object.assign(
7365
{},
7466
config,
75-
processToc(yaml.safeLoad(rawFile), absFilePath)
67+
processToc(JSON.parse(stripComments(rawFile)), absFilePath)
7668
);
77-
});
69+
}
70+
return Object.assign(
71+
{},
72+
config,
73+
processToc(yaml.safeLoad(rawFile), absFilePath)
74+
);
7875
}
7976

8077
return Promise.resolve(config || {});

src/output/markdown.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ var remark = require('remark'),
2323
* fs.writeFileSync('./output.md', output);
2424
* });
2525
*/
26-
function markdown(
26+
async function markdown(
2727
comments: Array<Comment>,
2828
args: Object = {}
2929
): Promise<string> {
30-
return markdownAST(comments, args).then(ast => remark().stringify(ast));
30+
const ast = await markdownAST(comments, args);
31+
return remark().stringify(ast);
3132
}
3233

3334
module.exports = markdown;

src/output/markdown_ast.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ var DEFAULT_LANGUAGE = 'javascript';
2424
* consult hljs.configure for the full list.
2525
* @returns {Promise<Object>} returns an eventual Markdown value
2626
*/
27-
function markdownAST(comments: Array<Comment>, args: Object) {
28-
return mergeConfig(args).then(config => buildMarkdownAST(comments, config));
27+
async function markdownAST(comments: Array<Comment>, args: Object) {
28+
const config = await mergeConfig(args);
29+
return buildMarkdownAST(comments, config);
2930
}
3031

3132
function buildMarkdownAST(

0 commit comments

Comments
 (0)