2022-10-12 07:48:29 -05:00
|
|
|
// @ts-check
|
|
|
|
|
2023-07-17 19:17:59 -05:00
|
|
|
import { spawn } from 'node:child_process';
|
2024-01-04 06:06:37 -05:00
|
|
|
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
2023-07-17 19:17:59 -05:00
|
|
|
import * as path from 'node:path';
|
2023-06-02 08:14:44 -05:00
|
|
|
import pLimit from 'p-limit';
|
2024-08-07 07:10:20 -05:00
|
|
|
import { toJson } from 'tsconfck';
|
2022-10-12 07:48:29 -05:00
|
|
|
|
2024-07-17 13:56:08 -05:00
|
|
|
const skippedExamples = ['toolbar-app', 'component', 'server-islands'];
|
2024-04-24 10:56:12 -05:00
|
|
|
|
2022-10-12 07:48:29 -05:00
|
|
|
function checkExamples() {
|
|
|
|
let examples = readdirSync('./examples', { withFileTypes: true });
|
2024-04-24 10:56:12 -05:00
|
|
|
examples = examples.filter((dirent) => dirent.isDirectory()).filter((dirent) => !skippedExamples.includes(dirent.name));
|
2022-10-12 07:48:29 -05:00
|
|
|
|
|
|
|
console.log(`Running astro check on ${examples.length} examples...`);
|
|
|
|
|
2023-06-02 08:14:44 -05:00
|
|
|
// Run astro check in parallel with 5 at most
|
|
|
|
const checkPromises = [];
|
|
|
|
const limit = pLimit(5);
|
|
|
|
|
|
|
|
for (const example of examples) {
|
|
|
|
checkPromises.push(
|
2023-08-04 10:53:54 -05:00
|
|
|
limit(
|
|
|
|
() =>
|
|
|
|
new Promise((resolve) => {
|
2024-01-04 06:06:37 -05:00
|
|
|
// Sometimes some examples may get deleted, but after a `git pull` the directory still exists.
|
|
|
|
// This can stall the process time as it'll typecheck the entire monorepo, so do a quick exist
|
|
|
|
// check here before typechecking this directory.
|
|
|
|
if (!existsSync(path.join('./examples/', example.name, 'package.json'))) {
|
|
|
|
resolve(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-04 10:53:54 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
})
|
2023-06-02 08:14:44 -05:00
|
|
|
)
|
2023-08-04 10:53:54 -05:00
|
|
|
);
|
2023-06-02 08:14:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Promise.all(checkPromises).then((codes) => {
|
2022-10-12 07:48:29 -05:00
|
|
|
if (codes.some((code) => code !== 0)) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2023-08-04 10:53:54 -05:00
|
|
|
console.log('No errors found!');
|
2022-10-12 07:48:29 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} examplePath
|
|
|
|
*/
|
|
|
|
function prepareExample(examplePath) {
|
|
|
|
const tsconfigPath = path.join('./examples/', examplePath, 'tsconfig.json');
|
2024-08-07 07:10:20 -05:00
|
|
|
if (!existsSync(tsconfigPath)) return
|
|
|
|
|
|
|
|
const originalConfig = readFileSync(tsconfigPath, 'utf-8');
|
|
|
|
const tsconfig = JSON.parse(toJson(originalConfig));
|
2022-10-12 07:48:29 -05:00
|
|
|
|
2024-08-07 07:10:20 -05:00
|
|
|
// Swap to strictest config to make sure it also passes
|
|
|
|
tsconfig.extends = 'astro/tsconfigs/strictest';
|
|
|
|
tsconfig.compilerOptions ??= {}
|
|
|
|
tsconfig.compilerOptions.types = tsconfig.compilerOptions.types ?? []; // Speeds up tests
|
2022-10-12 07:48:29 -05:00
|
|
|
|
2024-08-07 07:10:20 -05:00
|
|
|
writeFileSync(tsconfigPath, JSON.stringify(tsconfig));
|
2022-10-12 07:48:29 -05:00
|
|
|
|
|
|
|
return originalConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} examplePath
|
|
|
|
* @param {string} originalConfig
|
|
|
|
*/
|
|
|
|
function resetExample(examplePath, originalConfig) {
|
|
|
|
const tsconfigPath = path.join('./examples/', examplePath, 'tsconfig.json');
|
|
|
|
writeFileSync(tsconfigPath, originalConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkExamples();
|