diff --git a/.bumpy/_config.json b/.bumpy/_config.json index 6c359c0..02c50b5 100644 --- a/.bumpy/_config.json +++ b/.bumpy/_config.json @@ -1,6 +1,12 @@ { + // we use the local version, but normally you'd use + // "$schema": "../node_modules/@varlock/bumpy/config-schema.json", "$schema": "../packages/bumpy/config-schema.json", + + // main is already default, but no harm in being explicit "baseBranch": "main", + // only thank external contributors "changelog": ["github", { "internalAuthors": ["theoephraim", "philmillman"] }], + // narrows down which files are considered changes "changedFilePatterns": ["src/**", "skills/**", "package.json", "tsconfig.json", "tsdown.config.ts"] } diff --git a/.bumpy/empty-ci-setup.md b/.bumpy/empty-ci-setup.md deleted file mode 100644 index a845151..0000000 --- a/.bumpy/empty-ci-setup.md +++ /dev/null @@ -1,2 +0,0 @@ ---- ---- diff --git a/.bumpy/jsonc-config-support.md b/.bumpy/jsonc-config-support.md new file mode 100644 index 0000000..45717fa --- /dev/null +++ b/.bumpy/jsonc-config-support.md @@ -0,0 +1,5 @@ +--- +'@varlock/bumpy': patch +--- + +Support JSONC in config file — allows // line comments, block comments, and trailing commas in .bumpy/\_config.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..eb5d60d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + // use the project's TS version rather than the VSCode's built-in one + "typescript.tsdk": "./node_modules/typescript/lib", + "typescript.enablePromptUseWorkspaceTsdk": true, + "files.associations": { + // package.json needs to be "json" rather than "json with comments" so that the "version lens" plugin works + "package.json": "json", + "**/.bumpy/_config.json": "jsonc" + } +} diff --git a/docs/configuration.md b/docs/configuration.md index 22e2c8d..2a20684 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2,6 +2,8 @@ Bumpy is configured via `.bumpy/_config.json`, created by `bumpy init`. Per-package config can also be set in each `package.json` under the `"bumpy"` key. +> **Tip:** The config file supports JSONC — you can use `//` line comments, `/* */` block comments, and trailing commas. + ## Global config (`.bumpy/_config.json`) | Option | Type | Default | Description | diff --git a/packages/bumpy/src/core/config.ts b/packages/bumpy/src/core/config.ts index 662e98f..cb89d64 100644 --- a/packages/bumpy/src/core/config.ts +++ b/packages/bumpy/src/core/config.ts @@ -1,5 +1,5 @@ import { resolve } from 'node:path'; -import { readJson, exists } from '../utils/fs.ts'; +import { readJson, readJsonc, exists } from '../utils/fs.ts'; import { type BumpyConfig, type PackageConfig, DEFAULT_CONFIG } from '../types.ts'; const BUMPY_DIR = '.bumpy'; @@ -32,7 +32,7 @@ export async function loadConfig(rootDir: string): Promise { const configPath = resolve(rootDir, BUMPY_DIR, CONFIG_FILE); let userConfig: Partial = {}; if (await exists(configPath)) { - userConfig = await readJson>(configPath); + userConfig = await readJsonc>(configPath); } return mergeConfig(DEFAULT_CONFIG, userConfig); } diff --git a/packages/bumpy/src/utils/fs.ts b/packages/bumpy/src/utils/fs.ts index a05a7d2..519eb88 100644 --- a/packages/bumpy/src/utils/fs.ts +++ b/packages/bumpy/src/utils/fs.ts @@ -1,11 +1,17 @@ import { readFile, writeFile, readdir, unlink, mkdir, access } from 'node:fs/promises'; import { join } from 'node:path'; +import { parseJsonc } from './jsonc.ts'; export async function readJson>(filePath: string): Promise { const content = await readFile(filePath, 'utf-8'); return JSON.parse(content) as T; } +export async function readJsonc>(filePath: string): Promise { + const content = await readFile(filePath, 'utf-8'); + return parseJsonc(content) as T; +} + export async function writeJson(filePath: string, data: unknown, indent = 2): Promise { await writeFile(filePath, JSON.stringify(data, null, indent) + '\n', 'utf-8'); } diff --git a/packages/bumpy/src/utils/jsonc.ts b/packages/bumpy/src/utils/jsonc.ts new file mode 100644 index 0000000..e76b80d --- /dev/null +++ b/packages/bumpy/src/utils/jsonc.ts @@ -0,0 +1,11 @@ +// JSONC parsing adapted from https://github.com/fabiospampinato/tiny-jsonc +const stringOrCommentRe = /("(?:\\?[^])*?")|(\/\/.*)|(\/\*[^]*?\*\/)/g; +const stringOrTrailingCommaRe = /("(?:\\?[^])*?")|(,\s*)(?=]|})/g; + +export function parseJsonc(text: string): unknown { + try { + return JSON.parse(text); + } catch { + return JSON.parse(text.replace(stringOrCommentRe, '$1').replace(stringOrTrailingCommaRe, '$1')); + } +} diff --git a/packages/bumpy/test/utils/jsonc.test.ts b/packages/bumpy/test/utils/jsonc.test.ts new file mode 100644 index 0000000..6f10094 --- /dev/null +++ b/packages/bumpy/test/utils/jsonc.test.ts @@ -0,0 +1,42 @@ +import { test, expect, describe } from 'bun:test'; +import { parseJsonc } from '../../src/utils/jsonc.ts'; + +describe('parseJsonc', () => { + test('parses plain JSON', () => { + expect(parseJsonc('{"a": 1}')).toEqual({ a: 1 }); + }); + + test('strips line comments', () => { + const input = `{ + // this is a comment + "a": 1 + }`; + expect(parseJsonc(input)).toEqual({ a: 1 }); + }); + + test('strips block comments', () => { + const input = `{ + /* block comment */ + "a": 1 + }`; + expect(parseJsonc(input)).toEqual({ a: 1 }); + }); + + test('handles trailing commas', () => { + const input = `{ "a": 1, "b": 2, }`; + expect(parseJsonc(input)).toEqual({ a: 1, b: 2 }); + }); + + test('preserves comment-like strings in values', () => { + const input = `{ "url": "https://example.com // not a comment" }`; + expect(parseJsonc(input)).toEqual({ url: 'https://example.com // not a comment' }); + }); + + test('throws on invalid JSON', () => { + expect(() => parseJsonc('{ bad }')).toThrow(); + }); + + test('throws on empty input', () => { + expect(() => parseJsonc('')).toThrow(); + }); +});