11import assert from "node:assert/strict" ;
2+ import { mkdtemp , readFile , rm } from "node:fs/promises" ;
3+ import { tmpdir } from "node:os" ;
4+ import { join } from "node:path" ;
25import test from "node:test" ;
36import { message } from "@optique/core" ;
7+ import { kvStores , messageQueues } from "../lib.ts" ;
48import type { InitCommandData } from "../types.ts" ;
9+ import bareBonesDescription from "../webframeworks/bare-bones.ts" ;
510import { loadDenoConfig } from "./configs.ts" ;
11+ import { patchFiles } from "./patch.ts" ;
612
713function createInitData ( ) : InitCommandData {
814 const data = {
@@ -13,6 +19,7 @@ function createInitData(): InitCommandData {
1319 kvStore : "denokv" ,
1420 messageQueue : "denokv" ,
1521 dryRun : false ,
22+ allowNonEmpty : false ,
1623 testMode : false ,
1724 dir : "/tmp/example" ,
1825 initializer : {
@@ -94,3 +101,93 @@ test("loadDenoConfig keeps unstable.temporal before Deno 2.7.0", () => {
94101 restoreDeno ( originalDeno ) ;
95102 }
96103} ) ;
104+
105+ test ( "patchFiles creates a Biome config matching the npm package version" , async ( ) => {
106+ const dir = await mkdtemp ( join ( tmpdir ( ) , "fedify-init-biome-" ) ) ;
107+
108+ try {
109+ const data = await createNpmInitData ( dir ) ;
110+ await patchFiles ( data ) ;
111+
112+ const packageJson = JSON . parse (
113+ await readFile ( join ( dir , "package.json" ) , "utf8" ) ,
114+ ) as {
115+ devDependencies ?: Record < string , string > ;
116+ } ;
117+ const biomeConfig = JSON . parse (
118+ await readFile ( join ( dir , "biome.json" ) , "utf8" ) ,
119+ ) as Record < string , unknown > ;
120+
121+ const biomeVersion = packageJson . devDependencies ?. [ "@biomejs/biome" ] ;
122+ const schema = biomeConfig . $schema ;
123+ assert . ok ( typeof biomeVersion === "string" ) ;
124+ assert . ok ( typeof schema === "string" ) ;
125+ assert . equal ( getSchemaVersion ( schema ) , getPackageVersion ( biomeVersion ) ) ;
126+ assert . equal ( getOrganizeImportsSetting ( biomeConfig ) , "on" ) ;
127+ assert . equal (
128+ "organizeImports" in biomeConfig ,
129+ false ,
130+ ) ;
131+ } finally {
132+ await rm ( dir , { recursive : true , force : true } ) ;
133+ }
134+ } ) ;
135+
136+ async function createNpmInitData ( dir : string ) : Promise < InitCommandData > {
137+ const initializer = await bareBonesDescription . init ( {
138+ command : "init" ,
139+ projectName : "example" ,
140+ packageManager : "npm" ,
141+ webFramework : "bare-bones" ,
142+ kvStore : "in-memory" ,
143+ messageQueue : "in-process" ,
144+ dryRun : false ,
145+ allowNonEmpty : false ,
146+ testMode : false ,
147+ dir,
148+ } ) ;
149+
150+ const data = {
151+ command : "init" ,
152+ projectName : "example" ,
153+ packageManager : "npm" ,
154+ webFramework : "bare-bones" ,
155+ kvStore : "in-memory" ,
156+ messageQueue : "in-process" ,
157+ dryRun : false ,
158+ allowNonEmpty : false ,
159+ testMode : false ,
160+ dir,
161+ initializer,
162+ kv : kvStores [ "in-memory" ] ,
163+ mq : messageQueues [ "in-process" ] ,
164+ env : { } ,
165+ } satisfies InitCommandData ;
166+ return data ;
167+ }
168+
169+ function getSchemaVersion ( schema : string ) : string {
170+ const match = schema . match ( / \/ s c h e m a s \/ ( \d + \. \d + \. \d + ) \/ / ) ;
171+ assert . ok ( match , `Unexpected Biome schema URL: ${ schema } ` ) ;
172+ return match [ 1 ] ;
173+ }
174+
175+ function getPackageVersion ( version : string ) : string {
176+ const match = version . match ( / \d + \. \d + \. \d + / ) ;
177+ assert . ok ( match , `Unexpected Biome package version: ${ version } ` ) ;
178+ return match [ 0 ] ;
179+ }
180+
181+ function getOrganizeImportsSetting ( config : Record < string , unknown > ) : unknown {
182+ const assist = config . assist ;
183+ assert . ok ( isRecord ( assist ) ) ;
184+ const actions = assist . actions ;
185+ assert . ok ( isRecord ( actions ) ) ;
186+ const source = actions . source ;
187+ assert . ok ( isRecord ( source ) ) ;
188+ return source . organizeImports ;
189+ }
190+
191+ function isRecord ( value : unknown ) : value is Record < string , unknown > {
192+ return typeof value === "object" && value != null ;
193+ }
0 commit comments