-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeIndex.js
116 lines (99 loc) · 2.81 KB
/
makeIndex.js
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
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import ignore from 'ignore';
import minimist from 'minimist';
import { basename, extname, join, relative } from 'path';
import { fileURLToPath } from 'url';
const MODULE_ENDING_SPECIFIER = '.js';
const QUOTE_CHAR = `'`;
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) => relative(dir, x).replace('\\', '/'))
.filter((x) => x !== getImportPath('./', 'index'))
.sort(sortPath);
if (!comment) {
comment = `node ${relative(dir, fileURLToPath(import.meta.url))}`;
}
const index = [`// AUTO-GENERATED ${comment}`].concat(
files.map((x) => `export * from ${QUOTE_CHAR}./${x}${QUOTE_CHAR};`),
);
const output = index.join('\n') + '\n';
const outputFile = join(dir, 'index.ts');
if (opts.ci) {
if (readFileSync(outputFile, 'utf8') !== output) {
console.log(`ERROR: ${outputFile} would change`);
process.exit(1);
}
} else {
writeFileSync(outputFile, output);
}
}
function collectFiles(
dir,
{
currentDepth = 0,
ignoreFiles = defaultIgnore,
maxDepth = 5,
rootDir = dir,
},
) {
const ignorePath = join(dir, '.indexignore');
if (existsSync(ignorePath)) {
ignoreFiles = ignore()
.add(ignoreFiles)
.add(readFileSync(ignorePath, 'utf8'));
}
let files = 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(join(dir, file.name), {
currentDepth: currentDepth + 1,
ignoreFiles,
maxDepth,
rootDir,
}),
);
} else if (shouldIndexFile(file.name)) {
matches.push(getImportPath(dir, file.name));
}
}
return matches;
}
function getImportPath(basePath, path) {
return join(
basePath,
basename(path, extname(path)) + MODULE_ENDING_SPECIFIER,
);
}
function shouldIndexFile(name) {
return /\.tsx?$/.test(name);
}
function getIgnorePath(dir, file, rootDir) {
let filePath = relative(rootDir, join(dir, file.name));
if (file.isDirectory()) {
filePath += '/';
}
return filePath;
}
function sortPath(a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
}