| 8 | } |
| 9 | |
| 10 | export class OutputEngine { |
| 11 | private paths: Generator['paths'] |
| 12 | private fs: Runtime['fs'] |
| 13 | private path: Runtime['path'] |
| 14 | |
| 15 | constructor(options: OutputEngineOptions) { |
| 16 | const { paths, runtime } = options |
| 17 | |
| 18 | this.paths = paths |
| 19 | this.fs = runtime.fs |
| 20 | this.path = runtime.path |
| 21 | } |
| 22 | |
| 23 | empty = () => { |
| 24 | this.fs.rmDirSync(this.path.join(...this.paths.root)) |
| 25 | } |
| 26 | |
| 27 | ensure = (file: string, cwd: string) => { |
| 28 | const outPath = this.path.resolve(cwd, file) |
| 29 | const dirname = this.path.dirname(outPath) |
| 30 | this.fs.ensureDirSync(dirname) |
| 31 | return outPath |
| 32 | } |
| 33 | |
| 34 | writeFile = (filePath: string, code: string) => { |
| 35 | this.fs.ensureDirSync(this.path.dirname(filePath)) |
| 36 | |
| 37 | if (this.fs.existsSync(filePath) && this.fs.readFileSync(filePath) === code) { |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | logger.debug('write:file', filePath) |
| 42 | return this.fs.writeFile(filePath, code) |
| 43 | } |
| 44 | |
| 45 | write = (output: Artifact | undefined) => { |
| 46 | if (!output) return |
| 47 | |
| 48 | const { dir = this.paths.root, files } = output |
| 49 | this.fs.ensureDirSync(this.path.join(...dir)) |
| 50 | |
| 51 | return Promise.allSettled( |
| 52 | files.map(async (artifact) => { |
| 53 | if (!artifact?.code) return |
| 54 | |
| 55 | const { file, code } = artifact |
| 56 | const absPath = this.path.join(...dir, file) |
| 57 | return this.writeFile(absPath, code) |
| 58 | }), |
| 59 | ) |
| 60 | } |
| 61 | } |
nothing calls this directly
no test coverage detected