Skip to content

Commit

Permalink
Unit tests and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
macalinao committed May 29, 2012
1 parent 8ff70fa commit 6f3f324
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ npm-debug.log

*.sublime-project
*.sublime-workspace

test/sandbox/
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
REPORTER = spec

test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--reporter $(REPORTER) \
test/*.coffee

rm -rf test/sandbox/

.PHONY: test
27 changes: 16 additions & 11 deletions lib/archetype.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,37 @@ fs = require 'fs'
path = require 'path'
mkdirp = require 'mkdirp'

exports.generate = (path, dest, vars) ->
exports.generate = (src, dest, vars) ->
"""
Generates a new archetype.
@param path The path to the archetype.
@param src The path to the archetype.
@param dest The destination to write the generated archetype to.
@param vars The variables to replace as an object.
"""
if not vars?
vars = {}

mkdirp.sync dest
copyAndFilter path, dest
copyAndFilter src, dest, vars

copyAndFilter = (src, dest, vars) ->

filterStr = (str, vars) ->
for tvar of vars
str = str.replace new RegExp('\\$\{' + tvar.replace('/', '\\/') + '\\}', 'g'), vars[tvar]
return str

copyAndFilter = (src, dest) ->
if fs.statSync(src).isDirectory()
# Filter a directory recursively
mkdirp.sync dest

files = fs.readdirSync src
for file in files
newName = replaceStr path.basename(file), vars
copyAndFilter path.join(src, file), path.join(dest, newName)
newName = filterStr path.basename(file), vars
copyAndFilter path.join(src, file), path.join(dest, newName), vars

else
# Filter a file
fileStr = fs.readFileSync src, 'utf8'
fs.writeFileSync dest, replaceStr(fileStr, vars), 'utf8' # Save

replaceStr = (str, vars) ->
for var of vars
fileStr.replace new RegExp('\\$\{' + var.replace('/', '\\/') + '\\}', 'g'), vars[var]
fs.writeFileSync dest, filterStr(fileStr, vars), 'utf8' # Save
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
"name": "archetype",
"description": "Simple archetypes.",
"author": {
"name": "simplyianm",
"name": "Ian Macalinao",
"email": "ianmacalinao@gmail.com",
"website": "http://simplyian.com"
},
"version": "0.0.1",
"scripts": {
"test": "make test"
},
"dependencies": {
"mkdirp": "*"
"coffee-script": "*",
"mkdirp": "*",
"mocha": "*",
"should": "*"
}
}
17 changes: 17 additions & 0 deletions test/archetype.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
archetype = require '../lib/archetype'
fs = require 'fs'

describe 'archetype', ->
describe '#generate()' , ->
it 'should copy files verbatim without replaces', ->
archetype.generate 'test/archetypes/no_replace', 'test/sandbox/no_replace'
fs.readFileSync('test/sandbox/no_replace/myfile.arbitrary', 'utf8').should.equal "arbitrary file is arbitrary\n"

it 'should filter file names', ->
archetype.generate 'test/archetypes/file_names', 'test/sandbox/file_names', var1: 'awesome'
fs.readFileSync('test/sandbox/file_names/awesome.json', 'utf8').should.equal "nothing\n"

it 'should filter files', ->
archetype.generate 'test/archetypes/one_var', 'test/sandbox/one_var', asdf: 'cool site'
fs.readFileSync('test/sandbox/one_var/super.html', 'utf8').should.equal \
"<html><head><title>cool site</title></head><body>ASDF</body></html>\n"
1 change: 1 addition & 0 deletions test/archetypes/file_names/${var1}.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nothing
1 change: 1 addition & 0 deletions test/archetypes/no_replace/myfile.arbitrary
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
arbitrary file is arbitrary
1 change: 1 addition & 0 deletions test/archetypes/one_var/super.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><title>${asdf}</title></head><body>ASDF</body></html>
4 changes: 4 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--require should
--reporter dot
--ui bdd
--compilers coffee:coffee-script

0 comments on commit 6f3f324

Please sign in to comment.