|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Validates all examples in the `examples` directory by installing dependencies |
| 5 | + * and building each example using the local `rollbar.tgz` package. |
| 6 | + */ |
| 7 | + |
| 8 | +import { access, readdir, readFile } from 'node:fs/promises'; |
| 9 | +import path from 'node:path'; |
| 10 | +import { fileURLToPath } from 'node:url'; |
| 11 | +import os from 'node:os'; |
| 12 | + |
| 13 | +import { findUp, npm, parallelMap } from './util.js'; |
| 14 | + |
| 15 | +const dryRun = ['--dry-run', '-n'].some((f) => process.argv.includes(f)); |
| 16 | +const jobsCount = (() => { |
| 17 | + const i = process.argv.findIndex((a) => a === '--parallel' || a === '-p'); |
| 18 | + const n = i < 0 ? 0 : parseInt(process.argv[i + 1], 10) || os.cpus().length; |
| 19 | + return Math.max(n, 1); |
| 20 | +})(); |
| 21 | + |
| 22 | +async function validateExample(exampleDir, dryRun = false) { |
| 23 | + const name = `examples/${path.basename(exampleDir)}`; |
| 24 | + |
| 25 | + if (dryRun) { |
| 26 | + console.log(` - ${name} (dry run)`); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + await npm(['install'], { cwd: exampleDir, id: name }); |
| 31 | + await npm(['run', 'build'], { cwd: exampleDir, id: name }); |
| 32 | + console.log(` ✓ ${name}`); |
| 33 | +} |
| 34 | + |
| 35 | +async function validateExamples() { |
| 36 | + console.log('Validating examples using the local rollbar package...'); |
| 37 | + if (jobsCount > 1) { |
| 38 | + console.log(`Running with ${jobsCount} parallel jobs`); |
| 39 | + } |
| 40 | + console.log(); |
| 41 | + |
| 42 | + const cwd = path.dirname(fileURLToPath(import.meta.url)); |
| 43 | + const root = await findUp({ fileName: 'package.json', dir: cwd }); |
| 44 | + const examplesDir = path.join(root, 'examples'); |
| 45 | + |
| 46 | + try { |
| 47 | + await access(path.join(examplesDir, 'rollbar.tgz')); |
| 48 | + } catch { |
| 49 | + throw new Error( |
| 50 | + `No rollbar.tgz found in ${path.relative(root, examplesDir)}. ` + |
| 51 | + `Please build rollbar first.`, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + const entries = await readdir(examplesDir, { withFileTypes: true }); |
| 56 | + const subdirs = entries |
| 57 | + .filter((entry) => entry.isDirectory()) |
| 58 | + .map((entry) => path.join(examplesDir, entry.name)); |
| 59 | + |
| 60 | + let exampleDirs = []; |
| 61 | + for (const subdir of subdirs) { |
| 62 | + let pkg; |
| 63 | + |
| 64 | + try { |
| 65 | + pkg = await readFile(path.join(subdir, 'package.json'), 'utf8'); |
| 66 | + } catch { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + const { dependencies } = JSON.parse(pkg); |
| 71 | + if (dependencies?.rollbar === 'file:../rollbar.tgz') { |
| 72 | + exampleDirs.push(subdir); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (exampleDirs.length === 0) { |
| 77 | + throw new Error('No examples found using the local rollbar package.'); |
| 78 | + } |
| 79 | + |
| 80 | + await parallelMap( |
| 81 | + exampleDirs, |
| 82 | + async (dir) => validateExample(dir, dryRun), |
| 83 | + jobsCount, |
| 84 | + ); |
| 85 | + |
| 86 | + console.log('Validation succeeded'); |
| 87 | +} |
| 88 | + |
| 89 | +validateExamples().catch((err) => { |
| 90 | + console.error('Error validating examples:', err); |
| 91 | + console.error( |
| 92 | + '\nUsage: validate-examples [--dry-run|-n] [--parallel|-p <n>]', |
| 93 | + ); |
| 94 | + console.error(' --parallel | -p <n>: run <n> jobs in parallel'); |
| 95 | + console.error(' if no <n> is given, defaults to cpu cores'); |
| 96 | + console.error(' --dry-run | -n: do not run commands, just print'); |
| 97 | + console.error('\nExamples:'); |
| 98 | + console.error(' validate-examples --parallel 4'); |
| 99 | + console.error(' validate-examples --dry-run'); |
| 100 | + console.error(' validate-examples -n -p'); |
| 101 | + process.exit(1); |
| 102 | +}); |
0 commit comments