Skip to content

Commit

Permalink
standard
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed Feb 12, 2024
1 parent d85858c commit 863472d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*! cross-zip. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
module.exports = {
zip: zip,
zipSync: zipSync,
unzip: unzip,
unzipSync: unzipSync
zip,
zipSync,
unzip,
unzipSync
}

var cp = require('child_process')
var fs = require('fs')
var os = require('os')
var path = require('path')
const cp = require('child_process')
const fs = require('fs')
const os = require('os')
const path = require('path')

function zip (inPath, outPath, cb) {
if (!cb) cb = function () {}
Expand All @@ -31,7 +31,7 @@ function zip (inPath, outPath, cb) {
function copyToTemp () {
fs.readFile(inPath, function (err, inFile) {
if (err) return cb(err)
var tmpPath = path.join(os.tmpdir(), 'cross-zip-' + Date.now())
const tmpPath = path.join(os.tmpdir(), 'cross-zip-' + Date.now())
fs.mkdir(tmpPath, function (err) {
if (err) return cb(err)
fs.writeFile(path.join(tmpPath, path.basename(inPath)), inFile, function (err) {
Expand All @@ -53,7 +53,7 @@ function zip (inPath, outPath, cb) {
}

function doZip2 () {
var opts = {
const opts = {
cwd: path.dirname(inPath),
maxBuffer: Infinity
}
Expand All @@ -66,15 +66,15 @@ function zip (inPath, outPath, cb) {
function zipSync (inPath, outPath) {
if (process.platform === 'win32') {
if (fs.statSync(inPath).isFile()) {
var inFile = fs.readFileSync(inPath)
var tmpPath = path.join(os.tmpdir(), 'cross-zip-' + Date.now())
const inFile = fs.readFileSync(inPath)
const tmpPath = path.join(os.tmpdir(), 'cross-zip-' + Date.now())
fs.mkdirSync(tmpPath)
fs.writeFileSync(path.join(tmpPath, path.basename(inPath)), inFile)
inPath = tmpPath
}
fs.rmdirSync(outPath, { recursive: true, maxRetries: 3 })
}
var opts = {
const opts = {
cwd: path.dirname(inPath),
maxBuffer: Infinity
}
Expand All @@ -83,7 +83,7 @@ function zipSync (inPath, outPath) {

function unzip (inPath, outPath, cb) {
if (!cb) cb = function () {}
var opts = {
const opts = {
maxBuffer: Infinity
}
cp.execFile(getUnzipCommand(), getUnzipArgs(inPath, outPath), opts, function (err) {
Expand All @@ -92,7 +92,7 @@ function unzip (inPath, outPath, cb) {
}

function unzipSync (inPath, outPath) {
var opts = {
const opts = {
maxBuffer: Infinity
}
cp.execFileSync(getUnzipCommand(), getUnzipArgs(inPath, outPath), opts)
Expand Down Expand Up @@ -128,7 +128,7 @@ function getZipArgs (inPath, outPath) {
'-myOutPath', quotePath(outPath)
]
} else {
var fileName = path.basename(inPath)
const fileName = path.basename(inPath)
return ['-r', '-y', outPath, fileName]
}
}
Expand Down
34 changes: 17 additions & 17 deletions test/unzip.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
var fs = require('fs')
var path = require('path')
var test = require('tape')
var zip = require('../')
const fs = require('fs')
const path = require('path')
const test = require('tape')
const zip = require('../')

var filePath = path.join(__dirname, 'content', 'file.txt')
var fileZipPath = path.join(__dirname, 'content', 'file.txt.zip')
var tmpPath = path.join(__dirname, 'tmp')
const filePath = path.join(__dirname, 'content', 'file.txt')
const fileZipPath = path.join(__dirname, 'content', 'file.txt.zip')
const tmpPath = path.join(__dirname, 'tmp')

fs.mkdirSync(tmpPath, { recursive: true })

test('unzipSync', function (t) {
var tmpFilePath = path.join(tmpPath, 'file.txt')
const tmpFilePath = path.join(tmpPath, 'file.txt')
fs.rmdirSync(tmpFilePath, { recursive: true })
zip.unzipSync(fileZipPath, tmpPath)

var tmpFile = fs.readFileSync(tmpFilePath)
var file = fs.readFileSync(filePath)
const tmpFile = fs.readFileSync(tmpFilePath)
const file = fs.readFileSync(filePath)

t.deepEqual(tmpFile, file)
t.end()
Expand All @@ -24,15 +24,15 @@ test('unzipSync', function (t) {
test('unzip', function (t) {
t.plan(3)

var tmpFilePath = path.join(tmpPath, 'file.txt')
const tmpFilePath = path.join(tmpPath, 'file.txt')
fs.rmdir(tmpFilePath, { recursive: true }, function (err) {
t.error(err)

zip.unzip(fileZipPath, tmpPath, function (err) {
t.error(err)

var tmpFile = fs.readFileSync(tmpFilePath)
var file = fs.readFileSync(filePath)
const tmpFile = fs.readFileSync(tmpFilePath)
const file = fs.readFileSync(filePath)

t.deepEqual(tmpFile, file)
})
Expand All @@ -42,20 +42,20 @@ test('unzip', function (t) {
test('unzip from a folder with a space in it', function (t) {
t.plan(4)

var zipSpacePath = path.join(tmpPath, 'folder space', path.basename(fileZipPath))
const zipSpacePath = path.join(tmpPath, 'folder space', path.basename(fileZipPath))
fs.mkdirSync(path.dirname(zipSpacePath), { recursive: true })
fs.copyFileSync(fileZipPath, zipSpacePath)

var tmpFilePath = path.join(tmpPath, 'file.txt')
const tmpFilePath = path.join(tmpPath, 'file.txt')
fs.rmdir(tmpFilePath, { recursive: true }, function (err) {
t.error(err)

zip.unzip(zipSpacePath, tmpPath, function (err) {
t.error(err)

t.ok(fs.existsSync(tmpFilePath), 'extracted file should exist')
var tmpFile = fs.readFileSync(tmpFilePath)
var file = fs.readFileSync(filePath)
const tmpFile = fs.readFileSync(tmpFilePath)
const file = fs.readFileSync(filePath)

t.deepEqual(tmpFile, file)
})
Expand Down
28 changes: 14 additions & 14 deletions test/zip.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var fs = require('fs')
var path = require('path')
var test = require('tape')
var zip = require('../')
const fs = require('fs')
const path = require('path')
const test = require('tape')
const zip = require('../')

var filePath = path.join(__dirname, 'content', 'file.txt')
var tmpPath = path.join(__dirname, 'tmp')
const filePath = path.join(__dirname, 'content', 'file.txt')
const tmpPath = path.join(__dirname, 'tmp')

fs.mkdirSync(tmpPath, { recursive: true })

test('zipSync', function (t) {
var tmpFileZipPath = path.join(tmpPath, 'file.zip')
const tmpFileZipPath = path.join(tmpPath, 'file.zip')
zip.zipSync(filePath, tmpFileZipPath)

var tmpFilePath = path.join(tmpPath, 'file.txt')
const tmpFilePath = path.join(tmpPath, 'file.txt')
fs.rmdirSync(tmpFilePath, { recursive: true })
zip.unzipSync(tmpFileZipPath, tmpPath)

var tmpFile = fs.readFileSync(tmpFilePath)
var file = fs.readFileSync(filePath)
const tmpFile = fs.readFileSync(tmpFilePath)
const file = fs.readFileSync(filePath)

t.deepEqual(tmpFile, file)
t.end()
Expand All @@ -26,19 +26,19 @@ test('zipSync', function (t) {
test('zip', function (t) {
t.plan(4)

var tmpFileZipPath = path.join(tmpPath, 'file.zip')
const tmpFileZipPath = path.join(tmpPath, 'file.zip')
zip.zip(filePath, tmpFileZipPath, function (err) {
t.error(err)

var tmpFilePath = path.join(tmpPath, 'file.txt')
const tmpFilePath = path.join(tmpPath, 'file.txt')
fs.rmdir(tmpFilePath, { recursive: true }, function (err) {
t.error(err)

zip.unzip(tmpFileZipPath, tmpPath, function (err) {
t.error(err)

var tmpFile = fs.readFileSync(tmpFilePath)
var file = fs.readFileSync(filePath)
const tmpFile = fs.readFileSync(tmpFilePath)
const file = fs.readFileSync(filePath)

t.deepEqual(tmpFile, file)
})
Expand Down

0 comments on commit 863472d

Please sign in to comment.