forked from gram-js/gramjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_webpack.js
142 lines (130 loc) · 4.46 KB
/
generate_webpack.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
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const webpackConfig = require("./webpack.config");
webpackConfig.entry = path.resolve(__dirname, "browser/index.js");
/**
* Generates a webpack build and put it in browser folder
*/
function addBuffer(dir) {
fs.readdirSync(dir).forEach((file) => {
let fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
addBuffer(fullPath);
} else {
if (
(fullPath.endsWith(".ts") || fullPath.endsWith(".js")) &&
(!fullPath.endsWith(".d.ts") ||
fullPath.endsWith("api.d.ts") ||
fullPath.endsWith("define.d.ts"))
) {
const tsFile = fs.readFileSync(fullPath, "utf8");
if (tsFile.includes("Buffer")) {
const newTsFile = 'import { Buffer } from "buffer/";\n' + tsFile;
fs.writeFileSync(fullPath, newTsFile, "utf8");
}
}
}
});
}
function renameFiles(dir, action) {
fs.readdirSync(dir).forEach((file) => {
let fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
renameFiles(fullPath, action);
} else {
if (fullPath.includes("example")) {
fs.unlinkSync(fullPath);
}
if (fullPath.includes("-BROWSER")) {
console.log(action, fullPath);
if (action === "rename") {
fs.renameSync(fullPath, fullPath.replace("-BROWSER", ""));
} else if (action === "delete") {
fs.unlinkSync(fullPath);
}
}
}
});
}
function copyFolderSync(from, to) {
fs.mkdirSync(to);
fs.readdirSync(from).forEach((element) => {
if (fs.lstatSync(path.join(from, element)).isFile()) {
fs.copyFileSync(path.join(from, element), path.join(to, element));
} else {
copyFolderSync(path.join(from, element), path.join(to, element));
}
});
}
fs.rmSync("browser", { recursive: true, force: true });
fs.rmSync("tempBrowser", { recursive: true, force: true });
copyFolderSync("gramjs", "tempBrowser");
addBuffer("tempBrowser");
renameFiles("tempBrowser", "rename");
const tsconfig = fs.readFileSync("tsconfig.json", "utf8");
let newTsconfig = tsconfig.replace(/\.\/dist/g, "./browser");
newTsconfig = newTsconfig.replace(/gramjs/g, "tempBrowser");
fs.writeFileSync("tsconfig.json", newTsconfig, "utf8");
const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
const oldValueStorage = packageJSON.dependencies["node-localstorage"];
const oldValueSocks = packageJSON.dependencies["socks"];
delete packageJSON.dependencies["node-localstorage"];
delete packageJSON.dependencies["socks"];
fs.writeFileSync(
"package.json",
JSON.stringify(packageJSON, null, " "),
"utf8"
);
const npmi = exec("npm i");
npmi.on("close", (code) => {
if (code !== 0) {
throw new Error("Error happened " + code);
}
const tsc = exec("npx tsc");
tsc.stdout.on("data", function (data) {
console.log("stdout: " + data.toString());
});
tsc.stderr.on("data", function (data) {
console.error("stderr: " + data.toString());
});
tsc.on("close", (code) => {
if (code !== 0) {
throw new Error("Error happened " + code);
}
fs.copyFileSync("package.json", "browser/package.json");
fs.copyFileSync("README.md", "browser/README.md");
fs.copyFileSync("LICENSE", "browser/LICENSE");
fs.copyFileSync("gramjs/tl/api.d.ts", "browser/tl/api.d.ts");
fs.copyFileSync("gramjs/define.d.ts", "browser/define.d.ts");
fs.rmSync("tempBrowser", { recursive: true, force: true });
const tsconfig = fs.readFileSync("tsconfig.json", "utf8");
let newTsconfig = tsconfig.replace(/\.\/browser/g, "./dist");
newTsconfig = newTsconfig.replace(/tempBrowser/g, "gramjs");
fs.writeFileSync("tsconfig.json", newTsconfig, "utf8");
const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
packageJSON.dependencies["node-localstorage"] = oldValueStorage;
packageJSON.dependencies["socks"] = oldValueSocks;
fs.writeFileSync(
"package.json",
JSON.stringify(packageJSON, null, " "),
"utf8"
);
webpack(webpackConfig, (err, stats) => {
if (err || stats.hasErrors()) {
console.log("SOME ERROR HAPPENED");
process.exit(0);
}
if (process.env.CI) {
exec("npm ci");
} else {
exec("npm i");
}
console.log(
"DONE!. File created at ",
path.resolve(__dirname, "browser/telegram.js")
);
});
});
});