eltro/cli.mjs

74 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-03-31 17:27:36 +00:00
#!/usr/bin/env node
// Get arguments
const [,, ...args] = process.argv
2020-04-01 15:35:33 +00:00
import e from './lib/eltro.mjs'
2020-03-31 17:27:36 +00:00
import { CLI, printError } from './lib/cli.mjs'
function PrintHelp() {
console.log('')
2020-04-01 15:35:33 +00:00
console.log('Usage: eltro <options> <files>')
2020-03-31 17:27:36 +00:00
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')
2020-03-31 17:27:36 +00:00
console.log('')
2020-04-01 15:35:33 +00:00
console.log('eltro test/mytest.mjs')
console.log('eltro dot test/*.mjs')
console.log('eltro -r dot test/**/*.test.mjs')
2020-04-01 15:46:11 +00:00
console.log('')
2020-03-31 17:27:36 +00:00
process.exit(1)
}
function showErrorAndExit(message = '', err = null, code = 1) {
console.log('')
if (message) {
console.error(`\x1b[31m${message}\x1b[0m`)
}
if (err) {
printError(err)
} else {
2020-03-31 17:27:36 +00:00
PrintHelp()
}
process.exit(code)
}
2020-03-31 17:27:36 +00:00
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() {
2023-10-31 08:41:03 +00:00
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()
2020-03-31 17:27:36 +00:00
})
.catch(function(err) { showErrorAndExit('Unknown error occured while running the tests', err) })
.then(function(stats) {
if (stats.failed > 0) {
process.exit(10)
}
2020-03-31 17:27:36 +00:00
process.exit(0)
})