bigint-crypto-utils/build/bin/mocha-ts.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-03-25 12:40:04 +00:00
#! /usr/bin/env node
2022-08-01 17:10:00 +00:00
import { join, resolve } from 'path'
import { fork } from 'child_process'
import minimatch from 'minimatch'
import glob from 'glob'
import { fileURLToPath } from 'url'
const { sync } = glob
const __dirname = resolve(fileURLToPath(import.meta.url), '../')
const rootDir = join(__dirname, '../..')
2021-03-25 12:40:04 +00:00
const mochaTsRelativeDir = '.mocha-ts'
// First let us prepare the args to pass to mocha.
// ts.files will be replaced by their js-transpiled counterparts
// a watch file to our semaphore will be added
const processedArgs = processArgs(process.argv.slice(2))
// Now we can run a script and invoke a callback when complete, e.g.
2022-08-01 17:10:00 +00:00
runScript(join(rootDir, 'node_modules/mocha/bin/mocha'), processedArgs)
2021-03-25 12:40:04 +00:00
function processArgs (args) {
args = process.argv.slice(2).map(arg => {
2022-08-01 02:19:48 +00:00
// Let us first remove surrounding quotes in string (it gives issues in windows)
arg = arg.replace(/^['"]/, '').replace(/['"]$/, '')
2022-08-01 17:10:00 +00:00
const filenames = sync(arg, { cwd: rootDir, matchBase: true })
2021-03-25 12:40:04 +00:00
if (filenames.length > 0) {
return filenames.map(file => {
const isTsTestFile = minimatch(file, '{test/**/*.ts,src/**/*.spec.ts}', { matchBase: true })
if (isTsTestFile) {
return `${mochaTsRelativeDir}/${file.slice(0, -3)}.js`
}
return file
})
}
return arg
})
const processedArgs = []
let addSemaphore = false
let semaphoreAdded = false
for (const arg of args) {
if (Array.isArray(arg)) {
processedArgs.push(...arg)
} else {
processedArgs.push(arg)
if (arg === '--watch' || arg === '-w') {
addSemaphore = true
} else if (arg === '--watch-files') {
processedArgs.push(`${mochaTsRelativeDir}/semaphore`)
semaphoreAdded = true
}
}
}
if (addSemaphore === true || semaphoreAdded === false) {
processedArgs.push('--watch-files')
processedArgs.push(`${mochaTsRelativeDir}/semaphore`)
}
return processedArgs
}
function runScript (scriptPath, args) {
2022-08-01 17:10:00 +00:00
const mochaCmd = fork(scriptPath, args, {
2021-03-25 12:40:04 +00:00
cwd: rootDir
})
mochaCmd.on('error', (error) => {
throw error
})
// execute the callback once the process has finished running
mochaCmd.on('exit', function (code) {
if (code !== 0) {
throw new Error('exit code ' + code)
}
})
}