storybook/scripts/get-template.ts

85 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-08-12 14:01:38 +10:00
import { readdir } from 'fs/promises';
import { pathExists } from 'fs-extra';
import { resolve } from 'path';
2022-11-07 16:13:47 +01:00
import { allTemplates, templatesByCadence } from '../code/lib/cli/src/repro-templates';
2022-08-12 14:01:38 +10:00
const sandboxDir = process.env.SANDBOX_ROOT || resolve(__dirname, '../sandbox');
2022-08-12 14:01:38 +10:00
2022-11-07 16:13:47 +01:00
export type Cadence = keyof typeof templatesByCadence;
2022-08-12 14:01:38 +10:00
export type Template = {
cadence?: readonly Cadence[];
skipTasks?: string[];
2022-08-12 14:01:38 +10:00
// there are other fields but we don't use them here
};
2022-11-07 16:13:47 +01:00
export type TemplateKey = keyof typeof allTemplates;
2022-08-12 14:01:38 +10:00
export type Templates = Record<TemplateKey, Template>;
async function getDirectories(source: string) {
return (await readdir(source, { withFileTypes: true }))
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);
}
export async function getTemplate(
cadence: Cadence,
scriptName: string,
{ index, total }: { index: number; total: number }
) {
2022-08-12 16:20:03 +10:00
let potentialTemplateKeys: TemplateKey[] = [];
2022-08-12 15:36:16 +10:00
if (await pathExists(sandboxDir)) {
2022-08-12 14:01:38 +10:00
const sandboxes = await getDirectories(sandboxDir);
potentialTemplateKeys = sandboxes
.map((dirName) => {
2022-11-07 16:13:47 +01:00
return Object.keys(allTemplates).find(
2022-08-12 14:01:38 +10:00
(templateKey) => templateKey.replace('/', '-') === dirName
);
})
2022-10-05 15:55:57 +11:00
.filter(Boolean) as TemplateKey[];
2022-08-12 14:01:38 +10:00
}
if (potentialTemplateKeys.length === 0) {
2022-11-07 16:13:47 +01:00
const cadenceTemplates = Object.entries(allTemplates).filter(([key]) =>
templatesByCadence[cadence].includes(key as TemplateKey)
2022-08-12 14:01:38 +10:00
);
2022-10-05 15:55:57 +11:00
potentialTemplateKeys = cadenceTemplates.map(([k]) => k) as TemplateKey[];
2022-08-12 14:01:38 +10:00
}
2022-10-05 15:55:57 +11:00
potentialTemplateKeys = potentialTemplateKeys.filter(
2022-11-07 16:13:47 +01:00
(t) => !(allTemplates[t] as Template).skipTasks?.includes(scriptName)
2022-10-05 15:55:57 +11:00
);
2022-08-12 14:01:38 +10:00
if (potentialTemplateKeys.length !== total) {
throw new Error(`Circle parallelism set incorrectly.
Parallelism is set to ${total}, but there are ${
potentialTemplateKeys.length
} templates to run:
${potentialTemplateKeys.join(', ')}
`);
}
return potentialTemplateKeys[index];
}
async function run() {
const [, , cadence, scriptName] = process.argv;
if (!cadence) throw new Error('Need to supply cadence to get template script');
const { CIRCLE_NODE_INDEX = 0, CIRCLE_NODE_TOTAL = 1 } = process.env;
console.log(
await getTemplate(cadence as Cadence, scriptName, {
index: +CIRCLE_NODE_INDEX,
total: +CIRCLE_NODE_TOTAL,
})
);
}
if (require.main === module) {
2022-08-12 15:37:20 +10:00
run().catch((err) => {
console.error(err);
process.exit(1);
});
2022-08-12 14:01:38 +10:00
}