angular-docs-cn/tools/broccoli/broccoli-ts2dart.ts
Alex Eagle daf0f472b3 feat(build): enforce formatting of some files.
Our style guide includes formatting conventions. Instead of wasting time in reviewing PRs discussing things like indenting, and to avoid later deltas to fix bad formatting in earlier commits, we want to enforce these in the build.
The intent in this change is to fail the build as quickly as possible in travis, so those sending a PR immediately know they should run clang-format and update their commit. When running locally, we want users to know about formatting, but they may not want to act on it immediately, until they are done working. For this reason, it is only a warning outside of the continuous build.
This is done by having a check-format task which should run on most local builds, and an enforce-format task only run by travis.
2015-04-11 18:39:28 -07:00

57 lines
1.8 KiB
TypeScript

/// <reference path="./broccoli-writer.d.ts" />
/// <reference path="../typings/node/node.d.ts" />
/// <reference path="../typings/fs-extra/fs-extra.d.ts" />
import Writer = require('broccoli-writer');
import fs = require('fs');
import fse = require('fs-extra');
import path = require('path');
import ts2dart = require('ts2dart');
type Set = {
[s: string]: boolean
};
class TypeScriptToDartTranspiler extends Writer {
constructor(private inputTree, private includePattern = /\.(js|ts)$/) { super(); }
write(readTree, destDir): Promise<void> {
return readTree(this.inputTree).then(dir => this.transpile(dir, destDir));
}
private transpile(inputDir: string, destDir: string) {
var files = this.listRecursive(inputDir);
var toTranspile = [];
for (var f in files) {
// If it's not matching, don't translate.
if (!f.match(this.includePattern)) continue;
var dartVariant = f.replace(this.includePattern, '.dart');
// A .dart file of the same name takes precedence over transpiled code.
if (files.hasOwnProperty(dartVariant)) continue;
toTranspile.push(f);
}
var transpiler = new ts2dart.Transpiler(
{generateLibraryName: true, generateSourceMap: false, basePath: inputDir});
transpiler.transpile(toTranspile, destDir);
}
private listRecursive(root: string, res: Set = {}): Set {
var paths = fs.readdirSync(root);
paths.forEach((p) => {
p = path.join(root, p);
var stat = fs.statSync(p);
if (stat.isDirectory()) {
this.listRecursive(p, res);
} else {
// Collect *all* files so we can check .dart files that already exist and exclude them.
res[p] = true;
}
});
return res;
}
}
export function transpile(inputTree) {
return new TypeScriptToDartTranspiler(inputTree);
}