Skip to content

Commit

Permalink
Stop using rootDir option
Browse files Browse the repository at this point in the history
  • Loading branch information
smrq committed May 12, 2016
1 parent efe4f5a commit a585084
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
25 changes: 19 additions & 6 deletions lib/Host.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var events = require('events');
var fs = require('fs');
var log = require('util').debuglog(require('../package').name);
var os = require('os');
var path = require('path');
var util = require('util');
var commondir = require('commondir');
var events = require('events');
var fs = require('fs');
var log = require('util').debuglog(require('../package').name);
var os = require('os');
var path = require('path');
var util = require('util');

module.exports = function (ts) {
function Host(currentDirectory, languageVersion) {
Expand Down Expand Up @@ -118,5 +119,17 @@ module.exports = function (ts) {
return ts.sys.readFile(normalized);
};

Host.prototype._rootDir = function () {
var dirs = [];
for (var filename in this.files) {
if (!Object.hasOwnProperty.call(this.files, filename)) continue;
if (filename === this._normalizedRelative(this.getDefaultLibFileName())) continue;

dirs.push(path.dirname(filename));
}
var result = commondir(this.currentDirectory, dirs);
return result;
};

return Host;
};
9 changes: 7 additions & 2 deletions lib/Tsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module.exports = function (ts) {
fileExists);
}

var files;
if (configFile) {
log('Using tsconfig file at ' + configFile);
var configFileContents = require(path.resolve(currentDirectory, configFile));
Expand Down Expand Up @@ -91,7 +92,7 @@ module.exports = function (ts) {
// The output directory needs to be distinct from the input directory to prevent the TS
// compiler from thinking that it might accidentally overwrite source files, which would
// prevent it from outputting e.g. the results of transpiling ES6 JS files with --allowJs.
parsed.options.rootDir = currentDirectory;
delete parsed.options.rootDir;
parsed.options.outDir = '__tsify__';

if (configFile) {
Expand Down Expand Up @@ -269,9 +270,13 @@ module.exports = function (ts) {
Tsifier.prototype.getCompiledFile = function (inputFile, alreadyMissedCache) {
var self = this;
var normalized = ts.normalizePath(inputFile);
var rootDir = self.host._rootDir();

var outputExtension = (self.opts.jsx === ts.JsxEmit.Preserve && isTsx(inputFile)) ? '.jsx' : '.js';
var outputFile = '__tsify__/' + replaceFileExtension(normalized, outputExtension);
var outputFile = '__tsify__/' + path.relative(
rootDir,
path.resolve(replaceFileExtension(normalized, outputExtension))
).replace(/\\/g, '/');
var output = self.host.output[outputFile];

if (!output) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"node": ">=0.12"
},
"dependencies": {
"commondir": "^1.0.1",
"convert-source-map": "^1.1.0",
"through2": "^2.0.0",
"typescript": "~1.8.7"
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ function expectNoErrors(t, errors) {

function expectErrors(t, actual, expected) {
t.equal(actual.length, expected.length, 'Should have the correct error count');
for (var i = 0; i < actual.length; ++i) {
for (var i = 0; i < actual.length && i < expected.length; ++i) {
t.equal(actual[i].fileName, expected[i].file, 'Error #' + i + ' should have filename ' + expected[i].name);
t.equal(actual[i].line, expected[i].line, 'Error #' + i + ' should be on line ' + expected[i].line);
t.equal(actual[i].column, expected[i].column, 'Error #' + i + ' should be on column ' + expected[i].column);
Expand Down

0 comments on commit a585084

Please sign in to comment.