ts.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. var childProcess = require('child_process');
  3. var fs = require('fs');
  4. var path = require('path');
  5. var isTest = process.argv[2] === '--only-test';
  6. read(function(err, fileNames) {
  7. clean(fileNames, function() {
  8. var hasLintError = false;
  9. compile(fileNames, function(err) {
  10. if (err) {
  11. console.error(err);
  12. return process.exit(1);
  13. }
  14. lint(fileNames, function(message) {
  15. process.stdout.write(message);
  16. hasLintError = true;
  17. }, function() {
  18. process.exit(Number(hasLintError));
  19. });
  20. });
  21. });
  22. });
  23. /**
  24. * Clean the files.
  25. * @param {Array.<string>} filePaths
  26. * @param {function()} done
  27. */
  28. function clean(filePaths, done) {
  29. if (isTest) return done();
  30. var i = -1;
  31. (function next() {
  32. i += 1;
  33. if (i >= filePaths.length) return done();
  34. var filePath = filePaths[i];
  35. if (/\.d\.ts$/.test(filePath)) return next();
  36. var mapName = filePath.substring(4, filePath.length - 2) + 'js.map';
  37. var mapPath = path.join('dist', mapName);
  38. if (fs.existsSync(mapPath)) fs.unlinkSync(mapPath);
  39. next();
  40. })();
  41. }
  42. /**
  43. * Compile the files.
  44. * @param {Array.<string>} filePaths
  45. * @param {function(Error)} done
  46. */
  47. function compile(filePaths, done) {
  48. if (isTest) return done(null);
  49. var execPath = path.join(__dirname, 'node_modules/.bin/tsc');
  50. var options = '--declaration --module CommonJS --noImplicitAny --outDir dist';
  51. childProcess.exec([execPath, options].concat(filePaths).join(' '), function(err, stdout) {
  52. if (stdout) return done(new Error(stdout));
  53. done(null);
  54. });
  55. }
  56. /**
  57. * Lint the files.
  58. * @param {Array.<string>} filePaths
  59. * @param {function(string)} handler
  60. * @param {function()} done
  61. */
  62. function lint(filePaths, handler, done) {
  63. var i = -1;
  64. var execPath = path.join(__dirname, 'node_modules/.bin/tslint');
  65. (function next() {
  66. i += 1;
  67. if (i >= filePaths.length) return done();
  68. var filePath = filePaths[i];
  69. if (/\.d\.ts$/.test(filePath)) return next();
  70. childProcess.exec(execPath + ' -f ' + filePath, function(err, stdout) {
  71. if (stdout) handler(stdout);
  72. next();
  73. });
  74. })();
  75. }
  76. /**
  77. * Read the files from the project file.
  78. * @param {function(Error, Array.<string>)} done
  79. */
  80. function read(done) {
  81. var contents = fs.readFileSync('crunchyroll.js.njsproj', 'utf8');
  82. var expression = /<TypeScriptCompile\s+Include="([\w\W]+?\.ts)" \/>/g;
  83. var matches;
  84. var filePaths = [];
  85. while ((matches = expression.exec(contents))) filePaths.push(matches[1]);
  86. done(null, filePaths);
  87. }