common.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. (function() {
  2. var Config, Environment, NpmAdapter, async, defaults, fileExists, logger, path, readJSON, ref, stream,
  3. indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
  4. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  5. hasProp = {}.hasOwnProperty;
  6. path = require('path');
  7. async = require('async');
  8. stream = require('stream');
  9. Config = require('./../core/config').Config;
  10. Environment = require('./../core/environment').Environment;
  11. logger = require('./../core/logger').logger;
  12. ref = require('./../core/utils'), readJSON = ref.readJSON, fileExists = ref.fileExists;
  13. exports.commonOptions = defaults = {
  14. string: ['chdir', 'config', 'contents', 'templates', 'locals', 'require', 'plugins', 'ignore'],
  15. "default": {
  16. config: './config.json',
  17. chdir: null
  18. },
  19. alias: {
  20. config: 'c',
  21. chdir: 'C',
  22. contents: 'i',
  23. templates: 't',
  24. locals: 'L',
  25. require: 'R',
  26. plugins: 'P',
  27. ignore: 'I'
  28. }
  29. };
  30. exports.commonUsage = ["-C, --chdir [path] change the working directory", " -c, --config [path] path to config (defaults to " + defaults["default"].config + ")", " -i, --contents [path] contents location (defaults to " + Config.defaults.contents + ")", " -t, --templates [path] template location (defaults to " + Config.defaults.templates + ")", " -L, --locals [path] optional path to json file containing template context data", " -R, --require comma separated list of modules to add to the template context", " -P, --plugins comma separated list of modules to load as plugins", " -I, --ignore comma separated list of files/glob-patterns to ignore"].join('\n');
  31. exports.extendOptions = function(base, extra) {
  32. var i, j, key, len, len1, ref1, ref2, ref3, type, value;
  33. ref1 = ['string', 'boolean'];
  34. for (i = 0, len = ref1.length; i < len; i++) {
  35. type = ref1[i];
  36. if (base[type] == null) {
  37. base[type] = [];
  38. }
  39. if (extra[type] != null) {
  40. base[type] = base[type].concat(extra[type]);
  41. }
  42. }
  43. ref2 = ['alias', 'default'];
  44. for (j = 0, len1 = ref2.length; j < len1; j++) {
  45. type = ref2[j];
  46. if (base[type] == null) {
  47. base[type] = {};
  48. }
  49. if (extra[type] != null) {
  50. ref3 = extra[type];
  51. for (key in ref3) {
  52. value = ref3[key];
  53. base[type][key] = value;
  54. }
  55. }
  56. }
  57. };
  58. exports.loadEnv = function(argv, callback) {
  59. /* creates a new wintersmith environment
  60. options are resolved with the hierarchy: argv > configfile > defaults
  61. */
  62. var workDir;
  63. workDir = path.resolve(argv.chdir || process.cwd());
  64. logger.verbose("creating environment - work directory: " + workDir);
  65. return async.waterfall([
  66. function(callback) {
  67. var configPath;
  68. configPath = path.join(workDir, argv.config);
  69. return fileExists(configPath, function(exists) {
  70. if (exists) {
  71. logger.info("using config file: " + configPath);
  72. return Config.fromFile(configPath, callback);
  73. } else {
  74. logger.verbose("no config file found");
  75. return callback(null, new Config);
  76. }
  77. });
  78. }, function(config, callback) {
  79. var alias, excluded, i, key, len, module, ref1, reqs, v, value;
  80. config._cliopts = {};
  81. for (key in argv) {
  82. value = argv[key];
  83. excluded = ['_', 'chdir', 'config', 'clean'];
  84. if (indexOf.call(excluded, key) >= 0) {
  85. continue;
  86. }
  87. if (key === 'port') {
  88. value = Number(value);
  89. }
  90. if (key === 'ignore' || key === 'require' || key === 'plugins') {
  91. value = value.split(',');
  92. if (key === 'require') {
  93. reqs = {};
  94. for (i = 0, len = value.length; i < len; i++) {
  95. v = value[i];
  96. ref1 = v.split(':'), alias = ref1[0], module = ref1[1];
  97. if (module == null) {
  98. module = alias;
  99. alias = module.replace(/\/$/, '').split('/').slice(-1);
  100. }
  101. reqs[alias] = module;
  102. }
  103. value = reqs;
  104. }
  105. }
  106. config[key] = config._cliopts[key] = value;
  107. }
  108. return callback(null, config);
  109. }, function(config, callback) {
  110. var env;
  111. logger.verbose('config:', config);
  112. env = new Environment(config, workDir, logger);
  113. return callback(null, env);
  114. }, function(env, callback) {
  115. var paths;
  116. paths = ['contents', 'templates'];
  117. return async.forEach(paths, function(pathname, callback) {
  118. var resolved;
  119. resolved = env.resolvePath(env.config[pathname]);
  120. return fileExists(resolved, function(exists) {
  121. if (exists) {
  122. return callback();
  123. } else {
  124. return callback(new Error(pathname + " path invalid (" + resolved + ")"));
  125. }
  126. });
  127. }, function(error) {
  128. return callback(error, env);
  129. });
  130. }
  131. ], callback);
  132. };
  133. if (stream.Writable == null) {
  134. stream.Writable = (function(superClass) {
  135. extend(Writable, superClass);
  136. function Writable() {
  137. Writable.__super__.constructor.call(this);
  138. this.writable = true;
  139. }
  140. Writable.prototype.write = function(string, encodig) {
  141. if (encodig == null) {
  142. encodig = 'utf8';
  143. }
  144. return this._write(string, encodig, function() {});
  145. };
  146. return Writable;
  147. })(stream.Stream);
  148. }
  149. exports.NpmAdapter = NpmAdapter = (function(superClass) {
  150. extend(NpmAdapter, superClass);
  151. /* Redirects output of npm to a logger */
  152. function NpmAdapter(logger1) {
  153. this.logger = logger1;
  154. this.buffer = '';
  155. NpmAdapter.__super__.constructor.call(this, {
  156. decodeStrings: false
  157. });
  158. }
  159. NpmAdapter.prototype._write = function(chunk, encoding, callback) {
  160. this.buffer += chunk;
  161. if (chunk.indexOf('\n') !== -1) {
  162. this.flush();
  163. }
  164. return callback();
  165. };
  166. NpmAdapter.prototype.flush = function() {
  167. var i, len, line, lines, results;
  168. lines = this.buffer.split('\n');
  169. this.buffer = '';
  170. results = [];
  171. for (i = 0, len = lines.length; i < len; i++) {
  172. line = lines[i];
  173. if (!(line.length > 0)) {
  174. continue;
  175. }
  176. line = line.replace(/^npm /, '');
  177. if (line.slice(0, 4) === 'WARN') {
  178. results.push(this.logger.warn("npm: " + line.slice(5)));
  179. } else {
  180. results.push(this.logger.verbose("npm: " + line));
  181. }
  182. }
  183. return results;
  184. };
  185. return NpmAdapter;
  186. })(stream.Writable);
  187. exports.getStorageDir = function() {
  188. /* Return users wintersmith directory, used for cache and user templates. */
  189. var dir, home;
  190. if (process.env.WINTERSMITH_PATH != null) {
  191. return process.env.WINTERSMITH_PATH;
  192. }
  193. home = process.env.HOME || process.env.USERPROFILE;
  194. dir = 'wintersmith';
  195. if (process.platform !== 'win32') {
  196. dir = '.' + dir;
  197. }
  198. return path.resolve(home, dir);
  199. };
  200. }).call(this);