new.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. async = require 'async'
  2. fs = require 'fs'
  3. path = require 'path'
  4. npm = require 'npm'
  5. {ncp} = require 'ncp'
  6. {NpmAdapter, getStorageDir} = require './common'
  7. {fileExists, fileExistsSync} = require './../core/utils'
  8. {logger} = require './../core/logger'
  9. templates = {}
  10. loadTemplates = (directory) ->
  11. return unless fileExistsSync directory
  12. fs.readdirSync(directory)
  13. .map((filename) -> path.join(directory, filename))
  14. .filter((filename) -> fs.statSync(filename).isDirectory())
  15. .forEach((filename) -> templates[path.basename(filename)] = filename)
  16. loadTemplates path.join __dirname, '../../examples/'
  17. loadTemplates path.join getStorageDir(), 'templates/'
  18. usage = """
  19. usage: wintersmith new [options] <path>
  20. creates a skeleton site in <path>
  21. options:
  22. -f, --force overwrite existing files
  23. -T, --template <name> template to create new site from (defaults to 'blog')
  24. available templates are: #{ Object.keys(templates).join(', ') }
  25. example:
  26. create a new site in your home directory
  27. $ wintersmith new ~/my-blog
  28. """
  29. options =
  30. string: ['template']
  31. boolean: ['force']
  32. alias:
  33. force: 'f'
  34. template: 'T'
  35. default:
  36. template: 'blog'
  37. createSite = (argv) ->
  38. ### copy example directory to *location* ###
  39. location = argv._[3]
  40. if !location? or !location.length
  41. logger.error 'you must specify a location'
  42. return
  43. if not templates[argv.template]?
  44. logger.error "unknown template '#{ argv.template }'"
  45. return
  46. from = templates[argv.template]
  47. to = path.resolve location
  48. logger.info "initializing new wintersmith site in #{ to } using template #{ argv.template }"
  49. validateDestination = (callback) ->
  50. logger.verbose "checking validity of #{ to }"
  51. fileExists to, (exists) ->
  52. if exists and !argv.force
  53. callback new Error "#{ to } already exists. Add --force to overwrite"
  54. else
  55. callback()
  56. copyTemplate = (callback) ->
  57. logger.verbose "recursive copy #{ from } -> #{ to }"
  58. ncp from, to, {}, callback
  59. installDeps = (callback) ->
  60. packagePath = path.join to, 'package.json'
  61. fileExists packagePath, (exists) ->
  62. if exists
  63. logger.verbose "installing template dependencies"
  64. process.chdir to
  65. conf = {logstream: new NpmAdapter(logger)}
  66. npm.load conf, (error) ->
  67. return callback error if error?
  68. npm.install callback
  69. else
  70. callback()
  71. async.series [validateDestination, copyTemplate, installDeps], (error) ->
  72. if error
  73. logger.error error.message, error
  74. process.exit 1
  75. else
  76. logger.info 'done!'
  77. module.exports = createSite
  78. module.exports.usage = usage
  79. module.exports.options = options