Skip to content

Commit

Permalink
style: Use CommonJs syntax for import/export
Browse files Browse the repository at this point in the history
With the destructuring feature of ES6 is very easy to keep CommonJs
syntax. Attempt to fix #55

As most of the tests are written using ES6 imports the esm dependecy is
kept but moved to devDependencies.
  • Loading branch information
q2s2t committed Dec 16, 2018
1 parent ad12c66 commit 3c519f6
Show file tree
Hide file tree
Showing 26 changed files with 168 additions and 119 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "node-7z",
"version": "1.0.0",
"description": "A Node.js wrapper for 7-Zip with platform binaries",
"main": "./src/index.js",
"main": "./src/main.js",
"scripts": {
"test": "npx mocha -r esm --reporter=spec \"test/**/*.spec.js\" --timeout=0",
"test-unit": "npx mocha -r esm --reporter=spec \"test/unit/*.spec.js\"",
Expand Down Expand Up @@ -36,14 +36,14 @@
"dependencies": {
"cross-spawn": "^6.0.4",
"debug": "^4.1.0",
"esm": "^3.0.79",
"lodash": "^4.17.11",
"normalize-path": "^3.0.0"
},
"optionalDependencies": {},
"devDependencies": {
"chai": "^4.1.2",
"eslint-config-standard": "^12.0.0",
"esm": "^3.0.79",
"fs-readdir-recursive": "^1.1.0",
"is-child-process": "^1.0.2",
"mocha": "^5.2.0",
Expand Down
10 changes: 7 additions & 3 deletions src/args.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { flattenDeep, negate, isEmpty } from 'lodash'
import { COMMAND_LETTERS } from './references.js'
const flattenDeep = require('lodash/flattenDeep')
const negate = require('lodash/negate')
const isEmpty = require('lodash/isEmpty')
const { COMMAND_LETTERS } = require('./references')

// Transform user input into a args for child procress spawn
export const fromOptions = options => {
const fromOptions = options => {
return [COMMAND_LETTERS[options._command]]
.concat(flattenDeep(options._target))
.filter(negate(isEmpty))
}

module.exports = { fromOptions }
8 changes: 5 additions & 3 deletions src/bin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { defaultTo } from 'lodash'
import { BIN_DEFAULT } from './references.js'
const defaultTo = require('lodash/defaultTo')
const { BIN_DEFAULT } = require('./references')

// Transform user input into a args for child procress spawn
export const fromOptions = options => {
const fromOptions = options => {
return defaultTo(options.$bin, BIN_DEFAULT)
}

module.exports = { fromOptions }
8 changes: 5 additions & 3 deletions src/commands.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
export const standardFactory = ({ main, command }) => (archive, source, options = {}) => {
const standardFactory = ({ main, command }) => (archive, source, options = {}) => {
const { ..._options } = options
_options._command = command
_options._target = [archive, source]
return main(_options)
}

export const extractFactory = ({ main, command }) => (archive, output, options = {}) => {
const extractFactory = ({ main, command }) => (archive, output, options = {}) => {
const { ..._options } = options
_options._command = command
_options._target = [archive, options.$cherryPick]
_options.outputDir = output
return main(_options)
}

export const simplexFactory = ({ main, command }) => (target, options = {}) => {
const simplexFactory = ({ main, command }) => (target, options = {}) => {
const { ..._options } = options
_options._command = command
_options._target = [target, options.$cherryPick]
return main(_options)
}

module.exports = { standardFactory, extractFactory, simplexFactory }
8 changes: 5 additions & 3 deletions src/error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ERROR } from './regexp.js'
const { ERROR } = require('./regexp')

// Just assign an error to the stream. The event error is emitted on close
export const assign = (stream, err) => {
const assign = (stream, err) => {
if (stream.err) {
stream.err = Object.assign(err, stream.err)
} else {
Expand All @@ -10,7 +10,7 @@ export const assign = (stream, err) => {
return stream
}

export const fromBuffer = chunk => {
const fromBuffer = chunk => {
const stderr = chunk.toString()
const match = stderr.match(ERROR)
let err = new Error('unknown error')
Expand All @@ -21,3 +21,5 @@ export const fromBuffer = chunk => {
}
return err
}

module.exports = { assign, fromBuffer }
15 changes: 8 additions & 7 deletions src/events.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import Debug from 'debug'
import { STAGE_BODY } from './references.js'
const debug = Debug('node-7z')
const debug = require('debug')('node-7z')
const { STAGE_BODY } = require('./references')

export const onErrorFactory = ({ Err }) => (stream, err) => {
const onErrorFactory = ({ Err }) => (stream, err) => {
Err.assign(stream, err)
debug('error: from child process: %O', err)
return stream
}

export const onStderrFactory = ({ Err }) => (stream, buffer) => {
const onStderrFactory = ({ Err }) => (stream, buffer) => {
const err = Err.fromBuffer(buffer)
Err.assign(stream, err)
debug('error: from stderr: %O', err)
return stream
}

export const onStdoutFactory = ({ Lines, Maybe }) => (stream, chunk) => {
const onStdoutFactory = ({ Lines, Maybe }) => (stream, chunk) => {
const lines = Lines.fromBuffer(stream, chunk)

// Maybe functions check if a condition is true and run the corresponding
Expand Down Expand Up @@ -72,10 +71,12 @@ export const onStdoutFactory = ({ Lines, Maybe }) => (stream, chunk) => {
return stream
}

export const onEndFactory = () => (stream) => {
const onEndFactory = () => (stream) => {
if (stream.err) {
stream.emit('error', stream.err)
}
stream.emit('end')
return stream
}

module.exports = { onErrorFactory, onStderrFactory, onStdoutFactory, onEndFactory }
10 changes: 7 additions & 3 deletions src/flags.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { negate, isEmpty, defaultsDeep } from 'lodash'
import { FLAGS, OPTIONS_DEFAULT } from './references.js'
const negate = require('lodash/negate')
const isEmpty = require('lodash/isEmpty')
const defaultsDeep = require('lodash/defaultsDeep')
const { FLAGS, OPTIONS_DEFAULT } = require('./references')

// Build arguments ready to be passed to `childProcess.spawn()` from the
// `options` provided by the module consumer.
export const fromOptions = options => {
const fromOptions = options => {
let opts = { ...defaultsDeep(options, OPTIONS_DEFAULT) }
opts = populateOutputStreams(opts)
opts = populateOutputDir(opts)
Expand Down Expand Up @@ -80,3 +82,5 @@ const populateOutputDir = options => {
return optionsWithoutOuptutDir
}
}

module.exports = { fromOptions }
2 changes: 0 additions & 2 deletions src/index.js

This file was deleted.

18 changes: 9 additions & 9 deletions src/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import Debug from 'debug'
import spawn from 'cross-spawn'
import { Readable } from 'stream'
import { STAGE_HEADERS } from './references.js'
const debug = require('debug')('node-7z')
const spawn = require('cross-spawn')
const { Readable } = require('stream')
const { STAGE_HEADERS } = require('./references')

const debug = Debug('node-7z')

export const createFactory = ({
const createFactory = ({
Bin,
Args,
Flags,
Expand Down Expand Up @@ -41,7 +39,7 @@ export const createFactory = ({
return seven
}

export const listenFactory = ({
const listenFactory = ({
errorHandler,
stdoutHandler,
stderrHandler,
Expand All @@ -54,7 +52,9 @@ export const listenFactory = ({
return stream
}

export const run = stream => {
const run = stream => {
stream._childProcess = spawn(stream._bin, stream._args, { detached: true })
return stream
}

module.exports = { createFactory, listenFactory, run }
6 changes: 4 additions & 2 deletions src/lines.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { LINE_SPLIT } from './regexp.js'
const { LINE_SPLIT } = require('./regexp')

// Transform a Buffer into an Array of complete lines.
// Chunks of data aren't line-by-line, a chunk can begin and end in the middle
// of line. The following code insure that if a line is not complete it goes to
// the next stream push. Lines are separated by the END OF LINE char.
// When 7zip writes a progress value to stdout a new line is not created:
// Instead 7zip uses combination on backpaces and spaces char.
export const fromBuffer = (seven, buffer) => {
const fromBuffer = (seven, buffer) => {
const lines = buffer.toString().split(LINE_SPLIT)
if (seven._lastLinePartial) {
lines[0] = seven._lastLinePartial.concat(lines[0])
Expand All @@ -21,3 +21,5 @@ export const fromBuffer = (seven, buffer) => {
}
return lines
}

module.exports = { fromBuffer }
20 changes: 10 additions & 10 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as Lifecycle from './lifecycle.js'
import * as Bin from './bin.js'
import * as Args from './args.js'
import * as Flags from './flags.js'
import * as Parser from './parser.js'
import * as Events from './events.js'
import * as Err from './error.js'
import * as Lines from '../src/lines.js'
import * as Maybe from '../src/maybe.js'
import * as Commands from './commands.js'
const Lifecycle = require('./lifecycle')
const Bin = require('./bin')
const Args = require('./args')
const Flags = require('./flags')
const Parser = require('./parser')
const Events = require('./events')
const Err = require('./error')
const Lines = require('../src/lines')
const Maybe = require('../src/maybe')
const Commands = require('./commands')

// Expose the listen function to the API so a user can listen to a sdtio stream
// non emitted by the current (ie. in the run() function).
Expand Down
18 changes: 9 additions & 9 deletions src/maybe.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Debug from 'debug'
import { STAGE_HEADERS, STAGE_BODY, STAGE_FOOTERS } from './references.js'
const debug = require('debug')('node-7z')
const { STAGE_HEADERS, STAGE_BODY, STAGE_FOOTERS } = require('./references')

const debug = Debug('node-7z')

export const info = (stream, line) => {
const info = (stream, line) => {
const stageWithInfo = (stream._stage === STAGE_HEADERS || stream._stage === STAGE_FOOTERS)
if (!stageWithInfo) {
return false
Expand All @@ -18,7 +16,7 @@ export const info = (stream, line) => {
}
}

export const progress = (stream, line) => {
const progress = (stream, line) => {
const progress = stream._matchProgress(stream, line)
if (!progress) {
return false
Expand All @@ -29,7 +27,7 @@ export const progress = (stream, line) => {
}
}

export const endOfHeaders = (stream, line) => {
const endOfHeaders = (stream, line) => {
if (stream._stage !== STAGE_HEADERS) {
return false
} else {
Expand All @@ -44,7 +42,7 @@ export const endOfHeaders = (stream, line) => {
}
}

export const endOfBody = (stream, line) => {
const endOfBody = (stream, line) => {
const match = stream._matchEndOfBody(stream, line)
if (!match) {
return false
Expand All @@ -55,7 +53,7 @@ export const endOfBody = (stream, line) => {
}
}

export const bodyData = (stream, line) => {
const bodyData = (stream, line) => {
const match = stream._matchBodyData(stream, line)
if (!match) {
return false
Expand All @@ -66,3 +64,5 @@ export const bodyData = (stream, line) => {
return true
}
}

module.exports = { info, progress, endOfHeaders, endOfBody, bodyData }
Loading

0 comments on commit 3c519f6

Please sign in to comment.