2021-03-24 13:04:30 +00:00
|
|
|
const path = require('path')
|
|
|
|
const fs = require('fs')
|
|
|
|
|
|
|
|
const ts = require('typescript')
|
2022-10-03 15:35:35 +00:00
|
|
|
const json5 = require('json5')
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2022-08-01 02:04:00 +00:00
|
|
|
const Builder = require('./Builder.cjs')
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
const rootDir = path.join(__dirname, '../../../../')
|
2022-10-03 15:35:35 +00:00
|
|
|
|
|
|
|
const pkgJson = require(path.join(rootDir, 'package.json'))
|
|
|
|
|
|
|
|
const mochaTsRelativeDir = pkgJson.directories['mocha-ts']
|
2021-03-24 13:04:30 +00:00
|
|
|
const mochaTsDir = path.join(rootDir, mochaTsRelativeDir)
|
|
|
|
|
|
|
|
const formatHost = {
|
|
|
|
getCanonicalFileName: path => path,
|
|
|
|
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
|
|
getNewLine: () => ts.sys.newLine
|
|
|
|
}
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
function fileChecksum (filePath) {
|
|
|
|
return require('crypto')
|
|
|
|
.createHash('md5')
|
|
|
|
.update(fs.readFileSync(filePath, { encoding: 'utf-8' }), 'utf8')
|
|
|
|
.digest('hex')
|
|
|
|
}
|
|
|
|
|
|
|
|
function renameJsToCjs (dir, fileList = []) {
|
|
|
|
const files = fs.readdirSync(dir)
|
|
|
|
|
|
|
|
files.forEach(file => {
|
|
|
|
if (fs.statSync(path.join(dir, file)).isDirectory()) {
|
|
|
|
fileList = renameJsToCjs(path.join(dir, file), fileList)
|
|
|
|
} else {
|
|
|
|
const match = file.match(/(.*)\.js$/)
|
|
|
|
if (match !== null) {
|
|
|
|
const filename = match[1]
|
|
|
|
fs.renameSync(path.join(dir, file), path.join(dir, `${filename}.cjs`))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestsBuilder extends Builder {
|
|
|
|
constructor ({ name, configPath, tempDir }) {
|
2021-03-24 13:04:30 +00:00
|
|
|
super(path.join(tempDir, 'semaphore'), name)
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
this.tempDir = tempDir
|
|
|
|
|
2021-03-24 13:04:30 +00:00
|
|
|
if (fs.existsSync(configPath) !== true) throw new Error(`Couldn't find a tsconfig file at ${configPath}`)
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
this.tsConfigPath = configPath
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
this.testFilesChecksums = {}
|
|
|
|
}
|
2022-01-17 10:04:55 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
async start ({ testFiles = [], commonjs = false }) {
|
|
|
|
await super.start()
|
|
|
|
|
|
|
|
this.commonjs = commonjs
|
|
|
|
|
|
|
|
const tsConfig = json5.parse(fs.readFileSync(this.tsConfigPath, 'utf8'))
|
2022-01-17 10:04:55 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
if (testFiles.length > 0) {
|
|
|
|
delete tsConfig.files
|
|
|
|
tsConfig.include = ['build/typings/**/*.d.ts'].concat(testFiles)
|
|
|
|
for (let i = 0; i < testFiles.length; i++) {
|
|
|
|
this.testFilesChecksums[testFiles[i]] = fileChecksum(testFiles[i])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tsConfig.include = ['build/typings/**/*.d.ts', 'test/**/*', 'src/ts/**/*.spec.ts']
|
|
|
|
}
|
|
|
|
tsConfig.exclude = ['src/ts/**/!(.spec).ts']
|
2022-01-17 10:04:55 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
if (this.commonjs) {
|
|
|
|
tsConfig.compilerOptions.module = 'commonjs'
|
|
|
|
}
|
2022-01-17 10:04:55 +00:00
|
|
|
// "noResolve": true
|
2022-10-03 15:35:35 +00:00
|
|
|
// tsConfig.compilerOptions.noResolve = true
|
2022-01-17 10:04:55 +00:00
|
|
|
|
|
|
|
// we don't need declaration files
|
|
|
|
tsConfig.compilerOptions.declaration = false
|
|
|
|
|
|
|
|
// we need to emit files
|
|
|
|
tsConfig.compilerOptions.noEmit = false
|
|
|
|
|
|
|
|
// source mapping eases debuging
|
2022-10-03 15:35:35 +00:00
|
|
|
tsConfig.compilerOptions.inlineSourceMap = true
|
2022-01-17 10:04:55 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
tsConfig.compilerOptions.rootDir = '.'
|
2022-01-17 10:04:55 +00:00
|
|
|
|
|
|
|
// Removed typeroots (it causes issues)
|
|
|
|
tsConfig.compilerOptions.typeRoots = undefined
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
tsConfig.compilerOptions.outDir = path.isAbsolute(this.tempDir) ? path.relative(rootDir, this.tempDir) : this.tempDir
|
2022-01-17 10:04:55 +00:00
|
|
|
|
|
|
|
this.tempTsConfigPath = path.join(rootDir, '.tsconfig.json')
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
fs.writeFileSync(this.tempTsConfigPath, JSON.stringify(tsConfig, undefined, 2), { encoding: 'utf-8' })
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
const createProgram = ts.createSemanticDiagnosticsBuilderProgram
|
|
|
|
|
|
|
|
const reportDiagnostic = (diagnostic) => {
|
|
|
|
const filePath = path.relative(rootDir, diagnostic.file.fileName)
|
2022-10-03 15:35:35 +00:00
|
|
|
const tranpiledJsPath = `${path.join(this.tempDir, filePath).slice(0, -3)}.js`
|
2021-03-24 13:04:30 +00:00
|
|
|
const errorLine = diagnostic.file.text.slice(0, diagnostic.start).split(/\r\n|\r|\n/).length
|
|
|
|
if (fs.existsSync(tranpiledJsPath)) {
|
|
|
|
fs.writeFileSync(tranpiledJsPath, '', 'utf8')
|
|
|
|
}
|
|
|
|
this.emit('error', `[Error ${diagnostic.code}]`, `${filePath}:${errorLine}`, ':', ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()))
|
|
|
|
}
|
|
|
|
|
|
|
|
const reportWatchStatusChanged = (diagnostic, newLine, options, errorCount) => {
|
|
|
|
if (errorCount !== undefined) {
|
2022-10-03 15:35:35 +00:00
|
|
|
// only change semaphore if test files are modified
|
|
|
|
let updateSemaphore = false
|
|
|
|
for (let i = 0; i < testFiles.length; i++) {
|
|
|
|
const checksum = fileChecksum(testFiles[i])
|
|
|
|
if (this.testFilesChecksums[testFiles[i]] !== checksum) {
|
|
|
|
updateSemaphore = true
|
|
|
|
this.testFilesChecksums[testFiles[i]] = checksum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.commonjs) {
|
|
|
|
renameJsToCjs(mochaTsDir)
|
|
|
|
}
|
|
|
|
this.emit('ready', updateSemaphore)
|
2021-03-24 13:04:30 +00:00
|
|
|
} else {
|
|
|
|
this.emit('busy')
|
|
|
|
if (diagnostic.code === 6031) {
|
|
|
|
this.emit('message', 'transpiling your tests...')
|
|
|
|
} else if (diagnostic.code === 6032) {
|
|
|
|
this.emit('message', 'file changes detected. Transpiling your tests...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that there is another overload for `createWatchCompilerHost` that takes
|
|
|
|
// a set of root files.
|
|
|
|
this.host = ts.createWatchCompilerHost(
|
2022-01-17 10:04:55 +00:00
|
|
|
this.tempTsConfigPath,
|
|
|
|
{},
|
2021-03-24 13:04:30 +00:00
|
|
|
ts.sys,
|
|
|
|
createProgram,
|
|
|
|
reportDiagnostic,
|
|
|
|
reportWatchStatusChanged
|
|
|
|
)
|
|
|
|
|
|
|
|
// `createWatchProgram` creates an initial program, watches files, and updates
|
|
|
|
// the program over time.
|
|
|
|
this.watcher = ts.createWatchProgram(this.host)
|
2022-10-03 15:35:35 +00:00
|
|
|
this.watcher.getProgram()
|
2021-03-24 13:04:30 +00:00
|
|
|
return await this.ready()
|
|
|
|
}
|
|
|
|
|
|
|
|
async close () {
|
|
|
|
await super.close()
|
|
|
|
this.watcher.close()
|
2022-01-17 10:04:55 +00:00
|
|
|
fs.unlinkSync(this.tempTsConfigPath)
|
2021-03-24 13:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-03 15:35:35 +00:00
|
|
|
|
|
|
|
exports.TestsBuilder = TestsBuilder
|
|
|
|
exports.testBuilder = new TestsBuilder({
|
|
|
|
name: 'tsc',
|
|
|
|
configPath: path.join(rootDir, 'tsconfig.json'),
|
|
|
|
tempDir: mochaTsDir
|
|
|
|
})
|