You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.0 KiB

import * as fse from 'fs-extra'
import * as path from 'path'
import * as mkdirp from 'mkdirp'
import * as rimraf from 'rimraf'
import * as rfdc from 'rfdc'
import { Phase } from './Phase'
import { Logger } from '../Logger'
export class PreparePhase extends Phase {
public async run(): Promise<void> {
const dir = this.config.compileDir || 'exbuild'
Logger.verb('prepare', `remove ${dir}`)
await new Promise<void>((res, rej) => {
rimraf(dir, e => {
if ( e ) {
rej(e)
} else {
res()
}
})
})
Logger.verb('prepare', `create ${dir}`)
await mkdirp(dir)
for ( const src of this.tsconfig.include ) {
Logger.verb('prepare', `copy ${src}`)
await fse.copy(src, path.join(dir, src))
}
const transformerPath = path.resolve(__dirname, '..', 'transformer.js')
Logger.verb('prepare', `copy ${transformerPath}`)
await fse.copy(transformerPath, path.join(dir, 'transformer.js'))
const tsconfig = rfdc()(this.tsconfig)
const outDir = tsconfig?.compilerOptions?.outDir || './lib'
if ( !tsconfig.compilerOptions ) {
tsconfig.compilerOptions = {}
}
if ( !tsconfig.compilerOptions.plugins ) {
tsconfig.compilerOptions.plugins = []
}
tsconfig.compilerOptions.plugins.push({
transform: './transformer.js',
import: 'identifierProgram',
})
tsconfig.compilerOptions.plugins.push({
transform: './transformer.js',
import: 'transformProgram',
transformProgram: true,
})
/* tsconfig.compilerOptions.plugins.push({
transform: './transformer.js',
import: 'transformerProgram',
after: true,
})*/
tsconfig.compilerOptions.outDir = path.join('..', outDir)
fse.writeFileSync(path.join(dir, 'tsconfig.json'), JSON.stringify(tsconfig, undefined, 4))
}
}