-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·168 lines (155 loc) · 4.84 KB
/
index.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env node
import ergogen from "ergogen";
import fs from "node:fs/promises";
import http from "node:http";
import path from "node:path";
import yargs from "yargs";
async function generateErgogenResults(filename) {
const doc = await fs.readFile(filename).then((it) => it.toString());
return await ergogen.process(doc, true, () => null);
}
async function injectFootprints(directory) {
for (const filename of await fs.readdir(directory)) {
const name = path.parse(filename).name;
console.log("Injecting footprint:", name);
const data = await import("./" + path.join(directory, filename));
ergogen.inject("footprint", name, data.default);
}
}
async function writeOutputFiles(data, directory) {
await fs.mkdir(directory, { recursive: true });
for (const [name, content] of new Map(Object.entries(data.pcbs))) {
const filename = path.join(directory, name + ".kicad_pcb");
console.log("Writing file:", filename);
await fs.writeFile(filename, content);
}
}
const html = `
<!DOCTYPE html>
<html>
<head>
<style>
svg {
max-width: 100%;
max-height: 100%;
}
</style>
</head>
<body>
<div id="target">Edit ergogen.yml to see changes live!</div>
<select id="select">
</select>
<script>
function setOptions(select, data) {
const mappings = new Map(Object.entries(Object.assign({ demo: data.demo }, data.outlines)));
const options = new Map(Array.from(select.childNodes).map((it) => [it.innerText, it]));
for (const [key, value] of mappings) {
if (key.startsWith("_")) {
continue;
} else if (options.get(key)) {
options.get(key).value = value.svg;
} else {
const option = document.createElement("option");
option.innerText = key;
option.value = value.svg;
select.appendChild(option);
}
}
for (const [key, option] of options) {
if (!mappings.get(key)) {
option.remove();
}
}
}
const select = document.getElementById("select");
const target = document.getElementById("target");
select.addEventListener('change', (event) => {
target.innerHTML = event.target.value;
});
const source = new EventSource('/reload')
source.addEventListener('message', (event) => {
const data = JSON.parse(event.data);
setOptions(select, data);
target.innerHTML = select.value;
})
source.addEventListener('error', (event) => {
window.location.reload();
})
</script>
</body>
</html>
`;
const requestListener = (filename) => async (req, res) => {
switch (req.url) {
case "/reload":
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
});
try {
let results = await generateErgogenResults(filename);
res.write("data: " + JSON.stringify(results) + "\n\n");
console.log("New connection...");
} catch (err) {
console.log(err);
}
(async () => {
const watcher = fs.watch(filename);
for await (const event of watcher) {
try {
let results = await generateErgogenResults(filename);
res.write("data: " + JSON.stringify(results) + "\n\n");
console.log("Reloading...");
} catch (err) {
console.log(err);
}
}
})();
break;
default:
res.writeHead(200, {
"Content-Type": "text/html",
});
res.end(html);
}
};
yargs()
.scriptName("cystic-cli")
.usage("$0 <cmd> [args]")
.command({
command: "build <config>",
description: "Build full Ergogen configuration",
builder: (yargs) => {
yargs.option("output", {
description: "Output directory",
demandOption: true,
});
},
handler: async (argv) => {
await injectFootprints("footprints");
console.log("Generating ergogen:", argv.config);
const data = await generateErgogenResults(argv.config);
await writeOutputFiles(data, argv.output);
},
})
.command({
command: "preview <config>",
description: "Run auto-reloading Ergogen preview server",
handler: async (argv) => {
await injectFootprints("footprints");
try {
console.log("Generating ergogen:", argv.config);
// These results are thrown away, the generation is only run
// so we can show an error immediately if there are problems.
await generateErgogenResults(argv.config);
} catch (err) {
console.log(err);
}
console.log("Starting server at:", "http://localhost:8080/");
const server = http.createServer(requestListener(argv.config));
server.listen(8080);
},
})
.demandCommand(1)
.parse(process.argv.slice(2));