|
| 1 | +import type { DevframeNodeContext } from 'devframe/types' |
| 2 | +import type { ServerFunctions } from '../rpc' |
| 3 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { join } from 'pathe' |
| 6 | +import { RpcFunctionsCollectorBase } from 'devframe/rpc' |
| 7 | +import { afterEach, describe, expect, it } from 'vitest' |
| 8 | +import { oxcDevframe } from '../devframe' |
| 9 | + |
| 10 | +const fixtures: string[] = [] |
| 11 | + |
| 12 | +afterEach(async () => { |
| 13 | + await Promise.all(fixtures.splice(0).map(dir => rm(dir, { recursive: true, force: true }))) |
| 14 | +}) |
| 15 | + |
| 16 | +async function createProject() { |
| 17 | + const workspaceRoot = await mkdtemp(join(tmpdir(), 'oxc-monorepo-')) |
| 18 | + fixtures.push(workspaceRoot) |
| 19 | + const cwd = join(workspaceRoot, 'packages/app') |
| 20 | + await mkdir(cwd, { recursive: true }) |
| 21 | + const ctx = { cwd, workspaceRoot } as DevframeNodeContext |
| 22 | + const rpc = new RpcFunctionsCollectorBase<ServerFunctions, DevframeNodeContext>(ctx) |
| 23 | + ctx.rpc = rpc as unknown as DevframeNodeContext['rpc'] |
| 24 | + await oxcDevframe.setup!(ctx, {} as never) |
| 25 | + return { cwd, workspaceRoot, rpc } |
| 26 | +} |
| 27 | + |
| 28 | +describe('Oxc project context', () => { |
| 29 | + it('discovers and reads the project configs when the workspace has its own configs', async () => { |
| 30 | + const { cwd, workspaceRoot, rpc } = await createProject() |
| 31 | + const configs = [ |
| 32 | + ['oxlint.config.ts', 'devtools-oxc:get-lint-config-file'], |
| 33 | + ['oxfmt.config.ts', 'devtools-oxc:get-fmt-config-file'], |
| 34 | + ] as const |
| 35 | + for (const [filename] of configs) { |
| 36 | + await writeFile(join(workspaceRoot, filename), 'export default { /* workspace */ }') |
| 37 | + await writeFile(join(cwd, filename), 'export default { /* project */ }') |
| 38 | + } |
| 39 | + |
| 40 | + const discover = await rpc.getHandler('devtools-oxc:get-config-files') |
| 41 | + const files = await discover() |
| 42 | + expect(files).toEqual( |
| 43 | + expect.arrayContaining( |
| 44 | + configs.map(([path]) => |
| 45 | + expect.objectContaining({ |
| 46 | + path, |
| 47 | + content: 'export default { /* project */ }', |
| 48 | + }), |
| 49 | + ), |
| 50 | + ), |
| 51 | + ) |
| 52 | + expect(files).toHaveLength(2) |
| 53 | + for (const [, name] of configs) { |
| 54 | + const readConfig = await rpc.getHandler(name) |
| 55 | + await expect(readConfig()).resolves.toBe('export default { /* project */ }') |
| 56 | + } |
| 57 | + }) |
| 58 | +}) |
0 commit comments