Skip to content

Commit

Permalink
fix: merge master into bump-dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Sep 29, 2024
2 parents 71f77dc + ac00fc1 commit 9a64145
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 72 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"packages/*"
],
"version": "6.0.0-alpha.10"
}
}
1 change: 1 addition & 0 deletions packages/barrels/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/__mock__/**/index.ts
72 changes: 3 additions & 69 deletions packages/barrels/bin/barrels.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,13 @@
#!/usr/bin/env node
import {existsSync} from "node:fs";
import {readFile, writeFile} from "node:fs/promises";
import {dirname, join} from "node:path";

import {globby} from "globby";

function resolveConfig() {
return [join(process.cwd(), ".barrelsby.json"), join(process.cwd(), ".barrels.json")].find((path) => {
return existsSync(path);
});
}

async function readJSON(path) {
const content = await readFile(path, "utf-8");

return JSON.parse(content);
}

function getConfig() {
const configPath = resolveConfig();

if (!configPath) {
return {};
}

return readJSON(configPath);
}

async function cleanIndex(cwd, excluded) {
const patterns = ["**/index.ts", ...excluded];

const files = await globby(patterns, {
cwd: cwd
});

return Promise.all(files.map((file) => fs.unlink(join(cwd, file))));
}
import {generateBarrels} from "./generate-barrel.js";
import {getConfig} from "./get-config.js";

async function build() {
const {

Check failure on line 6 in packages/barrels/bin/barrels.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Replace `⏎····directory·=·["./src"],⏎····exclude·=·["**/__mock__",·"**/__mocks__",·"**/*.spec.ts",·"**/*.benchmark.ts"],⏎··` with `directory·=·["./src"],·exclude·=·["**/__mock__",·"**/__mocks__",·"**/*.spec.ts",·"**/*.benchmark.ts"]`
directory = ["./src"],
exclude = ["**/__mock__", "**/__mocks__", "**/*.spec.ts", "**/*.benchmark.ts"],
delete: shouldDelete
} = await getConfig();

const excluded = exclude.map((path) => `!${path}`).concat(directory.map((path) => `!${path}/index.ts`));

const directories = (
await globby(directory, {
cwd: process.cwd()
})
).reduce((set, file) => {
return set.add(dirname(file));
}, new Set());

const promises = [...directories.keys()].map(async (directory) => {
const baseIndex = join(process.cwd(), directory?.path ?? directory);

const files = await globby(["**/*.ts", "!index.ts", ...excluded], {
cwd: directory
});

const exports = files
.sort((a, b) => a.localeCompare(b))
.map((file) => {
// TODO set .js after all configuration are ok to resolve .js
return `export * from "./${file.replace(".ts", ".js")}";`;
});

const content = ["/**", " * @file Automatically generated by @tsed/barrels.", " */", ...exports];

await writeFile(join(baseIndex, "index.ts"), content.join("\n") + "\n", {encoding: "utf8"});
});

await Promise.all(promises);
await generateBarrels({exclude, directory, cwd: process.cwd()});
}

await build();
54 changes: 54 additions & 0 deletions packages/barrels/bin/generate-barrel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {writeFile} from "node:fs/promises";

Check failure on line 1 in packages/barrels/bin/generate-barrel.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Run autofix to sort these imports!
import path, {dirname, join} from "node:path";
import {globby} from "globby";

// async function cleanIndex(cwd, excluded) {
// const patterns = [
// "**/index.ts",
// ...excluded
// ];
//
// const files = await globby(patterns, {
// cwd: cwd
// });
//
// return Promise.all(files.map((file) => fs.unlink(join(cwd, file))));
// }

export async function generateBarrels({exclude, directory, cwd}) {
const excluded = exclude

Check failure on line 19 in packages/barrels/bin/generate-barrel.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Replace `⏎····.map((path)·=>·`!${path}`)⏎····` with `.map((path)·=>·`!${path}`)`
.map((path) => `!${path}`)
.concat(directory.map((path) => `!${path}/index.ts`));

const directories = (
await globby(directory.map((d) => {

Check failure on line 24 in packages/barrels/bin/generate-barrel.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Insert `⏎······`
return join(d, "*");
}),
{
cwd
})

Check failure on line 29 in packages/barrels/bin/generate-barrel.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Insert `⏎····`
).reduce((set, file) => {
return set.add(dirname(file));
}, new Set());

const promises = [...directories.keys()].map(async (directory) => {
const baseIndex = join(cwd, directory?.path ?? directory);

const files = await globby(["**/*.{ts,tsx}", "!index.{ts,tsx}", ...excluded], {
cwd: path.join(cwd, directory)
});

const exports = files
.sort((a, b) => a.localeCompare(b))
.map((file) => {
// TODO set .js after all configuration are ok to resolve .js
return `export * from "./${file.replace(".ts", ".js")}";`;
});

const content = ["/**", " * @file Automatically generated by @tsed/barrels.", " */", ...exports];

await writeFile(join(baseIndex, "index.ts"), content.join("\n") + "\n", {encoding: "utf8"});
});

await Promise.all(promises);
}
28 changes: 28 additions & 0 deletions packages/barrels/bin/get-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {join} from "node:path";

Check failure on line 1 in packages/barrels/bin/get-config.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Run autofix to sort these imports!
import {existsSync} from "node:fs";
import {readFile} from "node:fs/promises";

function resolveConfig() {
return [

Check failure on line 6 in packages/barrels/bin/get-config.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Replace `⏎····join(process.cwd(),·".barrelsby.json"),⏎····join(process.cwd(),·".barrels.json")⏎··` with `join(process.cwd(),·".barrelsby.json"),·join(process.cwd(),·".barrels.json")`
join(process.cwd(), ".barrelsby.json"),
join(process.cwd(), ".barrels.json")
].find((path) => {
return existsSync(path);
});
}

async function readJSON(path) {
const content = await readFile(path, "utf-8");

return JSON.parse(content);
}

export function getConfig() {
const configPath = resolveConfig();

if (!configPath) {
return {};
}

return readJSON(configPath);
}
4 changes: 3 additions & 1 deletion packages/barrels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"version": "6.0.0-alpha.10",
"type": "module",
"bin": "bin/barrels.js",
"files": [],
"keywords": [
"Ts.ED",
"barrels"
Expand All @@ -14,5 +13,8 @@
},
"dependencies": {
"globby": "14.0.2"
},
"scripts": {
"test": "node --test test/barrels.integration.spec.js"
}
}
2 changes: 1 addition & 1 deletion packages/barrels/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
npm install -g @tsed/barrels
```

Then create a `barrels.json` configuration:
Then create a `.barrels.json` configuration:

```
{
Expand Down
7 changes: 7 additions & 0 deletions packages/barrels/test/__mock__/scenario-1/file1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {file1} from "./file1";

describe("barrels", () => {
it("should", () => {
expect(file1).toEqual("file1")

Check failure on line 5 in packages/barrels/test/__mock__/scenario-1/file1.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

Insert `;`
})

Check failure on line 6 in packages/barrels/test/__mock__/scenario-1/file1.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

Insert `;`
})

Check failure on line 7 in packages/barrels/test/__mock__/scenario-1/file1.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

Insert `;`
1 change: 1 addition & 0 deletions packages/barrels/test/__mock__/scenario-1/file1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const file1 = "file1"
1 change: 1 addition & 0 deletions packages/barrels/test/__mock__/scenario-1/file2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const file2 = "file2"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const file3 = "file3"
27 changes: 27 additions & 0 deletions packages/barrels/test/barrels.integration.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {generateBarrels} from "../bin/generate-barrel.js";
import {describe, it} from "node:test";
import assert from "node:assert";
import {join} from "node:path";
import * as fs from "node:fs/promises";

describe('barrels.integration.ts', () => {
it('should generate barrels', async () => {
const cwd = join(import.meta.dirname, "__mock__")
// Test code here
await generateBarrels({
cwd,
"directory": ["./scenario-1"],
"exclude": ["**/__mock__", "**/__mocks__", "**/*.spec.ts"],
"delete": true
})

const result = await fs.readFile(join(cwd, "scenario-1", "index.ts"), "utf-8")

assert.strictEqual(result, "/**\n" +
" * @file Automatically generated by @tsed/barrels.\n" +
" */\n" +
"export * from \"./file1.js\";\n" +
"export * from \"./file2.js\";\n" +
"export * from \"./sub-directory/file2.js\";\n")
})
})

0 comments on commit 9a64145

Please sign in to comment.