Skip to content

Commit 9663799

Browse files
committed
refactor: Move import and process logic into standalone function
1 parent 339e4f3 commit 9663799

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

packages/cursorless-org/src/pages/component-sheet.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import { cheatsheetBodyClasses } from "@cursorless/cheatsheet";
1212

1313
const fixturesDir = path.join("../", "../", "data", "example-files");
1414

15-
async function loadYamlFiles(dir: string, selectedFiles?: string[]) {
15+
async function loadYamlFiles(dir: string, allowList?: string[]) {
1616
const directoryPath = path.join(process.cwd(), dir);
1717
const files = fs.readdirSync(directoryPath);
1818
const data: any[] = [];
1919

2020
files.forEach((file) => {
2121
if (
2222
path.extname(file) === ".yml" &&
23-
(!selectedFiles || selectedFiles.includes(file))
23+
(!allowList || allowList.includes(file))
2424
) {
2525
try {
2626
const filePath = path.join(directoryPath, file);
@@ -37,15 +37,31 @@ async function loadYamlFiles(dir: string, selectedFiles?: string[]) {
3737
return data;
3838
}
3939

40-
// See https://github.com/vercel/next.js/discussions/12325#discussioncomment-1116108
41-
export async function getStaticProps() {
42-
const dataActions = await loadYamlFiles(fixturesDir, testSelectedFiles);
40+
// Change argument to a single object for loadAndProcessFixtures
41+
interface LoadAndProcessFixturesOptions {
42+
fixturesDir: string;
43+
allowList?: string[];
44+
}
4345

46+
/**
47+
* Loads YAML test case files from a directory, processes them into fixtures, and returns an array of processed test case data.
48+
* Optionally filters which files to load using an allow list.
49+
*
50+
* @param {Object} options - Options for loading and processing fixtures.
51+
* @param {string} options.fixturesDir - Directory containing YAML fixture files.
52+
* @param {string[]=} options.allowList - Optional list of filenames to include.
53+
* @returns {Promise<any[]>} Array of processed test case data, each with a `raw` property containing the original YAML object.
54+
*/
55+
async function loadAndProcessFixtures({
56+
fixturesDir,
57+
allowList,
58+
}: LoadAndProcessFixturesOptions) {
59+
const dataActions = await loadYamlFiles(fixturesDir, allowList);
4460
const data_errors: any[] = [];
4561

4662
const data = (
4763
await Promise.all(
48-
[...dataActions].map(async (val) => {
64+
dataActions.map(async (val) => {
4965
try {
5066
const fixture = await loadTestCaseFixture(val);
5167
return { ...fixture, raw: val };
@@ -56,12 +72,21 @@ export async function getStaticProps() {
5672
}
5773
}),
5874
)
59-
).filter((test) => test !== undefined);
75+
).filter((test) => test != null);
6076

6177
if (data_errors.length > 0) {
6278
console.error("data errors:", data_errors);
6379
}
6480

81+
return data;
82+
}
83+
84+
// See https://github.com/vercel/next.js/discussions/12325#discussioncomment-1116108
85+
export async function getStaticProps() {
86+
const data = await loadAndProcessFixtures({
87+
fixturesDir,
88+
allowList: testSelectedFiles,
89+
});
6590
return { props: { data, bodyClasses: cheatsheetBodyClasses } };
6691
}
6792

0 commit comments

Comments
 (0)