|
| 1 | +const {basename, join, resolve} = require('path') |
| 2 | +const PromiseQueue = require('p-queue') |
| 3 | +const execa = require('execa') |
| 4 | +const globby = require('globby') |
| 5 | +const rootDir = resolve(__dirname, '../..') |
| 6 | +const lernaConfig = require(join(rootDir, 'lerna.json')) |
| 7 | +const modulesDir = join(rootDir, 'modules') |
| 8 | +require('console.table') |
| 9 | + |
| 10 | +const unique = list => Array.from(new Set(list)).sort() |
| 11 | + |
| 12 | +const matchAll = (pattern, text) => { |
| 13 | + const matches = [] |
| 14 | + let match |
| 15 | + while (match = pattern.exec(text)) { |
| 16 | + matches.push(match) |
| 17 | + } |
| 18 | + return matches |
| 19 | +} |
| 20 | + |
| 21 | +const checks = { |
| 22 | + 'has stories': (module, key) => { |
| 23 | + return globby(join(module.path, '**/stories.js')) |
| 24 | + .then(files => ({ |
| 25 | + [key]: files.length > 0 ? 'yes' : 'no' |
| 26 | + })) |
| 27 | + }, |
| 28 | + 'docs test': (module, key) => { |
| 29 | + return execa(join(rootDir, 'script/test-docs'), { |
| 30 | + cwd: module.path |
| 31 | + }) |
| 32 | + .then(result => ({[key]: 'pass'})) |
| 33 | + .catch(({stderr}) => { |
| 34 | + const pattern = /("\.[-\w]+") is not documented/g |
| 35 | + const matches = matchAll(pattern, stderr) |
| 36 | + .map(match => match[1]) |
| 37 | + let missing = matches ? Array.from(matches) : [] |
| 38 | + const max = 5 |
| 39 | + if (missing.length > max) { |
| 40 | + const more = missing.length - max |
| 41 | + missing = missing.slice(0, max).concat(`and ${more} more...`) |
| 42 | + } |
| 43 | + return { |
| 44 | + [key]: 'FAIL', |
| 45 | + 'missing docs': unique(missing).join(', ') |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +const args = process.argv.slice(2) |
| 52 | + |
| 53 | +const modules = args.length |
| 54 | + ? Promise.resolve(args) |
| 55 | + : globby(join(modulesDir, 'primer-*')) |
| 56 | + |
| 57 | +modules |
| 58 | + .then(moduleDirs => { |
| 59 | + console.log('Found %d module directories', moduleDirs.length) |
| 60 | + return moduleDirs |
| 61 | + .map(path => ({ |
| 62 | + path, |
| 63 | + name: basename(path), |
| 64 | + pkg: require(join(path, 'package.json')) |
| 65 | + })) |
| 66 | + .filter(({pkg}) => pkg.primer.module_type !== 'meta') |
| 67 | + }) |
| 68 | + .then(modules => { |
| 69 | + console.log('Filtered to %d modules (excluding meta-packages)', modules.length) |
| 70 | + |
| 71 | + const queue = new PromiseQueue({concurrency: 3}) |
| 72 | + |
| 73 | + for (const module of modules) { |
| 74 | + module.checks = {} |
| 75 | + for (const [name, check] of Object.entries(checks)) { |
| 76 | + queue.add(() => { |
| 77 | + // console.warn(`? check: ${module.name} ${name}`) |
| 78 | + return check(module, name) |
| 79 | + .then(result => { |
| 80 | + Object.assign(module.checks, result) |
| 81 | + }) |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + console.warn(`Running ${queue.size} checks...`) |
| 87 | + return queue.onIdle().then(() => modules) |
| 88 | + }) |
| 89 | + .then(modules => { |
| 90 | + console.warn('ran tests on %d modules', modules.length) |
| 91 | + const rows = modules.map(({name, checks}) => { |
| 92 | + return Object.assign({'package': name}, checks) |
| 93 | + }) |
| 94 | + console.table(rows) |
| 95 | + }) |
| 96 | + |
0 commit comments