summaryrefslogtreecommitdiff
path: root/packages/create-astro/test/utils.js
blob: 964ae6a2022f2ea262faa8134912456a66dd1b8f (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
import { execa } from 'execa';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
export const testDir = dirname(__filename);
export const timeout = 5000;

const createAstroError = new Error(
	'Timed out waiting for create-astro to respond with expected output.'
);

export function promiseWithTimeout(testFn) {
	return new Promise((resolve, reject) => {
		const timeoutEvent = setTimeout(() => {
			reject(createAstroError);
		}, timeout);
		function resolver() {
			clearTimeout(timeoutEvent);
			resolve();
		}
		testFn(resolver);
	});
}

export const PROMPT_MESSAGES = {
	directory: 'Where would you like to create your app?',
	template: 'Which app template would you like to use?',
	install: (pkgManager) => `Would you like us to run "${pkgManager} install?"`,
	astroAdd: (astroAddCommand = 'npx astro@latest add --yes') =>
		`Run "${astroAddCommand}?" This lets you optionally add component frameworks (ex. React), CSS frameworks (ex. Tailwind), and more.`,
	git: 'Initialize a git repository?',
};

export function setup(args = []) {
	const { stdout, stdin } = execa('../create-astro.mjs', [...args, '--dryrun'], { cwd: testDir });
	return {
		stdin,
		stdout,
	};
}