forked from meroxa/cli-fig-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-index.js
83 lines (74 loc) · 2.44 KB
/
generate-index.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
import fs from "fs";
import path from "path";
const normalize = (name) => {
let capitalizeNext = false;
const excluded = "-@";
return name.split("").reduce((out, char) => {
if (excluded.includes(char)) {
capitalizeNext = true;
return out;
}
if (capitalizeNext) {
capitalizeNext = false;
return out + char.toUpperCase();
}
return out + char;
}, "");
};
const resolvedSrc = path.resolve(process.cwd(), "src");
// ! This returns a tuple containig as first element a list of specs and as second element the diff-versioned specs of that list
const getSubfolderSpecNames = (dirPathRelativeToSrc) => {
const resolvedDirPath = path.join(resolvedSrc, dirPathRelativeToSrc);
try {
// if index.ts exists we are in a spec folder and we only return the current dir path e.g. fig/index.ts returns fig
fs.readFileSync(path.join(resolvedDirPath, "index.ts"));
return Array(2).fill([`"${dirPathRelativeToSrc}"`]);
} catch {
// otherwise the folder is just used to organize specs e.g. aws/*.ts
const specNames = [];
const diffVersionedSpecNames = [];
for (const dirent of fs.readdirSync(resolvedDirPath, {
withFileTypes: true,
})) {
if (dirent.isFile() && dirent.name.endsWith(".ts")) {
specNames.push(
`"${path.join(dirPathRelativeToSrc, dirent.name).slice(0, -3)}"`
);
} else if (dirent.isDirectory()) {
const [s, dvs] = getSubfolderSpecNames(
path.join(dirPathRelativeToSrc, dirent.name)
);
specNames.push(...s);
diffVersionedSpecNames.push(...dvs);
}
}
return [specNames, diffVersionedSpecNames];
}
};
const specNames = [];
const diffVersionedSpecNames = [];
for (const dirent of fs.readdirSync(resolvedSrc, { withFileTypes: true })) {
if (dirent.isFile() && dirent.name.endsWith(".ts")) {
specNames.push(`"${dirent.name.slice(0, -3)}"`);
} else if (dirent.isDirectory()) {
const [s, dvs] = getSubfolderSpecNames(dirent.name);
specNames.push(...s);
diffVersionedSpecNames.push(...dvs);
}
}
fs.writeFileSync(
"build/index.js",
`var e=[${specNames.join(
","
)}],diffVersionedCompletions=[${diffVersionedSpecNames.join(
","
)}];export{e as default,diffVersionedCompletions};`
);
fs.writeFileSync(
"build/index.d.ts",
`
declare const completions: string[]
declare const diffVersionedCompletions: string[]
export { completions as default, diffVersionedCompletions }
`
);