Exemple #1
0
function tsc(input, output, options, callback, log) {

	if (!output) {
		output = input.replace(/\.ts$/, '.js');
	}

	var args = _.extend({}, options, {out: output});
	compile([input], args);

	fs.readFile(output, 'utf8', function(error, content) {
		if (error) {
			callback(error);
			return;
		}
		fs.unlink(output);
		callback(null, { js: content });
	});
}
function tsc(file, content, typings, options, callback, log) {
	var args = _.clone(options);
	var input  = file.originalPath + '.ktp.ts';
	var output = file.originalPath + '.ktp.js';
	file.outputPath = output + '.ktp.js';
	file.sourceMapPath = output + '.map';

	if (!('module' in args)) {
		args.out = output;
	}

	var opts = {files: [input].concat(typings), args: args};

	fs.writeFileSync(input, content);

	if (options.compiler) {
		delete args.compiler;
		opts.compiler = options.compiler;
	}

	compile(opts, function(err) {
		log.error(err);
	});

	fs.unlinkSync(input);

	if (fs.existsSync(file.path)) {
		fs.unlinkSync(file.path);
	}

	log.debug('preprocessed "%s"', file.originalPath);

	fs.readFile(output, 'utf8', function(error, content) {
		if (error) {
			callback(error, null);
			return;
		}
		fs.unlinkSync(output);
		callback(null, content);
	});
}
Exemple #3
0
module.exports = function(command, done) {
  var withSourceMaps = command.options['source-maps'] == 'true' ? true : false;
  
  var queue = getAllFiles(command.indir, 'ts');
  var opts = ['--module', 'commonjs', '--outDir', command.outdir];
  if (withSourceMaps) {
    opts.push('--sourcemap')
  }
  var exitCode = tsc.compile(queue, opts, function(e) {
    console.error(e);
    return false;
  });
  console.log('tsc exitCode: ' + exitCode);
  if (exitCode != 0) {
    throw new Error('Failed to compile typescript');
  }
  if (withSourceMaps) {
    queue.forEach(function(f) {
      var rel = path.relative(command.indir, f);
      var target = path.join(command.outdir, rel.replace('.ts', '.js'));
      
      fs.readFile(target, {encoding:'utf8'}, function(e, data) {
        if (e) throw e;
        js = data;
        var sourceMap = convert.fromMapFileSource(js, command.outdir);
        sourceMap.setProperty('sources', [rel]);
        js = convert.removeMapFileComments(js);
        js += '\n' + sourceMap.toComment() + '\n';
        
        fs.writeFile(target, js, function() {
          queue = queue.slice(1);
          if (queue.length == 0) done();
        });
      });
    });
  } else {
    done();
  }
}