Permalink
Browse files

validators

  • Loading branch information...
1 parent 87a4a1b commit ce5ba051468ac2dd8d6fb4fef3c32bd59821c6ba @dbashford committed Jan 9, 2013
Showing with 30 additions and 66 deletions.
  1. +29 −65 config.coffee
  2. +1 −1 package.json
View
94 config.coffee
@@ -1,10 +1,7 @@
"use strict"
-path = require 'path'
fs = require 'fs'
-windowsDrive = /^[A-Za-z]:\\/
-
exports.defaults = ->
importSource:
copy:[]
@@ -19,75 +16,42 @@ exports.placeholder = ->
"""
-exports.validate = (config) ->
+exports.validate = (config, validators) ->
errors = []
- if config.importSource?
- importSource = config.importSource
- if typeof importSource is "object" and not Array.isArray(importSource)
- if importSource.copy?
- copies = importSource.copy
- if Array.isArray(copies)
- for c in copies
- if typeof c is "object" and not Array.isArray(c)
-
- if c.from?
- if typeof c.from is "string"
- fromPath = __determinePath c.from, config.root
- if fs.existsSync fromPath
- stat = fs.statSync fromPath
- c.from = fromPath
- c.isDirectory = stat.isDirectory()
- else
- errors.push "importSource.copy.from [[ #{c.from} ]] must exist."
+ if validators.ifExistsIsObject(errors, "importSource config", config.importSource)
+ if config.importSource.copy?
+ if validators.isArray(errors, "importSource.copy", config.importSource.copy)
+ for c in config.importSource.copy
+ if typeof c is "object" and not Array.isArray(c)
+
+ if c.from?
+ if typeof c.from is "string"
+ fromPath = validators.determinePath c.from, config.root
+ if fs.existsSync fromPath
+ stat = fs.statSync fromPath
+ c.from = fromPath
+ c.isDirectory = stat.isDirectory()
else
- errors.push "importSource.copy.from must be a string."
+ errors.push "importSource.copy.from [[ #{c.from} ]] must exist."
else
- errors.push "importSource.copy entries must have a from property."
+ errors.push "importSource.copy.from must be a string."
+ else
+ errors.push "importSource.copy entries must have a from property."
- if c.to?
- if typeof c.to is "string"
- c.to = __determinePath c.to, config.root
- else
- errors.push "importSource.copy.to must be a string."
+ if c.to?
+ if typeof c.to is "string"
+ c.to = validators.determinePath c.to, config.root
else
- errors.push "importSource.copy entries must have a to property."
-
- if c.from?
- if c.exclude?
- if Array.isArray(c.exclude)
- regexes = []
- newExclude = []
- for exclude in c.exclude
- if typeof exclude is "string"
- newExclude.push __determinePath exclude, c.from
- else if exclude instanceof RegExp
- regexes.push exclude.source
- else
- errors.push "importSource.copy.exclude must be an array of strings and/or regexes."
- break
-
- if regexes.length > 0
- c.excludeRegex = new RegExp regexes.join("|"), "i"
-
- c.exclude = newExclude
- else
- errors.push "importSource.copy.exclude must be an array."
- else
- c.excludeRegex = new RegExp /(^[.#]|(?:__|~)$)/
-
+ errors.push "importSource.copy.to must be a string."
else
- errors.push "importSource.copy must be an array of objects"
- break
- else
- errors.push "importSource.copy must be an array"
- else
- errors.push "importSource config must be an object."
+ errors.push "importSource.copy entries must have a to property."
- errors
+ if c.from?
+ validators.ifExistsFileExcludeWithRegexAndString(errors, "importSource.copy.exclude", c, c.from)
+ else
+ errors.push "importSource.copy must be an array of objects"
+ break
-__determinePath = (thePath, relativeTo) ->
- return thePath if windowsDrive.test thePath
- return thePath if thePath.indexOf("/") is 0
- path.join relativeTo, thePath
+ errors
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "mimosa-import-source",
"preferGlobal": "false",
- "version": "0.1.2",
+ "version": "0.2.0",
"homepage": "https://github.com/dbashford/mimosa-import-source",
"author": "David Bashford",
"description": "Mimosa module for copying files into the project before building starts",

0 comments on commit ce5ba05

Please sign in to comment.