-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: merge master into bump-dependencies
- Loading branch information
Showing
12 changed files
with
128 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
"packages/*" | ||
], | ||
"version": "6.0.0-alpha.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/__mock__/**/index.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / lint (20.x)
|
||
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {writeFile} from "node:fs/promises"; | ||
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 | ||
.map((path) => `!${path}`) | ||
.concat(directory.map((path) => `!${path}/index.ts`)); | ||
|
||
const directories = ( | ||
await globby(directory.map((d) => { | ||
return join(d, "*"); | ||
}), | ||
{ | ||
cwd | ||
}) | ||
).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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {join} from "node:path"; | ||
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 GitHub Actions / lint (20.x)
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
}) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const file1 = "file1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const file2 = "file2" |
1 change: 1 addition & 0 deletions
1
packages/barrels/test/__mock__/scenario-1/sub-directory/file2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const file3 = "file3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
}) |