UNPKG

@vidavidorra/create-project

Version:
54 lines 1.62 kB
import { join, dirname } from 'node:path'; import fs from 'node:fs'; import prettier from 'prettier'; import rootPath from '../root-path.js'; class File { path; _content; _options; _directory; constructor(path, options) { this.path = path; this._directory = dirname(path); this._options = { mode: 0o644, format: false, read: true, ...options }; this._content = this._options.read ? fs.readFileSync(join(rootPath, path), 'utf8') : ''; } get options() { return structuredClone(this._options); } get content() { return this._content; } async write() { this.process(); const path = join(this.options.path, this.path); if (!this._options.dryRun) { const directory = join(this.options.path, this._directory); if (!fs.existsSync(directory)) { await fs.promises.mkdir(directory, { recursive: true }); } await fs.promises.writeFile(path, await this.format(), { mode: this._options.mode, }); } } /** * Process file contents. * * @note This function is called automatically from `write`. */ process() { return this; } async format() { if (this._options.format) { const config = await prettier.resolveConfig(join(rootPath, 'package.json')); return prettier.format(this._content, { ...config, filepath: this.path }); } return this._content; } } export { File }; //# sourceMappingURL=file.js.map