build.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. (function() {
  2. var async, build, chalk, commonOptions, commonUsage, extendOptions, fileExistsSync, fs, loadEnv, logger, options, path, ref, rimraf, usage;
  3. async = require('async');
  4. chalk = require('chalk');
  5. fs = require('fs');
  6. path = require('path');
  7. rimraf = require('rimraf');
  8. fileExistsSync = require('./../core/utils').fileExistsSync;
  9. ref = require('./common'), loadEnv = ref.loadEnv, commonOptions = ref.commonOptions, commonUsage = ref.commonUsage, extendOptions = ref.extendOptions;
  10. logger = require('./../core/logger').logger;
  11. usage = "\nusage: wintersmith build [options]\n\noptions:\n\n -o, --output [path] directory to write build-output (defaults to ./build)\n -X, --clean clean before building (warning: will recursively delete everything at output path)\n " + commonUsage + "\n\n all options can also be set in the config file\n\nexamples:\n\n build using a config file (assuming config.json is found in working directory):\n $ wintersmith build\n\n build using command line options:\n $ wintersmith build -o /var/www/public/ -T extra_data.json -C ~/my-blog\n\n or using both (command-line options will override config options):\n $ wintersmith build --config another_config.json --clean\n";
  12. options = {
  13. alias: {
  14. output: 'o',
  15. clean: 'X'
  16. },
  17. boolean: ['clean'],
  18. string: ['output']
  19. };
  20. extendOptions(options, commonOptions);
  21. build = function(argv) {
  22. var prepareOutputDir, start;
  23. start = new Date();
  24. logger.info('building site');
  25. prepareOutputDir = function(env, callback) {
  26. var exists, outputDir;
  27. outputDir = env.resolvePath(env.config.output);
  28. exists = fileExistsSync(outputDir);
  29. if (exists) {
  30. if (argv.clean) {
  31. logger.verbose("cleaning - running rimraf on " + outputDir);
  32. return async.series([
  33. function(callback) {
  34. return rimraf(outputDir, callback);
  35. }, function(callback) {
  36. return fs.mkdir(outputDir, callback);
  37. }
  38. ], callback);
  39. } else {
  40. return callback();
  41. }
  42. } else {
  43. logger.verbose("creating output directory " + outputDir);
  44. return fs.mkdir(outputDir, callback);
  45. }
  46. };
  47. return async.waterfall([
  48. function(callback) {
  49. return loadEnv(argv, callback);
  50. }, function(env, callback) {
  51. return prepareOutputDir(env, function(error) {
  52. return callback(error, env);
  53. });
  54. }, function(env, callback) {
  55. return env.build(callback);
  56. }
  57. ], function(error) {
  58. var delta, stop;
  59. if (error) {
  60. logger.error(error.message, error);
  61. return process.exit(1);
  62. } else {
  63. stop = new Date();
  64. delta = stop - start;
  65. logger.info("done in " + (chalk.bold(delta)) + " ms\n");
  66. return process.exit();
  67. }
  68. });
  69. };
  70. module.exports = build;
  71. module.exports.usage = usage;
  72. module.exports.options = options;
  73. }).call(this);