|
1 | 1 | #!/usr/bin/env node
|
2 |
| -import {existsSync} from "node:fs"; |
3 |
| -import {readFile, writeFile} from "node:fs/promises"; |
4 |
| -import {dirname, join} from "node:path"; |
5 |
| -import {globby} from "globby"; |
6 |
| - |
7 |
| -function resolveConfig() { |
8 |
| - return [ |
9 |
| - join(process.cwd(), ".barrelsby.json"), |
10 |
| - join(process.cwd(), ".barrels.json") |
11 |
| - ].find((path) => { |
12 |
| - return existsSync(path); |
13 |
| - }); |
14 |
| -} |
15 |
| - |
16 |
| -async function readJSON(path) { |
17 |
| - const content = await readFile(path, "utf-8"); |
18 |
| - |
19 |
| - return JSON.parse(content); |
20 |
| -} |
21 |
| - |
22 |
| -function getConfig() { |
23 |
| - const configPath = resolveConfig(); |
24 |
| - |
25 |
| - if (!configPath) { |
26 |
| - return {}; |
27 |
| - } |
28 |
| - |
29 |
| - return readJSON(configPath); |
30 |
| -} |
31 |
| - |
32 |
| -async function cleanIndex(cwd, excluded) { |
33 |
| - const patterns = [ |
34 |
| - "**/index.ts", |
35 |
| - ...excluded |
36 |
| - ]; |
37 |
| - |
38 |
| - const files = await globby(patterns, { |
39 |
| - cwd: cwd |
40 |
| - }); |
41 |
| - |
42 |
| - return Promise.all(files.map((file) => fs.unlink(join(cwd, file)))); |
43 |
| -} |
| 2 | +import {generateBarrels} from "../src/generate-barrel.js"; |
| 3 | +import {getConfig} from "../src/get-config.js"; |
44 | 4 |
|
45 | 5 | async function build() {
|
46 | 6 | const {
|
47 | 7 | directory = ["./src"],
|
48 | 8 | exclude = ["**/__mock__", "**/__mocks__", "**/*.spec.ts", "**/*.benchmark.ts"],
|
49 |
| - delete: shouldDelete |
50 | 9 | } = await getConfig();
|
51 |
| - |
52 |
| - const excluded = exclude |
53 |
| - .map((path) => `!${path}`) |
54 |
| - .concat(directory.map((path) => `!${path}/index.ts`)); |
55 |
| - |
56 |
| - const directories = ( |
57 |
| - await globby(directory.map((d) => { |
58 |
| - return join(d, "*"); |
59 |
| - }), |
60 |
| - { |
61 |
| - cwd: process.cwd() |
62 |
| - }) |
63 |
| - ).reduce((set, file) => { |
64 |
| - return set.add(dirname(file)); |
65 |
| - }, new Set()); |
66 |
| - |
67 |
| - const promises = [...directories.keys()].map(async (directory) => { |
68 |
| - const baseIndex = join(process.cwd(), directory?.path ?? directory); |
69 |
| - |
70 |
| - const files = await globby(["**/*.ts", "!index.ts", ...excluded], { |
71 |
| - cwd: directory |
72 |
| - }); |
73 |
| - |
74 |
| - const exports = files |
75 |
| - .sort((a, b) => a.localeCompare(b)) |
76 |
| - .map((file) => { |
77 |
| - // TODO set .js after all configuration are ok to resolve .js |
78 |
| - return `export * from "./${file.replace(".ts", ".js")}";`; |
79 |
| - }); |
80 |
| - |
81 |
| - const content = ["/**", " * @file Automatically generated by @tsed/barrels.", " */", ...exports]; |
82 |
| - |
83 |
| - await writeFile(join(baseIndex, "index.ts"), content.join("\n") + "\n", {encoding: "utf8"}); |
84 |
| - }); |
85 |
| - |
86 |
| - await Promise.all(promises); |
| 10 | + await generateBarrels({exclude, directory, cwd: process.cwd()}); |
87 | 11 | }
|
88 | 12 |
|
89 | 13 | await build();
|
0 commit comments