tmod 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var TmodJS = require('../tmod.js');
  4. var version = require('../package.json').version;
  5. var fs = require('fs');
  6. var path = require('path');
  7. var exec = require('child_process').exec;
  8. var os = require('os');
  9. var options = {};
  10. var help = function () {
  11. var message = [
  12. '\x1B[7mTmodJS - Template Compiler\x1B[27m',
  13. '',
  14. 'Usage:',
  15. ' tmod <path> [options]',
  16. 'Options:',
  17. [
  18. ' -w, --watch',
  19. '\x1B[90m use tmod in watch mode (auto compile when file changed)\x1B[39m',
  20. ' -d, --debug',
  21. '\x1B[90m debugging Template\x1B[39m',
  22. ' --type',
  23. '\x1B[90m optional: templatejs (Default) | cmd (RequireJS/SeaJS) | amd (RequireJS) | commonjs (NodeJS)\x1B[39m',
  24. ' --output value',
  25. '\x1B[90m defining an output directory\x1B[39m',
  26. ' --charset value',
  27. '\x1B[90m charset, utf-8 by default\x1B[39m',
  28. ' --version',
  29. '\x1B[90m display the version of TmodJS\x1B[39m',
  30. ' --help',
  31. '\x1B[90m show this help infomation\x1B[39m'
  32. ].join('\n'),
  33. '',
  34. '\x1B[90m' + 'Documentation can be found at http://aui.github.io/tmodjs/' + '\x1B[39m'
  35. ];
  36. message = message.join('\n');
  37. process.stdout.write(message + '\n');
  38. };
  39. var dir;
  40. var value;
  41. var userConfig;
  42. var isWatch = false;
  43. var isEditConfig = false;
  44. var args = process.argv.slice(2);
  45. if (args[0] && /^[^-]|\//.test(args[0])) {
  46. dir = args.shift();
  47. }
  48. while (args.length > 0) {
  49. value = args.shift();
  50. switch (value) {
  51. // 监控修改
  52. case '-w':
  53. case '--watch':
  54. isWatch = true;
  55. break;
  56. // 调试模式
  57. case '-d':
  58. case '--debug':
  59. options.debug = true;
  60. break;
  61. // 输出目录
  62. case '--output':
  63. options.output = args.shift();
  64. break;
  65. // 嵌入引擎
  66. case '--engine':
  67. options.engine = true;
  68. break;
  69. // 模板输出类型
  70. case '--type':
  71. options.type = args.shift();
  72. break;
  73. // 模板编码
  74. case '--charset':
  75. options.charset = args.shift();
  76. break;
  77. // 模板语法
  78. case '--syntax':
  79. options.syntax = args.shift();
  80. break;
  81. // 辅助方法路径
  82. case '--helpers':
  83. options.helpers = args.shift();
  84. break;
  85. case '--config':
  86. isEditConfig = true;
  87. break;
  88. // 显示帮助
  89. case '-h':
  90. case '--help':
  91. help();
  92. process.exit();
  93. break;
  94. // 版本号
  95. case '-v':
  96. case '--version':
  97. process.stdout.write(version + '\n');
  98. process.exit();
  99. break;
  100. default:
  101. if (!dir) {
  102. dir = value;
  103. }
  104. }
  105. }
  106. if (!dir) {
  107. dir = './';
  108. }
  109. if (!fs.existsSync(dir)) {
  110. process.stdout.write('Error: directory does not exist\n');
  111. help();
  112. process.exit(1);
  113. };
  114. // 转换成相对于模板目录的路径
  115. if (options.output) {
  116. options.output = path.relative(dir, path.resolve(options.output));
  117. }
  118. TmodJS.init(dir, options);
  119. TmodJS.on('error', function (data) {
  120. if (!isWatch) {
  121. process.exit(1);
  122. }
  123. });
  124. userConfig = TmodJS.saveUserConfig();
  125. if (isEditConfig) {
  126. process.stdout.write('open: ' + userConfig + '\n');
  127. exec(
  128. (/windows/i.test(os.type()) ? 'start' : 'open')
  129. + ' ' + userConfig, {timeout: 0}, function () {}
  130. );
  131. } else {
  132. TmodJS.compile();
  133. if (isWatch) {
  134. TmodJS.watch();
  135. }
  136. }