2015-04-10 17:30:35 -04:00
|
|
|
/// <reference path="../typings/node/node.d.ts" />
|
|
|
|
/// <reference path="../typings/fs-extra/fs-extra.d.ts" />
|
2015-05-08 13:46:38 -04:00
|
|
|
/// <reference path="./ts2dart.d.ts" />
|
2015-04-10 17:30:35 -04:00
|
|
|
|
|
|
|
import fs = require('fs');
|
|
|
|
import fse = require('fs-extra');
|
|
|
|
import path = require('path');
|
|
|
|
import ts2dart = require('ts2dart');
|
2015-05-08 13:46:38 -04:00
|
|
|
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
|
2015-04-10 17:30:35 -04:00
|
|
|
|
2015-05-08 13:46:38 -04:00
|
|
|
class TSToDartTranspiler implements DiffingBroccoliPlugin {
|
2015-06-03 19:14:01 -04:00
|
|
|
static includeExtensions = ['.ts'];
|
2015-04-10 17:30:35 -04:00
|
|
|
|
2015-05-08 13:46:38 -04:00
|
|
|
private basePath: string;
|
|
|
|
private transpiler: ts2dart.Transpiler;
|
2015-04-10 17:30:35 -04:00
|
|
|
|
2015-05-08 13:46:38 -04:00
|
|
|
constructor(public inputPath: string, public cachePath: string, public options) {
|
|
|
|
options.basePath = inputPath;
|
|
|
|
this.transpiler = new ts2dart.Transpiler(options);
|
2015-04-10 17:30:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-08 13:46:38 -04:00
|
|
|
rebuild(treeDiff: DiffResult) {
|
2015-08-21 15:14:20 -04:00
|
|
|
// Matches rootFilePaths in node_tree.ts
|
|
|
|
// These files are not compatible with Typescript's ES6 library
|
|
|
|
// so they must be explicitly included when targetting ES5, as ts2dart does.
|
|
|
|
// see https://github.com/angular/angular/issues/3770
|
2015-08-21 17:45:17 -04:00
|
|
|
let toEmit = [path.resolve(this.inputPath, 'angular2/traceur-runtime.d.ts')];
|
2015-05-08 13:46:38 -04:00
|
|
|
let getDartFilePath = (path: string) => path.replace(/((\.js)|(\.ts))$/i, '.dart');
|
2015-05-31 20:24:21 -04:00
|
|
|
treeDiff.addedPaths.concat(treeDiff.changedPaths)
|
|
|
|
.forEach((changedPath) => {
|
|
|
|
let inputFilePath = path.resolve(this.inputPath, changedPath);
|
2015-05-08 13:46:38 -04:00
|
|
|
|
2015-05-31 20:24:21 -04:00
|
|
|
// Ignore files which don't need to be transpiled to Dart
|
|
|
|
let dartInputFilePath = getDartFilePath(inputFilePath);
|
|
|
|
if (fs.existsSync(dartInputFilePath)) return;
|
2015-05-08 13:46:38 -04:00
|
|
|
|
2015-05-31 20:24:21 -04:00
|
|
|
// Prepare to rebuild
|
|
|
|
toEmit.push(path.resolve(this.inputPath, changedPath));
|
|
|
|
});
|
2015-04-10 17:30:35 -04:00
|
|
|
|
2015-05-08 13:46:38 -04:00
|
|
|
treeDiff.removedPaths.forEach((removedPath) => {
|
|
|
|
let absolutePath = path.resolve(this.inputPath, removedPath);
|
|
|
|
|
|
|
|
// Ignore files which don't need to be transpiled to Dart
|
|
|
|
let dartInputFilePath = getDartFilePath(absolutePath);
|
|
|
|
if (fs.existsSync(dartInputFilePath)) return;
|
|
|
|
|
|
|
|
let dartOutputFilePath = getDartFilePath(removedPath);
|
|
|
|
fs.unlinkSync(path.join(this.cachePath, dartOutputFilePath));
|
2015-04-10 17:30:35 -04:00
|
|
|
});
|
2015-05-08 13:46:38 -04:00
|
|
|
|
|
|
|
this.transpiler.transpile(toEmit, this.cachePath);
|
2015-04-10 17:30:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 13:46:38 -04:00
|
|
|
export default wrapDiffingPlugin(TSToDartTranspiler);
|