@tsed/barrels 6.0.0-alpha.8 → 6.0.0-beta.1

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 CHANGED
@@ -1,79 +1,10 @@
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
-
6
- import {globby} from "globby";
7
-
8
- function resolveConfig() {
9
- return [join(process.cwd(), ".barrelsby.json"), join(process.cwd(), ".barrels.json")].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
- function getConfig() {
21
- const configPath = resolveConfig();
22
-
23
- if (!configPath) {
24
- return {};
25
- }
26
-
27
- return readJSON(configPath);
28
- }
29
-
30
- async function cleanIndex(cwd, excluded) {
31
- const patterns = ["**/index.ts", ...excluded];
32
-
33
- const files = await globby(patterns, {
34
- cwd: cwd
35
- });
36
-
37
- return Promise.all(files.map((file) => fs.unlink(join(cwd, file))));
38
- }
2
+ import {generateBarrels} from "./generate-barrel.js";
3
+ import {getConfig} from "./get-config.js";
39
4
 
40
5
  async function build() {
41
- const {
42
- directory = ["./src"],
43
- exclude = ["**/__mock__", "**/__mocks__", "**/*.spec.ts", "**/*.benchmark.ts"],
44
- delete: shouldDelete
45
- } = await getConfig();
46
-
47
- const excluded = exclude.map((path) => `!${path}`).concat(directory.map((path) => `!${path}/index.ts`));
48
-
49
- const directories = (
50
- await globby(directory, {
51
- cwd: process.cwd()
52
- })
53
- ).reduce((set, file) => {
54
- return set.add(dirname(file));
55
- }, new Set());
56
-
57
- const promises = [...directories.keys()].map(async (directory) => {
58
- const baseIndex = join(process.cwd(), directory?.path ?? directory);
59
-
60
- const files = await globby(["**/*.ts", "!index.ts", ...excluded], {
61
- cwd: directory
62
- });
63
-
64
- const exports = files
65
- .sort((a, b) => a.localeCompare(b))
66
- .map((file) => {
67
- // TODO set .js after all configuration are ok to resolve .js
68
- return `export * from "./${file.replace(".ts", ".js")}";`;
69
- });
70
-
71
- const content = ["/**", " * @file Automatically generated by @tsed/barrels.", " */", ...exports];
72
-
73
- await writeFile(join(baseIndex, "index.ts"), content.join("\n") + "\n", {encoding: "utf8"});
74
- });
75
-
76
- await Promise.all(promises);
6
+ const {directory = ["./src"], exclude = ["**/__mock__", "**/__mocks__", "**/*.spec.ts", "**/*.benchmark.ts"]} = await getConfig();
7
+ await generateBarrels({exclude, directory, cwd: process.cwd()});
77
8
  }
78
9
 
79
10
  await build();
@@ -0,0 +1,55 @@
1
+ import {writeFile} from "node:fs/promises";
2
+ import path, {dirname, join} from "node:path";
3
+
4
+ import {globby} from "globby";
5
+
6
+ // async function cleanIndex(cwd, excluded) {
7
+ // const patterns = [
8
+ // "**/index.ts",
9
+ // ...excluded
10
+ // ];
11
+ //
12
+ // const files = await globby(patterns, {
13
+ // cwd: cwd
14
+ // });
15
+ //
16
+ // return Promise.all(files.map((file) => fs.unlink(join(cwd, file))));
17
+ // }
18
+
19
+ export async function generateBarrels({exclude, directory, cwd}) {
20
+ const excluded = exclude.map((path) => `!${path}`).concat(directory.map((path) => `!${path}/index.ts`));
21
+
22
+ const directories = (
23
+ await globby(
24
+ directory.map((d) => {
25
+ return join(d, "*");
26
+ }),
27
+ {
28
+ cwd
29
+ }
30
+ )
31
+ ).reduce((set, file) => {
32
+ return set.add(dirname(file));
33
+ }, new Set());
34
+
35
+ const promises = [...directories.keys()].map(async (directory) => {
36
+ const baseIndex = join(cwd, directory?.path ?? directory);
37
+
38
+ const files = await globby(["**/*.{ts,tsx}", "!index.{ts,tsx}", ...excluded], {
39
+ cwd: path.join(cwd, directory)
40
+ });
41
+
42
+ const exports = files
43
+ .sort((a, b) => a.localeCompare(b))
44
+ .map((file) => {
45
+ // TODO set .js after all configuration are ok to resolve .js
46
+ return `export * from "./${file.replace(".ts", ".js")}";`;
47
+ });
48
+
49
+ const content = ["/**", " * @file Automatically generated by @tsed/barrels.", " */", ...exports];
50
+
51
+ await writeFile(join(baseIndex, "index.ts"), content.join("\n") + "\n", {encoding: "utf8"});
52
+ });
53
+
54
+ await Promise.all(promises);
55
+ }
@@ -0,0 +1,25 @@
1
+ import {existsSync} from "node:fs";
2
+ import {readFile} from "node:fs/promises";
3
+ import {join} from "node:path";
4
+
5
+ function resolveConfig() {
6
+ return [join(process.cwd(), ".barrelsby.json"), join(process.cwd(), ".barrels.json")].find((path) => {
7
+ return existsSync(path);
8
+ });
9
+ }
10
+
11
+ async function readJSON(path) {
12
+ const content = await readFile(path, "utf-8");
13
+
14
+ return JSON.parse(content);
15
+ }
16
+
17
+ export function getConfig() {
18
+ const configPath = resolveConfig();
19
+
20
+ if (!configPath) {
21
+ return {};
22
+ }
23
+
24
+ return readJSON(configPath);
25
+ }
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "@tsed/barrels",
3
3
  "description": "A simple tool to generate barrels for your TypeScript project",
4
- "version": "6.0.0-alpha.8",
4
+ "version": "6.0.0-beta.1",
5
5
  "type": "module",
6
6
  "bin": "bin/barrels.js",
7
- "files": [],
8
7
  "keywords": [
9
8
  "Ts.ED",
10
9
  "barrels"
@@ -15,6 +14,9 @@
15
14
  "dependencies": {
16
15
  "globby": "14.0.2"
17
16
  },
17
+ "scripts": {
18
+ "test": "node --test test/barrels.integration.spec.js"
19
+ },
18
20
  "devDependencies": {},
19
21
  "peerDependencies": {},
20
22
  "main": "",
@@ -26,6 +28,6 @@
26
28
  "author": "Romain Lenzotti",
27
29
  "license": "MIT",
28
30
  "publishConfig": {
29
- "tag": "alpha"
31
+ "tag": "beta"
30
32
  }
31
33
  }
package/readme.md CHANGED
@@ -19,7 +19,7 @@
19
19
  npm install -g @tsed/barrels
20
20
  ```
21
21
 
22
- Then create a `barrels.json` configuration:
22
+ Then create a `.barrels.json` configuration:
23
23
 
24
24
  ```
25
25
  {