-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-index.js
61 lines (56 loc) · 1.9 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
// this helper script was copied in from newts to automatically
// generate your index.ts in your src folder. Once that is transpiled,
// you'll get an index.js and index.d.ts in the dist folder and the
// initially-generated index.js and index.d.ts in the base of your
// package will defer to these, providing for a clean import of
// everything exported from base files in your src folder
const path = require("path");
const fs = require("fs");
const srcDir = "src";
function readdir(at) {
return new Promise((resolve, reject) => {
fs.readdir(at, (err, result) => err ? reject(err) : resolve(result));
});
}
function writeFile(at, contents) {
return new Promise((resolve, reject) => {
fs.writeFile(at, contents, { encoding: "utf-8" }, err =>
err ? reject(err) : resolve()
)
});
}
async function hasHashBang(f) {
const
readFile = fs.promises.readFile,
contents = await readFile(f, "utf8");
return contents.indexOf("#!") === 0;
}
async function filterHashBanged(paths) {
const result = [];
for (const p of paths) {
if (await hasHashBang(path.join(srcDir, p))) {
continue;
}
result.push(p);
}
return result;
}
(async () => {
const cwdContents = await readdir(".");
if (cwdContents.indexOf(srcDir) === -1) {
throw new Error(`No local '${srcDir}' folder -- nothing to do`);
}
const
tsFiles = (await readdir(srcDir))
.filter(f => f.match(/\.ts$/)),
nonExecutables = await filterHashBanged(tsFiles),
target = path.resolve(path.join("src", "index.ts")),
output = nonExecutables.sort()
.map(f => `export * from "./${ f.replace(/\.ts$/, "") }";`)
.join("\n");
await writeFile(
target,
`// this is a generated file: do not edit\n${ output }\n`
);
console.log(`Updated index at: ${ target }`);
})();