eltro/cli.mjs

79 lines
2.2 KiB
JavaScript

#!/usr/bin/env node
// Get arguments
const [,, ...args] = process.argv
import e from './lib/eltro.mjs'
import { CLI, printError } from './lib/cli.mjs'
function PrintHelp() {
console.log('')
console.log('Usage: eltro <options> <files>')
console.log('')
console.log('where <files> can either be a single file or a simple glob pattern.')
console.log('where <options> can be any of the following:')
console.log(' -r, --reporter - Specify the reporter to use.')
console.log(' Supported reporters: list, dot')
console.log(' -t, --timeout - Specify the timeout for tests in ms.')
console.log(' Default value is 2000ms')
console.log(' -w, --watch - specify which group of files to watch from package.json')
console.log(' ')
console.log(' --ignore-only - Specify to ignore any .only() tests found')
console.log('')
console.log('eltro test/mytest.mjs')
console.log('eltro dot test/*.mjs')
console.log('eltro -r dot test/**/*.test.mjs')
console.log('')
process.exit(1)
}
function showErrorAndExit(message = '', err = null, code = 1, clean = false) {
if (!clean) {
console.log('')
}
if (message) {
console.error(`\x1b[31m${message}\x1b[0m`)
}
if (err) {
printError(err, '', clean)
if (err.inner) {
return showErrorAndExit(null, err.inner, code, true)
}
} else {
PrintHelp()
}
process.exit(code)
}
const cli = new CLI(e)
cli.parseOptions(args)
.catch(function(err) { showErrorAndExit(err.message) })
.then(function() {
return cli.startWatcher()
})
.catch(function(err) { showErrorAndExit('Unknown error while starting watcher', err) })
.then(function() {
return cli.getFiles()
})
.catch(function(err) { showErrorAndExit('Unknown error while processing arguments', err) })
.then(function() {
if (!cli.files.length && cli.run === 'test') {
showErrorAndExit('No files were found with pattern', cli.targets.join(','))
}
return cli.loadFiles()
})
.catch(function(err) { showErrorAndExit('', err) })
.then(function() {
return cli.beginRun()
})
.catch(function(err) { showErrorAndExit('Unknown error occured while running the tests', err) })
.then(function(stats) {
if (stats.failed > 0) {
process.exit(10)
}
process.exit(0)
})