import {Phase} from './Phase' import {ExtolloCompileConfig} from '../types' import * as fse from 'fs-extra' import * as path from 'path' import {Logger} from '../Logger' export class NonSourcePhase extends Phase { constructor( config: ExtolloCompileConfig, tsconfig: any, protected readonly nonSourcePath: string, ) { super(config, tsconfig) } async run(): Promise { const outDir = this.tsconfig?.compilerOptions?.outDir || './lib' const source = this.nonSourcePath let dest = path.join(outDir, source) if ( this.shouldUp(source) ) { const upLevel = path.join(outDir).split(path.sep).length dest = path.join( outDir, source.split(path.sep) .slice(upLevel) .join(path.sep), ) } const destParentDir = path.join(dest, '..') Logger.verb('non-source', 'ensure', destParentDir) await fse.mkdirp(destParentDir) Logger.verb('non-source', 'copy', source, '->', dest) await fse.copy(source, dest) } public shouldUp(nonSourcePath: string): boolean { if ( Array.isArray(this.tsconfig.include) ) { for ( const sourcePath of this.tsconfig.include ) { if ( nonSourcePath.startsWith(sourcePath) ) { return true } } } return false } }