Skip to content

Commit

Permalink
feat: Add extract() command
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Rossetti committed Oct 17, 2018
1 parent 77ab93a commit 6888108
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 25 deletions.
11 changes: 6 additions & 5 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Code quality:
☐ Windows testing on AppVeyor
☐ https://conventionalcommits.org
☐ Use c8 for coverage
Move to CircleCI because TravisCI is not using p7zip-full v16.x
CircleCi local test with docker (add circleci to system dev deps)
Move to CircleCI because TravisCI is not using p7zip-full v16.x @done(18-10-17 20:35)
CircleCi local test with docker (add circleci to system dev deps) @done(18-10-17 20:36)
☐ git hooks: standardjs, circleci config validate
Package:
Update all dependencies
Rewite scripts
Update all dependencies @done(18-10-17 20:36)
Rewite scripts @done(18-10-17 20:36)
✘ Helper script to optionnaly fetch 7zip bins? @cancelled(18-09-09 19:02)
☐ 7zip-bin as peer dependecy?
Switches:
Expand All @@ -37,7 +37,7 @@ Features:
☐ Babel/ES5 etc support
☐ Close/end SevenZipStream
☐ Full-named switches for cleaner API
Stream methods returns stream
Stream methods returns stream @done(18-10-17 20:35)
☐ Esier to use progress
☐ Status (TU+R. and others?) as plain text
Documentation:
Expand All @@ -52,3 +52,4 @@ Documentation:
☐ add(archive, [source1, source2]) usage
☐ No benchmark command: It will output based on the system that it is on, "system-static"
☐ Hack to use delete() command (delete is a JS reserved word)
☐ use `cherryPick` as argument name for clarity
34 changes: 17 additions & 17 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,18 @@ function commandStandard (commandLetter, archive, source, options) {
return stream
}

export function add (archive, source, options) {
return commandStandard('a', archive, source, options)
}

export function del (archive, source, options) {
return commandStandard('d', archive, source, options)
}

export function extract (archive, output = '', target = '', options) {
function commandExtract (commandLetter, archive, output, target, options) {
const opts = Object.assign({}, options)
opts._commandArgs = ['e']
const isDefaultOuput = (output === '')
if (!isDefaultOuput) {
opts._commandArgs = [commandLetter]
opts._commandArgs.push(archive)
if (output) {
opts['o'] = output
}
const isDefaultTarget = (target === '')
if (!isDefaultTarget) {
opts._commandArgs.push(archive)
}

const isTargetMultiple = (Array.isArray(target))
if (isTargetMultiple) {
opts._commandArgs = opts._commandArgs.concat(target)
} else {
} else if (target) {
opts._commandArgs.push(target)
}

Expand All @@ -53,3 +41,15 @@ export function extract (archive, output = '', target = '', options) {
const stream = new SevenZipStream(opts)
return stream
}

export function add (archive, source, options) {
return commandStandard('a', archive, source, options)
}

export function remove (archive, source, options) {
return commandStandard('d', archive, source, options)
}

export function extract (archive, output, cherryPick, options) {
return commandExtract('e', archive, output, cherryPick, options)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "A Node.js wrapper for 7-Zip with platform binaries",
"main": "lib/index.js",
"scripts": {
"test": "npx mocha -r esm --reporter=spec 'test/**/*.spec.js' --timeout=0 ; rm -rf test/_tmp/*.*",
"test": "npx mocha -r esm --reporter=spec 'test/**/*.spec.js' --timeout=0 ; npm run test-cleanup",
"test-docker": "circleci local execute --job build",
"test-cleanup": "rm -rf test/_tmp/* ; touch test/_tmp/.gitkeep",
"coverage": "npx nyc --reporter=lcov npm test",
"coverage-html": "npx nyc --reporter=html npm test",
"coveralls": "cat ./coverage/lcov.info | npx coveralls"
Expand Down
74 changes: 72 additions & 2 deletions test/func/extract.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global describe, it */
import { expect } from 'chai'
import { copyFileSync, statSync } from 'fs'
import { copyFileSync, readdirSync } from 'fs'
import { extract } from '../../lib/commands.js'

const mockDir = './test/_mock'
Expand Down Expand Up @@ -32,7 +32,77 @@ describe('Functional: extract()', function () {
})
})

// it('should reduce archive size by deleting content', function (done) {
it('should overwrite -o switch with output argument', function () {
const seven = extract('archive.7z', 'this/path/is/better', undefined, {
o: 'than/this/path',
$defer: true
})
expect(seven._args).to.contain('-othis/path/is/better')
expect(seven._args).not.to.contain('-othan/this/path')
})

it('should set default 7zip ouptut when non or falsy', function () {
const sevenUndefined = extract('archive.7z', 'output', undefined, { $defer: true })
const sevenFalse = extract('archive.7z', 'output', null, { $defer: true })
const sevenNull = extract('archive.7z', 'output', false, { $defer: true })
const sevenEmptyString = extract('archive.7z', 'output', '', { $defer: true })
expect(sevenUndefined._args).not.to.contain('-o')
expect(sevenFalse._args).not.to.contain('-o')
expect(sevenNull._args).not.to.contain('-o')
expect(sevenEmptyString._args).not.to.contain('-o')
})

it('should set default 7zip target when non or falsy', function () {
const sevenUndefined = extract('archive.7z', undefined, undefined, { $defer: true })
const sevenFalse = extract('archive.7z', null, undefined, { $defer: true })
const sevenNull = extract('archive.7z', false, undefined, { $defer: true })
const sevenEmptyString = extract('archive.7z', '', undefined, { $defer: true })
sevenUndefined._args.forEach(v => expect(v).not.to.match(/(^-o.)/))
sevenFalse._args.forEach(v => expect(v).not.to.match(/(^-o.)/))
sevenNull._args.forEach(v => expect(v).not.to.match(/(^-o.)/))
sevenEmptyString._args.forEach(v => expect(v).not.to.match(/(^-o.)/))
})

it('should single accept target as string', function () {
const seven = extract('archive.7z', undefined, 'target1', { $defer: true })
expect(seven._args).to.contain('target1')
})

it('should multiple accept target as array', function () {
const seven = extract('archive.7z', undefined, ['target1', 'target2'], { $defer: true })
expect(seven._args).to.contain('target1')
expect(seven._args).to.contain('target2')
})

it('should extract on the right path', function (done) {
const archiveBase = `${mockDir}/DirNew/ExtArchive.7z`
const archive = `${tmpDir}/extract-flat-exist.7z`
const output = `${tmpDir}/extract-flat-exist`
copyFileSync(archiveBase, archive)
const seven = extract(archive, output, false, { r: true })
seven.on('end', function () {
expect(seven.info['Files']).to.equal('9')
expect(seven.info['Folders']).to.equal('3')
expect(seven.info['Path']).to.equal(archive)
const ls = readdirSync(output)
expect(ls).to.contain('DirExt')
expect(ls).to.contain('DirExt')
expect(ls).to.contain('root.md')
expect(ls).to.contain('root.not')
expect(ls).to.contain('root.txt')
expect(ls).to.contain('sub1')
expect(ls).to.contain('sub1.md')
expect(ls).to.contain('sub1.not')
expect(ls).to.contain('sub1.txt')
expect(ls).to.contain('sub2')
expect(ls).to.contain('sub2.md')
expect(ls).to.contain('sub2.not')
expect(ls).to.contain('sub2.txt')
done()
})
})

// it('should reduce archive size by deleting content') function (done) {
// const archiveBase = `${mockDir}/DirNew/ExtArchive.7z`
// const archive = `${tmpDir}/del-md.7z`
// const target = `DirExt/*.md`
Expand Down

0 comments on commit 6888108

Please sign in to comment.