aboutsummaryrefslogtreecommitdiff
path: root/node_modules/gulp-typescript/release/file.js
blob: 61eeff7dcdd3859d3a4fb0b2983f79edac371112 (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
var path = require('path');
var tsApi = require('./tsapi');
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.typescript = typescript;
    }
    FileDictionary.prototype.addGulp = function (gFile) {
        this.addFile(File.fromGulp(gFile));
    };
    FileDictionary.prototype.addContent = function (fileName, content) {
        this.addFile(File.fromContent(fileName, content));
    };
    FileDictionary.prototype.addFile = function (file) {
        if (file.kind === FileKind.Source)
            this.initTypeScriptSourceFile(file);
        this.files[file.fileNameNormalized] = file;
    };
    FileDictionary.prototype.getFile = function (name) {
        return this.files[utils.normalizePath(name)];
    };
    FileDictionary.prototype.getGulpFileNames = 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;
    };
    return FileDictionary;
})();
exports.FileDictionary = FileDictionary;
var FileCache = (function () {
    function FileCache(typescript, options) {
        this.previous = undefined;
        this.version = 0;
        this.typescript = typescript;
        this.options = options;
        this.createDictionary();
    }
    FileCache.prototype.addGulp = function (gFile) {
        this.current.addGulp(gFile);
    };
    FileCache.prototype.addContent = function (fileName, content) {
        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.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 = tsApi.createSourceFile(this.typescript, file.fileNameOriginal, file.content, this.options.target, this.version + '');
    };
    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.getGulpFileNames(onlyGulp);
    };
    return FileCache;
})();
exports.FileCache = FileCache;