Skip to content

Commit

Permalink
Refactor comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 13, 2019
1 parent 7f911be commit f65e28e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 49 deletions.
32 changes: 15 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ var options = require('./options')

module.exports = start

/* No-op. */
var noop = Function.prototype

/* Fake TTY stream. */
// Fake TTY stream.
var ttyStream = new stream.Readable()
ttyStream.isTTY = true

/* Exit, lazily, with the correct exit status code. */
// Exit, lazily, with the correct exit status code.
var exitStatus = 0

process.on('exit', onexit)

/* Handle uncaught errors, such as from unexpected async
* behaviour. */
// Handle uncaught errors, such as from unexpected async behaviour.
process.on('uncaughtException', fail)

/* Start the CLI. */
// Start the CLI.
function start(cliConfig) {
var config
var output
Expand Down Expand Up @@ -60,22 +58,22 @@ function start(cliConfig) {
return
}

/* Modify `config` for watching. */
// Modify `config` for watching.
if (config.watch) {
output = config.output

/* Do not read from stdin(4). */
// Do not read from stdin(4).
config.streamIn = ttyStream

/* Do not write to stdout(4). */
// Do not write to stdout(4).
config.out = false

process.stderr.write(
chalk.bold('Watching...') + ' (press CTRL+C to exit)\n',
noop
)

/* Prevent infinite loop if set to regeneration. */
// Prevent infinite loop if set to regeneration.
if (output === true) {
config.output = false

Expand All @@ -86,10 +84,10 @@ function start(cliConfig) {
}
}

/* Initial run */
// Initial run.
engine(config, done)

/* Handle complete run. */
// Handle complete run.
function done(err, code, context) {
if (err) {
clean()
Expand All @@ -103,15 +101,15 @@ function start(cliConfig) {
}
}

/* Clean the watcher. */
// Clean the watcher.
function clean() {
if (watcher) {
watcher.close()
watcher = null
}
}

/* Subscribe a chokidar watcher to all processed files. */
// Subscribe a chokidar watcher to all processed files.
function subscribe(context) {
watcher = chokidar
.watch(context.fileSet.origins, {cwd: config.cwd, ignoreInitial: true})
Expand All @@ -127,12 +125,12 @@ function start(cliConfig) {
}

function onsigint() {
/* Hide the `^C` in terminal. */
// Hide the `^C` in terminal.
process.stderr.write('\n', noop)

clean()

/* Do another process if `output` specified regeneration. */
// Do another process if `output` specified regeneration.
if (output === true) {
config.output = output
config.watch = false
Expand All @@ -142,7 +140,7 @@ function start(cliConfig) {
}
}

/* Print an error, optionally with stack. */
// Print an error, optionally with stack.
function fail(err, pretty) {
var message =
(pretty ? String(err).trim() : err.stack) ||
Expand Down
39 changes: 19 additions & 20 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var schema = require('./schema')

module.exports = options

/* Schema for `minimist`. */
// Schema for `minimist`.
var minischema = {
unknown: handleUnknownArgument,
default: {},
Expand All @@ -20,7 +20,7 @@ var minischema = {

schema.forEach(addEach)

/* Parse CLI options. */
// Parse CLI options.
function options(flags, configuration) {
var extension = configuration.extensions[0]
var name = configuration.name
Expand Down Expand Up @@ -55,7 +55,7 @@ function options(flags, configuration) {

return {
helpMessage: help,
/* “hidden” feature, makes testing easier. */
// “hidden” feature, makes testing easier.
cwd: configuration.cwd,
processor: configuration.processor,
help: config.help,
Expand Down Expand Up @@ -102,12 +102,12 @@ function addEach(option) {
}
}

/* Parse `extensions`. */
// Parse `extensions`.
function extensions(value) {
return flatten(normalize(value).map(splitList))
}

/* Parse `plugins`. */
// Parse `plugins`.
function plugins(value) {
var result = {}

Expand All @@ -120,7 +120,7 @@ function plugins(value) {
return result
}

/* Parse `reporter`: only one is accepted. */
// Parse `reporter`: only one is accepted.
function reporter(value) {
var all = normalize(value)
.map(splitOptions)
Expand All @@ -131,7 +131,7 @@ function reporter(value) {
return all[all.length - 1] || []
}

/* Parse `settings`. */
// Parse `settings`.
function settings(value) {
var cache = {}

Expand All @@ -142,15 +142,15 @@ function settings(value) {
return cache
}

/* Parse configuration. */
// Parse configuration.
function parseConfig(flags, cache) {
var flag
var message

try {
flags = toCamelCase(parseJSON(flags))
} catch (error) {
/* Fix position */
// Fix position
message = error.message.replace(/at(?= position)/, 'around')

throw fault('Cannot parse `%s` as JSON: %s', flags, message)
Expand All @@ -163,19 +163,19 @@ function parseConfig(flags, cache) {
return cache
}

/* Handle an unknown flag. */
// Handle an unknown flag.
function handleUnknownArgument(flag) {
/* Glob. */
// Glob.
if (flag.charAt(0) !== '-') {
return
}

/* Long options. Always unknown. */
// Long options, always unknown.
if (flag.charAt(1) === '-') {
throw fault('Unknown option `%s`, expected:\n%s', flag, inspectAll(schema))
}

/* Short options. Can be grouped. */
// Short options, can be grouped.
flag
.slice(1)
.split('')
Expand Down Expand Up @@ -206,12 +206,12 @@ function handleUnknownArgument(flag) {
}
}

/* Inspect all `options`. */
// Inspect all `options`.
function inspectAll(options) {
return table(options.map(inspect))
}

/* Inspect one `option`. */
// Inspect one `option`.
function inspect(option) {
var description = option.description
var long = option.long
Expand All @@ -229,7 +229,7 @@ function inspect(option) {
]
}

/* Normalize `value`. */
// Normalize `value`.
function normalize(value) {
if (!value) {
return []
Expand All @@ -242,7 +242,7 @@ function normalize(value) {
return flatten(value.map(normalize))
}

/* Flatten `values`. */
// Flatten `values`.
function flatten(values) {
return [].concat.apply([], values)
}
Expand All @@ -255,8 +255,7 @@ function splitList(value) {
return value.split(',')
}

/* Transform the keys on an object to camel-case,
* recursivly. */
// Transform the keys on an object to camel-case, recursivly.
function toCamelCase(object) {
var result = {}
var value
Expand All @@ -275,7 +274,7 @@ function toCamelCase(object) {
return result
}

/* Parse a (lazy?) JSON config. */
// Parse a (lazy?) JSON config.
function parseJSON(value) {
return json5.parse('{' + value + '}')
}
10 changes: 2 additions & 8 deletions test/fixtures/example/cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#!/usr/bin/env node
'use strict'

var extend = require('xtend')
var xtend = require('xtend')
var start = require('../../..')
var config = require('../config')
var processor = require('../processor')

start(
extend(config, {
/* Hidden feature, for tests. */
cwd: __dirname,
processor: processor
})
)
start(xtend(config, {cwd: __dirname, processor: processor}))
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ test('unified-args', function(t) {

st.plan(1)

/* Should be quoted. */
// Should be quoted.
execa(bin, ['.', flag, 'foo:bar']).then(st.fail, onfail)

function onfail(res) {
Expand All @@ -288,7 +288,7 @@ test('unified-args', function(t) {
execa(bin, ['one.txt', flag, '"foo-bar":"baz"']).then(onsuccess, st.fail)

function onsuccess(res) {
/* Parser and Compiler both log stringified settings. */
// Parser and Compiler both log stringified settings.
st.deepEqual(
[res.stdout, strip(res.stderr)],
['{"fooBar":"baz"}\none', 'one.txt: no issues found'],
Expand Down Expand Up @@ -325,7 +325,7 @@ test('unified-args', function(t) {
execa(bin, ['one.txt', flag, './plugin']).then(onsuccess, st.fail)

function onsuccess(res) {
/* Attacher logs options, which are `undefined`. */
// Attacher logs options, which are `undefined`.
st.deepEqual(
[res.stdout, strip(res.stderr)],
['undefined\none', 'one.txt: no issues found'],
Expand All @@ -340,7 +340,7 @@ test('unified-args', function(t) {

st.plan(1)

/* Should be quoted. */
// Should be quoted.
execa(bin, ['.', flag, './plugin=foo:bar']).then(st.fail, onfail)

function onfail(res) {
Expand Down

0 comments on commit f65e28e

Please sign in to comment.