 npm = require 'npm'
 {ncp} = require 'ncp'
 
-{NpmAdapter} = require './common'
-{fileExists} = require './../core/utils'
+{NpmAdapter, getStorageDir} = require './common'
+{fileExists, fileExistsSync} = require './../core/utils'
 {logger} = require './../core/logger'
 
-templatesDir = path.join __dirname, '../../examples/'
-templateTypes = fs.readdirSync(templatesDir).filter (filename) ->
-  fs.statSync(path.join(templatesDir, filename)).isDirectory()
+templates = {}
+loadTemplates = (directory) ->
+  return unless fileExistsSync directory
+  fs.readdirSync(directory)
+    .map((filename) -> path.join(directory, filename))
+    .filter((filename) -> fs.statSync(filename).isDirectory())
+    .forEach((filename) -> templates[path.basename(filename)] = filename)
+
+loadTemplates path.join __dirname, '../../examples/'
+loadTemplates path.join getStorageDir(), 'templates/'
 
 usage = """
 

    -f, --force             overwrite existing files
    -T, --template <name>   template to create new site from (defaults to 'blog')

    available templates are: #{ templateTypes.join(', ') }

  example:


    logger.error 'you must specify a location'
    return

  if not templates[argv.template]?
    logger.error "unknown template '#{ argv.template }'"
    return

  from = templates[argv.template]
  to = path.resolve location

  logger.info "initializing new wintersmith site in #{ to } using template #{ argv.template }"
