2022-10-12 12:48:29 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-07-18 00:17:59 +00:00
|
|
|
import { spawn } from 'node:child_process';
|
|
|
|
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
|
|
import * as path from 'node:path';
|
2023-06-02 13:14:44 +00:00
|
|
|
import pLimit from 'p-limit';
|
2022-10-12 12:48:29 +00:00
|
|
|
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...`);
|
|
|
|
|
2023-06-02 13:14:44 +00: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 15:53:54 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
})
|
2023-06-02 13:14:44 +00:00
|
|
|
)
|
2023-08-04 15:53:54 +00:00
|
|
|
);
|
2023-06-02 13:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Promise.all(checkPromises).then((codes) => {
|
2022-10-12 12:48:29 +00:00
|
|
|
if (codes.some((code) => code !== 0)) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2023-08-04 15:53:54 +00:00
|
|
|
console.log('No errors found!');
|
2022-10-12 12:48:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-04 15:53:54 +00:00
|
|
|
if (tsconfig.config) {
|
|
|
|
writeFileSync(tsconfigPath, JSON.stringify(tsconfig.config));
|
|
|
|
}
|
2022-10-12 12:48:29 +00: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();
|