mirror of
https://github.com/Effect-TS/effect.git
synced 2026-11-22 03:25:35 +08:00
Co-authored-by: Viacheslav <bezff@MacBook-Pro-3.local> Co-authored-by: Effect Bot <tech-ops@effectful.co>
166 lines
4.6 KiB
TypeScript
166 lines
4.6 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices"
|
|
import * as Codegen from "@effect/utils/Codegen"
|
|
import * as Glob from "@effect/utils/Glob"
|
|
import { assert, describe, it } from "@effect/vitest"
|
|
import * as Effect from "effect/Effect"
|
|
import * as FileSystem from "effect/FileSystem"
|
|
import * as Layer from "effect/Layer"
|
|
import * as path from "node:path"
|
|
|
|
const MainLayer = Codegen.layer.pipe(Layer.provide(Glob.layer), Layer.provideMerge(NodeServices.layer))
|
|
|
|
describe("BarrelGenerator", () => {
|
|
it.effect("discovers a barrel from an absolute pattern", () =>
|
|
Effect.gen(function*() {
|
|
const fs = yield* FileSystem.FileSystem
|
|
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "codegen-" })
|
|
const barrel = path.join(directory, "index.ts")
|
|
yield* fs.writeFileString(barrel, "// @barrel\n")
|
|
|
|
const generator = yield* Codegen.BarrelGenerator
|
|
const files = yield* generator.discoverFiles(barrel, directory)
|
|
|
|
assert.deepStrictEqual(files, [{ path: barrel, pattern: "*.ts", offset: 1 }])
|
|
}).pipe(Effect.provide(MainLayer)))
|
|
|
|
it.effect("excludes modules with a parenthesized barrel glob", () =>
|
|
Effect.gen(function*() {
|
|
const fs = yield* FileSystem.FileSystem
|
|
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "codegen-" })
|
|
const barrel = path.join(directory, "index.ts")
|
|
yield* fs.writeFileString(barrel, "// @barrel(!(MigratorFileSystem).ts)\n")
|
|
for (const module of ["Migrator", "MigratorFileSystem"]) {
|
|
yield* fs.writeFileString(path.join(directory, `${module}.ts`), "/**\n * @since 4.0.0\n */\n")
|
|
}
|
|
|
|
const generator = yield* Codegen.BarrelGenerator
|
|
const files = yield* generator.discoverFiles(barrel, directory)
|
|
yield* Effect.forEach(files, generator.processFile)
|
|
|
|
assert.strictEqual(
|
|
yield* fs.readFileString(barrel),
|
|
`// @barrel(!(MigratorFileSystem).ts)
|
|
|
|
/**
|
|
* @since 4.0.0
|
|
*/
|
|
export * as Migrator from "./Migrator.ts"
|
|
`
|
|
)
|
|
}).pipe(Effect.provide(MainLayer)))
|
|
|
|
it.effect("copies module stability tags into barrel exports", () =>
|
|
Effect.gen(function*() {
|
|
const fs = yield* FileSystem.FileSystem
|
|
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "codegen-" })
|
|
const barrel = path.join(directory, "index.ts")
|
|
yield* fs.writeFileString(barrel, "// @barrel\n")
|
|
yield* fs.writeFileString(
|
|
path.join(directory, "Experimental.ts"),
|
|
`/**
|
|
* Experimental module.
|
|
*
|
|
* @stability experimental
|
|
* @since 3.0.0
|
|
*/
|
|
export const experimental = 1
|
|
`
|
|
)
|
|
yield* fs.writeFileString(
|
|
path.join(directory, "ExplicitStable.ts"),
|
|
`/**
|
|
* Explicitly stable module.
|
|
*
|
|
* @stability stable
|
|
* @since 1.0.0
|
|
*/
|
|
export const explicitStable = 1
|
|
`
|
|
)
|
|
yield* fs.writeFileString(
|
|
path.join(directory, "Stable.ts"),
|
|
`/**
|
|
* Stable module.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
export const stable = 1
|
|
`
|
|
)
|
|
yield* fs.writeFileString(
|
|
path.join(directory, "Unstable.ts"),
|
|
`/**
|
|
* Unstable module.
|
|
*
|
|
* @stability unstable
|
|
* @since 2.0.0
|
|
*/
|
|
export const unstable = 1
|
|
`
|
|
)
|
|
|
|
const generator = yield* Codegen.BarrelGenerator
|
|
yield* generator.processFile({ path: barrel, pattern: "*.ts", offset: 1 })
|
|
|
|
assert.strictEqual(
|
|
yield* fs.readFileString(barrel),
|
|
`// @barrel
|
|
|
|
/**
|
|
* @stability experimental
|
|
* @since 3.0.0
|
|
*/
|
|
export * as Experimental from "./Experimental.ts"
|
|
|
|
/**
|
|
* @stability stable
|
|
* @since 1.0.0
|
|
*/
|
|
export * as ExplicitStable from "./ExplicitStable.ts"
|
|
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
export * as Stable from "./Stable.ts"
|
|
|
|
/**
|
|
* @stability unstable
|
|
* @since 2.0.0
|
|
*/
|
|
export * as Unstable from "./Unstable.ts"
|
|
`
|
|
)
|
|
}).pipe(Effect.provide(MainLayer)))
|
|
|
|
it.effect("reads the module JSDoc after triple-slash directives", () =>
|
|
Effect.gen(function*() {
|
|
const fs = yield* FileSystem.FileSystem
|
|
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "codegen-" })
|
|
const barrel = path.join(directory, "index.ts")
|
|
yield* fs.writeFileString(barrel, "// @barrel\n")
|
|
yield* fs.writeFileString(
|
|
path.join(directory, "Disposable.ts"),
|
|
`/// <reference lib="esnext.disposable" preserve="true" />
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
export const disposable = 1
|
|
`
|
|
)
|
|
|
|
const generator = yield* Codegen.BarrelGenerator
|
|
yield* generator.processFile({ path: barrel, pattern: "*.ts", offset: 1 })
|
|
|
|
assert.strictEqual(
|
|
yield* fs.readFileString(barrel),
|
|
`// @barrel
|
|
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
export * as Disposable from "./Disposable.ts"
|
|
`
|
|
)
|
|
}).pipe(Effect.provide(MainLayer)))
|
|
})
|