copyTemplate() — astro Function Reference
Architecture documentation for the copyTemplate() function in template.ts from the astro codebase.
Entity Profile
Dependency Diagram
graph TD 0fddab5d_7945_0e3f_5243_8599487fed07["copyTemplate()"] 33b7e98e_3257_427a_5948_5ab38c474d26["template.ts"] 0fddab5d_7945_0e3f_5243_8599487fed07 -->|defined in| 33b7e98e_3257_427a_5948_5ab38c474d26 4cd724a0_ec41_439e_3ac2_a00dde55ae2a["template()"] 4cd724a0_ec41_439e_3ac2_a00dde55ae2a -->|calls| 0fddab5d_7945_0e3f_5243_8599487fed07 f663c14e_a34e_e8db_e978_7ac40aef9737["getTemplateTarget()"] 0fddab5d_7945_0e3f_5243_8599487fed07 -->|calls| f663c14e_a34e_e8db_e978_7ac40aef9737 5efefaab_aaa7_f01a_cf9f_0451f47845c8["processTemplateReadme()"] 0fddab5d_7945_0e3f_5243_8599487fed07 -->|calls| 5efefaab_aaa7_f01a_cf9f_0451f47845c8 style 0fddab5d_7945_0e3f_5243_8599487fed07 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/create-astro/src/actions/template.ts lines 135–202
async function copyTemplate(tmpl: string, ctx: Context) {
const templateTarget = getTemplateTarget(tmpl, ctx.ref);
// Copy
if (!ctx.dryRun) {
try {
await downloadTemplate(templateTarget, {
force: true,
cwd: ctx.cwd,
dir: '.',
});
// Process the README file to remove marked sections and update package manager
const readmePath = path.resolve(ctx.cwd, 'README.md');
if (fs.existsSync(readmePath)) {
const readme = fs.readFileSync(readmePath, 'utf8');
const processedReadme = processTemplateReadme(readme, ctx.packageManager);
fs.writeFileSync(readmePath, processedReadme);
}
} catch (err: any) {
// Only remove the directory if it's most likely created by us.
if (ctx.cwd !== '.' && ctx.cwd !== './' && !ctx.cwd.startsWith('../')) {
try {
fs.rmdirSync(ctx.cwd);
} catch (_) {
// Ignore any errors from removing the directory,
// make sure we throw and display the original error.
}
}
if (err.message?.includes('404')) {
throw new Error(`Template ${color.reset(tmpl)} ${color.dim('does not exist!')}`);
}
if (err.message) {
error('error', err.message);
}
try {
// The underlying error is often buried deep in the `cause` property
// This is in a try/catch block in case of weirdnesses in accessing the `cause` property
if ('cause' in err) {
// This is probably included in err.message, but we can log it just in case it has extra info
error('error', err.cause);
if ('cause' in err.cause) {
// Hopefully the actual fetch error message
error('error', err.cause?.cause);
}
}
} catch {}
throw new Error(`Unable to download template ${color.reset(tmpl)}`);
}
// Post-process in parallel
const removeFiles = FILES_TO_REMOVE.map(async (file) => {
const fileLoc = path.resolve(path.join(ctx.cwd, file));
if (fs.existsSync(fileLoc)) {
return fs.promises.rm(fileLoc, { recursive: true });
}
});
const updateFiles = Object.entries(FILES_TO_UPDATE).map(async ([file, update]) => {
const fileLoc = path.resolve(path.join(ctx.cwd, file));
if (fs.existsSync(fileLoc)) {
return update(fileLoc, { name: ctx.projectName! });
}
});
await Promise.all([...removeFiles, ...updateFiles]);
}
}
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does copyTemplate() do?
copyTemplate() is a function in the astro codebase, defined in packages/create-astro/src/actions/template.ts.
Where is copyTemplate() defined?
copyTemplate() is defined in packages/create-astro/src/actions/template.ts at line 135.
What does copyTemplate() call?
copyTemplate() calls 2 function(s): getTemplateTarget, processTemplateReadme.
What calls copyTemplate()?
copyTemplate() is called by 1 function(s): template.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free