1 | import * as ts from "typescript";
|
2 | import * as fs from "fs";
|
3 | import * as _ from "lodash";
|
4 | import AssertionError = require("assertion-error");
|
5 |
|
6 | var defaultCompilerOptions: ts.CompilerOptions = {
|
7 | noEmitOnError: true,
|
8 | noImplicitAny: true,
|
9 | target: ts.ScriptTarget.ES5,
|
10 | module: ts.ModuleKind.CommonJS
|
11 | };
|
12 |
|
13 | function handleDiagnostics(type: string, diagnostics: ReadonlyArray<ts.Diagnostic>) {
|
14 | diagnostics.forEach(diagnostic => {
|
15 | var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
16 | var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
17 | throw new AssertionError(`${type}: ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`, {
|
18 | actual: diagnostic
|
19 | });
|
20 | });
|
21 | }
|
22 |
|
23 | export type DoneFunction = (err: any, results?: string[]) => void;
|
24 | export type FilterFunction = (fileName: string) => boolean;
|
25 |
|
26 | export function compile(fileNames: string[], options: ts.CompilerOptions, done: Function): void {
|
27 | try {
|
28 | const program = ts.createProgram(fileNames, options);
|
29 |
|
30 |
|
31 |
|
32 | handleDiagnostics('Global', program.getGlobalDiagnostics());
|
33 | handleDiagnostics('Semantic', program.getSemanticDiagnostics());
|
34 | handleDiagnostics('Syntactic', program.getSyntacticDiagnostics());
|
35 | done();
|
36 | }
|
37 | catch (e) {
|
38 | done(e);
|
39 | }
|
40 | }
|
41 |
|
42 | export function compileDirectory(path: string, done: Function): void;
|
43 | export function compileDirectory(path: string, options: ts.CompilerOptions, done: Function): void;
|
44 | export function compileDirectory(path: string, filter: FilterFunction, done: Function): void;
|
45 | export function compileDirectory(path: string, filter: FilterFunction, options: ts.CompilerOptions, done: Function): void;
|
46 | export function compileDirectory(path: string, filter?: any, options?: any, done?: Function): void {
|
47 | if (!done) {
|
48 | if (!options) {
|
49 | done = filter;
|
50 | filter = undefined;
|
51 | } else {
|
52 | done = options;
|
53 | if (!_.isFunction(filter)) {
|
54 | options = filter;
|
55 | options = undefined;
|
56 | }
|
57 | }
|
58 | }
|
59 |
|
60 | options = _.merge(defaultCompilerOptions, (options || {}));
|
61 |
|
62 | walk(path, filter, (err, results) => {
|
63 | if (err) {
|
64 | console.log('error error error')
|
65 | throw new AssertionError('Error while walking directory for files.', {
|
66 | actual: err
|
67 | });
|
68 | } else {
|
69 | compile(results, options, done);
|
70 | }
|
71 | });
|
72 | }
|
73 |
|
74 | export function walk(dir: string, done: DoneFunction): void;
|
75 | export function walk(dir: string, filter: FilterFunction, done: DoneFunction): void;
|
76 | export function walk(dir: string, filter: FilterFunction | DoneFunction, done?: DoneFunction): void {
|
77 | if (!done) {
|
78 | done = filter;
|
79 | filter = undefined;
|
80 | }
|
81 |
|
82 | var results = [];
|
83 | fs.readdir(dir, function(err, list) {
|
84 | if (err) {
|
85 | return done(err);
|
86 | }
|
87 | var i = 0;
|
88 | (function next() {
|
89 | var file = list[i++];
|
90 | if (!file) return done(null, results);
|
91 | file = dir + '/' + file;
|
92 | fs.stat(file, function(err, stat) {
|
93 | if (stat && stat.isDirectory()) {
|
94 | walk(file, function(err, res) {
|
95 | results = results.concat(res);
|
96 | next();
|
97 | });
|
98 | } else {
|
99 | if (!filter || (filter as FilterFunction)(file)) {
|
100 | results.push(file);
|
101 | }
|
102 | next();
|
103 | }
|
104 | });
|
105 | })();
|
106 | });
|
107 | };
|