Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/asar.js
3520 views
1
"use strict";
2
var __importDefault = (this && this.__importDefault) || function (mod) {
3
return (mod && mod.__esModule) ? mod : { "default": mod };
4
};
5
Object.defineProperty(exports, "__esModule", { value: true });
6
exports.createAsar = createAsar;
7
/*---------------------------------------------------------------------------------------------
8
* Copyright (c) Microsoft Corporation. All rights reserved.
9
* Licensed under the MIT License. See License.txt in the project root for license information.
10
*--------------------------------------------------------------------------------------------*/
11
const path_1 = __importDefault(require("path"));
12
const event_stream_1 = __importDefault(require("event-stream"));
13
const pickle = require('chromium-pickle-js');
14
const Filesystem = require('asar/lib/filesystem');
15
const vinyl_1 = __importDefault(require("vinyl"));
16
const minimatch_1 = __importDefault(require("minimatch"));
17
function createAsar(folderPath, unpackGlobs, skipGlobs, duplicateGlobs, destFilename) {
18
const shouldUnpackFile = (file) => {
19
for (let i = 0; i < unpackGlobs.length; i++) {
20
if ((0, minimatch_1.default)(file.relative, unpackGlobs[i])) {
21
return true;
22
}
23
}
24
return false;
25
};
26
const shouldSkipFile = (file) => {
27
for (const skipGlob of skipGlobs) {
28
if ((0, minimatch_1.default)(file.relative, skipGlob)) {
29
return true;
30
}
31
}
32
return false;
33
};
34
// Files that should be duplicated between
35
// node_modules.asar and node_modules
36
const shouldDuplicateFile = (file) => {
37
for (const duplicateGlob of duplicateGlobs) {
38
if ((0, minimatch_1.default)(file.relative, duplicateGlob)) {
39
return true;
40
}
41
}
42
return false;
43
};
44
const filesystem = new Filesystem(folderPath);
45
const out = [];
46
// Keep track of pending inserts
47
let pendingInserts = 0;
48
let onFileInserted = () => { pendingInserts--; };
49
// Do not insert twice the same directory
50
const seenDir = {};
51
const insertDirectoryRecursive = (dir) => {
52
if (seenDir[dir]) {
53
return;
54
}
55
let lastSlash = dir.lastIndexOf('/');
56
if (lastSlash === -1) {
57
lastSlash = dir.lastIndexOf('\\');
58
}
59
if (lastSlash !== -1) {
60
insertDirectoryRecursive(dir.substring(0, lastSlash));
61
}
62
seenDir[dir] = true;
63
filesystem.insertDirectory(dir);
64
};
65
const insertDirectoryForFile = (file) => {
66
let lastSlash = file.lastIndexOf('/');
67
if (lastSlash === -1) {
68
lastSlash = file.lastIndexOf('\\');
69
}
70
if (lastSlash !== -1) {
71
insertDirectoryRecursive(file.substring(0, lastSlash));
72
}
73
};
74
const insertFile = (relativePath, stat, shouldUnpack) => {
75
insertDirectoryForFile(relativePath);
76
pendingInserts++;
77
// Do not pass `onFileInserted` directly because it gets overwritten below.
78
// Create a closure capturing `onFileInserted`.
79
filesystem.insertFile(relativePath, shouldUnpack, { stat: stat }, {}).then(() => onFileInserted(), () => onFileInserted());
80
};
81
return event_stream_1.default.through(function (file) {
82
if (file.stat.isDirectory()) {
83
return;
84
}
85
if (!file.stat.isFile()) {
86
throw new Error(`unknown item in stream!`);
87
}
88
if (shouldSkipFile(file)) {
89
this.queue(new vinyl_1.default({
90
base: '.',
91
path: file.path,
92
stat: file.stat,
93
contents: file.contents
94
}));
95
return;
96
}
97
if (shouldDuplicateFile(file)) {
98
this.queue(new vinyl_1.default({
99
base: '.',
100
path: file.path,
101
stat: file.stat,
102
contents: file.contents
103
}));
104
}
105
const shouldUnpack = shouldUnpackFile(file);
106
insertFile(file.relative, { size: file.contents.length, mode: file.stat.mode }, shouldUnpack);
107
if (shouldUnpack) {
108
// The file goes outside of xx.asar, in a folder xx.asar.unpacked
109
const relative = path_1.default.relative(folderPath, file.path);
110
this.queue(new vinyl_1.default({
111
base: '.',
112
path: path_1.default.join(destFilename + '.unpacked', relative),
113
stat: file.stat,
114
contents: file.contents
115
}));
116
}
117
else {
118
// The file goes inside of xx.asar
119
out.push(file.contents);
120
}
121
}, function () {
122
const finish = () => {
123
{
124
const headerPickle = pickle.createEmpty();
125
headerPickle.writeString(JSON.stringify(filesystem.header));
126
const headerBuf = headerPickle.toBuffer();
127
const sizePickle = pickle.createEmpty();
128
sizePickle.writeUInt32(headerBuf.length);
129
const sizeBuf = sizePickle.toBuffer();
130
out.unshift(headerBuf);
131
out.unshift(sizeBuf);
132
}
133
const contents = Buffer.concat(out);
134
out.length = 0;
135
this.queue(new vinyl_1.default({
136
base: '.',
137
path: destFilename,
138
contents: contents
139
}));
140
this.queue(null);
141
};
142
// Call finish() only when all file inserts have finished...
143
if (pendingInserts === 0) {
144
finish();
145
}
146
else {
147
onFileInserted = () => {
148
pendingInserts--;
149
if (pendingInserts === 0) {
150
finish();
151
}
152
};
153
}
154
});
155
}
156
//# sourceMappingURL=asar.js.map
157