Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 9, 2019
1 parent 34ad1b5 commit 451105e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
30 changes: 0 additions & 30 deletions src/command.js

This file was deleted.

38 changes: 37 additions & 1 deletion src/exec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import execa from 'execa'
import PluginError from 'plugin-error'

import { parseOpts } from './options.js'
import { execCommand } from './command.js'
import { printEcho } from './echo.js'

// Execute a shell command
// To create a Gulp task, one should not use `bind()` as it removes
Expand All @@ -13,3 +16,36 @@ export const exec = function(input, opts) {
const optsA = parseOpts(opts)
return execCommand(input, optsA)
}

// Same but delayed.
// Options are parsed right away, in case there are validation errors.
export const execBind = function(input, opts) {
const optsA = parseOpts(opts)
return execCommand.bind(null, input, optsA)
}

// Fire the command with `execa()`
const execCommand = async function(input, opts) {
printEcho({ input, opts })

try {
return await execa(input, opts)
} catch (error) {
throw getError({ error })
}
}

// Buld a Gulp error
// TODO: when error is due to wrong input (not normal Execa error)
// TODO: sometimes execa adds stdout|stderr, but we don't want that
// (maybe removes anything after first newline?)
const getError = function({ error, error: { message } }) {
// TODO: try passing `error` instead of `error.message`
const errorA = new PluginError('gulp-execa', message, {
showProperties: false,
})
// Keep `execa` error properties
// eslint-disable-next-line fp/no-mutating-assign
Object.assign(errorA, error)
return errorA
}
7 changes: 2 additions & 5 deletions src/task.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { parseOpts } from './options.js'
import { execCommand } from './command.js'
import { execBind } from './exec.js'

// Create a Gulp task
export const task = function(input, opts) {
const optsA = parseOpts(opts)

const gulpTask = execCommand.bind(null, input, optsA)
const gulpTask = execBind(input, opts)

// We want to allow users to do `const gulpTask = execa(...)` instead of the
// more verbose `const gulpTask = () => execa(...)`. This is especially
Expand Down

0 comments on commit 451105e

Please sign in to comment.