Skip to content

Commit 2a31bc4

Browse files
committed
fix(barrels): add integration test and fix issue
1 parent 650ac1e commit 2a31bc4

File tree

10 files changed

+126
-79
lines changed

10 files changed

+126
-79
lines changed

packages/barrels/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/__mock__/**/index.ts

packages/barrels/bin/barrels.js

+3-79
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,13 @@
11
#!/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";
444

455
async function build() {
466
const {
477
directory = ["./src"],
488
exclude = ["**/__mock__", "**/__mocks__", "**/*.spec.ts", "**/*.benchmark.ts"],
49-
delete: shouldDelete
509
} = 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()});
8711
}
8812

8913
await build();

packages/barrels/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
},
1515
"dependencies": {
1616
"globby": "14.0.2"
17+
},
18+
"scripts": {
19+
"test": "node --test test/barrels.integration.spec.js"
1720
}
1821
}
+54
Original file line numberDiff line numberDiff line change
@@ -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+
}

packages/barrels/src/get-config.js

+28
Original file line numberDiff line numberDiff line change
@@ -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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {file1} from "./file1";
2+
3+
describe("barrels", () => {
4+
it("should", () => {
5+
expect(file1).toEqual("file1")
6+
})
7+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const file1 = "file1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const file2 = "file2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const file3 = "file3"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {generateBarrels} from "../src/generate-barrel.js";
2+
import {describe, it} from "node:test";
3+
import assert from "node:assert";
4+
import {join} from "node:path";
5+
import * as fs from "node:fs/promises";
6+
7+
describe('barrels.integration.ts', () => {
8+
it('should generate barrels', async () => {
9+
const cwd = join(import.meta.dirname, "__mock__")
10+
// Test code here
11+
await generateBarrels({
12+
cwd,
13+
"directory": ["./scenario-1"],
14+
"exclude": ["**/__mock__", "**/__mocks__", "**/*.spec.ts"],
15+
"delete": true
16+
})
17+
18+
const result = await fs.readFile(join(cwd, "scenario-1", "index.ts"), "utf-8")
19+
20+
assert.strictEqual(result, "/**\n" +
21+
" * @file Automatically generated by @tsed/barrels.\n" +
22+
" */\n" +
23+
"export * from \"./file1.js\";\n" +
24+
"export * from \"./file2.js\";\n" +
25+
"export * from \"./sub-directory/file2.js\";\n")
26+
})
27+
})

0 commit comments

Comments
 (0)