import { Arguments , CodedError , RawResponse , Registrar } from '@kui-shell/core' ; import { dirname } from 'path' ; export type FwriteOptions = { append ? : boolean ; a ? : boolean ; } ; export async function _fwrite ( _fullpath , data , options = { } ) { const { mkdir , writeFile , appendFile } = await import ( 'fs' ) ; const fullpath = _fullpath . replace ( /"/g , '' ) ; await new Promise < boolean > ( ( resolve , reject ) => { const errorHandler = ( err ) => { if ( err ) { if ( err . code === 'ENOENT' ) { const error = new Error ( err . message ) ; error . stack = err . stack ; error . code = 404 ; reject ( error ) ; } else { reject ( err ) ; } } else { resolve ( true ) ; } } ; const write = ( path , data ) => { if ( options . append ) { return appendFile ( path , data , errorHandler ) ; } else { return writeFile ( path , data , errorHandler ) ; } } ; const dir = dirname ( fullpath ) ; if ( dir !== '.' ) { mkdir ( dir , { recursive : true } , ( ) => { return write ( fullpath , data ) ; } ) ; } else { write ( fullpath , data ) ; } } ) ; } /**
 * Kui command for fs.write
 *
 */ const fwrite = async ( { argvNoOptions , execOptions , parsedOptions } ) => { const fullpath = argvNoOptions [ 1 ] ; const data = execOptions . data as string | Buffer ; const append = parsedOptions && ( parsedOptions . append || parsedOptions . a ) ; await _fwrite ( fullpath , data , { append } ) ; return true ; } ; async function fwriteTemp ( args ) { const { mkTemp } = await import ( './mkTemp' ) ; const data = args . execOptions . data as string | Buffer ; const { content : tmp } = await mkTemp ( ) ; await _fwrite ( tmp , data ) ; return { mode : 'raw' , content : tmp } ; } /**
 * Register command handlers
 *
 */ export default ( registrar ) => { registrar . listen ( '/fwrite' , fwrite , { hidden : true , requiresLocal : true } ) ; registrar . listen ( '/fwriteTemp' , fwriteTemp , { hidden : true , requiresLocal : true } ) ; } ;