-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
179 lines (156 loc) · 5.38 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// @flow
/*::
import type { CompileOptions, CompilerOptions, Diagnostic, DiagnosticByGroup } from './types';
*/
"use strict";
const fs = require("fs");
const chalk = require("chalk");
const ts = require("typescript");
const merge = require("lodash.merge");
const matchFiles = ts.matchFiles;
function createProgramConfig(
opts /*: CompileOptions */
) /*: Promise<{ files: Array<string>, compilerOptions: CompilerOptions }> */ {
const projectDir = (opts.compilerOptions && opts.compilerOptions.project) || opts.cwd || process.cwd();
return new Promise((resolve, reject) => {
const configPath = ts.findConfigFile(projectDir, ts.sys.fileExists);
const tsconfig = configPath ? ts.readConfigFile(configPath, ts.sys.readFile) : { config: {} };
if (tsconfig.error) return reject([tsconfig.error]);
const mergedConfig = merge(tsconfig.config, opts);
const jsonConfig = ts.parseJsonConfigFileContent(mergedConfig, createParseConfigHost(projectDir), projectDir);
if (jsonConfig.errors && jsonConfig.errors.length) {
return reject(jsonConfig.errors);
}
resolve({
files: jsonConfig.fileNames,
compilerOptions: jsonConfig.options
});
});
}
function getAccessibleFileSystemEntries(path /*: string*/) /* { files: Array<string>, directories: Array<string> } */ {
try {
const entries = fs.readdirSync(path || ".").sort();
const files = [];
const directories = [];
return entries.reduce(
(acc, entry) => {
// This is necessary because on some file system node fails to exclude
// "." and "..". See https://github.com/nodejs/node/issues/4002
if (entry === "." || entry === "..") {
return acc;
}
const name = ts.combinePaths(path, entry);
let stat;
try {
stat = fs.statSync(name);
} catch (e) {
return acc;
}
if (stat.isFile()) {
acc.files.push(entry);
} else if (stat.isDirectory()) {
acc.directories.push(entry);
}
return acc;
},
{ files: [], directories: [] }
);
} catch (e) {
return { files: [], directories: [] };
}
}
function createParseConfigHost(projectPath /*: string */) {
function readDirectory(
rootDir /*: string */,
extensions /*: Array<string> */,
excludes /*: Array<string> */,
includes /*: Array<string> */,
depth /*: number */
) {
return matchFiles(
rootDir,
extensions,
excludes,
includes,
ts.sys.useCaseSensitiveFileNames,
projectPath,
depth,
getAccessibleFileSystemEntries
);
}
function fileExists(path /*: string */) {
return ts.sys.fileExists(path);
}
function readFile(path /*: string */) {
return ts.sys.readFile(path);
}
return {
fileExists,
readDirectory,
readFile,
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames
};
}
function splitDiagnosticsByType(diagnostics /*: Diagnostic[] */) /*: DiagnosticByGroup */ {
return diagnostics.reduce(
(acc, diagnostic) => {
const category = ts.DiagnosticCategory[diagnostic.category].toLowerCase();
acc[category].push(diagnostic);
return acc;
},
{ warning: [], error: [], message: [] }
);
}
function groupDeagnosticsByFile(diagnostics /*: Diagnostic[] */) /* {: [name: string]: Diagnostic[] } */ {
return diagnostics.reduce((acc, diagnostic) => {
const group = diagnostic.file ? diagnostic.file.fileName : "Compiler";
acc[group] = acc[group] || [];
acc[group].push(diagnostic);
return acc;
}, {});
}
function formatDiagnosticMessage(diagnostic /*: Diagnostic */) {
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
if (diagnostic.file) {
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
return `${chalk.dim("[" + position.line + 1 + "," + (position.character + 1) + "]")} ${message} ${chalk.magenta(
"[TS" + diagnostic.code + "]"
)}`;
} else {
return `${message} ${chalk.magenta("[TS" + diagnostic.code + "]")}`;
}
}
function padString(str /*: string */, count /*: number */) /*: string */ {
const pad = new Array(count).fill(" ").join("");
return pad + str;
}
function printDiagnosticsGroup(
title /*: string*/,
color /*: (text: string) => string */,
diagnostics /*: ?Diagnostic[] */
) {
if (!diagnostics || !diagnostics.length) return;
const prefix = chalk.dim("[TS] ");
console.log(prefix + color(`${title}:`));
const diagnosticsByFile = groupDeagnosticsByFile(diagnostics);
Object.keys(diagnosticsByFile).forEach(fileName => {
console.log(`${prefix}${padString(fileName, 2)}:`);
diagnosticsByFile[fileName].forEach(diagnostic => {
console.log(`${prefix}${padString(formatDiagnosticMessage(diagnostic), 4)}`);
});
});
}
function printDiagnostics(diagnostics /*: DiagnosticByGroup */) {
printDiagnosticsGroup("Errors", chalk.red, diagnostics.error);
printDiagnosticsGroup("Warnings", chalk.yellow, diagnostics.warning);
printDiagnosticsGroup("Messages", chalk.blue, diagnostics.message);
}
module.exports = {
createProgramConfig: createProgramConfig,
getAccessibleFileSystemEntries: getAccessibleFileSystemEntries,
createParseConfigHost: createParseConfigHost,
printDiagnostics: printDiagnostics,
splitDiagnosticsByType: splitDiagnosticsByType,
formatDiagnosticMessage: formatDiagnosticMessage,
groupByFile: groupDeagnosticsByFile
};