import { InstanceClient , HostClient } from "@scramjet/api-client" ; import { Command } from "commander" ; import { access , readdir } from "fs/promises" ; import { createReadStream , createWriteStream , constants , PathLike } from "fs" ; import { resolve } from "path" ; import { displayEntity } from "./output" ; import { c } from "tar" ; import { StringStream } from "scramjet" ; import { filter as mmfilter } from "minimatch" ; import { Readable } from "stream" ; const { F_OK , R_OK } = constants ; let hostClient ; param { Command } command returns { HostClient } export const getHostClient = ( command ) : HostClient => { if ( hostClient ) return hostClient ; hostClient = new HostClient ( command . opts ( ) . apiUrl ) ; if ( command . opts ( ) . log ) { hostClient . client . addLogger ( { ok ( result ) { const { status , statusText , url } = result ; console . error ( "Request ok:" , url , `status: ${ status } ${ statusText }` ) ; } , error ( error ) { const { code , reason : result } = error ; const { message } = result || { } ; console . error ( `Request failed with code "${ code }" status: ${ message }` ) ; } } ) ; } return hostClient ; } ; param { Command } command param { string } id returns { InstanceClient } export const getInstance = ( command , id ) => InstanceClient . from ( id , getHostClient ( command ) ) ; param { Command } command param { InstanceClient } instanceClient returns { Promise < void > } export const attachStdio = ( command , instanceClient ) => { return displayEntity ( command , Promise . all ( [ instanceClient . sendStdin ( process . stdin ) , instanceClient . getStream ( "stdout" ) . then ( out => out . data ?. pipe ( process . stdout ) ) , instanceClient . getStream ( "stderr" ) . then ( err => err . data ?. pipe ( process . stderr ) ) ] ) . then ( ( ) => undefined ) ) ; } ; param { PathLike } file returns export const getIgnoreFunction = async ( file ) => { try { await access ( file , R_OK ) ; } catch { return ( ) => true ; } const rules = await StringStream . from ( createReadStream ( file ) ) . lines ( ) . filter ( ( line ) => line . substr ( 0 , line . indexOf ( "#" ) ) . trim ( ) === "" ) . parse ( ( line ) => mmfilter ( line ) ) . catch ( ( ) => undefined ) . toArray ( ) ; const fakeArr = [ ] ; return ( f ) => ! rules . find ( x => x ( f , 0 , fakeArr ) ) ; } ; param { string } directory param { boolean } stdout param { output } string export const packAction = async ( directory , { stdout , output } ) => { const cwd = resolve ( process . cwd ( ) , directory ) ; const packageLocation = resolve ( cwd , "package.json" ) ; await access ( packageLocation , F_OK | R_OK ) . catch ( ( ) => { throw new Error ( `${ packageLocation } not found.` ) ; } ) ; const target = stdout ? process . stdout : createWriteStream ( output ? resolve ( process . cwd ( ) , output ) : `${ cwd }.tar.gz` ) ; const ignoreLocation = resolve ( cwd , ".siignore" ) ; const filter = await getIgnoreFunction ( ignoreLocation ) ; const out = c ( { gzip : true , cwd , filter } , await readdir ( directory ) ) . pipe ( target ) ; await new Promise ( ( res , rej ) => { out . on ( "finish" , res ) ; out . on ( "error" , rej ) ; } ) ; } ; param { string } file returns { Promise < Readable > } export const getReadStreamFromFile = async ( file ) : Promise < Readable > => { const resolvedFilePath = resolve ( process . cwd ( ) , file ) ; return access ( resolvedFilePath , F_OK ) . then ( ( ) => createReadStream ( resolvedFilePath ) ) . catch ( ( ) => { throw new Error ( `File "${ file }" not found.` ) ; } ) ; } ;