ts.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 build task should be removed upon release of TypeScript 1.5 with
  7. // the support for `tsconfig.json`. Invoking `tsc` from `package.json` will then
  8. // read the configuration and compile accordingly. It seems that `TSLint` will,
  9. // eventually, support this mechanism too. That prevents the need for any kind
  10. // of build task and will run entirely based on instructions from `npm`.
  11. //
  12. // Reference #1: https://github.com/Microsoft/TypeScript/issues/1667
  13. // Reference #2: https://github.com/palantir/tslint/issues/281
  14. read(function(err, fileNames) {
  15. clean(fileNames, function() {
  16. var hasLintError = false;
  17. compile(fileNames, function(err) {
  18. if (err) {
  19. console.error(err);
  20. return process.exit(1);
  21. }
  22. lint(fileNames, function(message) {
  23. process.stdout.write(message);
  24. hasLintError = true;
  25. }, function() {
  26. process.exit(Number(hasLintError));
  27. });
  28. });
  29. });
  30. });
  31. /**
  32. * Clean the files.
  33. * @param {Array.<string>} filePaths
  34. * @param {function()} done
  35. */
  36. function clean(filePaths, done) {
  37. if (isTest) return done();
  38. var i = -1;
  39. (function next() {
  40. i += 1;
  41. if (i >= filePaths.length) return done();
  42. var filePath = filePaths[i];
  43. if (/\.d\.ts$/.test(filePath)) return next();
  44. var mapName = filePath.substring(4, filePath.length - 2) + 'js.map';
  45. var mapPath = path.join('dist', mapName);
  46. if (fs.existsSync(mapPath)) fs.unlinkSync(mapPath);
  47. next();
  48. })();
  49. }
  50. /**
  51. * Compile the files.
  52. * @param {Array.<string>} filePaths
  53. * @param {function(Error)} done
  54. */
  55. function compile(filePaths, done) {
  56. if (isTest) return done(null);
  57. var execPath = path.join(__dirname, 'node_modules/.bin/tsc');
  58. var options = '--declaration --module CommonJS --noImplicitAny --outDir dist --target ES5';
  59. childProcess.exec([execPath, options].concat(filePaths).join(' '), function(err, stdout) {
  60. if (stdout) return done(new Error(stdout));
  61. done(null);
  62. });
  63. }
  64. /**
  65. * Lint the files.
  66. * @param {Array.<string>} filePaths
  67. * @param {function(string)} handler
  68. * @param {function()} done
  69. */
  70. function lint(filePaths, handler, done) {
  71. var i = -1;
  72. var execPath = path.join(__dirname, 'node_modules/.bin/tslint');
  73. (function next() {
  74. i += 1;
  75. if (i >= filePaths.length) return done();
  76. var filePath = filePaths[i];
  77. if (/\.d\.ts$/.test(filePath)) return next();
  78. childProcess.exec(execPath + ' -f ' + filePath, function(err, stdout) {
  79. if (stdout) handler(stdout);
  80. next();
  81. });
  82. })();
  83. }
  84. /**
  85. * Read the files from the project file.
  86. * @param {function(Error, Array.<string>)} done
  87. */
  88. function read(done) {
  89. done(null, JSON.parse(fs.readFileSync('tsconfig.json', 'utf8')).files);
  90. }