-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.mts
64 lines (51 loc) · 2.27 KB
/
setup.mts
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
import { promises as fs } from 'fs';
const prepareDir = async (dir: string) => {
try {
await fs.rm(dir, { recursive: true, force: true });
await fs.mkdir(dir, { recursive: true });
} catch (error) {
console.error(`Error preparing directory: ${dir} ${error}`);
}
}
const downloadFile = async (url: string, destinationFile: string) => {
const content = await (await fetch(url)).text();
await fs.writeFile(destinationFile, content);
}
const modifyFile = async (file: string, callback: (content: string) => string) => {
const content = (await fs.readFile(file)).toString();
const newContent = callback(content);
await fs.writeFile(file, newContent);
}
const downloadEspHomeSchemas = async () => {
const fileList: any[] = await (await fetch("https://api.github.com/repos/esphome/dashboard/contents/schema")).json();
const targetRoot = "./public/esphome_schemas";
await prepareDir(targetRoot);
const promises = fileList.map((file) => downloadFile(file.download_url, `${targetRoot}/${file.name}`));
await Promise.all(promises)
console.log(`Downloaded ${promises.length} EspHome schema files`);
}
const downloadEspHomeMonacoFiles = async () => {
const downloadSrcEditor = async (fileName: string) =>
downloadFile(`https://raw.githubusercontent.com/esphome/dashboard/main/src/editor/${fileName}`, `${targetRoot}/${fileName}`);
const targetRoot = "./src/3rd-party/esphome-dashboard/src/editor";
await prepareDir(`${targetRoot}`);
await prepareDir(`${targetRoot}/utils`);
const promises = [
"completions-handler.ts",
"definition-handler.ts",
"editor-shims.ts",
"esphome-document.ts",
"esphome-schema.ts",
"hover-handler.ts",
"utils/objects.ts",
"utils/text-buffer.ts",
]
.map((file) => downloadSrcEditor(file));
await Promise.all(promises)
console.log(`Downloaded ${promises.length} EspHome Monaco-Editor files`);
await modifyFile(`${targetRoot}/editor-shims.ts`, (content) =>
content.replace("static/schema/${name}.json", "./esphome_schemas/${name}.json")
);
}
await downloadEspHomeSchemas();
await downloadEspHomeMonacoFiles();