From b84e7675c146484f576a0b96b22bd00a83d50322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AD=90=E5=87=8C?= Date: Thu, 6 Mar 2025 16:53:56 +0800 Subject: [PATCH 1/3] build: bump the version of the `biome` json schema --- biome.json | 2 +- template-biome/biome.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/biome.json b/biome.json index 348ebd5..ec026a2 100644 --- a/biome.json +++ b/biome.json @@ -1,5 +1,5 @@ { - "$schema": "https://biomejs.dev/schemas/1.8.3/schema.json", + "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", "organizeImports": { "enabled": true }, diff --git a/template-biome/biome.json b/template-biome/biome.json index f281009..78a8d9f 100644 --- a/template-biome/biome.json +++ b/template-biome/biome.json @@ -1,5 +1,5 @@ { - "$schema": "https://biomejs.dev/schemas/1.8.0/schema.json", + "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", "organizeImports": { "enabled": true }, From d9d2f55039e3e2801dd0ac8fbe761cc61a8c85a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AD=90=E5=87=8C?= Date: Thu, 6 Mar 2025 18:39:11 +0800 Subject: [PATCH 2/3] chore: enable to automatically set the `biome` version in its outputted config file --- src/index.ts | 91 ++++++++++++++++++++++++++++++++++++--- template-biome/biome.json | 2 +- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 60da1cc..ea5674b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,9 @@ import { logger } from 'rslog'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); +// Fill up the string placeholders with the provided plain object. +// `{a} - {b}` with `{a: 1, b: 2}` -> `1 - 2`. +const FILL_UP_PLACEHOLDER = /\{\s*([^{}]+)\s*}/g; export { select, multiselect, text }; @@ -74,6 +77,58 @@ function isEmptyDir(path: string) { return files.length === 0 || (files.length === 1 && files[0] === '.git'); } +function fillUpStrPlaceholder(str: string, payload: Record) { + return str.replace( + FILL_UP_PLACEHOLDER, + function replacer(_matchedStr, matchedPlaceholder) { + return payload[matchedPlaceholder]; + }, + ); +} + +function deepFillUpJsonValuePlaceholder( + jsonObj: object, + payload: Record, +) { + for (const key in jsonObj) { + if (!Object.prototype.hasOwnProperty.call(jsonObj, key)) { + continue; + } + + // @ts-expect-error + const value = jsonObj[key]; + if (typeof value === 'object' && value != null) { + deepFillUpJsonValuePlaceholder(value, payload); + continue; + } + + if (typeof value !== 'string') { + continue; + } + + // @ts-expect-error + jsonObj[key] = fillUpStrPlaceholder(value, payload); + } + + return jsonObj; +} + +async function deepFillUpJsonFilePlaceholder( + jsonFilePath: string, + payload: Record, +) { + const biomeJson = JSON.parse( + await fs.promises.readFile(jsonFilePath, 'utf-8'), + ); + deepFillUpJsonValuePlaceholder(biomeJson, payload); + + await fs.promises.writeFile( + jsonFilePath, + `${JSON.stringify(biomeJson, null, 2)}\n`, + 'utf-8', + ); +} + export type Argv = { help?: boolean; dir?: string; @@ -257,13 +312,35 @@ export async function create({ skipFiles, isMergePackageJson: true, }); - } else { - copyFolder({ - from: toolFolder, - to: distFolder, - version, - skipFiles, - isMergePackageJson: true, + + continue; + } + + copyFolder({ + from: toolFolder, + to: distFolder, + version, + skipFiles, + isMergePackageJson: true, + }); + + if (tool === 'biome') { + let biomeVersion: string = + JSON.parse( + await fs.promises.readFile( + path.join(distFolder, 'package.json'), + 'utf-8', + ), + ).devDependencies?.['@biomejs/biome'] ?? '1.9.4'; + + biomeVersion = biomeVersion + .split('.') + .slice(0, 3) + .map((s) => s.replace(/\W/g, '')) + .join('.'); + + await deepFillUpJsonFilePlaceholder(path.join(distFolder, 'biome.json'), { + version: biomeVersion, }); } } diff --git a/template-biome/biome.json b/template-biome/biome.json index 78a8d9f..237fba1 100644 --- a/template-biome/biome.json +++ b/template-biome/biome.json @@ -1,5 +1,5 @@ { - "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", + "$schema": "https://biomejs.dev/schemas/{version}/schema.json", "organizeImports": { "enabled": true }, From 45d674ff1d1ebd6b3780d69b6478cfaf1f1a1732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AD=90=E5=87=8C?= Date: Thu, 6 Mar 2025 19:06:37 +0800 Subject: [PATCH 3/3] chore: enable to automatically set the `biome` version in its outputted config file --- src/index.ts | 70 +++++++++------------------------------------------- 1 file changed, 12 insertions(+), 58 deletions(-) diff --git a/src/index.ts b/src/index.ts index ea5674b..9dcf6a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,9 +18,6 @@ import { logger } from 'rslog'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); -// Fill up the string placeholders with the provided plain object. -// `{a} - {b}` with `{a: 1, b: 2}` -> `1 - 2`. -const FILL_UP_PLACEHOLDER = /\{\s*([^{}]+)\s*}/g; export { select, multiselect, text }; @@ -77,58 +74,6 @@ function isEmptyDir(path: string) { return files.length === 0 || (files.length === 1 && files[0] === '.git'); } -function fillUpStrPlaceholder(str: string, payload: Record) { - return str.replace( - FILL_UP_PLACEHOLDER, - function replacer(_matchedStr, matchedPlaceholder) { - return payload[matchedPlaceholder]; - }, - ); -} - -function deepFillUpJsonValuePlaceholder( - jsonObj: object, - payload: Record, -) { - for (const key in jsonObj) { - if (!Object.prototype.hasOwnProperty.call(jsonObj, key)) { - continue; - } - - // @ts-expect-error - const value = jsonObj[key]; - if (typeof value === 'object' && value != null) { - deepFillUpJsonValuePlaceholder(value, payload); - continue; - } - - if (typeof value !== 'string') { - continue; - } - - // @ts-expect-error - jsonObj[key] = fillUpStrPlaceholder(value, payload); - } - - return jsonObj; -} - -async function deepFillUpJsonFilePlaceholder( - jsonFilePath: string, - payload: Record, -) { - const biomeJson = JSON.parse( - await fs.promises.readFile(jsonFilePath, 'utf-8'), - ); - deepFillUpJsonValuePlaceholder(biomeJson, payload); - - await fs.promises.writeFile( - jsonFilePath, - `${JSON.stringify(biomeJson, null, 2)}\n`, - 'utf-8', - ); -} - export type Argv = { help?: boolean; dir?: string; @@ -339,9 +284,18 @@ export async function create({ .map((s) => s.replace(/\W/g, '')) .join('.'); - await deepFillUpJsonFilePlaceholder(path.join(distFolder, 'biome.json'), { - version: biomeVersion, - }); + const biomeJsonPath = path.join(distFolder, 'biome.json'); + const biomeJson = JSON.parse( + await fs.promises.readFile(biomeJsonPath, 'utf-8'), + ); + + biomeJson.$schema = biomeJson.$schema.replace('{version}', biomeVersion); + + await fs.promises.writeFile( + biomeJsonPath, + `${JSON.stringify(biomeJson, null, 2)}\n`, + 'utf-8', + ); } }