Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 15, 2021
1 parent f2eaa2e commit f60403c
Show file tree
Hide file tree
Showing 19 changed files with 654 additions and 499 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage/
node_modules/
.DS_Store
*.d.ts
*.log
yarn.lock
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/**
* @typedef {import('./lib/index.js').Options} Options
*/

export {args} from './lib/index.js'
68 changes: 48 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/**
* @typedef {import('unified-engine').Options} EngineOptions
* @typedef {import('unified-engine/lib/index.js').Context} EngineContext
* @typedef {import('unified-engine').Callback} EngineCallback
* @typedef {import('./options.js').Options} Options
*/

import stream from 'stream'
import chalk from 'chalk'
import chokidar from 'chokidar'
import {engine} from 'unified-engine'
import {options} from './options.js'

const noop = Function.prototype

// Fake TTY stream.
const ttyStream = new stream.Readable()
ttyStream.isTTY = true
const ttyStream = Object.assign(new stream.Readable(), {isTTY: true})

// Exit, lazily, with the correct exit status code.
let exitStatus = 0
Expand All @@ -18,13 +22,21 @@ process.on('exit', onexit)
// Handle uncaught errors, such as from unexpected async behaviour.
process.on('uncaughtException', fail)

// Start the CLI.
/**
* Start the CLI.
*
* @param {Options} cliConfig
*/
export function args(cliConfig) {
/** @type {EngineOptions & {help: boolean, helpMessage: string, watch: boolean, version: boolean}} */
let config
let output
/** @type {chokidar.FSWatcher|undefined} */
let watcher
/** @type {boolean|string|undefined} */
let output

try {
// @ts-expect-error: Close enough.
config = options(process.argv.slice(2), cliConfig)
} catch (error) {
return fail(error, true)
Expand Down Expand Up @@ -83,15 +95,19 @@ export function args(cliConfig) {
// Initial run.
engine(config, done)

// Handle complete run.
/**
* Handle complete run.
*
* @type {EngineCallback}
*/
function done(error, code, context) {
if (error) {
clean()
fail(error)
} else {
exitStatus = code
exitStatus = code || 0

if (config.watch && !watcher) {
if (config.watch && !watcher && context) {
subscribe(context)
}
}
Expand All @@ -101,25 +117,30 @@ export function args(cliConfig) {
function clean() {
if (watcher) {
watcher.close()
watcher = null
watcher = undefined
}
}

// Subscribe a chokidar watcher to all processed files.
/**
* Subscribe a chokidar watcher to all processed files.
*
* @param {EngineContext} context
*/
function subscribe(context) {
watcher = chokidar
// @ts-expect-error: `fileSet` is available.
.watch(context.fileSet.origins, {cwd: config.cwd, ignoreInitial: true})
.on('error', done)
.on('change', onchange)
.on('change', (filePath) => {
config.files = [filePath]
engine(config, done)
})

process.on('SIGINT', onsigint)

function onchange(filePath) {
config.files = [filePath]

engine(config, done)
}

/**
* Handle a SIGINT.
*/
function onsigint() {
// Hide the `^C` in terminal.
process.stderr.write('\n', noop)
Expand All @@ -136,11 +157,16 @@ export function args(cliConfig) {
}
}

// Print an error, optionally with stack.
/**
* Print an error, optionally with stack.
*
* @param {Error} error
* @param {boolean} [pretty=false]
*/
function fail(error, pretty) {
// Old versions of Node
/* c8 ignore next 1 */
const message = (pretty ? String(error).trim() : error.stack) || error
const message = String((pretty ? error : error.stack) || error)

exitStatus = 1

Expand All @@ -152,3 +178,5 @@ function onexit() {
process.exit(exitStatus)
/* eslint-enable unicorn/no-process-exit */
}

function noop() {}

0 comments on commit f60403c

Please sign in to comment.