-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.cjs
120 lines (118 loc) · 4.98 KB
/
install.cjs
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
const execSync = require("child_process").execSync;
const fs = require("fs");
const os = require("os");
const _path = require("path");
const process = require("process");
function del(path) {
let info;
try {
info = fs.statSync(path);
} catch (e) {
console.log("Failed to get info for " + path);
console.log("Will assume it just does not exist");
console.log(e);
return;
}
if (info.isDirectory()) {
const dir = fs.readdirSync(path);
for (let i = 0; i < dir.length; i++) {
del(_path.join(path, dir[i]));
}
fs.rmdirSync(path);
} else if (info.isFile() || info.isSymbolicLink()) {
fs.unlinkSync(path);
} else {
throw new Error("Only know how to use files and directories");
}
}
/**
* @param {string} command
* @param {string=} cwd
*/
function exec(command, cwd) {
if (cwd) {
console.log(" " + cwd + ":");
} else {
cwd = process.cwd();
}
console.log(" " + command);
execSync(command, {
stdio: "inherit",
cwd,
});
}
console.log("It is recommended to install the package globally and then use:");
console.log("npm link TdNode");
console.log("locally. Doing so could give a boost to startup time");
console.log();
console.log("Note: Building can take a LONG time.");
console.log();
if (process.platform === "win32") {
if (!process.env.VCPKG_DOWNLOADS) {
console.warn("Note: If installing this module to multiple places in the system, it is recommended that you set the VCPKG_DOWNLOADS environment variable to a dedicated folder. Doing so can reduce future build times significantly.");
}
console.log("Executing vcpkg bootstrap");
exec("bootstrap-vcpkg.bat", "./vcpkg");
console.log("Installing vcpkg dependencies");
exec("vcpkg.exe install openssl:" + os.arch() + "-windows zlib:" + os.arch() + "-windows", "./vcpkg");
console.log("Deleting last TDLib build");
del("./td/build");
console.log("Creating new TDLib build directory");
fs.mkdirSync("./td/build");
console.log("Executing cmake preparation");
exec("cmake -A " + process.arch + " -DCMAKE_INSTALL_PREFIX:PATH=../../tdlib -DCMAKE_TOOLCHAIN_FILE:FILEPATH=../../vcpkg/scripts/buildsystems/vcpkg.cmake ..", "./td/build");
console.log("Executing cmake build");
exec("cmake --build . --target install --config Release", "./td/build");
console.log("Copying dll files up to module root");
for (const i of fs.readdirSync("./td/build/Release")) {
if (i.endsWith(".dll")) {
console.log(" " + i);
fs.copyFileSync("./td/build/Release/" + i, "./" + i);
}
}
console.log("Parsing the schema");
const schema = require("./parsing_tl.cjs");
console.log("Saving JSON schema to td_api.json");
fs.writeFileSync("./td_api.json", JSON.stringify(schema.TD_API));
console.log("Generating TypeScript definitions for schema");
const ts_def = require("./generate_tl_ts.cjs");
console.log("Saving TypeScript definitions for schema");
fs.writeFileSync("./index.d.ts", ts_def.result);
require("./generate_converters.cjs");
console.log("Configuring TdNode build");
exec("cmake-js configure", ".");
console.log("Building TdNode");
exec("cmake-js build", ".");
console.log("Copying build result");
fs.copyFileSync("./build/Release/TdNode.node", "./TdNode.node");
} else if (process.platform === "linux") {
console.log("It is assumed that you have all TDLib dependencies already installed");
console.log("Deleting last TDLib build");
del("./td/build");
console.log("Creating new TDLib build directory");
fs.mkdirSync("./td/build");
process.env.CXXFLAGS = "";
console.log("Configuring TDLib build");
exec("cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=../../tdlib -DTD_ENABLE_LTO=ON ..", "./td/build");
console.log("Building TDLib");
exec("cmake --build . --target install --config Release", "./td/build");
console.log("Parsing the schema");
const schema = require("./parsing_tl.cjs");
console.log("Saving JSON schema to td_api.json");
fs.writeFileSync("./td_api.json", JSON.stringify(schema.TD_API));
console.log("Generating TypeScript definitions for schema");
const ts_def = require("./generate_tl_ts.cjs");
console.log("Saving TypeScript definitions for schema");
fs.writeFileSync("./index.d.ts", ts_def.result);
require("./generate_converters.cjs");
console.log("Configuring TdNode build");
exec("cmake-js configure", ".");
console.log("Building TdNode");
exec("cmake-js build", ".");
console.log("Copying build result");
fs.copyFileSync("./build/Release/TdNode.node", "./TdNode.node");
} else {
throw new Error("Platform does not support an automatic build. You can contribute to the project by creating a set of build instructions for your system and submitting a pull request at https://github.com/puppy0cam/TdNode");
}
console.log("Done. Now running tests.");
require("./tests.cjs");