summaryrefslogtreecommitdiff
path: root/scripts/smoke/check.js
blob: f574f5e5d5e3353d0f6f68b13fda25db891372cc (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
// @ts-check

import { spawn } from 'node:child_process';
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
import * as path from 'node:path';
import pLimit from 'p-limit';
import { tsconfigResolverSync } from 'tsconfig-resolver';

function checkExamples() {
	let examples = readdirSync('./examples', { withFileTypes: true });
	examples = examples.filter((dirent) => dirent.isDirectory());

	console.log(`Running astro check on ${examples.length} examples...`);

	// Run astro check in parallel with 5 at most
	const checkPromises = [];
	const limit = pLimit(5);

	for (const example of examples) {
		checkPromises.push(
			limit(
				() =>
					new Promise((resolve) => {
						const originalConfig = prepareExample(example.name);
						let data = '';
						const child = spawn('node', ['../../packages/astro/astro.js', 'check'], {
							cwd: path.join('./examples', example.name),
							env: { ...process.env, FORCE_COLOR: 'true' },
						});

						child.stdout.on('data', function (buffer) {
							data += buffer.toString();
						});

						child.on('exit', (code) => {
							if (code !== 0) {
								console.error(data);
							}
							if (originalConfig) {
								resetExample(example.name, originalConfig);
							}
							resolve(code);
						});
					})
			)
		);
	}

	Promise.all(checkPromises).then((codes) => {
		if (codes.some((code) => code !== 0)) {
			process.exit(1);
		}

		console.log('No errors found!');
	});
}

/**
 * @param {string} examplePath
 */
function prepareExample(examplePath) {
	const tsconfigPath = path.join('./examples/', examplePath, 'tsconfig.json');
	const tsconfig = tsconfigResolverSync({ filePath: tsconfigPath, cache: false });
	let originalConfig = undefined;

	if (tsconfig.exists) {
		tsconfig.config.extends = 'astro/tsconfigs/strictest';
		originalConfig = readFileSync(tsconfigPath).toString();

		if (!tsconfig.config.compilerOptions) {
			tsconfig.config.compilerOptions = {};
		}

		tsconfig.config.compilerOptions = Object.assign(tsconfig.config.compilerOptions, {
			types: tsconfig.config.compilerOptions.types ?? [], // Speeds up tests
		});
	}

	if (tsconfig.config) {
		writeFileSync(tsconfigPath, JSON.stringify(tsconfig.config));
	}

	return originalConfig;
}

/**
 * @param {string} examplePath
 * @param {string} originalConfig
 */
function resetExample(examplePath, originalConfig) {
	const tsconfigPath = path.join('./examples/', examplePath, 'tsconfig.json');
	writeFileSync(tsconfigPath, originalConfig);
}

checkExamples();