(name: string, slug: string, template: Template, outDir: string)
| 196 | } |
| 197 | |
| 198 | async function writeToFile(name: string, slug: string, template: Template, outDir: string) { |
| 199 | // Build the full output file path + name |
| 200 | const outFile = join(outDir, template.relative); |
| 201 | |
| 202 | // String replace the file path |
| 203 | const fileOutput = inject(outFile, [ |
| 204 | [SLUG_KEY, slug], |
| 205 | ['.template', ''], |
| 206 | ]); |
| 207 | |
| 208 | // Exit if the module already exists |
| 209 | if (fs.existsSync(fileOutput)) { |
| 210 | const filename = fileOutput.split('/').pop(); |
| 211 | throw new Error(`"${filename}" already exists in "${outDir}"`); |
| 212 | } |
| 213 | |
| 214 | // Get the template content |
| 215 | const text = await fs.promises.readFile(template.absolute, { encoding: 'utf-8' }); |
| 216 | |
| 217 | // String replace the template content |
| 218 | const templateOut = inject(text, [ |
| 219 | [SLUG_KEY, slug], |
| 220 | [NAME_KEY, name], |
| 221 | ]); |
| 222 | |
| 223 | // Create recursive folders |
| 224 | await fs.promises.mkdir(outDir, { recursive: true }); |
| 225 | |
| 226 | // Write to file |
| 227 | await fs.promises.writeFile(fileOutput, templateOut, { encoding: 'utf-8' }); |
| 228 | |
| 229 | return fileOutput; |
| 230 | } |
| 231 | |
| 232 | function inject(raw: string, vars: string[][]) { |
| 233 | let output = raw; |
no test coverage detected
searching dependent graphs…