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.

507 lines
18 KiB

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