-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundleProjectExamples.ts
84 lines (78 loc) · 2.56 KB
/
bundleProjectExamples.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import fs from "fs-extra";
import path from "node:path";
import { resolveConfig, format } from "prettier";
import type { RawProject } from "./src/lib/classes/automaton/raw/RawProject";
import { DirectoryHandleNodejs } from "./src/lib/classes/fileSystem/FileSystemNodejs";
import { FileStructureProject } from "./src/lib/classes/projectHandler/FileStructure";
import { serializeRaw } from "./src/lib/classes/projectHandler/zodSerializers";
import chalk from "chalk";
const sourcePath = "./Ecdar-Common/Project-Examples/examples/";
const bundlePath = "./src/lib/projectExamples";
export const bundleProjectExamples = {
name: "Bundle project examples",
async buildStart() {
try {
await fs.emptyDir(bundlePath);
await fs.remove(`${bundlePath}.ts`);
} catch (error) {
throw new Error(
`Could not remove old project example bundle: ${
(error as Error).message
}`,
);
}
const projects: RawProject[] = [];
try {
const projectNames = await fs.readdir(sourcePath);
await Promise.all(
projectNames.map(async (name) => {
const directoryHandle = new DirectoryHandleNodejs(
name,
path.join(sourcePath, name),
);
const project = await new FileStructureProject().toRaw(
directoryHandle,
);
project.name = name;
projects.push(project);
}),
);
} catch (error) {
throw new Error(
`Could not load project examples: ${(error as Error).message}`,
);
}
try {
projects.sort((a, b): number =>
(a.name ?? "").localeCompare(b.name ?? "", "en"),
);
for (const project of projects) {
const filepath = path.join(bundlePath, `${project.name}.ts`);
const projectCode = `export default ${serializeRaw(project)}`;
await fs.writeFile(filepath, projectCode);
}
let projectsCode = `export const projectExamples = {`;
for (const project of projects) {
projectsCode += `"${project.name}": async () => {return (await import("./projectExamples/${project.name}")).default},`;
}
projectsCode += `}`;
const filepath = `${bundlePath}.ts`;
const prettierConfig = await resolveConfig(filepath, {
editorconfig: true,
});
projectsCode = await format(projectsCode, {
filepath,
...prettierConfig,
});
await fs.writeFile(filepath, projectsCode);
} catch (error) {
throw new Error(
`Writing project example bundle to lib: ${
(error as Error).message
}`,
);
}
/* eslint-disable-next-line no-console -- It is okay to do this in the build phase to let the dev know what is happening */
console.log(`${chalk.green("✔")} bundled project examples`);
},
};