Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 7, 2019
1 parent 3b008d7 commit e146799
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
26 changes: 2 additions & 24 deletions src/nve/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { readFile, writeFile } from 'fs'
import { promisify } from 'util'

import pathExists from 'path-exists'
import { validRange, clean as cleanRange, maxSatisfying, ltr } from 'semver'
import { clean as cleanRange, maxSatisfying, ltr } from 'semver'

import { validateInput } from './validate.js'
import { CACHE_DIR } from './cache.js'
import { runNode } from './run.js'
import { cleanupOnError } from './cleanup.js'
Expand Down Expand Up @@ -38,29 +39,6 @@ const nve = async function(versionRange, args = []) {
return { exitCode, signal }
}

// Validate input parameters
// `versionRange` can start with `v` or not.
const validateInput = function(versionRange, args) {
validateVersion(versionRange)
validateArgs(args)
}

const validateVersion = function(versionRange) {
if (typeof versionRange !== 'string' || validRange(versionRange) === null) {
throw new TypeError('First argument must be a valid Node version range')
}
}

const validateArgs = function(args) {
if (!Array.isArray(args) || !args.every(isString)) {
throw new TypeError('Second argument must be an array of strings')
}
}

const isString = function(arg) {
return typeof arg === 'string'
}

// Retrieve the Node version matching a specific `versionRange`
const getVersion = async function(versionRange) {
const versions = await getVersions(versionRange)
Expand Down
24 changes: 24 additions & 0 deletions src/nve/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { validRange } from 'semver'

// Validate input parameters
// `versionRange` can start with `v` or not.
export const validateInput = function(versionRange, args) {
validateVersion(versionRange)
validateArgs(args)
}

const validateVersion = function(versionRange) {
if (typeof versionRange !== 'string' || validRange(versionRange) === null) {
throw new TypeError('First argument must be a valid Node version range')
}
}

const validateArgs = function(args) {
if (!Array.isArray(args) || !args.every(isString)) {
throw new TypeError('Second argument must be an array of strings')
}
}

const isString = function(arg) {
return typeof arg === 'string'
}

0 comments on commit e146799

Please sign in to comment.