import path from 'path' ; import fs from 'fs-extra' ; import ts from 'typescript' ; import chalk from 'chalk' ; import { Package } from './workspaces' ; type CompileOption = { currentPath : string ; target ? : number ; module ? : number ; outDir ? : string ; declarationDir ? : string ; inputFile ? : string ; } ; const defaultOutDir = 'build' ; const defaultDeclarationDir = 'dist' ; const defaultInputFile = 'src/index.ts' ; const compileTypeScript = ( { currentPath , target , module , outDir = path . resolve ( currentPath , defaultOutDir ) , declarationDir = path . resolve ( currentPath , defaultDeclarationDir ) , inputFile = path . resolve ( currentPath , defaultInputFile ) , } ) => { fs . removeSync ( declarationDir ) ; fs . removeSync ( outDir ) ; console . log ( `Compiling TypeScript:` ) ; console . log ( chalk . grey ( `-> ${ outDir }` ) ) ; const tsConfig = path . resolve ( 'tsconfig.json' ) ; const json = ts . parseConfigFileTextToJson ( tsConfig , ts . sys . readFile ( tsConfig ) ! ) ; const { options } = ts . parseJsonConfigFileContent ( json . config , ts . sys , path . dirname ( tsConfig ) ) ; options . target = target || options . module ; options . module = module || options . module ; options . outDir = outDir ; options . declaration = true ; options . declarationDir = declarationDir ; const compilerHost = ts . createCompilerHost ( options , true ) ; const program = ts . createProgram ( [ inputFile ] , options , compilerHost ) ; const result = program . emit ( ) ; if ( result . emitSkipped ) { const message = result . diagnostics . map ( ( diagnostic ) => `${ ts . DiagnosticCategory [ diagnostic . category ] } ${ diagnostic . code } (${ diagnostic . file }:${ diagnostic . start }): ${ diagnostic . messageText }` ) . join ( '\n' ) ; console . log ( chalk . red ( `Failed to compile Typescript:\n\n${ message }` ) ) ; } console . log ( chalk . green ( `Succeed to compile Typescript.\n` ) ) ; } ; const getCamelCase = ( name ) => { return name . replace ( /-(\w)/g , ( _ , str ) => str . toUpperCase ( ) ) ; } ; export type Generate = ( option : { currentPath : string ; name : string ; packageJson : Package ; } ) => Promise < void > ; export const compileProject = async ( generate , packageChildPath ) => { console . log ( packageChildPath , `\nBuilding...\n` ) ; const packageJsonPath = path . resolve ( packageChildPath , 'package.json' ) ; try { const packageJson = fs . readJSONSync ( packageJsonPath ) ; if ( ! packageJson . private ) { compileTypeScript ( { currentPath : packageChildPath , target : ts . ScriptTarget . ES5 , module : ts . ModuleKind . ES2015 , outDir : path . resolve ( packageChildPath , defaultOutDir ) , } ) ; await generate ( { currentPath : packageChildPath , name : getCamelCase ( packageJson . name ) , packageJson , } ) ; } } catch ( e ) { } } ; const compile = async ( projects , generate ) => { if ( Array . isArray ( projects ) && projects . length > 0 ) { for ( const project of projects ) { if ( typeof project === 'string' ) { await compileProject ( generate , path . resolve ( project ) ) ; } } } } ; export { compileTypeScript , compile } ;