forked from wjohnsto/tsconfig-glob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
142 lines (112 loc) · 3.16 KB
/
index.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
import * as fs from 'fs';
import * as path from 'path';
import * as glob from 'glob';
const stable: any = require('stable');
function unique(arr: Array<string>) {
var keys: { [key: string]: boolean; } = {},
out: Array<string> = [];
for (var i = 0, l = arr.length; i < l; ++i) {
if (keys.hasOwnProperty(arr[i])) {
continue;
}
out.push(arr[i]);
keys[arr[i]] = true;
}
return out;
}
function sort(a: string, b: string) {
var aTsd = a.indexOf('tsd.d.ts') > -1,
bTsd = b.indexOf('tsd.d.ts') > -1,
aD = a.indexOf('.d.ts') > -1,
bD = b.indexOf('.d.ts') > -1;
if (aTsd) {
return -1;
}
if (bTsd) {
return 1;
}
if (aD && bD) {
return 0;
}
if (aD) {
return -1;
}
if (bD) {
return 1;
}
return 0;
}
function getFiles(options: IOptions, configFile: IConfigFile) {
let root = options.cwd || process.cwd(),
configDir = path.resolve(root, options.configPath || '.'),
filesGlob: Array<string> = configFile.filesGlob || [],
files: Array<string> = [];
files = unique(files.concat(filesGlob));
let include = files.filter((file) => {
return file[0] !== '!';
}),
ignore = files.filter((file) => {
return file[0] === '!';
}),
sortedFiles: Array<Array<string>> = [];
for (let pattern of include) {
sortedFiles.push(glob.sync(pattern, {
cwd: configDir,
root: root,
ignore: ignore.map(file => file.slice(1))
}));
}
sortedFiles = sortedFiles.map((files) => {
return stable(files);
});
files = unique(sortedFiles.reduce((files, current) => {
return files.concat(current);
}, []));
return stable(files, sort);
}
function eol(str: string): string {
let cr = '\r',
lf = '\n',
r = /\r/.test(str),
n = /\n/.test(str);
if (r && n) {
return cr + lf;
}
return lf;
}
export = function(options: IOptions = {}, done: Function = () => {}): any {
let root = options.cwd || process.cwd(),
configDir = path.resolve(root, options.configPath || '.'),
filePath = path.resolve(configDir, 'tsconfig.json'),
fileStr = fs.readFileSync(filePath, 'utf8'),
configFile: IConfigFile = JSON.parse(fileStr),
EOL = eol(fileStr);
if (options.empty) {
configFile.files = [];
} else {
configFile.files = getFiles(options, configFile);
}
if(!options.indent || Number(options.indent) === 0) {
options.indent = 4;
}
let outputStr = JSON.stringify(configFile, null, options.indent);
outputStr = outputStr.replace(/\n\r|\n|\r/g, EOL) + EOL;
fileStr = fileStr.replace(/\n\r|\n|\r/g, EOL) + EOL;
if(outputStr === fileStr) {
setImmediate(done);
} else {
fs.writeFile(filePath, outputStr, done);
}
return configFile;
};
interface IConfigFile {
filesGlob: Array<string>;
files: Array<string>;
}
interface IOptions {
args?: Array<string>;
configPath?: string;
cwd?: string;
indent?: number;
empty?: boolean;
}