|
| 1 | +import * as yaml from "js-yaml"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import Head from "next/head"; |
| 5 | + |
| 6 | +import { loadTestCaseFixture } from "@cursorless/test-case-component"; |
| 7 | +import { TestCaseComponentPage } from "@cursorless/test-case-component"; |
| 8 | +import type { TestCaseComponentProps } from "@cursorless/test-case-component"; |
| 9 | +import { testSelectedFiles } from "./allowList"; |
| 10 | + |
| 11 | +import { cheatsheetBodyClasses } from "@cursorless/cheatsheet"; |
| 12 | + |
| 13 | +const fixturesDir = path.join("../", "../", "data", "fixtures", "recorded"); |
| 14 | + |
| 15 | +async function loadYamlFiles(dir: string, selectedFiles?: string[]) { |
| 16 | + const directoryPath = path.join(process.cwd(), dir); |
| 17 | + const files = fs.readdirSync(directoryPath); |
| 18 | + const data: any[] = []; |
| 19 | + |
| 20 | + files.forEach((file) => { |
| 21 | + if ( |
| 22 | + path.extname(file) === ".yml" && |
| 23 | + (!selectedFiles || selectedFiles.includes(file)) |
| 24 | + ) { |
| 25 | + try { |
| 26 | + const filePath = path.join(directoryPath, file); |
| 27 | + const fileContents = fs.readFileSync(filePath, "utf8"); |
| 28 | + const yamlData: any = yaml.load(fileContents); |
| 29 | + yamlData.filename = file; |
| 30 | + data.push(yamlData); |
| 31 | + } catch { |
| 32 | + console.error("File load failure", file); |
| 33 | + } |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + return data; |
| 38 | +} |
| 39 | + |
| 40 | +// See https://github.com/vercel/next.js/discussions/12325#discussioncomment-1116108 |
| 41 | +export async function getStaticProps() { |
| 42 | + const itemsDirActions = path.join(fixturesDir, "actions"); |
| 43 | + const itemsDirDecorations = path.join(fixturesDir, "decorations"); |
| 44 | + const itemsDirInsertEmptyLines = path.join( |
| 45 | + fixturesDir, |
| 46 | + "actions/insertEmptyLines", |
| 47 | + ); |
| 48 | + |
| 49 | + const dataActions = await loadYamlFiles(itemsDirActions, testSelectedFiles); |
| 50 | + const dataInsertEmptyLines = await loadYamlFiles( |
| 51 | + itemsDirInsertEmptyLines, |
| 52 | + testSelectedFiles, |
| 53 | + ); |
| 54 | + |
| 55 | + const dataDecorations = await loadYamlFiles( |
| 56 | + itemsDirDecorations, |
| 57 | + testSelectedFiles, |
| 58 | + ); |
| 59 | + |
| 60 | + const data_errors: any[] = []; |
| 61 | + |
| 62 | + const data = ( |
| 63 | + await Promise.all( |
| 64 | + [...dataActions, ...dataDecorations, ...dataInsertEmptyLines].map( |
| 65 | + async (val) => { |
| 66 | + try { |
| 67 | + const fixture = await loadTestCaseFixture(val); |
| 68 | + return { ...fixture, raw: val }; |
| 69 | + } catch (err) { |
| 70 | + console.error(err); |
| 71 | + data_errors.push(val); |
| 72 | + return null; |
| 73 | + } |
| 74 | + }, |
| 75 | + ), |
| 76 | + ) |
| 77 | + ).filter((test) => test !== undefined); |
| 78 | + |
| 79 | + if (data_errors.length > 0) { |
| 80 | + console.error("data errors:", data_errors); |
| 81 | + } |
| 82 | + |
| 83 | + return { props: { data, bodyClasses: cheatsheetBodyClasses } }; |
| 84 | +} |
| 85 | + |
| 86 | +export function App({ data }: { data: TestCaseComponentProps[] }) { |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <Head> |
| 90 | + <title>Cursorless Test Case Component Page</title> |
| 91 | + </Head> |
| 92 | + <TestCaseComponentPage data={data} debug={true} /> |
| 93 | + </> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +export default App; |
0 commit comments