forked from onivim/oni2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-extension.js
71 lines (56 loc) · 2.31 KB
/
install-extension.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
const yauzl = require("yauzl");
const fs = require("fs");
const fse = require("fs-extra");
const path = require("path");
const extensionPath = process.argv[2];
const extensionFolder = process.argv[3];
const extensionName = process.argv[4] || path.basename(extensionPath, path.extname(extensionPath));
const extensionVersion = process.argv[5] || "";
const extensionDestFolder = extensionName + extensionVersion;
if (!path.isAbsolute(extensionPath) || !fs.existsSync(extensionPath) || fs.lstatSync(extensionPath).isDirectory()) {
throw "Extension path must be an absolute path to an extension"
}
if (!path.isAbsolute(extensionFolder) || !fs.lstatSync(extensionFolder).isDirectory()) {
throw "Extension folder must be an absolute path"
}
const zipFilePath = extensionPath;
const destPath = extensionFolder;
const sourcePathRegex = new RegExp("^extension/");
// From: https://github.com/microsoft/vscode/blob/c2e8b28a9a2396d3f7c1af731fe3cdbca9fa46bf/src/vs/base/node/zip.ts#L51
function modeFromEntry(entry) {
const attr = entry.externalFileAttributes >> 16 || 33188;
return [448 /* S_IRWXU */, 56 /* S_IRWXG */, 7 /* S_IRWXO */]
.map(mask => attr & mask)
.reduce((a, b) => a + b, attr & 61440 /* S_IFMT */);
}
yauzl.open(zipFilePath, {lazyEntries: true}, (err, zipfile) => {
if (err) throw err;
zipfile.readEntry();
zipfile.on("entry", (entry) => {
if (!sourcePathRegex.test(entry.fileName)) {
// Directory file names end with '/'
// Note that entries for directories themselves are optional
zipfile.readEntry();
} else {
// file entry
console.log("Opening read stream: " + entry.fileName);
const fileName = entry.fileName.replace(sourcePathRegex, '');
const outputPath = path.join(destPath, extensionDestFolder, fileName);
const outputDirectory = path.dirname(outputPath);
console.log("Creating directory: " + outputDirectory);
fse.mkdirpSync(outputDirectory);
zipfile.openReadStream(entry, (err, readStream) => {
if (err) throw err;
const mode = modeFromEntry(entry);
const istream = fs.createWriteStream(outputPath, { mode });
readStream.pipe(istream);
readStream.on("end", () => {
zipfile.readEntry();
});
istream.once("error", e => { console.error(e) });
readStream.once("error", e => console.error(e));
})
}
})
});
console.log("Installing extension...");