Skip to content

Commit

Permalink
Validate input
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 10, 2019
1 parent f1c5e6b commit c58e7c2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import isCi from 'is-ci'
import execa from 'execa'
import PluginError from 'plugin-error'

import { validateInput } from './input.js'
import { parseOpts } from './options.js'
import { printEcho } from './echo.js'

Expand All @@ -14,6 +15,7 @@ import { printEcho } from './echo.js'
// with whitespaces escaping. Also escaping is shell-specific, e.g. on Windows
// `cmd.exe` only use double quotes not single quotes.
export const exec = function(input, opts) {
validateInput({ input })
const optsB = parseOpts({ opts, defaultOpts })
return execCommand(input, optsB)
}
Expand Down
10 changes: 10 additions & 0 deletions src/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Validate main command and arguments
export const validateInput = function({ input }) {
if (!isValidInput({ input })) {
throw new Error('The command must be a non-empty string')
}
}

export const isValidInput = function({ input }) {
return typeof input === 'string' && input.trim() !== ''
}
2 changes: 1 addition & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validate } from 'jest-validate'

import { pickBy } from './utils.js'

// Parse main arguments and options
// Parse options
// TODO: validate options (including that `input` is a string)
export const parseOpts = function({ opts = {}, defaultOpts, forcedOpts = {} }) {
const optsA = pickBy(opts, value => value !== undefined)
Expand Down
3 changes: 2 additions & 1 deletion src/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { callbackify } from 'util'

import through from 'through2-concurrent'

import { isValidInput } from './input.js'
import { parseOpts } from './options.js'
import { execCommand } from './exec.js'

Expand Down Expand Up @@ -48,7 +49,7 @@ const execVinyl = callbackify(cExecVinyl)
const fireCommand = function({ input, opts }) {
// Returning `undefined` or invalid command skips it silently.
// `file.exec` array will be pushed with `undefined`.
if (typeof input !== 'string' || input.trim() === '') {
if (!isValidInput({ input })) {
return
}

Expand Down
2 changes: 2 additions & 0 deletions src/task.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import isCi from 'is-ci'
import renameFn from 'rename-fn'

import { validateInput } from './input.js'
import { parseOpts } from './options.js'
import { execCommand } from './exec.js'

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

const gulpTask = execCommand.bind(null, input, optsA)
Expand Down

0 comments on commit c58e7c2

Please sign in to comment.