@tsed/barrels 5.4.2 → 5.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/barrels.js +2 -2
- package/bin/generate-barrel.js +54 -0
- package/bin/get-config.js +28 -0
- package/package.json +1 -2
package/bin/barrels.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {generateBarrels} from "
|
|
3
|
-
import {getConfig} from "
|
|
2
|
+
import {generateBarrels} from "./generate-barrel.js";
|
|
3
|
+
import {getConfig} from "./get-config.js";
|
|
4
4
|
|
|
5
5
|
async function build() {
|
|
6
6
|
const {
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {writeFile} from "node:fs/promises";
|
|
2
|
+
import path, {dirname, join} from "node:path";
|
|
3
|
+
import {globby} from "globby";
|
|
4
|
+
|
|
5
|
+
// async function cleanIndex(cwd, excluded) {
|
|
6
|
+
// const patterns = [
|
|
7
|
+
// "**/index.ts",
|
|
8
|
+
// ...excluded
|
|
9
|
+
// ];
|
|
10
|
+
//
|
|
11
|
+
// const files = await globby(patterns, {
|
|
12
|
+
// cwd: cwd
|
|
13
|
+
// });
|
|
14
|
+
//
|
|
15
|
+
// return Promise.all(files.map((file) => fs.unlink(join(cwd, file))));
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
export async function generateBarrels({exclude, directory, cwd}) {
|
|
19
|
+
const excluded = exclude
|
|
20
|
+
.map((path) => `!${path}`)
|
|
21
|
+
.concat(directory.map((path) => `!${path}/index.ts`));
|
|
22
|
+
|
|
23
|
+
const directories = (
|
|
24
|
+
await globby(directory.map((d) => {
|
|
25
|
+
return join(d, "*");
|
|
26
|
+
}),
|
|
27
|
+
{
|
|
28
|
+
cwd
|
|
29
|
+
})
|
|
30
|
+
).reduce((set, file) => {
|
|
31
|
+
return set.add(dirname(file));
|
|
32
|
+
}, new Set());
|
|
33
|
+
|
|
34
|
+
const promises = [...directories.keys()].map(async (directory) => {
|
|
35
|
+
const baseIndex = join(cwd, directory?.path ?? directory);
|
|
36
|
+
|
|
37
|
+
const files = await globby(["**/*.{ts,tsx}", "!index.{ts,tsx}", ...excluded], {
|
|
38
|
+
cwd: path.join(cwd, directory)
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const exports = files
|
|
42
|
+
.sort((a, b) => a.localeCompare(b))
|
|
43
|
+
.map((file) => {
|
|
44
|
+
// TODO set .js after all configuration are ok to resolve .js
|
|
45
|
+
return `export * from "./${file.replace(".ts", ".js")}";`;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const content = ["/**", " * @file Automatically generated by @tsed/barrels.", " */", ...exports];
|
|
49
|
+
|
|
50
|
+
await writeFile(join(baseIndex, "index.ts"), content.join("\n") + "\n", {encoding: "utf8"});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await Promise.all(promises);
|
|
54
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {join} from "node:path";
|
|
2
|
+
import {existsSync} from "node:fs";
|
|
3
|
+
import {readFile} from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
function resolveConfig() {
|
|
6
|
+
return [
|
|
7
|
+
join(process.cwd(), ".barrelsby.json"),
|
|
8
|
+
join(process.cwd(), ".barrels.json")
|
|
9
|
+
].find((path) => {
|
|
10
|
+
return existsSync(path);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function readJSON(path) {
|
|
15
|
+
const content = await readFile(path, "utf-8");
|
|
16
|
+
|
|
17
|
+
return JSON.parse(content);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getConfig() {
|
|
21
|
+
const configPath = resolveConfig();
|
|
22
|
+
|
|
23
|
+
if (!configPath) {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return readJSON(configPath);
|
|
28
|
+
}
|
package/package.json
CHANGED