MCPcopy Create free account
hub / github.com/observablehq/framework / recursiveCopyTemplate

Function recursiveCopyTemplate

src/create.ts:191–225  ·  view source on GitHub ↗
(
  inputRoot: string,
  outputRoot: string,
  context: Record<string, string>,
  effects: CreateEffects,
  stepPath: string = "."
)

Source from the content-addressed store, hash-verified

189}
190
191async function recursiveCopyTemplate(
192 inputRoot: string,
193 outputRoot: string,
194 context: Record<string, string>,
195 effects: CreateEffects,
196 stepPath: string = "."
197) {
198 const templatePath = join(inputRoot, stepPath);
199 const templateStat = await stat(templatePath);
200 let outputPath = join(outputRoot, stepPath);
201 if (templateStat.isDirectory()) {
202 try {
203 await effects.mkdir(outputPath, {recursive: true});
204 } catch {
205 // that's ok
206 }
207 for (const entry of await readdir(templatePath)) {
208 await recursiveCopyTemplate(inputRoot, outputRoot, context, effects, join(stepPath, entry));
209 }
210 } else {
211 if (templatePath.endsWith(".DS_Store")) return;
212 if (templatePath.endsWith(".tmpl")) {
213 outputPath = outputPath.replace(/\.tmpl$/, "");
214 let contents = await readFile(templatePath, "utf8");
215 contents = contents.replaceAll(/\{\{\s*(\w+)\s*\}\}/g, (_, key) => {
216 const val = context[key];
217 if (val) return val;
218 throw new Error(`no template variable ${key}`);
219 });
220 await effects.writeFile(outputPath, contents);
221 } else {
222 await effects.copyFile(templatePath, outputPath);
223 }
224 }
225}
226
227function inferPackageManager(defaultValue: string | null): string | null {
228 const userAgent = process.env["npm_config_user_agent"];

Callers 1

createFunction · 0.85

Calls 3

mkdirMethod · 0.65
writeFileMethod · 0.65
copyFileMethod · 0.65

Tested by

no test coverage detected