From f4c25e1869fc557d43b8798d8071de90d4e22152 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Tue, 22 May 2018 13:21:32 +0200 Subject: [PATCH] Simplify exit workflow --- bin/cli/build.js | 65 +++++++++++++++++++++++++++++++++++++++++ bin/cli/create-build.js | 28 ------------------ bin/cli/index.js | 25 +++++----------- bin/view/index.js | 7 +++-- package.json | 1 - 5 files changed, 77 insertions(+), 49 deletions(-) create mode 100644 bin/cli/build.js delete mode 100644 bin/cli/create-build.js diff --git a/bin/cli/build.js b/bin/cli/build.js new file mode 100644 index 0000000..5c9159f --- /dev/null +++ b/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 diff --git a/bin/cli/create-build.js b/bin/cli/create-build.js deleted file mode 100644 index 981b77d..0000000 --- a/bin/cli/create-build.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict' - -const Build = require('github-build') - -const { repo, sha } = require('ci-env') - -const label = 'urlint' -const description = 'Checking URLs availability...' - -const token = - process.env.github_token || - process.env.GITHUB_TOKEN || - process.env.bundlesize_github_token || - process.env.BUNDLESIZE_GITHUB_TOKEN - -const meta = { repo, sha, token, label, description } - -const noopBuild = { - start: () => {}, - pass: () => {}, - fail: () => {}, - error: () => {} -} - -module.exports = () => { - console.log('token loaded?', !!token) - return !token ? noopBuild : new Build(meta) -} diff --git a/bin/cli/index.js b/bin/cli/index.js index eb389b9..089e4af 100755 --- a/bin/cli/index.js +++ b/bin/cli/index.js @@ -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 }) } })() diff --git a/bin/view/index.js b/bin/view/index.js index b77dc37..8f266cd 100644 --- a/bin/view/index.js +++ b/bin/view/index.js @@ -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) }) } diff --git a/package.json b/package.json index ba5ac90..dc05e40 100644 --- a/package.json +++ b/package.json @@ -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"