import fs from "fs" ; import * as path from "path" ; import * as tmp from "tmp" ; import { AuthConfig , AUTH_FILE_NAME , CONFIG_FILE_NAME , LOADER_CONFIG_FILE_NAME , PlasmicConfig , PlasmicLoaderConfig , } from "../utils/config-utils" ; import { deleteFileBuffered , readFileText , writeFileText } from "./file-utils" ; export class TempRepo { tmpDir : tmp . DirResult ; constructor ( ) { this . tmpDir = tmp . dirSync ( { unsafeCleanup : true } ) ; } destroy ( ) { this . tmpDir . removeCallback ( ) ; } resolveFile ( relativePath ) { return path . resolve ( this . tmpDir . name , relativePath ) ; } readFile ( relativePath ) { const absPath = this . resolveFile ( relativePath ) ; const buf = readFileText ( absPath ) ; return buf . toString ( ) ; } writeFile ( relativePath , data ) { const absPath = this . resolveFile ( relativePath ) ; writeFileText ( absPath , data ) ; } deleteFile ( relativePath ) { const absPath = this . resolveFile ( relativePath ) ; deleteFileBuffered ( absPath ) ; } checkFile ( relativePath ) { const absPath = this . resolveFile ( relativePath ) ; try { const stats = fs . statSync ( absPath ) ; return ! ! stats ? true : false ; } catch ( e ) { return false ; } } getComponentFileContents ( projectId , componentId ) { const plasmicJson = JSON . parse ( this . readFile ( CONFIG_FILE_NAME ) ) ; const srcDir = plasmicJson . srcDir ; const projectConfig = plasmicJson . projects . find ( ( p ) => p . projectId === projectId ) ; if ( ! projectConfig ) { return ; } const componentConfig = projectConfig . components . find ( ( c ) => c . id === componentId ) ; if ( ! componentConfig ) { return ; } const data = this . readFile ( path . join ( srcDir , componentConfig . renderModuleFilePath ) ) ; return data ; } plasmicAuthPath ( ) { return this . resolveFile ( AUTH_FILE_NAME ) ; } writePlasmicAuth ( json ) { this . writeFile ( AUTH_FILE_NAME , JSON . stringify ( json ) ) ; } deletePlasmicAuth ( ) { this . deleteFile ( AUTH_FILE_NAME ) ; } plasmicJsonPath ( ) { return this . resolveFile ( CONFIG_FILE_NAME ) ; } readPlasmicJson ( ) { return JSON . parse ( this . readFile ( CONFIG_FILE_NAME ) ) ; } writePlasmicJson ( json ) { this . writeFile ( CONFIG_FILE_NAME , JSON . stringify ( json ) ) ; } deletePlasmicJson ( ) { this . deleteFile ( CONFIG_FILE_NAME ) ; } plasmicLoaderJsonPath ( ) { return this . resolveFile ( LOADER_CONFIG_FILE_NAME ) ; } readPlasmicLoaderJson ( ) { return JSON . parse ( this . readFile ( LOADER_CONFIG_FILE_NAME ) ) ; } }