MCPcopy
hub / github.com/QwikDev/qwik / writeToFile

Function writeToFile

packages/qwik/src/cli/new/run-new-command.ts:198–230  ·  view source on GitHub ↗
(name: string, slug: string, template: Template, outDir: string)

Source from the content-addressed store, hash-verified

196}
197
198async 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
232function inject(raw: string, vars: string[][]) {
233 let output = raw;

Callers 1

runNewCommandFunction · 0.85

Calls 2

joinFunction · 0.90
injectFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…