ascli.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright 2013 Daniel Wirtz <dcode@dcode.io>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var util = require("util"),
  14. path = require("path"),
  15. colour = require("colour");
  16. // Default alphabet
  17. var alphabet = require(path.join(__dirname, "alphabet", "straight.json"));
  18. module.exports = function(appName) {
  19. ascli.appName = appName;
  20. return ascli;
  21. };
  22. module.exports.app = module.exports; // For backward compatibility
  23. /**
  24. * Builds a banner.
  25. * @param {string=} title App name
  26. * @param {string=} appendix Appendix, e.g. version
  27. * @returns {string}
  28. */
  29. function ascli(title, appendix) {
  30. title = title || ascli.appName;
  31. appendix = appendix || "";
  32. var lines = ["", "", ""], c, a, j, ac = "";
  33. for (var i=0; i<title.length; i++) {
  34. c = title.charAt(i);
  35. if (c == '\x1B') {
  36. while ((c=title.charAt(i)) != 'm') {
  37. ac += c;
  38. i++;
  39. }
  40. ac += c;
  41. } else if ((a=alphabet[c])||(a=alphabet[c.toLowerCase()]))
  42. for (j=0; j<3; j++)
  43. lines[j] += ac+a[j];
  44. }
  45. for (i=0; i<lines.length; i++) lines[i] = lines[i]+"\x1B[0m";
  46. lines[1] += " "+appendix;
  47. if (lines[lines.length-1].strip.trim().length == 0) {
  48. lines.pop();
  49. }
  50. return '\n'+lines.join('\n')+'\n';
  51. }
  52. // Indent by one
  53. function indent1() {
  54. this.write(" "+util.format.apply(null, arguments).replace(/\n/g, "\n ")+"\n");
  55. }
  56. ascli.log = indent1.bind(process.stdout);
  57. ascli.info = indent1.bind(process.stdout);
  58. ascli.warn = indent1.bind(process.stderr);
  59. ascli.error = indent1.bind(process.stderr);
  60. /**
  61. * App name.
  62. * @type {string}
  63. */
  64. ascli.appName = "app";
  65. /**
  66. * Prints a banner to console.
  67. * @param {string=} title Title in dojo alphabet
  68. * @param {string=} appendix Title appendix
  69. * @returns {Function} ascli
  70. */
  71. ascli.banner = function(title, appendix) {
  72. console.log(ascli(title, appendix));
  73. return ascli;
  74. };
  75. /**
  76. * Uses another alphabet.
  77. * @param {string|Object.<string,Array.<string>} alpha File name or alphabet to use
  78. * @returns {Function} ascli
  79. */
  80. ascli.use = function(alpha) {
  81. if (typeof alpha === 'string')
  82. alphabet = require(alpha);
  83. else
  84. alphabet = alpha;
  85. return ascli;
  86. };
  87. /**
  88. * Prints a final success message.
  89. * @param {string} msg Message text
  90. * @param {number=} code Exit code, defaults not to send it explicitly
  91. */
  92. ascli.ok = function(msg, code) {
  93. process.stderr.write('\n '+ascli.appName.green.bold+' OK'.white.bold+(msg ? ' '+msg : '')+'\n');
  94. if (typeof code !== 'undefined')
  95. process.exit(code);
  96. };
  97. /**
  98. * Prints a final failure message.
  99. * @param {string} msg Message text
  100. * @param {number=} code Exit code, defaults to not send it explicitly
  101. */
  102. ascli.fail = function(msg, code) {
  103. process.stderr.write('\n '+ascli.appName.red.bold+' ERROR'.white.bold+(msg ? ' '+msg : '')+'\n');
  104. if (typeof code !== 'undefined')
  105. process.exit(code);
  106. };
  107. /**
  108. * opt.js
  109. * @param {Array.<string>=} argv
  110. * @returns {{node: string, script: string, argv: Array.<string>, opt: Object.<string,boolean|string>}}
  111. */
  112. ascli.optjs = require("optjs");
  113. // Pre-run it
  114. var opt = ascli.optjs();
  115. ascli.node = opt.node;
  116. ascli.script = opt.script;
  117. ascli.argv = opt.argv;
  118. ascli.opt = opt.opt;
  119. // Expose colour.js
  120. ascli.colour = ascli.colors = colour;