|
1 | 1 | /// <reference path="./broccoli-writer.d.ts" /> |
2 | 2 | /// <reference path="../typings/node/node.d.ts" /> |
3 | 3 | /// <reference path="../typings/fs-extra/fs-extra.d.ts" /> |
| 4 | +/// <reference path="./ts2dart.d.ts" /> |
4 | 5 |
|
5 | | -import Writer = require('broccoli-writer'); |
6 | 6 | import fs = require('fs'); |
7 | 7 | import fse = require('fs-extra'); |
8 | 8 | import path = require('path'); |
9 | 9 | import ts2dart = require('ts2dart'); |
| 10 | +import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin'; |
10 | 11 |
|
11 | | -type Set = { |
12 | | - [s: string]: boolean |
13 | | -}; |
| 12 | +class TSToDartTranspiler implements DiffingBroccoliPlugin { |
| 13 | + static includeExtensions = ['.js', '.ts']; |
14 | 14 |
|
15 | | -class TypeScriptToDartTranspiler extends Writer { |
16 | | - constructor(private inputTree, private includePattern = /\.(js|ts)$/) { super(); } |
| 15 | + private basePath: string; |
| 16 | + private transpiler: ts2dart.Transpiler; |
17 | 17 |
|
18 | | - write(readTree, destDir): Promise<void> { |
19 | | - return readTree(this.inputTree).then(dir => this.transpile(dir, destDir)); |
| 18 | + constructor(public inputPath: string, public cachePath: string, public options) { |
| 19 | + options.basePath = inputPath; |
| 20 | + this.transpiler = new ts2dart.Transpiler(options); |
20 | 21 | } |
21 | 22 |
|
22 | | - private transpile(inputDir: string, destDir: string) { |
23 | | - var files = this.listRecursive(inputDir); |
24 | | - var toTranspile = []; |
25 | | - for (var f in files) { |
26 | | - // If it's not matching, don't translate. |
27 | | - if (!f.match(this.includePattern)) continue; |
28 | | - var dartVariant = f.replace(this.includePattern, '.dart'); |
29 | | - // A .dart file of the same name takes precedence over transpiled code. |
30 | | - if (files.hasOwnProperty(dartVariant)) continue; |
31 | | - toTranspile.push(f); |
32 | | - } |
33 | | - var transpiler = new ts2dart.Transpiler( |
34 | | - {generateLibraryName: true, generateSourceMap: false, basePath: inputDir}); |
35 | | - transpiler.transpile(toTranspile, destDir); |
36 | | - } |
| 23 | + rebuild(treeDiff: DiffResult) { |
| 24 | + let toEmit = []; |
| 25 | + let getDartFilePath = (path: string) => path.replace(/((\.js)|(\.ts))$/i, '.dart'); |
| 26 | + treeDiff.changedPaths.forEach((changedPath) => { |
| 27 | + let inputFilePath = path.resolve(this.inputPath, changedPath); |
| 28 | + |
| 29 | + // Ignore files which don't need to be transpiled to Dart |
| 30 | + let dartInputFilePath = getDartFilePath(inputFilePath); |
| 31 | + if (fs.existsSync(dartInputFilePath)) return; |
| 32 | + |
| 33 | + // Prepare to rebuild |
| 34 | + toEmit.push(path.resolve(this.inputPath, changedPath)); |
| 35 | + }); |
37 | 36 |
|
38 | | - private listRecursive(root: string, res: Set = {}): Set { |
39 | | - var paths = fs.readdirSync(root); |
40 | | - paths.forEach((p) => { |
41 | | - p = path.join(root, p); |
42 | | - var stat = fs.statSync(p); |
43 | | - if (stat.isDirectory()) { |
44 | | - this.listRecursive(p, res); |
45 | | - } else { |
46 | | - // Collect *all* files so we can check .dart files that already exist and exclude them. |
47 | | - res[p] = true; |
48 | | - } |
| 37 | + treeDiff.removedPaths.forEach((removedPath) => { |
| 38 | + let absolutePath = path.resolve(this.inputPath, removedPath); |
| 39 | + |
| 40 | + // Ignore files which don't need to be transpiled to Dart |
| 41 | + let dartInputFilePath = getDartFilePath(absolutePath); |
| 42 | + if (fs.existsSync(dartInputFilePath)) return; |
| 43 | + |
| 44 | + let dartOutputFilePath = getDartFilePath(removedPath); |
| 45 | + fs.unlinkSync(path.join(this.cachePath, dartOutputFilePath)); |
49 | 46 | }); |
50 | | - return res; |
| 47 | + |
| 48 | + this.transpiler.transpile(toEmit, this.cachePath); |
51 | 49 | } |
52 | 50 | } |
53 | 51 |
|
54 | | -export function transpile(inputTree) { |
55 | | - return new TypeScriptToDartTranspiler(inputTree); |
56 | | -} |
| 52 | +export default wrapDiffingPlugin(TSToDartTranspiler); |
0 commit comments