vitest/scripts/update-examples.ts
2022-01-13 15:45:14 +01:00

35 lines
1.1 KiB
TypeScript

import { fileURLToPath } from 'url'
import { promises as fs } from 'fs'
import { resolve } from 'pathe'
import { notNullish } from '../packages/vitest/src/utils'
const noUI = ['ruby', 'vitesse', 'vue', 'vue2', 'vue-jsx']
async function run() {
const examplesRoot = resolve(fileURLToPath(import.meta.url), '../../examples')
const examples = await fs.readdir(examplesRoot)
const data = await Promise.all(examples.map(async(name) => {
const path = resolve(examplesRoot, name)
if ((await fs.lstat(path)).isFile())
return
const github = `https://github.com/vitest-dev/vitest/tree/main/examples/${name}`
const test = noUI.includes(name) ? '' : ':ui'
const stackblitz = `https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/${name}?terminal=test${test}`
return {
name,
path,
github,
stackblitz,
}
}))
const table = `| Example | Source | Playground |\n|---|---|---|\n${data.filter(notNullish).map(i => `| \`${i.name}\` | [GitHub](${i.github}) | [Play Online](${i.stackblitz}) |`).join('\n')}`
await fs.writeFile(resolve(examplesRoot, 'README.md'), `${table}\n`, 'utf-8')
}
run()