import * as plugins from './plugins.js'; import * as paths from '../paths.js'; export class AssetsHandler { public defaultFromDirPath: string = plugins.path.join(paths.cwd, './assets'); public defaultToDirPath: string = plugins.path.join( paths.cwd, './dist_serve/assets', ); public async ensureAssetsDir() { const dirExists = await plugins.fs .directory(this.defaultFromDirPath) .exists(); if (!dirExists) { await plugins.fs.directory(this.defaultFromDirPath).create(); console.log(`created assets directory at ${this.defaultFromDirPath}`); } } // copies the assets directory recursively private async copyDirectoryRecursive(from: string, to: string) { const entries = await plugins.fs.directory(from).recursive().list(); await plugins.fs.directory(to).create(); for (const entry of entries) { const fromPath = plugins.path.join(from, entry.path); const toPath = plugins.path.join(to, entry.path); if (entry.isDirectory) { await plugins.fs.directory(toPath).create(); } else { const toDir = plugins.path.dirname(toPath); await plugins.fs.directory(toDir).create(); await plugins.fs.file(fromPath).copy(toPath); } } } // copies the html public async processAssets(optionsArg?: { from?: string; to?: string }) { // lets assemble the options optionsArg = { ...{ from: this.defaultFromDirPath, to: this.defaultToDirPath, }, ...(optionsArg || {}), }; await this.ensureAssetsDir(); optionsArg.from = plugins.smartpath.transform.toAbsolute( optionsArg.from, paths.cwd, ) as string; optionsArg.to = plugins.smartpath.transform.toAbsolute( optionsArg.to, paths.cwd, ) as string; // lets clean the target directory const toExists = await plugins.fs.directory(optionsArg.to).exists(); if (toExists) { await plugins.fs.directory(optionsArg.to).delete(); } await this.copyDirectoryRecursive(optionsArg.from, optionsArg.to); } }