| 189 | } |
| 190 | |
| 191 | async 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 | |
| 227 | function inferPackageManager(defaultValue: string | null): string | null { |
| 228 | const userAgent = process.env["npm_config_user_agent"]; |