aboutsummaryrefslogtreecommitdiff
path: root/node_modules/gulp-typescript/release/input.js
blob: 15f0ece1493e090caef100d2f72f02c77400583d (plain)
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
"use strict";
var path = require('path');
var utils = require('./utils');
(function (FileChangeState) {
    FileChangeState[FileChangeState["New"] = 0] = "New";
    FileChangeState[FileChangeState["Equal"] = 1] = "Equal";
    FileChangeState[FileChangeState["Modified"] = 2] = "Modified";
    FileChangeState[FileChangeState["Deleted"] = 3] = "Deleted";
    FileChangeState[FileChangeState["NotFound"] = 4] = "NotFound";
})(exports.FileChangeState || (exports.FileChangeState = {}));
var FileChangeState = exports.FileChangeState;
(function (FileKind) {
    FileKind[FileKind["Source"] = 0] = "Source";
    FileKind[FileKind["Config"] = 1] = "Config";
})(exports.FileKind || (exports.FileKind = {}));
var FileKind = exports.FileKind;
var File;
(function (File) {
    function fromContent(fileName, content) {
        var kind = FileKind.Source;
        if (path.extname(fileName).toLowerCase() === 'json')
            kind = FileKind.Config;
        return {
            fileNameNormalized: utils.normalizePath(fileName),
            fileNameOriginal: fileName,
            content: content,
            kind: kind
        };
    }
    File.fromContent = fromContent;
    function fromGulp(file) {
        var str = file.contents.toString('utf8');
        var data = fromContent(file.path, str);
        data.gulp = file;
        return data;
    }
    File.fromGulp = fromGulp;
    function equal(a, b) {
        if (a === undefined || b === undefined)
            return a === b; // They could be both undefined.
        return (a.fileNameOriginal === b.fileNameOriginal)
            && (a.content === b.content);
    }
    File.equal = equal;
    function getChangeState(previous, current) {
        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;
    }
    File.getChangeState = getChangeState;
})(File = exports.File || (exports.File = {}));
var FileDictionary = (function () {
    function FileDictionary(typescript) {
        this.files = {};
        this.firstSourceFile = undefined;
        this.typescript = typescript;
    }
    FileDictionary.prototype.addGulp = function (gFile) {
        return this.addFile(File.fromGulp(gFile));
    };
    FileDictionary.prototype.addContent = function (fileName, content) {
        return this.addFile(File.fromContent(fileName, content));
    };
    FileDictionary.prototype.addFile = function (file) {
        if (file.kind === FileKind.Source) {
            this.initTypeScriptSourceFile(file);
            if (!this.firstSourceFile)
                this.firstSourceFile = file;
        }
        this.files[file.fileNameNormalized] = file;
        return file;
    };
    FileDictionary.prototype.getFile = function (name) {
        return this.files[utils.normalizePath(name)];
    };
    FileDictionary.prototype.getFileNames = function (onlyGulp) {
        if (onlyGulp === void 0) { onlyGulp = false; }
        var fileNames = [];
        for (var fileName in this.files) {
            if (!this.files.hasOwnProperty(fileName))
                continue;
            var file = this.files[fileName];
            if (onlyGulp && !file.gulp)
                continue;
            fileNames.push(file.fileNameOriginal);
        }
        return fileNames;
    };
    FileDictionary.prototype.getSourceFileNames = function (onlyGulp) {
        var fileNames = this.getFileNames(onlyGulp);
        var sourceFileNames = fileNames
            .filter(function (fileName) { return 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;
    };
    Object.defineProperty(FileDictionary.prototype, "commonBasePath", {
        get: function () {
            var _this = this;
            var fileNames = this.getSourceFileNames(true);
            return utils.getCommonBasePathOfArray(fileNames.map(function (fileName) {
                var file = _this.files[utils.normalizePath(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: function (value) { },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(FileDictionary.prototype, "commonSourceDirectory", {
        get: function () {
            var _this = this;
            var fileNames = this.getSourceFileNames();
            return utils.getCommonBasePathOfArray(fileNames.map(function (fileName) {
                var file = _this.files[utils.normalizePath(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: function (value) { },
        enumerable: true,
        configurable: true
    });
    return FileDictionary;
}());
exports.FileDictionary = FileDictionary;
var FileCache = (function () {
    function FileCache(typescript, options) {
        this.previous = undefined;
        this.noParse = false; // true when using a file based compiler.
        this.version = 0;
        this.typescript = typescript;
        this.options = options;
        this.createDictionary();
    }
    FileCache.prototype.addGulp = function (gFile) {
        return this.current.addGulp(gFile);
    };
    FileCache.prototype.addContent = function (fileName, content) {
        return this.current.addContent(fileName, content);
    };
    FileCache.prototype.reset = function () {
        this.version++;
        this.previous = this.current;
        this.createDictionary();
    };
    FileCache.prototype.createDictionary = function () {
        var _this = this;
        this.current = new FileDictionary(this.typescript);
        this.current.initTypeScriptSourceFile = function (file) { return _this.initTypeScriptSourceFile(file); };
    };
    FileCache.prototype.initTypeScriptSourceFile = function (file) {
        if (this.noParse)
            return;
        if (this.previous) {
            var 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);
    };
    FileCache.prototype.getFile = function (name) {
        return this.current.getFile(name);
    };
    FileCache.prototype.getFileChange = function (name) {
        var previous;
        if (this.previous) {
            previous = this.previous.getFile(name);
        }
        var current = this.current.getFile(name);
        return {
            previous: previous,
            current: current,
            state: File.getChangeState(previous, current)
        };
    };
    FileCache.prototype.getFileNames = function (onlyGulp) {
        if (onlyGulp === void 0) { onlyGulp = false; }
        return this.current.getFileNames(onlyGulp);
    };
    Object.defineProperty(FileCache.prototype, "firstSourceFile", {
        get: function () {
            return this.current.firstSourceFile;
        },
        // This empty setter will prevent that TS emits 'readonly' modifier.
        // 'readonly' is not supported in current stable release.
        set: function (value) { },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(FileCache.prototype, "commonBasePath", {
        get: function () {
            return this.current.commonBasePath;
        },
        set: function (value) { },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(FileCache.prototype, "commonSourceDirectory", {
        get: function () {
            return this.current.commonSourceDirectory;
        },
        set: function (value) { },
        enumerable: true,
        configurable: true
    });
    FileCache.prototype.isChanged = function (onlyGulp) {
        if (onlyGulp === void 0) { onlyGulp = false; }
        if (!this.previous)
            return true;
        var files = this.getFileNames(onlyGulp);
        var oldFiles = this.previous.getFileNames(onlyGulp);
        if (files.length !== oldFiles.length)
            return true;
        for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
            var fileName = files_1[_i];
            if (oldFiles.indexOf(fileName) === -1)
                return true;
        }
        for (var _a = 0, files_2 = files; _a < files_2.length; _a++) {
            var fileName = files_2[_a];
            var change = this.getFileChange(fileName);
            if (change.state !== FileChangeState.Equal)
                return true;
        }
        return false;
    };
    return FileCache;
}());
exports.FileCache = FileCache;