-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeIndex.js
More file actions
105 lines (90 loc) · 2.5 KB
/
makeIndex.js
File metadata and controls
105 lines (90 loc) · 2.5 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
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
const fs = require('fs');
const ignore = require('ignore');
const minimist = require('minimist');
const path = require('path');
const opts = minimist(process.argv.slice(2));
const dirs = opts._.length ? opts._ : [process.cwd()];
const depth = opts.depth && parseInt(opts.depth, 10);
const defaultIgnore = ignore().add([
'internal/',
'*.internal.*',
'*.test.ts',
'*.d.ts',
'__test__/',
]);
dirs.forEach((x) => makeIndex(x, depth, opts.comment));
function makeIndex(dir, maxDepth, comment) {
const files = collectFiles(dir, { maxDepth })
.map((x) => path.relative(dir, x).replace('\\', '/'))
.sort(sortPath)
.filter((x) => x !== 'index');
if (!comment) {
comment = `node ${path.relative(dir, __filename)}`;
}
const index = [`// AUTO-GENERATED ${comment}`].concat(
files.map((x) => `export * from './${x}';`),
);
fs.writeFileSync(path.join(dir, 'index.ts'), index.join('\n') + '\n');
}
function collectFiles(
dir,
{
currentDepth = 0,
ignoreFiles = defaultIgnore,
maxDepth = 5,
rootDir = dir,
},
) {
const ignorePath = path.join(dir, '.indexignore');
if (fs.existsSync(ignorePath)) {
ignoreFiles = ignore()
.add(ignoreFiles)
.add(fs.readFileSync(ignorePath, 'utf8'));
}
let files = fs.readdirSync(dir, { withFileTypes: true });
const matches = [];
if (currentDepth > 0 && files.find((x) => x.name === 'index.ts')) {
return [dir];
}
for (const file of files) {
const relPath = getIgnorePath(dir, file, rootDir);
if (ignoreFiles && ignoreFiles.ignores(relPath)) {
continue;
}
if (file.isDirectory() && currentDepth <= maxDepth) {
matches.push(
...collectFiles(path.join(dir, file.name), {
currentDepth: currentDepth + 1,
ignoreFiles,
maxDepth,
rootDir,
}),
);
} else if (shouldIndexFile(file.name)) {
matches.push(
path.join(dir, path.basename(file.name, path.extname(file.name))),
);
}
}
return matches;
}
function shouldIndexFile(name) {
return /\.tsx?$/.test(name);
}
function getIgnorePath(dir, file, rootDir) {
let filePath = path.relative(rootDir, path.join(dir, file.name));
if (file.isDirectory()) {
filePath += '/';
}
return filePath;
}
function sortPath(a, b) {
const aParts = a.split('/');
const bParts = b.split('/');
if (aParts.length > bParts.length) {
return -1;
} else if (aParts.length < bParts.length) {
return 1;
}
return a.toLowerCase().localeCompare(b.toLowerCase());
}