Skip to content

Commit

Permalink
Simplify exit workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 22, 2018
1 parent 850299e commit f4c25e1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 49 deletions.
65 changes: 65 additions & 0 deletions bin/cli/build.js
@@ -0,0 +1,65 @@
'use strict'

const Build = require('github-build')

const { repo, sha } = require('ci-env')

const MESSAGE = {
START: 'Checking URLs availability...',
PASS: 'Yours links are fine',
FAIL: 'Something is wrong in your links',
ERROR: 'Uh, something unexpected happened'
}

const EXIT_CODE_METHOD_MAPPER = {
0: 'pass',
1: 'fail'
}

const token =
process.env.github_token ||
process.env.GITHUB_TOKEN ||
process.env.urlint_github_token ||
process.env.URLINT_GITHUB_TOKEN

const url = 'https://foobar.com'
const meta = {
repo,
sha,
token,
label: 'urlint',
description: MESSAGE.START,
url
}

const noopBuild = {
start: () => Promise.resolve(),
pass: () => Promise.resolve(),
fail: () => Promise.resolve(),
error: () => Promise.resolve()
}

const handleError = err => {
const message = `Could not add github status.${err.status}: ${
err.error.message
}`
console.error(message)
}

const createBuild = build => ({
pass: () => build.pass(MESSAGE.PASS).catch(handleError),
fail: () => build.fail(MESSAGE.FAIL).catch(handleError),
error: () => build.error(MESSAGE.ERROR).catch(handleError),
start: () => build.start(MESSAGE.START).catch(handleError)
})

const createExit = build => async ({ buildCode = 0, exitCode = buildCode }) => {
const method = EXIT_CODE_METHOD_MAPPER[buildCode] || 'error'
await build[method]()
process.exit(exitCode)
}

const build = createBuild(token ? new Build(meta) : noopBuild)
build.exit = createExit(build)

module.exports = build
28 changes: 0 additions & 28 deletions bin/cli/create-build.js

This file was deleted.

25 changes: 7 additions & 18 deletions bin/cli/index.js
Expand Up @@ -3,16 +3,15 @@
'use strict'

const { size, concat, isEmpty } = require('lodash')
const onExit = require('signal-exit')
const urlint = require('urlint')
const isCI = require('is-ci')

const extractUrls = require('./extract-urls')
const renderError = require('./render-error')
const createBuild = require('./create-build')
const pkg = require('../../package.json')
const getError = require('./get-error')
const getUrl = require('./get-url')
const build = require('./build')
const view = require('../view')

require('update-notifier')({ pkg }).notify()
Expand Down Expand Up @@ -67,24 +66,14 @@ const cli = require('meow')(require('./help'), {
}
}
})

if (isEmpty(cli.input)) {
cli.showHelp()
process.exit()
}

;(async () => {
try {
const url = await getUrl(cli)
const build = createBuild()

onExit(async (code, signal) => {
console.log('exiting with code', code)
if (code === 0) await build.pass()
if (code === 1) await build.fail()
await build.error()
})
if (isEmpty(cli.input)) {
cli.showHelp()
await build.exit({ buildCode: 1, exitCode: 0 })
}

const url = await getUrl(cli)
const opts = Object.assign({}, cli.flags, {
whitelist: cli.flags.whitelist && concat(cli.flags.whitelist)
})
Expand All @@ -97,6 +86,6 @@ if (isEmpty(cli.input)) {
const error = getError(genericError)
const prettyError = renderError(error)
console.log(prettyError)
process.exit(1)
await build.exit({ buildCode: 1, exitCode: 1 })
}
})()
7 changes: 5 additions & 2 deletions bin/view/index.js
Expand Up @@ -4,6 +4,7 @@ const { isNil, includes, isEmpty, first, toNumber, chain } = require('lodash')
const neatLog = require('neat-log')

const { SUCCESS_STATUS_CODES } = require('./constant')
const build = require('../cli/build')
const render = require('./render')

const setState = (state, data) => {
Expand Down Expand Up @@ -67,9 +68,11 @@ module.exports = ({ total, emitter, quiet, verbose, logspeed, ...opts }) => {
state.exitCode = isEmpty(errorCodes) ? 0 : 1
})

setInterval(() => {
setInterval(async () => {
bus.emit('render')
if (!isNil(state.exitCode)) process.exit(state.exitCode)
if (!isNil(state.exitCode)) {
await build.exit({ buildCode: state.exitCode })
}
}, logspeed)
})
}
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -49,7 +49,6 @@
"pretty-error": "~2.1.1",
"pretty-ms": "~3.1.0",
"reachable-url": "~1.0.1",
"signal-exit": "~3.0.2",
"update-notifier": "~2.5.0",
"urlint": "~1.2.1",
"xml-urls": "~1.0.1"
Expand Down

0 comments on commit f4c25e1

Please sign in to comment.