-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathprepareBuild.js
148 lines (139 loc) · 4.61 KB
/
prepareBuild.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
const path = require("path");
const fs = require("fs");
function getZeroMQPreBuildsFoldersToKeep() {
// Possible values of 'VSC_VSCE_TARGET' include platforms supported by `vsce package --target`
// See here https://code.visualstudio.com/api/working-with-extensions/publishing-extension#platformspecific-extensions
const vsceTarget = process.env.VSC_VSCE_TARGET;
console.log("vsceTarget", vsceTarget);
if (!vsceTarget) {
// Keep all of them, as we're not building platform specific bundles.
return [];
} else if (vsceTarget === "web") {
throw new Error("Not supported when targeting the Web");
} else if (vsceTarget.includes("win32")) {
if (vsceTarget.includes("x64")) {
return ["win32-x64"];
} else if (vsceTarget.includes("arm64")) {
return ["win32-arm64"];
} else {
return ["win32-x64", "win32-arm64"];
}
} else if (vsceTarget.includes("linux")) {
if (vsceTarget.includes("arm64")) {
return ["linux-arm64"];
} else if (vsceTarget.includes("x64")) {
return ["linux-x64"];
} else if (vsceTarget.includes("arm")) {
return ["linux-arm"];
} else {
return ["linux-arm64", "linux-x64", "linux-arm"];
}
} else if (vsceTarget.includes("alpine")) {
if (vsceTarget.includes("arm64")) {
return ["linux-arm64"];
} else if (vsceTarget.includes("x64")) {
return ["linux-x64"];
} else if (vsceTarget.includes("armhf")) {
return ["linux-arm"];
} else {
return ["linux-arm64", "linux-x64", "linux-arm"];
}
} else if (vsceTarget.includes("darwin")) {
if (vsceTarget.includes("arm64")) {
return ["darwin-arm64"];
} else if (vsceTarget.includes("x64")) {
return ["darwin-x64"];
} else {
return ["darwin-x64", "darwin-arm64"];
}
} else {
throw new Error(`Unknown platform '${vsceTarget}'}`);
}
}
function shouldCopyFileFromZmqFolder(resourcePath) {
const parentFolder = path.dirname(resourcePath);
if (fs.statSync(resourcePath).isDirectory()) {
return true;
}
// return true;
const filename = path.basename(resourcePath);
// Ensure the code is platform agnostic.
resourcePath = (resourcePath || "")
.toString()
.toLowerCase()
.replace(/\\/g, "/");
// We do not need to bundle these folders
const foldersToIgnore = ["build", "script", "src", "node_modules", "vendor"];
if (
foldersToIgnore.some((folder) =>
resourcePath
.toLowerCase()
.startsWith(
path.join(parentFolder, folder).replace(/\\/g, "/").toLowerCase(),
),
)
) {
return false;
}
if (
resourcePath.endsWith(".js") ||
resourcePath.endsWith(".json") ||
resourcePath.endsWith(".md") ||
resourcePath.endsWith("license")
) {
return true;
}
// if (!resourcePath.includes(path.join(parentFolder, 'prebuilds').replace(/\\/g, '/').toLowerCase())) {
if (!parentFolder.includes(`${path.sep}prebuilds${path.sep}`)) {
// We do not ship any other sub directory.
return false;
}
if (filename.includes("electron.") && resourcePath.endsWith(".node")) {
// We do not ship electron binaries.
return false;
}
const preBuildsFoldersToCopy = getZeroMQPreBuildsFoldersToKeep();
console.log("preBuildsFoldersToCopy", preBuildsFoldersToCopy);
if (preBuildsFoldersToCopy.length === 0) {
// Copy everything from all prebuilds folder.
return true;
}
// Copy if this is a prebuilds folder that needs to be copied across.
// Use path.sep as the delimiter, as we do not want linux-arm64 to get compiled with search criteria is linux-arm.
if (
preBuildsFoldersToCopy.some((folder) =>
resourcePath.includes(`${folder.toLowerCase()}/`),
)
) {
return true;
}
return false;
}
const extensionFolder = path.join(__dirname);
async function deleteUnnecessaryZeromqPrebuilts() {
const vsceTarget = process.env.VSC_VSCE_TARGET;
if (!vsceTarget) {
// Keep all of them, as we're not building platform specific bundles.
console.log("vsceTarget is not set");
return;
}
console.log("deleting ZeroMQ prebuilts");
const necessaryPrebuilds = getZeroMQPreBuildsFoldersToKeep();
const zmqPrebuildFolder = path.join(
extensionFolder,
"dist",
"node_modules",
"zeromq",
"prebuilds",
);
// delete all folders except the ones in necessaryPrebuilds
const files = fs.readdirSync(zmqPrebuildFolder);
for (const file of files) {
if (!necessaryPrebuilds.includes(file)) {
console.log("deleting", path.join(zmqPrebuildFolder, file));
fs.rmSync(path.join(zmqPrebuildFolder, file), { recursive: true });
}
}
console.log("copied ZeroMQ");
}
deleteUnnecessaryZeromqPrebuilts();