ts.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // TODO: This file can use some cleaning up. We want to use the tsconfig.json
  7. // and go from there, but then without source maps. That should give us a final
  8. // build output. For now, this legacy build file will remain to do its job.
  9. read(function(err, fileNames) {
  10. clean(fileNames, function() {
  11. var hasLintError = false;
  12. compile(fileNames, function(err) {
  13. if (err) {
  14. console.error(err);
  15. return process.exit(1);
  16. }
  17. lint(fileNames, function(message) {
  18. process.stdout.write(message);
  19. hasLintError = true;
  20. }, function() {
  21. process.exit(Number(hasLintError));
  22. });
  23. });
  24. });
  25. });
  26. /**
  27. * Clean the files.
  28. * @param {Array.<string>} filePaths
  29. * @param {function()} done
  30. */
  31. function clean(filePaths, done) {
  32. if (isTest) return done();
  33. var i = -1;
  34. (function next() {
  35. i += 1;
  36. if (i >= filePaths.length) return done();
  37. var filePath = filePaths[i];
  38. if (/\.d\.ts$/.test(filePath)) return next();
  39. var mapName = filePath.substring(4, filePath.length - 2) + 'js.map';
  40. var mapPath = path.join('dist', mapName);
  41. if (fs.existsSync(mapPath)) fs.unlinkSync(mapPath);
  42. next();
  43. })();
  44. }
  45. /**
  46. * Compile the files.
  47. * @param {Array.<string>} filePaths
  48. * @param {function(Error)} done
  49. */
  50. function compile(filePaths, done) {
  51. if (isTest) return done(null);
  52. var execPath = path.join(__dirname, 'node_modules/.bin/tsc');
  53. var options = '--declaration --module CommonJS --noImplicitAny --outDir dist --target ES5';
  54. childProcess.exec([execPath, options].concat(filePaths).join(' '), function(err, stdout) {
  55. if (stdout) return done(new Error(stdout));
  56. done(null);
  57. });
  58. }
  59. /**
  60. * Lint the files.
  61. * @param {Array.<string>} filePaths
  62. * @param {function(string)} handler
  63. * @param {function()} done
  64. */
  65. function lint(filePaths, handler, done) {
  66. var i = -1;
  67. var execPath = path.join(__dirname, 'node_modules/.bin/tslint');
  68. (function next() {
  69. i += 1;
  70. if (i >= filePaths.length) return done();
  71. var filePath = filePaths[i];
  72. if (/\.d\.ts$/.test(filePath)) return next();
  73. childProcess.exec(execPath + ' -f ' + filePath, function(err, stdout) {
  74. if (stdout) handler(stdout);
  75. next();
  76. });
  77. })();
  78. }
  79. /**
  80. * Read the files from the project file.
  81. * @param {function(Error, Array.<string>)} done
  82. */
  83. function read(done) {
  84. done(null, JSON.parse(fs.readFileSync('tsconfig.json', 'utf8')).files);
  85. }