Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 44 lines (37 sloc) 1.147 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fs = require('fs')
fd = require('path')
mkdirp = require('./mkdirp')
ansi = require('./ansi')

isDir = (path) ->
  fs.statSync(path).isDirectory()

class Template
  constructor: (@template, @path, @values = {}) ->
    
  files: ->
    return [ @template ] unless isDir(@template)
    files = []
    next = (dir) ->
      for file in fs.readdirSync(dir)
        files.push(file = "#{dir}/#{file}")
        next(file) if isDir(file)
    next @template
    files
    
  @::__defineGetter__ 'files', @::files
  
  write: ->
    mkdirp fd.dirname(@path)
    for path in @files
      out = path.replace(@template, '')
      out = fd.join(@path, out)
      out = fd.normalize(out)
      if isDir(path)
        fs.mkdirSync out, 0o0775
        console.log ansi("\tcreate\t", 'green'), out
      else if fs.existsSync(out)
        throw ("#{path} already exists")
      else
        data = @parse(fs.readFileSync(path, 'utf8'))
        fs.writeFileSync out, data
        console.log ansi("\tcreate\t", 'green'), out
        
  parse: (data) ->
    data.replace /\{\{([^}]+)\}\}/g, (_, key) =>
      @values[key]
      
module.exports = Template
Something went wrong with that request. Please try again.