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) {
|
|
|
|
let toEmit = [];
|
|
|
|
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);
|