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

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

const timeoutError = function (details) {
	let errorMsg = 'Timed out waiting for create-astro to respond with expected output.';
	if (details) {
		errorMsg += '\nLast output: "' + details + '"';
	}
	return new Error(errorMsg);
};

export function promiseWithTimeout(testFn) {
	return new Promise((resolve, reject) => {
		let lastStdout;
		function onStdout(chunk) {
			lastStdout = stripAnsi(chunk.toString()).trim() || lastStdout;
		}

		const timeoutEvent = setTimeout(() => {
			reject(timeoutError(lastStdout));
		}, timeout);
		function resolver() {
			clearTimeout(timeoutEvent);
			resolve();
		}

		testFn(resolver, onStdout);
	});
}

export const PROMPT_MESSAGES = {
	directory: 'Where would you like to create your new project?',
	template: 'How would you like to setup your new project?',
	typescript: 'How would you like to setup TypeScript?',
	typescriptSucceed: 'next',
};

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