This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsc-cleanup
192 lines (159 loc) · 4.72 KB
/
tsc-cleanup
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
#!/usr/bin/env node
/**
* tsc-cleanup.js: Small NodeJs script to help cleanup compiled JS/MAP files
* https://github.com/alphazygma/typescript-cleanup/blob/master/tsc-cleanup
*/
var fs = require('fs');
var path = require('path');
var tscu;
(function (tscu) {
function cleanup() {
var configFile = _findConfigFile();
var jsonText = fs.readFileSync(configFile, 'utf8');
var config = _parseConfigFileTextToJson(jsonText);
var excludeList = config.exclude;
var fileList = _getTSfileList(_getCurrentDirectory(), excludeList);
var removedList = _removeCompiled(fileList);
// console.log(removedList);
}
tscu.cleanup = cleanup;
function _findConfigFile() {
var searchPath = _getCurrentDirectory();
var fileName = "tsconfig.json";
while (true) {
if (_fileExists(fileName)) {
return fileName;
}
var parentPath = _getDirectoryPath(searchPath);
if (parentPath === searchPath) {
break;
}
searchPath = parentPath;
fileName = "../" + fileName;
}
return undefined;
}
function _parseConfigFileTextToJson(jsonText) {
try {
return /\S/.test(jsonText) ? JSON.parse(jsonText) : {};
}
catch (e) {
return { error: "ts.createCompilerDiagnostic(ts.Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message)" };
}
}
function _getTSfileList(dir, excludeList) {
var containsNodeModules = false;
var containsTypings = false;
for (var i = 0; i < excludeList.length; i++) {
if (excludeList[i] === 'node_modules') {
containsNodeModules = true;
} else if (excludeList[i] === 'typings') {
containsTypings = true;
}
}
if (!containsNodeModules) { excludeList.push('node_modules'); }
if (!containsTypings) { excludeList.push('typings'); }
var fileList = __getTSfileListDelegate(dir, excludeList);
return fileList;
}
function _removeCompiled(cleanupList) {
var removedFileList = new Array();
for (var i = 0; i < cleanupList.length; i++) {
var tsFile = cleanupList[i];
var baseName = tsFile.substr(0, tsFile.lastIndexOf('.'));
var jsFile = baseName + '.js';
var mapFile = baseName + '.js.map';
if (_fileExists(jsFile)) {
fs.unlinkSync(jsFile);
removedFileList.push(jsFile);
}
if (_fileExists(mapFile)) {
fs.unlinkSync(mapFile);
removedFileList.push(mapFile);
}
}
return removedFileList;
}
function _fileExists(filePath) {
try {
fs.accessSync(filePath, fs.F_OK);
return true;
} catch (e) {
return false;
}
}
function _getCurrentDirectory() {
return process.cwd();
}
function _getDirectoryPath(path) {
return path.substr(0, Math.max(_getRootLength(path), path.lastIndexOf("/")));
}
function _getRootLength(path) {
if (path.charCodeAt(0) === 47) {
if (path.charCodeAt(1) !== 47)
return 1;
var p1 = path.indexOf("/", 2);
if (p1 < 0)
return 2;
var p2 = path.indexOf("/", p1 + 1);
if (p2 < 0)
return p1 + 1;
return p2 + 1;
}
if (path.charCodeAt(1) === 58) {
if (path.charCodeAt(2) === 47)
return 3;
return 2;
}
if (path.lastIndexOf("file:///", 0) === 0) {
return "file:///".length;
}
var idx = path.indexOf("://");
if (idx !== -1) {
return idx + "://".length;
}
return 0;
}
function __getTSfileListDelegate(dir, excludeList) {
var fileList = fs.readdirSync(dir);
var filteredFileList = new Array();
for (var i = 0; i < fileList.length; i++) {
var fileName = dir + '/' + fileList[i];
var stat = fs.statSync(fileName);
if (!stat) { continue; }
if (stat.isDirectory()) {
if (_isInExcludeList(fileName, excludeList)) {
continue;
}
var subDirList = __getTSfileListDelegate(fileName, excludeList);
filteredFileList = filteredFileList.concat(subDirList);
} else {
if (!_isValidTSfile(fileName, excludeList)) {
continue;
}
filteredFileList.push(fileName);
}
}
return filteredFileList;
}
function _isValidTSfile(fileName, excludeList) {
var ext = fileName.substr(fileName.lastIndexOf('.') + 1);
if (ext !== 'ts') {
return false;
}
var isInExcludeList = _isInExcludeList(fileName, excludeList);
return !isInExcludeList;
}
function _isInExcludeList(fileName, excludeList) {
var isInExcludeList = false;
for (var i = 0; i < excludeList.length; i++) {
var exclusion = excludeList[i];
if (fileName.indexOf(exclusion) > -1) {
isInExcludeList = true;
break;
}
}
return isInExcludeList;
}
})(tscu || (tscu = {}));
tscu.cleanup();