summaryrefslogtreecommitdiff
path: root/packages/create-astro/src/actions/template.ts
blob: 2382c61dc6f693a2247a5eb85a4504bd881592c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint no-console: 'off' */
import type { Context } from './context';

import { color } from '@astrojs/cli-kit';
import { downloadTemplate } from 'giget';
import fs from 'node:fs';
import path from 'node:path';
import { error, info, spinner, title } from '../messages.js';

export async function template(
	ctx: Pick<Context, 'template' | 'prompt' | 'dryRun' | 'exit' | 'exit'>
) {
	if (!ctx.template) {
		const { template: tmpl } = await ctx.prompt({
			name: 'template',
			type: 'select',
			label: title('tmpl'),
			message: 'How would you like to start your new project?',
			initial: 'basics',
			choices: [
				{ value: 'basics', label: 'Include sample files', hint: '(recommended)' },
				{ value: 'blog', label: 'Use blog template' },
				{ value: 'minimal', label: 'Empty' },
			],
		});
		ctx.template = tmpl;
	} else {
		await info('tmpl', `Using ${color.reset(ctx.template)}${color.dim(' as project template')}`);
	}

	if (ctx.dryRun) {
		await info('--dry-run', `Skipping template copying`);
	} else if (ctx.template) {
		await spinner({
			start: 'Template copying...',
			end: 'Template copied',
			while: () =>
				copyTemplate(ctx.template!, ctx as Context).catch((e) => {
					// eslint-disable-next-line no-console
					error('error', e);
					process.exit(1);
				}),
		});
	} else {
		ctx.exit(1);
	}
}

// some files are only needed for online editors when using astro.new. Remove for create-astro installs.
const FILES_TO_REMOVE = ['sandbox.config.json', 'CHANGELOG.md'];
const FILES_TO_UPDATE = {
	'package.json': (file: string, overrides: { name: string }) =>
		fs.promises
			.readFile(file, 'utf-8')
			.then((value) =>
				fs.promises.writeFile(
					file,
					JSON.stringify(
						Object.assign(JSON.parse(value), Object.assign(overrides, { private: undefined })),
						null,
						'\t'
					),
					'utf-8'
				)
			),
};

export default async function copyTemplate(tmpl: string, ctx: Context) {
	const ref = ctx.ref || 'latest';
	const isThirdParty = tmpl.includes('/');

	const templateTarget = isThirdParty ? tmpl : `github:withastro/astro/examples/${tmpl}#${ref}`;

	// Copy
	if (!ctx.dryRun) {
		try {
			await downloadTemplate(templateTarget, {
				force: true,
				provider: 'github',
				cwd: ctx.cwd,
				dir: '.',
			});
		} catch (err: any) {
			fs.rmdirSync(ctx.cwd);
			if (err.message.includes('404')) {
				await error('Error', `Template ${color.reset(tmpl)} ${color.dim('does not exist!')}`);
			} else {
				console.error(err.message);
			}
			ctx.exit(1);
		}

		// 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]);
	}
}