From 09e0405ea2fc4a62be84ed0a89825f3c13388af3 Mon Sep 17 00:00:00 2001 From: Ion Gireada Date: Sun, 2 Nov 2025 22:38:35 +0200 Subject: [PATCH] [BUG] transform entry emptied in markdown-magic configuration file Fixes #20 --- .vscode/launch.json | 14 +++++++++++++ README.md | 39 +++++++++++++++++----------------- _descriptions.json | 20 ++++++++++++++++++ md.config.js | 31 --------------------------- package.json | 4 ++-- src/describe.js | 51 +++++++++++++++++++++++++++++++++++++++++---- 6 files changed, 103 insertions(+), 56 deletions(-) create mode 100644 _descriptions.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 0d3c893..09bc5ca 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,20 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Launch Config Program", + "program": "${workspaceFolder}\\src\\cli.js", + "request": "launch", + "skipFiles": ["/**"], + "type": "node", + "args": [ + ".", + "--update-config", + "md.config.js", + "--transform-name", + "fileTreeExtended" + ] + }, { "name": "Launch Programmatic Program", "program": "${workspaceFolder}\\example\\programmatic-use.js", diff --git a/README.md b/README.md index 7161b51..78abaf4 100644 --- a/README.md +++ b/README.md @@ -179,49 +179,49 @@ Options: -- `describe` — Update repository descriptions in md.config.js transformDefaults for fileTreeExtended. (line [93](./package.json#L93)) +- `describe` — Update repository descriptions in md.config.js transformDefaults for fileTreeExtended. (line [92](./package.json#L92)) ```bash node src/cli.js . --update-config md.config.js --transform-name fileTreeExtended ``` -- `describe:file` — Generates AI-powered descriptions for repository files and outputs them in various formats. (line [87](./package.json#L87)) +- `describe:file` — Generates AI-powered descriptions for repository files and outputs them in various formats. (line [86](./package.json#L86)) ```bash - node src/cli.js . --output descriptions.json + node src/cli.js . --output _descriptions.json ``` -- `docs` — Generates documentation by processing Markdown files with markdown-magic. (line [92](./package.json#L92)) +- `docs` — Generates documentation by processing Markdown files with markdown-magic. (line [91](./package.json#L91)) ```bash npx markdown-magic@3.7.0 **/*.md -c md.config.js ``` -- `format` — Formats the codebase using Prettier. (line [90](./package.json#L90)) +- `format` — Formats the codebase using Prettier. (line [89](./package.json#L89)) ```bash prettier --write . ``` -- `lint` — Lints the codebase for potential errors and style violations. (line [88](./package.json#L88)) +- `lint` — Lints the codebase for potential errors and style violations. (line [87](./package.json#L87)) ```bash eslint src/ **/*.js **/*.json ``` -- `lint:fix` — Lints the codebase and automatically fixes fixable issues. (line [89](./package.json#L89)) +- `lint:fix` — Lints the codebase and automatically fixes fixable issues. (line [88](./package.json#L88)) ```bash eslint --fix src/ **/*.js **/*.json ``` -- `prep` — Prepares the codebase by generating documentation, linting, and formatting. (line [91](./package.json#L91)) +- `prep` — Prepares the codebase by generating documentation, linting, and formatting. (line [90](./package.json#L90)) ```bash - npm run docs && npm run lint:fix && npm run format + npm run describe:file && npm run docs && npm run lint:fix && npm run format ``` -- `test` — Runs the test suite using Jest. (line [86](./package.json#L86)) +- `test` — Runs the test suite using Jest. (line [85](./package.json#L85)) ```bash jest --passWithNoTests @@ -266,7 +266,7 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file ## Project Structure - + ``` repo-description/ @@ -281,15 +281,16 @@ repo-description/ │ ├── describe.js # Description unavailable. │ ├── index.js # Description unavailable. │ └── utils.js # Description unavailable. -├── .env # Configure... the environment by setting the `GROQ_API_KEY` variable to provide authentication for Groq API access. +├── _descriptions.json +├── .env # Defines the GROQ_API_KEY environment variable for authentication. ├── .gitignore -├── .prettierrc.json # Configure Prettier to enforce single quotes, trailing commas (es5), an 80‑character print width, and a specific ordering of fields in package.json via the prettier‑plugin‑packagejson. -├── babel.config.js # [configure] Babel to use @babel/preset‑env with the target set to the current Node version… -├── CHANGELOG.md # Documenting the project's version history by listing each release’s features, bug fixes, and related metadata in a Keep‑a‑Changelog format. -├── CONTRIBUTING.md # Guide contributors on how to report bugs, suggest enhancements, and submit code via pull requests. -├── eslint.config.js # Configure ESLint with global browser, node, and jest globals, recommended core and Prettier rules, and custom JSON/JSONC and YAML parsers and plugins for those file types. -├── LICENSE # Granting permission to use, copy, modify, merge, publish, distribute, sublicense, and sell the software freely under the MIT License. -├── md.config.js # [Exports] a configuration object that defines default settings (like file descriptions and badge style) and registers the markdown‑magic transform modules used to generate and augment the project’s documentation. +├── .prettierrc.json # Description unavailable. +├── babel.config.js # Description unavailable. +├── CHANGELOG.md # Description unavailable. +├── CONTRIBUTING.md # Description unavailable. +├── eslint.config.js # Description unavailable. +├── LICENSE # Description unavailable. +├── md.config.js # Description unavailable. ├── package-lock.json # Description unavailable. ├── package.json # Description unavailable. ├── README.md # Description unavailable. diff --git a/_descriptions.json b/_descriptions.json new file mode 100644 index 0000000..7f77db1 --- /dev/null +++ b/_descriptions.json @@ -0,0 +1,20 @@ +{ + ".env": "Defines the GROQ_API_KEY environment variable for authentication.", + ".prettierrc.json": "Description unavailable.", + ".vscode/launch.json": "Description unavailable.", + "babel.config.js": "Description unavailable.", + "CHANGELOG.md": "Description unavailable.", + "CONTRIBUTING.md": "Description unavailable.", + "eslint.config.js": "Description unavailable.", + "LICENSE": "Description unavailable.", + "md.config.js": "Description unavailable.", + "package-lock.json": "Description unavailable.", + "package.json": "Description unavailable.", + "README.md": "Description unavailable.", + "RULES_OF_CONDUCT.md": "Description unavailable.", + "src/cli.js": "Description unavailable.", + "src/describe.js": "Description unavailable.", + "src/index.js": "Description unavailable.", + "src/utils.js": "Description unavailable.", + "__tests__/cli.test.js": "Description unavailable." +} diff --git a/md.config.js b/md.config.js index 4b3d56a..fa83a3b 100644 --- a/md.config.js +++ b/md.config.js @@ -1,36 +1,5 @@ module.exports = { transformDefaults: { - fileTreeExtended: { - descriptions: { - '.env': - 'Configure... the environment by setting the `GROQ_API_KEY` variable to provide authentication for Groq API access.', - '.prettierrc.json': - 'Configure Prettier to enforce single quotes, trailing commas (es5), an 80‑character print width, and a specific ordering of fields in package.json via the prettier‑plugin‑packagejson.', - '.vscode/launch.json': - 'Configure launch configurations for debugging Node.js programs in VS Code.', - 'babel.config.js': - '[configure] Babel to use @babel/preset‑env with the target set to the current Node version…', - 'CHANGELOG.md': - "Documenting the project's version history by listing each release’s features, bug fixes, and related metadata in a Keep‑a‑Changelog format.", - 'CONTRIBUTING.md': - 'Guide contributors on how to report bugs, suggest enhancements, and submit code via pull requests.', - 'eslint.config.js': - 'Configure ESLint with global browser, node, and jest globals, recommended core and Prettier rules, and custom JSON/JSONC and YAML parsers and plugins for those file types.', - LICENSE: - 'Granting permission to use, copy, modify, merge, publish, distribute, sublicense, and sell the software freely under the MIT License.', - 'md.config.js': - '[Exports] a configuration object that defines default settings (like file descriptions and badge style) and registers the markdown‑magic transform modules used to generate and augment the project’s documentation.', - 'package-lock.json': 'Description unavailable.', - 'package.json': 'Description unavailable.', - 'README.md': 'Description unavailable.', - 'RULES_OF_CONDUCT.md': 'Description unavailable.', - 'src/cli.js': 'Description unavailable.', - 'src/describe.js': 'Description unavailable.', - 'src/index.js': 'Description unavailable.', - 'src/utils.js': 'Description unavailable.', - '__tests__/cli.test.js': 'Description unavailable.', - }, - }, BADGES: { style: 'for-the-badge', }, diff --git a/package.json b/package.json index d401be4..f381cc0 100644 --- a/package.json +++ b/package.json @@ -43,12 +43,12 @@ ], "scripts": { "describe": "node src/cli.js . --update-config md.config.js --transform-name fileTreeExtended", - "describe:file": "node src/cli.js . --output descriptions.json", + "describe:file": "node src/cli.js . --output _descriptions.json", "docs": "npx markdown-magic@3.7.0 **/*.md -c md.config.js", "format": "prettier --write .", "lint": "eslint src/ **/*.js **/*.json", "lint:fix": "eslint --fix src/ **/*.js **/*.json", - "prep": "npm run docs && npm run lint:fix && npm run format", + "prep": "npm run describe:file && npm run docs && npm run lint:fix && npm run format", "test": "jest --passWithNoTests" }, "jest": { diff --git a/src/describe.js b/src/describe.js index e5ce5f7..a527184 100644 --- a/src/describe.js +++ b/src/describe.js @@ -144,8 +144,13 @@ async function updateMarkdownMagicConfig( throw new Error(`Config file not found: ${absPath}`); } - const config = require(absPath); + let fileContent = fs.readFileSync(absPath, 'utf8'); + // Use require to get the current config object for in-memory manipulation. + // This allows us to easily update the descriptions. + let config = require(absPath); + + // Ensure transformDefaults and the specific transformName exist in the in-memory config if (!config.transformDefaults) { config.transformDefaults = {}; } @@ -153,11 +158,49 @@ async function updateMarkdownMagicConfig( config.transformDefaults[transformName] = {}; } + // Update the descriptions in the in-memory config object config.transformDefaults[transformName].descriptions = descriptions; - // Write back to file - const newConfig = `module.exports = ${JSON.stringify(config, null, 2)};\n`; - fs.writeFileSync(absPath, newConfig); + // Now, we need to update the file content string. + // We will find the 'transformDefaults' property and replace its value. + // This regex is designed to find 'transformDefaults:' followed by its object literal. + // It's still somewhat fragile and relies on consistent formatting. + const transformDefaultsRegex = /(transformDefaults:\s*)(\{[\s\S]*?\})/m; + const match = fileContent.match(transformDefaultsRegex); + + if (match) { + // The original string for transformDefaults (including the key and colon) + const originalTransformDefaultsString = match[0]; + // The object literal part of transformDefaults + // const originalTransformDefaultsObjectString = match[2]; // Not directly used for replacement + + // Stringify only the updated transformDefaults from the in-memory config. + // This is safe because 'descriptions' is expected to be a JSON-serializable object. + const updatedTransformDefaultsObjectString = JSON.stringify( + config.transformDefaults, + null, + 2 + ); + + // Reconstruct the new transformDefaults string to be inserted back into the file + const newTransformDefaultsString = + match[1] + updatedTransformDefaultsObjectString; + + // Replace the old transformDefaults string with the new one in the file content + fileContent = fileContent.replace( + originalTransformDefaultsString, + newTransformDefaultsString + ); + } else { + // If the 'transformDefaults' block is not found, we'll throw an error. + // A more complete solution would involve adding the block if it's missing, + // but that significantly increases complexity and fragility with string manipulation. + throw new Error( + 'Could not find transformDefaults block in config file. Please ensure it exists and is correctly formatted.' + ); + } + + fs.writeFileSync(absPath, fileContent); console.log(`Updated ${transformName}.descriptions in ${configPath}`); }