forked from ProjectEvergreen/greenwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.js
More file actions
137 lines (117 loc) · 4.16 KB
/
Copy pathcompile.js
File metadata and controls
137 lines (117 loc) · 4.16 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { checkResourceExists } from "../lib/resource-utils.js";
import { generateGraph } from "./graph.js";
import { initContext } from "./context.js";
import { readAndMergeConfig } from "./config.js";
import fs from "node:fs/promises";
const generateCompilation = async () => {
let compilation = {
graph: [],
context: {},
config: {},
// TODO put resources into manifest
resources: new Map(),
manifest: {
apis: new Map(),
},
collections: {},
};
console.info("Initializing project config");
compilation.config = await readAndMergeConfig();
// determine whether to use default layout or user detected workspace
console.info("Initializing project workspace contexts");
compilation.context = await initContext(compilation);
const { scratchDir, outputDir } = compilation.context;
if (process.env.__GWD_COMMAND__ !== "serve") {
// clear out the pre-build output dir first if it exists
if (await checkResourceExists(scratchDir)) {
await fs.rm(scratchDir, {
recursive: true,
});
}
// prep an empty directory for any pre-build output
await fs.mkdir(scratchDir, {
recursive: true,
});
}
if (process.env.__GWD_COMMAND__ === "build") {
// clear out the output dir first before we generate any build output
if (await checkResourceExists(outputDir)) {
await fs.rm(outputDir, {
recursive: true,
});
}
// prep an empty directory for the build output
await fs.mkdir(outputDir, {
recursive: true,
});
}
if (process.env.__GWD_COMMAND__ === "serve") {
console.info("Loading graph from build output...");
if (!(await checkResourceExists(new URL("./graph.json", outputDir)))) {
return Promise.reject(
new Error("No build output detected. Make sure you have run greenwood build"),
);
}
compilation.graph = JSON.parse(await fs.readFile(new URL("./graph.json", outputDir), "utf-8"));
if (await checkResourceExists(new URL("./manifest.json", outputDir))) {
console.info("Loading manifest from build output...");
// TODO put reviver into a utility?
const manifest = JSON.parse(
// @ts-expect-error see https://github.com/microsoft/TypeScript/issues/42866
await fs.readFile(new URL("./manifest.json", outputDir)),
function reviver(key, value) {
if (typeof value === "object" && value !== null) {
if (value.dataType === "Map") {
return new Map(value.value);
}
}
return value;
},
);
compilation.manifest = manifest;
}
if (await checkResourceExists(new URL("./resources.json", outputDir))) {
console.info("Loading resources from build output...");
// TODO put reviver into a utility?
const resources = JSON.parse(
// @ts-expect-error see https://github.com/microsoft/TypeScript/issues/42866
await fs.readFile(new URL("./resources.json", outputDir)),
function reviver(key, value) {
if (typeof value === "object" && value !== null) {
if (value.dataType === "Map") {
// revive URLs
if (value.value.sourcePathURL) {
value.value.sourcePathURL = new URL(value.value.sourcePathURL);
}
return new Map(value.value);
}
}
return value;
},
);
compilation.resources = resources;
}
} else {
// generate a graph of all pages / components to build
console.info("Generating graph of workspace files...");
compilation = await generateGraph(compilation);
// https://stackoverflow.com/a/56150320/417806
// TODO put reviver into a util?
await fs.writeFile(
new URL("./manifest.json", scratchDir),
JSON.stringify(compilation.manifest, (key, value) => {
if (value instanceof Map) {
return {
dataType: "Map",
value: [...value],
};
} else {
return value;
}
}),
);
await fs.writeFile(new URL("./graph.json", scratchDir), JSON.stringify(compilation.graph));
}
return Promise.resolve(compilation);
};
export { generateCompilation };