You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
18 KiB

  1. // Generated by CoffeeScript 1.6.3
  2. var ConsoleInterface, TaskGroup, balUtil, extendr, pathUtil, safefs,
  3. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  4. __slice = [].slice,
  5. __hasProp = {}.hasOwnProperty;
  6. pathUtil = require('path');
  7. balUtil = require('bal-util');
  8. safefs = require('safefs');
  9. TaskGroup = require('taskgroup').TaskGroup;
  10. extendr = require('extendr');
  11. ConsoleInterface = (function() {
  12. function ConsoleInterface(opts, next) {
  13. this.watch = __bind(this.watch, this);
  14. this.skeleton = __bind(this.skeleton, this);
  15. this.clean = __bind(this.clean, this);
  16. this.server = __bind(this.server, this);
  17. this.run = __bind(this.run, this);
  18. this.render = __bind(this.render, this);
  19. this.install = __bind(this.install, this);
  20. this.update = __bind(this.update, this);
  21. this.info = __bind(this.info, this);
  22. this.help = __bind(this.help, this);
  23. this.generate = __bind(this.generate, this);
  24. this.init = __bind(this.init, this);
  25. this.action = __bind(this.action, this);
  26. this.welcomeCallback = __bind(this.welcomeCallback, this);
  27. this.selectSkeletonCallback = __bind(this.selectSkeletonCallback, this);
  28. this.extractConfig = __bind(this.extractConfig, this);
  29. this.completeAction = __bind(this.completeAction, this);
  30. this.performAction = __bind(this.performAction, this);
  31. this.handleError = __bind(this.handleError, this);
  32. this.getCommander = __bind(this.getCommander, this);
  33. this.start = __bind(this.start, this);
  34. var commander, consoleInterface, docpad, locale, version;
  35. consoleInterface = this;
  36. this.docpad = docpad = opts.docpad;
  37. this.commander = commander = require('commander');
  38. locale = docpad.getLocale();
  39. version = require('../../../package.json').version;
  40. commander.version(version).option('-o, --out <outPath>', locale.consoleOptionOut).option('-c, --config <configPath>', locale.consoleOptionConfig).option('-e, --env <environment>', locale.consoleOptionEnv).option('-d, --debug [logLevel]', locale.consoleOptionDebug, parseInt).option('-f, --force', locale.consoleOptionForce).option('-p, --port <port>', locale.consoleOptionPort, parseInt).option('-s, --skeleton <skeleton>', locale.consoleOptionSkeleton).option('--profile', locale.consoleOptionProfile).option('--offline', locale.consoleOptionOffline);
  41. commander.command('action <actions>').description(locale.consoleDescriptionRun).action(consoleInterface.wrapAction(consoleInterface.action));
  42. commander.command('init').description(locale.consoleDescriptionInit).action(consoleInterface.wrapAction(consoleInterface.init));
  43. commander.command('run').description(locale.consoleDescriptionRun).action(consoleInterface.wrapAction(consoleInterface.run));
  44. commander.command('server').description(locale.consoleDescriptionServer).action(consoleInterface.wrapAction(consoleInterface.server));
  45. commander.command('skeleton').description(locale.consoleDescriptionSkeleton).option('-s, --skeleton <skeleton>', locale.consoleOptionSkeleton).action(consoleInterface.wrapAction(consoleInterface.skeleton));
  46. commander.command('render [path]').description(locale.consoleDescriptionRender).action(consoleInterface.wrapAction(consoleInterface.render, {
  47. logLevel: 3,
  48. checkVersion: false,
  49. welcome: false,
  50. prompts: false
  51. }));
  52. commander.command('generate').description(locale.consoleDescriptionGenerate).action(consoleInterface.wrapAction(consoleInterface.generate));
  53. commander.command('watch').description(locale.consoleDescriptionWatch).action(consoleInterface.wrapAction(consoleInterface.watch));
  54. commander.command('update').description(locale.consoleDescriptionUpdate).action(consoleInterface.wrapAction(consoleInterface.update));
  55. commander.command('install [pluginName]').description(locale.consoleDescriptionInstall).action(consoleInterface.wrapAction(consoleInterface.install));
  56. commander.command('clean').description(locale.consoleDescriptionClean).action(consoleInterface.wrapAction(consoleInterface.clean));
  57. commander.command('info').description(locale.consoleDescriptionInfo).action(consoleInterface.wrapAction(consoleInterface.info));
  58. commander.command('help').description(locale.consoleDescriptionHelp).action(consoleInterface.wrapAction(consoleInterface.help));
  59. commander.command('*').description(locale.consoleDescriptionUnknown).action(consoleInterface.wrapAction(consoleInterface.help));
  60. docpad.on('welcome', function(data, next) {
  61. return consoleInterface.welcomeCallback(data, next);
  62. });
  63. docpad.emitSerial('consoleSetup', {
  64. consoleInterface: consoleInterface,
  65. commander: commander
  66. }, function(err) {
  67. if (err) {
  68. return consoleInterface.handleError(err);
  69. }
  70. return next(null, consoleInterface);
  71. });
  72. this;
  73. }
  74. ConsoleInterface.prototype.start = function(argv) {
  75. this.commander.parse(argv || process.argv);
  76. return this;
  77. };
  78. ConsoleInterface.prototype.getCommander = function() {
  79. return this.commander;
  80. };
  81. ConsoleInterface.prototype.handleError = function(err) {
  82. var docpad, locale;
  83. docpad = this.docpad;
  84. locale = docpad.getLocale();
  85. docpad.log('error', locale.consoleError);
  86. docpad.error(err);
  87. process.exit(1);
  88. return this;
  89. };
  90. ConsoleInterface.prototype.wrapAction = function(action, config) {
  91. var consoleInterface;
  92. consoleInterface = this;
  93. return function() {
  94. var args;
  95. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  96. return consoleInterface.performAction(action, args, config);
  97. };
  98. };
  99. ConsoleInterface.prototype.performAction = function(action, args, config) {
  100. var opts,
  101. _this = this;
  102. opts = {};
  103. opts.commander = args.slice(-1)[0];
  104. opts.args = args.slice(0, -1);
  105. opts.instanceConfig = extendr.safeDeepExtendPlainObjects({}, this.extractConfig(opts.commander), config);
  106. this.docpad.action('load ready', opts.instanceConfig, function(err) {
  107. if (err) {
  108. return _this.completeAction(err);
  109. }
  110. return action(_this.completeAction, opts);
  111. });
  112. return this;
  113. };
  114. ConsoleInterface.prototype.completeAction = function(err) {
  115. var docpad, locale;
  116. docpad = this.docpad;
  117. locale = docpad.getLocale();
  118. if (err) {
  119. this.handleError(err);
  120. } else {
  121. docpad.log('info', locale.consoleSuccess);
  122. }
  123. return this;
  124. };
  125. ConsoleInterface.prototype.extractConfig = function(customConfig) {
  126. var commanderConfig, config, configPath, key, outPath, sourceConfig, value;
  127. if (customConfig == null) {
  128. customConfig = {};
  129. }
  130. config = {};
  131. commanderConfig = this.commander;
  132. sourceConfig = this.docpad.initialConfig;
  133. if (commanderConfig.debug) {
  134. if (commanderConfig.debug === true) {
  135. commanderConfig.debug = 7;
  136. }
  137. commanderConfig.logLevel = commanderConfig.debug;
  138. }
  139. if (commanderConfig.config) {
  140. configPath = pathUtil.resolve(process.cwd(), commanderConfig.config);
  141. commanderConfig.configPaths = [configPath];
  142. }
  143. if (commanderConfig.out) {
  144. outPath = pathUtil.resolve(process.cwd(), commanderConfig.out);
  145. commanderConfig.outPath = outPath;
  146. }
  147. for (key in commanderConfig) {
  148. if (!__hasProp.call(commanderConfig, key)) continue;
  149. value = commanderConfig[key];
  150. if (typeof sourceConfig[key] !== 'undefined') {
  151. config[key] = value;
  152. }
  153. }
  154. for (key in customConfig) {
  155. if (!__hasProp.call(customConfig, key)) continue;
  156. value = customConfig[key];
  157. if (typeof sourceConfig[key] !== 'undefined') {
  158. config[key] = value;
  159. }
  160. }
  161. return config;
  162. };
  163. ConsoleInterface.prototype.selectSkeletonCallback = function(skeletonsCollection, next) {
  164. var commander, docpad, locale, skeletonNames;
  165. commander = this.commander;
  166. docpad = this.docpad;
  167. locale = docpad.getLocale();
  168. skeletonNames = [];
  169. docpad.log('info', locale.skeletonSelectionIntroduction + '\n');
  170. skeletonsCollection.forEach(function(skeletonModel) {
  171. var skeletonDescription, skeletonName;
  172. skeletonName = skeletonModel.get('name');
  173. skeletonDescription = skeletonModel.get('description').replace(/\n/g, '\n\t');
  174. skeletonNames.push(skeletonName);
  175. return console.log("" + (skeletonModel.get('position') + 1) + ". " + skeletonName + "\n " + skeletonDescription + "\n");
  176. });
  177. docpad.log('info', locale.skeletonSelectionPrompt);
  178. commander.choose(skeletonNames, function(i) {
  179. process.stdin.destroy();
  180. return next(null, skeletonsCollection.at(i));
  181. });
  182. return this;
  183. };
  184. ConsoleInterface.prototype.welcomeCallback = function(opts, next) {
  185. var commander, consoleInterface, docpad, locale, userConfig, welcomeTasks;
  186. consoleInterface = this;
  187. commander = this.commander;
  188. docpad = this.docpad;
  189. locale = docpad.getLocale();
  190. userConfig = docpad.userConfig;
  191. welcomeTasks = new TaskGroup().once('complete', next);
  192. welcomeTasks.addTask(function(complete) {
  193. if (docpad.config.prompts === false || userConfig.tos === true) {
  194. return complete();
  195. }
  196. return consoleInterface.confirm(locale.tosPrompt, true, function(ok) {
  197. return docpad.track('tos', {
  198. ok: ok
  199. }, function(err) {
  200. if (ok) {
  201. userConfig.tos = true;
  202. console.log(locale.tosAgree);
  203. docpad.updateUserConfig(complete);
  204. } else {
  205. console.log(locale.tosDisagree);
  206. process.exit();
  207. }
  208. });
  209. });
  210. });
  211. welcomeTasks.addTask(function(complete) {
  212. if (docpad.config.prompts === false || (userConfig.subscribed != null) || ((userConfig.subscribeTryAgain != null) && (new Date()) > (new Date(userConfig.subscribeTryAgain)))) {
  213. return complete();
  214. }
  215. return consoleInterface.confirm(locale.subscribePrompt, true, function(ok) {
  216. return docpad.track('subscribe', {
  217. ok: ok
  218. }, function(err) {
  219. var commands;
  220. if (!ok) {
  221. console.log(locale.subscribeIgnore);
  222. userConfig.subscribed = false;
  223. docpad.updateUserConfig(function(err) {
  224. if (err) {
  225. return complete(err);
  226. }
  227. return balUtil.wait(2000, complete);
  228. });
  229. return;
  230. }
  231. commands = [['config', '--get', 'user.name'], ['config', '--get', 'user.email'], ['config', '--get', 'github.user']];
  232. return safeps.spawnCommands('git', commands, function(err, results) {
  233. var subscribeTasks, _ref, _ref1, _ref2;
  234. userConfig.name || (userConfig.name = String((results != null ? (_ref = results[0]) != null ? _ref[1] : void 0 : void 0) || '').trim() || null);
  235. userConfig.email || (userConfig.email = String((results != null ? (_ref1 = results[1]) != null ? _ref1[1] : void 0 : void 0) || '').trim() || null);
  236. userConfig.username || (userConfig.username = String((results != null ? (_ref2 = results[2]) != null ? _ref2[1] : void 0 : void 0) || '').trim() || null);
  237. if (userConfig.name || userConfig.email || userConfig.username) {
  238. console.log(locale.subscribeConfigNotify);
  239. }
  240. subscribeTasks = new TaskGroup().once('complete', function(err) {
  241. if (err) {
  242. console.log(locale.subscribeError);
  243. userConfig.subscribeTryAgain = new Date().getTime() + 1000 * 60 * 60 * 24;
  244. } else {
  245. console.log(locale.subscribeSuccess);
  246. userConfig.subscribed = true;
  247. userConfig.subscribeTryAgain = null;
  248. }
  249. return docpad.updateUserConfig(userConfig, complete);
  250. });
  251. subscribeTasks.addTask(function(complete) {
  252. return consoleInterface.prompt(locale.subscribeNamePrompt, userConfig.name, function(result) {
  253. userConfig.name = result;
  254. return complete();
  255. });
  256. });
  257. subscribeTasks.addTask(function(complete) {
  258. return consoleInterface.prompt(locale.subscribeEmailPrompt, userConfig.email, function(result) {
  259. userConfig.email = result;
  260. return complete();
  261. });
  262. });
  263. subscribeTasks.addTask(function(complete) {
  264. return consoleInterface.prompt(locale.subscribeUsernamePrompt, userConfig.username, function(result) {
  265. userConfig.username = result;
  266. return complete();
  267. });
  268. });
  269. subscribeTasks.addTask(function(complete) {
  270. return docpad.updateUserConfig(complete);
  271. });
  272. subscribeTasks.addTask(function(complete) {
  273. console.log(locale.subscribeProgress);
  274. return docpad.subscribe(function(err, res) {
  275. if (err) {
  276. docpad.log('debug', locale.subscribeRequestError, err.message);
  277. return complete(err);
  278. }
  279. docpad.log('debug', locale.subscribeRequestData, res.text);
  280. return complete();
  281. });
  282. });
  283. return subscribeTasks.run();
  284. });
  285. });
  286. });
  287. });
  288. welcomeTasks.run();
  289. return this;
  290. };
  291. ConsoleInterface.prototype.prompt = function(message, fallback, next) {
  292. var commander, consoleInterface;
  293. consoleInterface = this;
  294. commander = this.commander;
  295. if (fallback) {
  296. message += " [" + fallback + "]";
  297. }
  298. commander.prompt(message + ' ', function(result) {
  299. if (!result.trim()) {
  300. if (fallback != null) {
  301. result = fallback;
  302. } else {
  303. return consoleInterface.prompt(message, fallback, next);
  304. }
  305. }
  306. return next(result);
  307. });
  308. return this;
  309. };
  310. ConsoleInterface.prototype.confirm = function(message, fallback, next) {
  311. var commander, consoleInterface;
  312. consoleInterface = this;
  313. commander = this.commander;
  314. if (fallback === true) {
  315. message += " [Y/n]";
  316. } else if (fallback === false) {
  317. message += " [y/N]";
  318. }
  319. commander.prompt(message + ' ', function(ok) {
  320. if (!ok.trim()) {
  321. if (fallback != null) {
  322. ok = fallback;
  323. } else {
  324. return consoleInterface.confirm(message, fallback, next);
  325. }
  326. } else {
  327. ok = /^y|yes|ok|true$/i.test(ok);
  328. }
  329. return next(ok);
  330. });
  331. return this;
  332. };
  333. ConsoleInterface.prototype.action = function(next, opts) {
  334. var actions;
  335. actions = opts.args[0];
  336. this.docpad.log('info', 'Performing the actions:', actions);
  337. this.docpad.action(actions, next);
  338. return this;
  339. };
  340. ConsoleInterface.prototype.init = function(next) {
  341. this.docpad.action('init', next);
  342. return this;
  343. };
  344. ConsoleInterface.prototype.generate = function(next) {
  345. this.docpad.action('generate', next);
  346. return this;
  347. };
  348. ConsoleInterface.prototype.help = function(next) {
  349. var help;
  350. help = this.commander.helpInformation();
  351. console.log(help);
  352. next();
  353. return this;
  354. };
  355. ConsoleInterface.prototype.info = function(next) {
  356. var info;
  357. info = require('util').inspect(this.docpad.config);
  358. console.log(info);
  359. next();
  360. return this;
  361. };
  362. ConsoleInterface.prototype.update = function(next, opts) {
  363. this.docpad.action('update', next);
  364. return this;
  365. };
  366. ConsoleInterface.prototype.install = function(next, opts) {
  367. var plugin;
  368. plugin = opts.args[0] || null;
  369. this.docpad.action('install', {
  370. plugin: plugin
  371. }, next);
  372. return this;
  373. };
  374. ConsoleInterface.prototype.render = function(next, opts) {
  375. var basename, commander, data, docpad, filename, renderDocument, renderOpts, stdin, timeout, useStdin;
  376. docpad = this.docpad;
  377. commander = this.commander;
  378. renderOpts = {};
  379. filename = opts.args[0] || null;
  380. basename = pathUtil.basename(filename);
  381. renderOpts.filename = filename;
  382. renderOpts.renderSingleExtensions = 'auto';
  383. data = '';
  384. useStdin = true;
  385. renderDocument = function() {
  386. return docpad.action('render', renderOpts, function(err, result) {
  387. if (err) {
  388. return docpad.fatal(err);
  389. }
  390. if (commander.out != null) {
  391. return safefs.writeFile(commander.out, result, next);
  392. } else {
  393. process.stdout.write(result);
  394. return next();
  395. }
  396. });
  397. };
  398. timeout = setTimeout(function() {
  399. timeout = null;
  400. if (data.replace(/\s+/, '')) {
  401. return;
  402. }
  403. useStdin = false;
  404. stdin.pause();
  405. return renderDocument();
  406. }, 1000);
  407. stdin = process.stdin;
  408. stdin.resume();
  409. stdin.setEncoding('utf8');
  410. stdin.on('data', function(_data) {
  411. return data += _data.toString();
  412. });
  413. process.stdin.on('end', function() {
  414. if (!useStdin) {
  415. return;
  416. }
  417. if (timeout) {
  418. clearTimeout(timeout);
  419. timeout = null;
  420. }
  421. renderOpts.data = data;
  422. return renderDocument();
  423. });
  424. return this;
  425. };
  426. ConsoleInterface.prototype.run = function(next) {
  427. this.docpad.action('run', {
  428. selectSkeletonCallback: this.selectSkeletonCallback
  429. }, next);
  430. return this;
  431. };
  432. ConsoleInterface.prototype.server = function(next) {
  433. this.docpad.action('server generate', next);
  434. return this;
  435. };
  436. ConsoleInterface.prototype.clean = function(next) {
  437. this.docpad.action('clean', next);
  438. return this;
  439. };
  440. ConsoleInterface.prototype.skeleton = function(next) {
  441. this.docpad.action('skeleton', {
  442. selectSkeletonCallback: this.selectSkeletonCallback
  443. }, next);
  444. return this;
  445. };
  446. ConsoleInterface.prototype.watch = function(next) {
  447. this.docpad.action('generate watch', next);
  448. return this;
  449. };
  450. return ConsoleInterface;
  451. })();
  452. module.exports = ConsoleInterface;