Skip to content

Commit 5f2a1c6

Browse files
committed
fix: auto-unpacked files are not copied
Closes #843
1 parent 508f7d0 commit 5f2a1c6

File tree

6 files changed

+170
-164
lines changed

6 files changed

+170
-164
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ before_install:
3030
install:
3131
- nvm install $NODE_VERSION
3232
- nvm use --delete-prefix $NODE_VERSION
33+
- mkdir -p ~/Library/Caches/Yarn
3334
- curl -o- -L https://yarnpkg.com/install.sh | bash
3435
- export PATH="$PATH:$HOME/.yarn/bin"
3536
- yarn install --pure-lockfile

nsis-auto-updater/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "electron-auto-updater",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"description": "NSIS Auto Updater",
55
"main": "out/nsis-auto-updater/src/main.js",
66
"author": "Vladimir Krivosheev",
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"bluebird-lst-c": "^1.0.4",
1616
"debug": "^2.3.2",
17-
"fs-extra-p": "^2.0.6",
17+
"fs-extra-p": "^2.0.7",
1818
"ini": "^1.3.4",
1919
"js-yaml": "^3.7.0",
2020
"semver": "^5.3.0",

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@
6262
"archiver": "^1.2.0",
6363
"archiver-utils": "^1.3.0",
6464
"asar-electron-builder": "^0.13.5",
65-
"bluebird-lst-c": "^1.0.4",
65+
"bluebird-lst-c": "^1.0.5",
6666
"chalk": "^1.1.3",
6767
"chromium-pickle-js": "^0.2.0",
6868
"cli-cursor": "^1.0.2",
6969
"cuint": "^0.2.2",
7070
"debug": "^2.3.2",
7171
"electron-download": "2.1.2",
7272
"electron-macos-sign": "1.0.1",
73-
"fs-extra-p": "^2.0.6",
73+
"fs-extra-p": "^2.0.7",
7474
"hosted-git-info": "^2.1.5",
7575
"ini": "^1.3.4",
7676
"isbinaryfile": "^3.0.1",
@@ -92,7 +92,7 @@
9292
"tunnel-agent": "^0.4.3",
9393
"update-notifier": "^1.0.2",
9494
"uuid-1345": "^0.99.6",
95-
"yargs": "^6.3.0"
95+
"yargs": "^6.4.0"
9696
},
9797
"devDependencies": {
9898
"@develar/semantic-release": "^6.3.21",
@@ -106,6 +106,7 @@
106106
"babel-plugin-transform-es2015-parameters": "^6.18.0",
107107
"babel-plugin-transform-es2015-spread": "^6.8.0",
108108
"babel-plugin-transform-inline-imports-commonjs": "^1.2.0",
109+
"babel-register": "^6.18.0",
109110
"decompress-zip": "^0.3.0",
110111
"depcheck": "^0.6.4",
111112
"diff": "^3.0.1",

src/asarUtil.ts

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ function isUnpackDir(path: string, pattern: Minimatch, rawPattern: string): bool
7575
return path.startsWith(rawPattern) || pattern.match(path)
7676
}
7777

78+
function addValue(map: Map<string, Array<string>>, key: string, value: string) {
79+
let list = map.get(key)
80+
if (list == null) {
81+
list = [value]
82+
map.set(key, list)
83+
}
84+
else {
85+
list.push(value)
86+
}
87+
}
88+
7889
class AsarPackager {
7990
private readonly toPack: Array<string> = []
8091
private readonly fs = new Filesystem(this.src)
@@ -104,9 +115,10 @@ class AsarPackager {
104115
return this.srcRealPath
105116
}
106117

107-
async detectUnpackedDirs(files: Array<string>, metadata: Map<string, Stats>, autoUnpackDirs: Set<string>, createDirPromises: Array<Promise<any>>, unpackedDest: string, fileIndexToModulePackageData: Array<BluebirdPromise<string>>) {
118+
async detectUnpackedDirs(files: Array<string>, metadata: Map<string, Stats>, autoUnpackDirs: Set<string>, unpackedDest: string, fileIndexToModulePackageData: Map<number, BluebirdPromise<string>>) {
108119
const packageJsonStringLength = "package.json".length
109-
const readPackageJsonPromises: Array<Promise<any>> = []
120+
const dirToCreate = new Map<string, Array<string>>()
121+
110122
for (let i = 0, n = files.length; i < n; i++) {
111123
const file = files[i]
112124
const index = file.lastIndexOf(NODE_MODULES_PATTERN)
@@ -126,25 +138,14 @@ class AsarPackager {
126138
const nodeModuleDir = file.substring(0, nextSlashIndex)
127139

128140
if (file.length === (nodeModuleDir.length + 1 + packageJsonStringLength) && file.endsWith("package.json")) {
129-
const promise = readJson(file)
130-
131-
if (readPackageJsonPromises.length > MAX_FILE_REQUESTS) {
132-
await BluebirdPromise.all(readPackageJsonPromises)
133-
readPackageJsonPromises.length = 0
134-
}
135-
readPackageJsonPromises.push(promise)
136-
fileIndexToModulePackageData[i] = promise
141+
fileIndexToModulePackageData.set(i, readJson(file).then(it => cleanupPackageJson(it)))
137142
}
138143

139144
if (autoUnpackDirs.has(nodeModuleDir)) {
140145
const fileParent = path.dirname(file)
141146
if (fileParent !== nodeModuleDir && !autoUnpackDirs.has(fileParent)) {
142147
autoUnpackDirs.add(fileParent)
143-
createDirPromises.push(ensureDir(path.join(unpackedDest, path.relative(this.src, fileParent))))
144-
if (createDirPromises.length > MAX_FILE_REQUESTS) {
145-
await BluebirdPromise.all(createDirPromises)
146-
createDirPromises.length = 0
147-
}
148+
addValue(dirToCreate, path.relative(this.src, nodeModuleDir), path.relative(nodeModuleDir, fileParent))
148149
}
149150
continue
150151
}
@@ -167,11 +168,7 @@ class AsarPackager {
167168
let fileParent = path.dirname(file)
168169

169170
// create parent dir to be able to copy file later without directory existence check
170-
createDirPromises.push(ensureDir(path.join(unpackedDest, path.relative(this.src, fileParent))))
171-
if (createDirPromises.length > MAX_FILE_REQUESTS) {
172-
await BluebirdPromise.all(createDirPromises)
173-
createDirPromises.length = 0
174-
}
171+
addValue(dirToCreate, path.relative(this.src, nodeModuleDir), path.relative(nodeModuleDir, fileParent))
175172

176173
while (fileParent !== nodeModuleDir) {
177174
autoUnpackDirs.add(fileParent)
@@ -180,32 +177,35 @@ class AsarPackager {
180177
autoUnpackDirs.add(nodeModuleDir)
181178
}
182179

183-
if (readPackageJsonPromises.length > 0) {
184-
await BluebirdPromise.all(readPackageJsonPromises)
180+
if (fileIndexToModulePackageData.size > 0) {
181+
await BluebirdPromise.all(<any>fileIndexToModulePackageData.values())
185182
}
186-
if (createDirPromises.length > 0) {
187-
await BluebirdPromise.all(createDirPromises)
188-
createDirPromises.length = 0
183+
184+
if (dirToCreate.size > 0) {
185+
// child directories should be not created asynchronously - parent directories should be created first
186+
await BluebirdPromise.map(dirToCreate.keys(), async (it) => {
187+
const base = path.join(unpackedDest, it)
188+
await ensureDir(base)
189+
await BluebirdPromise.each(dirToCreate.get(it)!, it => ensureDir(path.join(base, it)))
190+
}, concurrency)
189191
}
190192
}
191193

192194
async createPackageFromFiles(files: Array<string>, metadata: Map<string, Stats>) {
193195
// search auto unpacked dir
194196
const autoUnpackDirs = new Set<string>()
195-
196-
const createDirPromises: Array<Promise<any>> = [ensureDir(path.dirname(this.outFile))]
197197
const unpackedDest = `${this.outFile}.unpacked`
198-
199-
const fileIndexToModulePackageData: Array<BluebirdPromise<string>> = new Array(files.length)
198+
const fileIndexToModulePackageData = new Map<number, BluebirdPromise<string>>()
200199
if (this.options.smartUnpack !== false) {
201-
await this.detectUnpackedDirs(files, metadata, autoUnpackDirs, createDirPromises, unpackedDest, fileIndexToModulePackageData)
200+
await this.detectUnpackedDirs(files, metadata, autoUnpackDirs, unpackedDest, fileIndexToModulePackageData)
202201
}
203202

204203
const unpackDir = this.options.unpackDir == null ? null : new Minimatch(this.options.unpackDir)
205204
const unpack = this.options.unpack == null ? null : new Minimatch(this.options.unpack, {
206205
matchBase: true
207206
})
208207

208+
const createDirPromises: Array<Promise<any>> = [ensureDir(path.dirname(this.outFile))]
209209
const copyPromises: Array<Promise<any>> = []
210210
const mainPackageJson = path.join(this.src, "package.json")
211211
for (let i = 0, n = files.length; i < n; i++) {
@@ -220,15 +220,15 @@ class AsarPackager {
220220
createDirPromises.length = 0
221221
}
222222

223-
const packageDataPromise = fileIndexToModulePackageData[i]
223+
const packageDataPromise = fileIndexToModulePackageData.get(i)
224224
let newData: any | null = null
225225
if (packageDataPromise == null) {
226226
if (this.options.extraMetadata != null && file === mainPackageJson) {
227227
newData = JSON.stringify(deepAssign(await readJson(file), this.options.extraMetadata), null, 2)
228228
}
229229
}
230230
else {
231-
newData = cleanupPackageJson(packageDataPromise.value())
231+
newData = packageDataPromise.value()
232232
}
233233

234234
const fileSize = newData == null ? stat.size : Buffer.byteLength(newData)
@@ -238,9 +238,15 @@ class AsarPackager {
238238
node.unpacked = true
239239

240240
if (!dirNode.unpacked) {
241-
createDirPromises.push(ensureDir(path.join(unpackedDest, path.relative(this.src, fileParent))))
242-
await BluebirdPromise.all(createDirPromises)
243-
createDirPromises.length = 0
241+
const promise = ensureDir(path.join(unpackedDest, path.relative(this.src, fileParent)))
242+
if (createDirPromises.length === 0) {
243+
await createDirPromises
244+
}
245+
else {
246+
createDirPromises.push(promise)
247+
await BluebirdPromise.all(createDirPromises)
248+
createDirPromises.length = 0
249+
}
244250
}
245251

246252
const unpackedFile = path.join(unpackedDest, path.relative(this.src, file))
@@ -298,7 +304,9 @@ class AsarPackager {
298304
}
299305
}
300306

301-
await BluebirdPromise.all(copyPromises)
307+
if (copyPromises.length > 0) {
308+
await BluebirdPromise.all(copyPromises)
309+
}
302310
}
303311

304312
private async addLink(file: string) {

0 commit comments

Comments
 (0)