import { copySync , CopyOptionsSync , existsSync , mkdirSync , readFileSync , removeSync , writeFileSync , moveSync , MoveOptions , readdirSync , lstatSync , } from "fs-extra" ; import { join } from "path" ; import { log } from "./log" ; import { CliError } from "./CliError" ; export class Files { mkdirSync ( path ) { mkdirSync ( path ) ; } copySync ( src , dest , options = { } ) { const { renameTspFiles , ... copyOptions } = { overwrite : false , errorOnExist : true , renameTspFiles : true , ... options , } ; this . throwIfMissing ( src ) ; if ( options . errorOnExist ) { this . throwIfExists ( dest ) ; } log . info ( `Copying from: ${ src } to ${ dest }` ) ; copySync ( src , dest , copyOptions ) ; if ( renameTspFiles !== false ) { this . renameTspFilesSync ( dest ) ; } } renameTspFilesSync ( path ) { const files = readdirSync ( path ) ; files . forEach ( ( name ) => { const filePath = join ( path , name ) ; if ( lstatSync ( filePath ) . isDirectory ( ) ) { this . renameTspFilesSync ( filePath ) ; } if ( name . startsWith ( "_tsp_" ) ) { const dest = join ( path , name . substr ( "_tsp_" . length ) ) ; this . moveSync ( filePath , dest ) ; } } ) ; } moveSync ( src , dest , options = { overwrite : false } ) { log . info ( `Moving "${ src }" to "${ dest }"` ) ; moveSync ( src , dest , options ) ; } removeSync ( path ) { if ( ! existsSync ( path ) ) { log . warn ( `Directory not deleted because it does not exist: ${ path }` ) ; return ; } log . info ( `Deleting the directory: ${ path }` ) ; removeSync ( path ) ; } readJsonSync < T = any > ( path ) { const contents = readFileSync ( path , "utf-8" ) ; return JSON . parse ( contents ) as T ; } writeJsonSync ( path , json ) { this . writeFileSync ( path , JSON . stringify ( json , null , 2 ) ) ; } writeFileSync ( path , value ) { log . info ( `Updating file: ${ path }` ) ; writeFileSync ( path , value ) ; } throwIfExists ( path , message ? ) { if ( existsSync ( path ) ) { throw new CliError ( ( message || "The directory already exists." ) + `\n\t${ path }` ) ; } } throwIfMissing = ( path , message ? ) => { if ( ! existsSync ( path ) ) { throw new CliError ( ( message || "A required directory is missing." ) + `\n\t${ path }` ) ; } } ; } let files ; export const getFiles = ( ) => { if ( ! files ) { files = new Files ( ) ; } return files ; } ;