forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear-private-definitions.ts
More file actions
59 lines (50 loc) · 1.67 KB
/
clear-private-definitions.ts
File metadata and controls
59 lines (50 loc) · 1.67 KB
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
import * as path from 'path';
import * as fs from 'fs';
import readdirp, { EntryInfo } from 'readdirp';
const inputFolder = path.resolve(process.argv[2]);
console.log(`Clearing private definitions in ${inputFolder}`);
function filterTypeScriptFiles(content: string) {
var leadingPrivate = /^.*@private/gi;
if (leadingPrivate.test(content)) {
return { shouldDelete: true };
}
let blockCommentPrivate = /\/\*\*([^](?!\*\/))*@module([^](?!\*\/))*@private[^]*?\*\//g;
if (blockCommentPrivate.test(content)) {
return { shouldDelete: true };
}
let newContent = content;
newContent = newContent.replace(/\/\/[\/\s]*@private[^]*?\/\/[\/\s]*?@endprivate/gm, '');
if (newContent !== content) {
return { shouldReplace: true, newContent: newContent };
}
return { shouldReplace: false, shouldDelete: false };
}
readdirp(inputFolder, {
fileFilter: ['*.d.ts'],
directoryFilter: function (di) {
return !di.path.includes('node_modules');
},
})
.on('data', (entry: EntryInfo) => {
const { fullPath } = entry;
const content = fs.readFileSync(fullPath, 'utf8');
const { shouldDelete, shouldReplace, newContent } = filterTypeScriptFiles(content);
if (shouldDelete) {
console.log('[Delete]', fullPath);
fs.unlinkSync(fullPath);
} else if (shouldReplace) {
console.log('[Cleared]', fullPath);
try {
fs.writeFileSync(fullPath, newContent || '', 'utf8');
} catch (error) {
console.log('ERROR writing file: ' + fullPath, error);
process.exit(1);
}
}
})
.on('warn', (error: Error) => console.error('non-fatal error', error))
.on('error', (error: Error) => {
console.error('fatal error', error);
process.exit(1);
})
.on('end', () => console.log('done'));