UNPKG

3.95 kBPlain TextView Raw
1import * as ts from "typescript";
2import * as fs from "fs";
3import * as _ from "lodash";
4import AssertionError = require("assertion-error");
5
6var defaultCompilerOptions: ts.CompilerOptions = {
7 noEmitOnError: true,
8 noImplicitAny: true,
9 target: ts.ScriptTarget.ES5,
10 module: ts.ModuleKind.CommonJS
11};
12
13function 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
23export type DoneFunction = (err: any, results?: string[]) => void;
24export type FilterFunction = (fileName: string) => boolean;
25
26export function compile(fileNames: string[], options: ts.CompilerOptions, done: Function): void {
27 try {
28 const program = ts.createProgram(fileNames, options);
29
30 // TODO: this is generating errors so disabling for now. Will continue to investigate.
31 // handleDiagnostics('Declaration', program.getDeclarationDiagnostics());
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
42export function compileDirectory(path: string, done: Function): void;
43export function compileDirectory(path: string, options: ts.CompilerOptions, done: Function): void;
44export function compileDirectory(path: string, filter: FilterFunction, done: Function): void;
45export function compileDirectory(path: string, filter: FilterFunction, options: ts.CompilerOptions, done: Function): void;
46export 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
74export function walk(dir: string, done: DoneFunction): void;
75export function walk(dir: string, filter: FilterFunction, done: DoneFunction): void;
76export 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};