Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: automatically set the biome version in its outputted config file #31

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -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
},
Expand Down
91 changes: 84 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -74,6 +77,58 @@ function isEmptyDir(path: string) {
return files.length === 0 || (files.length === 1 && files[0] === '.git');
}

function fillUpStrPlaceholder(str: string, payload: Record<string, string>) {
return str.replace(
FILL_UP_PLACEHOLDER,
function replacer(_matchedStr, matchedPlaceholder) {
return payload[matchedPlaceholder];
},
);
}

function deepFillUpJsonValuePlaceholder(
jsonObj: object,
payload: Record<string, string>,
) {
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<string, string>,
) {
const biomeJson = JSON.parse(
await fs.promises.readFile(jsonFilePath, 'utf-8'),
);
deepFillUpJsonValuePlaceholder(biomeJson, payload);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deepFillUpJsonFilePlaceholder helper is quite complex. Can we keep it simple by just replace biomeJson['$schema']?

biomeJson['$schema'] = biomeJson['$schema'].replace('{version}', version);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure~~~


await fs.promises.writeFile(
jsonFilePath,
`${JSON.stringify(biomeJson, null, 2)}\n`,
'utf-8',
);
}

export type Argv = {
help?: boolean;
dir?: string;
Expand Down Expand Up @@ -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,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion template-biome/biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.0/schema.json",
"$schema": "https://biomejs.dev/schemas/{version}/schema.json",
"organizeImports": {
"enabled": true
},
Expand Down