import { dirname , normalize , relative , resolve } from 'path' ; import { createWriteStream , chmodSync , statSync } from 'fs' ; import { NexeCompiler } from '../compiler' ; import { NexeTarget } from '../target' ; import { STDIN_FLAG } from '../util' ; import mkdirp = require ( 'mkdirp' ) ; param { * } compiler param { * } next export default async function cli ( compiler , next ) { await next ( ) ; const { log } = compiler , target = compiler . options . targets . shift ( ) as NexeTarget , deliverable = await compiler . compileAsync ( target ) , output = normalize ( compiler . output ! ) ; mkdirp . sync ( dirname ( output ) ) ; return new Promise ( ( res , rej ) => { const step = log . step ( 'Writing result to file' ) ; deliverable . pipe ( createWriteStream ( output ) ) . on ( 'error' , rej ) . once ( 'close' , ( e ) => { if ( e ) { rej ( e ) ; } else if ( compiler . output ) { const output = compiler . output , mode = statSync ( output ) . mode | 0o111 , inputFileLogOutput = relative ( process . cwd ( ) , resolve ( compiler . options . cwd , compiler . entrypoint || compiler . options . input ) ) , outputFileLogOutput = relative ( process . cwd ( ) , output ) ; chmodSync ( output , mode . toString ( 8 ) . slice ( - 3 ) ) ; step . log ( `Entry: '${ compiler . stdinUsed ? compiler . options . mangle ? STDIN_FLAG : '[none]' : inputFileLogOutput }' written to: ${ outputFileLogOutput }` ) ; compiler . quit ( ) ; res ( output ) ; } } ) ; } ) ; }