diff options
author | 2022-10-12 09:48:29 -0300 | |
---|---|---|
committer | 2022-10-12 09:48:29 -0300 | |
commit | f604ef6c694e88bcf2f22ce97a33bf131074326f (patch) | |
tree | c5579e65fb4c6c0ba854933998e5993137a9c0ce /scripts/smoke/check.js | |
parent | 640ce72d336101ff3bc02a596373ddbf1713e5db (diff) | |
download | astro-f604ef6c694e88bcf2f22ce97a33bf131074326f.tar.gz astro-f604ef6c694e88bcf2f22ce97a33bf131074326f.tar.zst astro-f604ef6c694e88bcf2f22ce97a33bf131074326f.zip |
Run astro check on all examples in CI (#5022)
* Run astro check on all examples in CI
* Output stderr
* Build Astro before running checks
* Making things faster + colors
* Fix errors inside examples
* Add congrats message
* Revert unentional change to tsconfigs
* Remove more unneeded changes
Diffstat (limited to '')
-rw-r--r-- | scripts/smoke/check.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/scripts/smoke/check.js b/scripts/smoke/check.js new file mode 100644 index 000000000..447a76edc --- /dev/null +++ b/scripts/smoke/check.js @@ -0,0 +1,84 @@ +// @ts-check + +import { spawn } from 'child_process'; +import { readdirSync, readFileSync, writeFileSync } from 'fs'; +import * as path from 'path'; +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...`); + + Promise.all( + examples.map( + (example) => + 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); + }); + }) + ) + ).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 + }); + } + + 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(); |