-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-index.js
41 lines (38 loc) · 1.32 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
// 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");
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 () => {
const cwdContents = await readdir(".");
if (cwdContents.indexOf("src") === -1) {
throw new Error("No local 'src' folder -- nothing to do");
}
const
tsFiles = (await readdir("src"))
.filter(f => f.match(/\.ts$/)),
target = path.resolve(path.join("src", "index.ts")),
output = tsFiles.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}`);
})();