storybook/scripts/get-template.ts

90 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 {
allTemplates,
templatesByCadence,
type Cadence,
type Template as TTemplate,
type SkippableTask,
} from '../code/lib/cli/src/sandbox-templates';
2023-03-29 14:52:14 +02:00
import { SANDBOX_DIRECTORY } from './utils/constants';
2022-08-12 14:01:38 +10:00
2023-03-29 14:52:14 +02:00
const sandboxDir = process.env.SANDBOX_ROOT || SANDBOX_DIRECTORY;
2022-08-12 14:01:38 +10:00
type Template = Pick<TTemplate, 'inDevelopment' | 'skipTasks'>;
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
}
potentialTemplateKeys = potentialTemplateKeys.filter((t) => {
const currentTemplate = allTemplates[t] as Template;
return (
currentTemplate.inDevelopment !== true &&
!currentTemplate.skipTasks?.includes(scriptName as SkippableTask)
);
});
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
}