-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathinput.ts
260 lines (223 loc) · 6.8 KB
/
input.ts
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import * as ts from 'typescript';
import * as path from 'path';
import * as utils from './utils';
import { VinylFile } from './types';
export enum FileChangeState {
New,
Equal,
Modified,
Deleted,
NotFound
}
export enum FileKind {
Source,
Config
}
export interface FileChange {
previous: File;
current: File;
state: FileChangeState;
}
export interface File {
gulp?: VinylFile;
fileNameNormalized: string;
fileNameOriginal: string;
content: string;
kind: FileKind;
ts?: ts.SourceFile;
}
export module File {
export function fromContent(caseSensitive: boolean, fileName: string, content: string): File {
let kind = FileKind.Source;
if (path.extname(fileName).toLowerCase() === 'json') kind = FileKind.Config;
return {
fileNameNormalized: utils.normalizePath(caseSensitive, fileName),
fileNameOriginal: fileName,
content,
kind
};
}
export function fromGulp(caseSensitive: boolean, file: VinylFile): File {
let str = (<Buffer> file.contents).toString('utf8');
let data = fromContent(caseSensitive, file.path, str);
data.gulp = file;
return data;
}
export function equal(a: File, b: File): boolean {
if (a === undefined || b === undefined) return a === b; // They could be both undefined.
return (a.fileNameOriginal === b.fileNameOriginal)
&& (a.content === b.content);
}
export function getChangeState(previous: File, current: File): FileChangeState {
if (previous === undefined) {
return current === undefined ? FileChangeState.NotFound : FileChangeState.New;
}
if (current === undefined) {
return FileChangeState.Deleted;
}
if (equal(previous, current)) {
return FileChangeState.Equal;
}
return FileChangeState.Modified;
}
}
export class FileDictionary {
files: utils.Map<File> = {};
firstSourceFile: File = undefined;
caseSensitive: boolean;
typescript: typeof ts;
constructor(caseSensitive: boolean, typescript: typeof ts) {
this.caseSensitive = caseSensitive;
this.typescript = typescript;
}
addGulp(gFile: VinylFile) {
return this.addFile(File.fromGulp(this.caseSensitive, gFile));
}
addContent(fileName: string, content: string) {
return this.addFile(File.fromContent(this.caseSensitive, fileName, content));
}
private addFile(file: File) {
if (file.kind === FileKind.Source) {
this.initTypeScriptSourceFile(file);
if (!this.firstSourceFile) this.firstSourceFile = file;
}
this.files[file.fileNameNormalized] = file;
return file;
}
getFile(name: string) {
return this.files[utils.normalizePath(this.caseSensitive, name)];
}
initTypeScriptSourceFile: (file: File) => void;
getFileNames(onlyGulp = false) {
const fileNames: string[] = [];
for (const fileName in this.files) {
if (!this.files.hasOwnProperty(fileName)) continue;
let file = this.files[fileName];
if (onlyGulp && !file.gulp) continue;
fileNames.push(file.fileNameOriginal);
}
return fileNames;
}
private getSourceFileNames(onlyGulp?: boolean) {
const fileNames = this.getFileNames(onlyGulp);
const sourceFileNames = fileNames
.filter(fileName => fileName.substr(fileName.length - 5).toLowerCase() !== '.d.ts');
if (sourceFileNames.length === 0) {
// Only definition files, so we will calculate the common base path based on the
// paths of the definition files.
return fileNames;
}
return sourceFileNames;
}
get commonBasePath() {
const fileNames = this.getSourceFileNames(true);
return utils.getCommonBasePathOfArray(
fileNames.map(fileName => {
const file = this.files[utils.normalizePath(this.caseSensitive, fileName)];
return path.resolve(process.cwd(), file.gulp.base);
})
);
}
// This empty setter will prevent that TS emits 'readonly' modifier.
// 'readonly' is not supported in current stable release.
set commonBasePath(value) {}
get commonSourceDirectory() {
const fileNames = this.getSourceFileNames();
return utils.getCommonBasePathOfArray(
fileNames.map(fileName => {
const file = this.files[utils.normalizePath(this.caseSensitive, fileName)];
return path.dirname(file.fileNameNormalized);
})
);
}
// This empty setter will prevent that TS emits 'readonly' modifier.
// 'readonly' is not supported in current stable release.
set commonSourceDirectory(value) {}
}
export class FileCache {
previous: FileDictionary = undefined;
current: FileDictionary;
options: ts.CompilerOptions;
caseSensitive: boolean;
noParse: boolean = false; // true when using a file based compiler.
typescript: typeof ts;
version: number = 0;
constructor(typescript: typeof ts, options: ts.CompilerOptions, caseSensitive: boolean) {
this.typescript = typescript;
this.options = options;
this.caseSensitive = caseSensitive;
this.createDictionary();
}
addGulp(gFile: VinylFile) {
return this.current.addGulp(gFile);
}
addContent(fileName: string, content: string) {
return this.current.addContent(fileName, content);
}
reset() {
this.version++;
this.previous = this.current;
this.createDictionary();
}
private createDictionary() {
this.current = new FileDictionary(this.caseSensitive, this.typescript);
this.current.initTypeScriptSourceFile = (file) => this.initTypeScriptSourceFile(file);
}
private initTypeScriptSourceFile(file: File) {
if (this.noParse) return;
if (this.previous) {
let previous = this.previous.getFile(file.fileNameOriginal);
if (File.equal(previous, file)) {
file.ts = previous.ts; // Re-use previous source file.
return;
}
}
file.ts = this.typescript.createSourceFile(file.fileNameOriginal, file.content, this.options.target);
}
getFile(name: string) {
return this.current.getFile(name);
}
getFileChange(name: string): FileChange {
let previous: File;
if (this.previous) {
previous = this.previous.getFile(name);
}
let current = this.current.getFile(name);
return {
previous,
current,
state: File.getChangeState(previous, current)
};
}
getFileNames(onlyGulp = false) {
return this.current.getFileNames(onlyGulp);
}
get firstSourceFile() {
return this.current.firstSourceFile;
}
// This empty setter will prevent that TS emits 'readonly' modifier.
// 'readonly' is not supported in current stable release.
set firstSourceFile(value) {}
get commonBasePath() {
return this.current.commonBasePath;
}
set commonBasePath(value) {}
get commonSourceDirectory() {
return this.current.commonSourceDirectory;
}
set commonSourceDirectory(value) {}
isChanged(onlyGulp = false) {
if (!this.previous) return true;
const files = this.getFileNames(onlyGulp);
const oldFiles = this.previous.getFileNames(onlyGulp);
if (files.length !== oldFiles.length) return true;
for (const fileName of files) {
if (oldFiles.indexOf(fileName) === -1) return true;
}
for (const fileName of files) {
const change = this.getFileChange(fileName);
if (change.state !== FileChangeState.Equal) return true;
}
return false;
}
}