forked from streetwriters/notesnook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-sources.mjs
161 lines (137 loc) · 4.57 KB
/
generate-sources.mjs
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
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import fs from "fs/promises";
import os from "os";
import fsSync from "fs";
import path from "path";
import { spawnSync } from "child_process";
import { fileURLToPath } from "url";
const customRoot = process.argv[2] === "--root";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ROOT_PATH = customRoot ? process.argv[3] : path.join(__dirname, "..");
const IS_ROOT_URL = ROOT_PATH.startsWith("https://");
function execute(cmd, args) {
spawnSync(cmd, args, { env: process.env, stdio: "inherit" });
}
function joinPath(url, ...segments) {
if (url) return segments.join("/");
return path.join(...segments);
}
const directoryTree = {
"package.json": ["package.json"],
"package-lock.json": ["package-lock.json"],
apps: {
desktop: {
"package.json": ["apps", "desktop", "package.json"],
"package-lock.json": ["apps", "desktop", "package-lock.json"]
}
},
packages: {
sodium: {
"package.json": ["packages", "sodium", "package.json"],
"package-lock.json": ["packages", "sodium", "package-lock.json"]
},
crypto: {
"package.json": ["packages", "crypto", "package.json"],
"package-lock.json": ["packages", "crypto", "package-lock.json"]
}
}
};
async function createDirectoryTree(tree, dest) {
if (!fsSync.existsSync(dest)) await fs.mkdir(dest, { recursive: true });
for (const key in tree) {
const value = tree[key];
if (typeof value === "object" && Array.isArray(value)) {
const src = joinPath(IS_ROOT_URL, ROOT_PATH, ...value);
if (IS_ROOT_URL) {
console.log("Downloading", src);
const response = await fetch(src);
if (!response.ok) throw new Error(`Failed to download ${src}`);
await fs.writeFile(
path.join(dest, path.basename(src)),
Buffer.from(await response.arrayBuffer())
);
} else {
await fs.copyFile(src, path.join(dest, path.basename(src)));
}
} else if (typeof value === "object") {
await createDirectoryTree(value, path.join(dest, key));
}
}
}
const TEMP_FOLDER = await fs.mkdtemp(path.join(os.tmpdir(), "nn-"));
await createDirectoryTree(directoryTree, TEMP_FOLDER);
console.log("Created directory tree at", TEMP_FOLDER);
const lockfiles = [
{
name: "main",
path: path.join(TEMP_FOLDER, "package-lock.json"),
ignoreDev: true
},
{
name: "desktop",
path: path.join(TEMP_FOLDER, "apps", "desktop", "package-lock.json")
},
{
name: "sodium",
path: path.join(TEMP_FOLDER, "packages", "sodium", "package-lock.json")
},
{
name: "crypto",
path: path.join(TEMP_FOLDER, "packages", "crypto", "package-lock.json")
}
];
process.chdir(TEMP_FOLDER);
let allSources = [];
for (const lockfile of lockfiles) {
if (!fsSync.existsSync(lockfile.path)) {
console.error("skipping. lockfile does not exist at", lockfile.path);
continue;
}
console.log("generating sources for", lockfile.path);
const output = path.join(TEMP_FOLDER, `${lockfile.name}-sources.json`);
execute(
`flatpak-node-generator`,
[
lockfile.ignoreDev ? "--no-devel" : false,
"npm",
lockfile.path,
"-o",
output
].filter(Boolean)
);
const sources = JSON.parse(await fs.readFile(output, "utf-8"));
allSources = [...allSources, ...sources];
await fs.rm(output, { force: true });
}
console.log("writing sources");
for (const source of allSources) {
for (let key in source) {
const value = source[key];
if (typeof value === "string" && value.includes("\\"))
source[key] = source[key].replace(/\\/gm, "/");
else if (Array.isArray(value)) {
value.forEach((elem, index) => {
value[index] = elem.replace(/\\/gm, "/");
});
}
}
}
await fs.writeFile(
path.join(__dirname, "..", "generated-sources.json"),
JSON.stringify(allSources, undefined, 4)
);
console.log("done");