40 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

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 assetsDirExists = await plugins.smartfile.fs.isDirectory(this.defaultFromDirPath);
if (!assetsDirExists) {
await plugins.smartfile.fs.ensureDir(this.defaultFromDirPath);
console.log(`created assets directory at ${this.defaultFromDirPath}`);
}
}
// 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 theh target directory
await plugins.smartfile.fs.ensureEmptyDir(optionsArg.to);
plugins.smartfile.fs.copySync(optionsArg.from, optionsArg.to, {
replaceTargetDir: true,
});
}
}