Skip to content

Commit

Permalink
refactor: reduce the usage of anonymous functions (#188)
Browse files Browse the repository at this point in the history
* refactor: remove unnecessary anonymous function from listPackages
* refactor: reduce the number of anonymous functions for extractAll()
  • Loading branch information
malept authored Feb 19, 2020
1 parent e49bdc7 commit 170d05d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
17 changes: 8 additions & 9 deletions lib/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ module.exports.extractAll = function (archive, dest) {
// create destination directory
fs.mkdirpSync(dest)

return filenames.map((filename) => {
filename = filename.substr(1) // get rid of leading slash
for (const fullPath of filenames) {
// Remove leading slash
const filename = fullPath.substr(1)
const destFilename = path.join(dest, filename)
const file = filesystem.getFile(filename, followLinks)
if (file.files) {
Expand All @@ -187,21 +188,19 @@ module.exports.extractAll = function (archive, dest) {
// it's a symlink, create a symlink
const linkSrcPath = path.dirname(path.join(dest, file.link))
const linkDestPath = path.dirname(destFilename)
const relativePath = path.relative(linkDestPath, linkSrcPath);
const relativePath = path.relative(linkDestPath, linkSrcPath)
// try to delete output file, because we can't overwrite a link
(() => {
try {
fs.unlinkSync(destFilename)
} catch (error) {}
})()
try {
fs.unlinkSync(destFilename)
} catch (error) {}
const linkTo = path.join(relativePath, path.basename(file.link))
fs.symlinkSync(linkTo, destFilename)
} else {
// it's a file, extract it
const content = disk.readFileSync(filesystem, filename, file)
fs.writeFileSync(destFilename, content)
}
})
}
}

module.exports.uncache = function (archive) {
Expand Down
24 changes: 11 additions & 13 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,21 @@ class Filesystem {

listFiles (options) {
const files = []
const fillFilesFromHeader = function (p, json) {
if (!json.files) {

const fillFilesFromMetadata = function (basePath, metadata) {
if (!metadata.files) {
return
}
return (() => {
const result = []
for (const f in json.files) {
const fullPath = path.join(p, f)
const packState = json.files[f].unpacked === true ? 'unpack' : 'pack '
files.push((options && options.isPack) ? `${packState} : ${fullPath}` : fullPath)
result.push(fillFilesFromHeader(fullPath, json.files[f]))
}
return result
})()

for (const [childPath, childMetadata] of Object.entries(metadata.files)) {
const fullPath = path.join(basePath, childPath)
const packState = childMetadata.unpacked ? 'unpack' : 'pack '
files.push((options && options.isPack) ? `${packState} : ${fullPath}` : fullPath)
fillFilesFromMetadata(fullPath, childMetadata)
}
}

fillFilesFromHeader('/', this.header)
fillFilesFromMetadata('/', this.header)
return files
}

Expand Down

0 comments on commit 170d05d

Please sign in to comment.