Permalink
Browse files

Simplify exit workflow

  • Loading branch information...
Kikobeats committed May 22, 2018
1 parent 850299e commit f4c25e1869fc557d43b8798d8071de90d4e22152
Showing with 77 additions and 49 deletions.
  1. +65 −0 bin/cli/build.js
  2. +0 −28 bin/cli/create-build.js
  3. +7 −18 bin/cli/index.js
  4. +5 −2 bin/view/index.js
  5. +0 −1 package.json
@@ -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

This file was deleted.

Oops, something went wrong.
@@ -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()
@@ -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)
})
@@ -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 })
}
})()
@@ -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) => {
@@ -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)
})
}
@@ -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"

0 comments on commit f4c25e1

Please sign in to comment.