2015-04-10 17:30:35 -04:00
|
|
|
import fs = require('fs');
|
|
|
|
import fse = require('fs-extra');
|
|
|
|
import path = require('path');
|
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
|
|
|
|
2016-02-12 17:21:15 -05:00
|
|
|
private transpiler: any /*ts2dart.Transpiler*/;
|
2015-04-10 17:30:35 -04:00
|
|
|
|
2016-05-26 13:45:37 -04:00
|
|
|
constructor(
|
|
|
|
public inputPath: string, public cachePath: string,
|
|
|
|
public options: any /*ts2dart.TranspilerOptions*/) {
|
2015-05-08 13:46:38 -04:00
|
|
|
options.basePath = inputPath;
|
2016-04-26 00:31:46 -04:00
|
|
|
options.tsconfig = path.join(inputPath, options.tsconfig);
|
2016-02-12 17:21:15 -05:00
|
|
|
// Workaround for https://github.com/dart-lang/dart_style/issues/493
|
|
|
|
var ts2dart = require('ts2dart');
|
2015-05-08 13:46:38 -04:00
|
|
|
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) {
|
2016-02-17 11:34:58 -05:00
|
|
|
let toEmit = [
|
|
|
|
path.resolve(this.inputPath, 'angular2/manual_typings/globals.d.ts'),
|
|
|
|
path.resolve(this.inputPath, 'angular2/typings/es6-promise/es6-promise.d.ts'),
|
|
|
|
path.resolve(this.inputPath, 'angular2/typings/es6-collections/es6-collections.d.ts')
|
|
|
|
];
|
2015-05-08 13:46:38 -04:00
|
|
|
let getDartFilePath = (path: string) => path.replace(/((\.js)|(\.ts))$/i, '.dart');
|
2016-05-26 13:45:37 -04:00
|
|
|
treeDiff.addedPaths.concat(treeDiff.changedPaths).forEach((changedPath) => {
|
|
|
|
let inputFilePath = path.resolve(this.inputPath, changedPath);
|
2015-05-08 13:46:38 -04:00
|
|
|
|
2016-05-26 13:45:37 -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
|
|
|
|
2016-05-26 13:45:37 -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);
|