new.js 3.7 KB

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