Skip to content

Commit 0c5e5fb

Browse files
authored
Merge pull request #21 from ioncakephper:ioncakephper/issue20
[BUG] transform entry emptied in markdown-magic configuration file
2 parents 579188a + 09e0405 commit 0c5e5fb

File tree

6 files changed

+103
-56
lines changed

6 files changed

+103
-56
lines changed

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "Launch Config Program",
9+
"program": "${workspaceFolder}\\src\\cli.js",
10+
"request": "launch",
11+
"skipFiles": ["<node_internals>/**"],
12+
"type": "node",
13+
"args": [
14+
".",
15+
"--update-config",
16+
"md.config.js",
17+
"--transform-name",
18+
"fileTreeExtended"
19+
]
20+
},
721
{
822
"name": "Launch Programmatic Program",
923
"program": "${workspaceFolder}\\example\\programmatic-use.js",

README.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -179,49 +179,49 @@ Options:
179179
180180
<!-- doc-gen COMMANDS format=list -->
181181
182-
- `describe` — Update repository descriptions in md.config.js transformDefaults for fileTreeExtended. (line [93](./package.json#L93))
182+
- `describe` — Update repository descriptions in md.config.js transformDefaults for fileTreeExtended. (line [92](./package.json#L92))
183183
184184
```bash
185185
node src/cli.js . --update-config md.config.js --transform-name fileTreeExtended
186186
```
187187

188-
- `describe:file` — Generates AI-powered descriptions for repository files and outputs them in various formats. (line [87](./package.json#L87))
188+
- `describe:file` — Generates AI-powered descriptions for repository files and outputs them in various formats. (line [86](./package.json#L86))
189189

190190
```bash
191-
node src/cli.js . --output descriptions.json
191+
node src/cli.js . --output _descriptions.json
192192
```
193193

194-
- `docs` — Generates documentation by processing Markdown files with markdown-magic. (line [92](./package.json#L92))
194+
- `docs` — Generates documentation by processing Markdown files with markdown-magic. (line [91](./package.json#L91))
195195

196196
```bash
197197
npx [email protected] **/*.md -c md.config.js
198198
```
199199

200-
- `format` — Formats the codebase using Prettier. (line [90](./package.json#L90))
200+
- `format` — Formats the codebase using Prettier. (line [89](./package.json#L89))
201201

202202
```bash
203203
prettier --write .
204204
```
205205

206-
- `lint` — Lints the codebase for potential errors and style violations. (line [88](./package.json#L88))
206+
- `lint` — Lints the codebase for potential errors and style violations. (line [87](./package.json#L87))
207207

208208
```bash
209209
eslint src/ **/*.js **/*.json
210210
```
211211

212-
- `lint:fix` — Lints the codebase and automatically fixes fixable issues. (line [89](./package.json#L89))
212+
- `lint:fix` — Lints the codebase and automatically fixes fixable issues. (line [88](./package.json#L88))
213213

214214
```bash
215215
eslint --fix src/ **/*.js **/*.json
216216
```
217217

218-
- `prep` — Prepares the codebase by generating documentation, linting, and formatting. (line [91](./package.json#L91))
218+
- `prep` — Prepares the codebase by generating documentation, linting, and formatting. (line [90](./package.json#L90))
219219

220220
```bash
221-
npm run docs && npm run lint:fix && npm run format
221+
npm run describe:file && npm run docs && npm run lint:fix && npm run format
222222
```
223223

224-
- `test` — Runs the test suite using Jest. (line [86](./package.json#L86))
224+
- `test` — Runs the test suite using Jest. (line [85](./package.json#L85))
225225

226226
```bash
227227
jest --passWithNoTests
@@ -266,7 +266,7 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
266266

267267
## Project Structure
268268

269-
<!-- doc-gen fileTreeExtended showSize=false showDescriptions=true descriptionsFile=descriptions.json -->
269+
<!-- doc-gen fileTreeExtended showSize=false showDescriptions=true descriptionsFile=_descriptions.json -->
270270

271271
```
272272
repo-description/
@@ -281,15 +281,16 @@ repo-description/
281281
│ ├── describe.js # Description unavailable.
282282
│ ├── index.js # Description unavailable.
283283
│ └── utils.js # Description unavailable.
284-
├── .env # Configure... the environment by setting the `GROQ_API_KEY` variable to provide authentication for Groq API access.
284+
├── _descriptions.json
285+
├── .env # Defines the GROQ_API_KEY environment variable for authentication.
285286
├── .gitignore
286-
├── .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.
287-
├── babel.config.js # [configure] Babel to use @babel/preset‑env with the target set to the current Node version…
288-
├── 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.
289-
├── CONTRIBUTING.md # Guide contributors on how to report bugs, suggest enhancements, and submit code via pull requests.
290-
├── 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.
291-
├── LICENSE # Granting permission to use, copy, modify, merge, publish, distribute, sublicense, and sell the software freely under the MIT License.
292-
├── 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.
287+
├── .prettierrc.json # Description unavailable.
288+
├── babel.config.js # Description unavailable.
289+
├── CHANGELOG.md # Description unavailable.
290+
├── CONTRIBUTING.md # Description unavailable.
291+
├── eslint.config.js # Description unavailable.
292+
├── LICENSE # Description unavailable.
293+
├── md.config.js # Description unavailable.
293294
├── package-lock.json # Description unavailable.
294295
├── package.json # Description unavailable.
295296
├── README.md # Description unavailable.

_descriptions.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
".env": "Defines the GROQ_API_KEY environment variable for authentication.",
3+
".prettierrc.json": "Description unavailable.",
4+
".vscode/launch.json": "Description unavailable.",
5+
"babel.config.js": "Description unavailable.",
6+
"CHANGELOG.md": "Description unavailable.",
7+
"CONTRIBUTING.md": "Description unavailable.",
8+
"eslint.config.js": "Description unavailable.",
9+
"LICENSE": "Description unavailable.",
10+
"md.config.js": "Description unavailable.",
11+
"package-lock.json": "Description unavailable.",
12+
"package.json": "Description unavailable.",
13+
"README.md": "Description unavailable.",
14+
"RULES_OF_CONDUCT.md": "Description unavailable.",
15+
"src/cli.js": "Description unavailable.",
16+
"src/describe.js": "Description unavailable.",
17+
"src/index.js": "Description unavailable.",
18+
"src/utils.js": "Description unavailable.",
19+
"__tests__/cli.test.js": "Description unavailable."
20+
}

md.config.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,5 @@
11
module.exports = {
22
transformDefaults: {
3-
fileTreeExtended: {
4-
descriptions: {
5-
'.env':
6-
'Configure... the environment by setting the `GROQ_API_KEY` variable to provide authentication for Groq API access.',
7-
'.prettierrc.json':
8-
'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.',
9-
'.vscode/launch.json':
10-
'Configure launch configurations for debugging Node.js programs in VS Code.',
11-
'babel.config.js':
12-
'[configure] Babel to use @babel/preset‑env with the target set to the current Node version…',
13-
'CHANGELOG.md':
14-
"Documenting the project's version history by listing each release’s features, bug fixes, and related metadata in a Keep‑a‑Changelog format.",
15-
'CONTRIBUTING.md':
16-
'Guide contributors on how to report bugs, suggest enhancements, and submit code via pull requests.',
17-
'eslint.config.js':
18-
'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.',
19-
LICENSE:
20-
'Granting permission to use, copy, modify, merge, publish, distribute, sublicense, and sell the software freely under the MIT License.',
21-
'md.config.js':
22-
'[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.',
23-
'package-lock.json': 'Description unavailable.',
24-
'package.json': 'Description unavailable.',
25-
'README.md': 'Description unavailable.',
26-
'RULES_OF_CONDUCT.md': 'Description unavailable.',
27-
'src/cli.js': 'Description unavailable.',
28-
'src/describe.js': 'Description unavailable.',
29-
'src/index.js': 'Description unavailable.',
30-
'src/utils.js': 'Description unavailable.',
31-
'__tests__/cli.test.js': 'Description unavailable.',
32-
},
33-
},
343
BADGES: {
354
style: 'for-the-badge',
365
},

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
4343
],
4444
"scripts": {
4545
"describe": "node src/cli.js . --update-config md.config.js --transform-name fileTreeExtended",
46-
"describe:file": "node src/cli.js . --output descriptions.json",
46+
"describe:file": "node src/cli.js . --output _descriptions.json",
4747
"docs": "npx [email protected] **/*.md -c md.config.js",
4848
"format": "prettier --write .",
4949
"lint": "eslint src/ **/*.js **/*.json",
5050
"lint:fix": "eslint --fix src/ **/*.js **/*.json",
51-
"prep": "npm run docs && npm run lint:fix && npm run format",
51+
"prep": "npm run describe:file && npm run docs && npm run lint:fix && npm run format",
5252
"test": "jest --passWithNoTests"
5353
},
5454
"jest": {

src/describe.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,63 @@ async function updateMarkdownMagicConfig(
144144
throw new Error(`Config file not found: ${absPath}`);
145145
}
146146

147-
const config = require(absPath);
147+
let fileContent = fs.readFileSync(absPath, 'utf8');
148148

149+
// Use require to get the current config object for in-memory manipulation.
150+
// This allows us to easily update the descriptions.
151+
let config = require(absPath);
152+
153+
// Ensure transformDefaults and the specific transformName exist in the in-memory config
149154
if (!config.transformDefaults) {
150155
config.transformDefaults = {};
151156
}
152157
if (!config.transformDefaults[transformName]) {
153158
config.transformDefaults[transformName] = {};
154159
}
155160

161+
// Update the descriptions in the in-memory config object
156162
config.transformDefaults[transformName].descriptions = descriptions;
157163

158-
// Write back to file
159-
const newConfig = `module.exports = ${JSON.stringify(config, null, 2)};\n`;
160-
fs.writeFileSync(absPath, newConfig);
164+
// Now, we need to update the file content string.
165+
// We will find the 'transformDefaults' property and replace its value.
166+
// This regex is designed to find 'transformDefaults:' followed by its object literal.
167+
// It's still somewhat fragile and relies on consistent formatting.
168+
const transformDefaultsRegex = /(transformDefaults:\s*)(\{[\s\S]*?\})/m;
169+
const match = fileContent.match(transformDefaultsRegex);
170+
171+
if (match) {
172+
// The original string for transformDefaults (including the key and colon)
173+
const originalTransformDefaultsString = match[0];
174+
// The object literal part of transformDefaults
175+
// const originalTransformDefaultsObjectString = match[2]; // Not directly used for replacement
176+
177+
// Stringify only the updated transformDefaults from the in-memory config.
178+
// This is safe because 'descriptions' is expected to be a JSON-serializable object.
179+
const updatedTransformDefaultsObjectString = JSON.stringify(
180+
config.transformDefaults,
181+
null,
182+
2
183+
);
184+
185+
// Reconstruct the new transformDefaults string to be inserted back into the file
186+
const newTransformDefaultsString =
187+
match[1] + updatedTransformDefaultsObjectString;
188+
189+
// Replace the old transformDefaults string with the new one in the file content
190+
fileContent = fileContent.replace(
191+
originalTransformDefaultsString,
192+
newTransformDefaultsString
193+
);
194+
} else {
195+
// If the 'transformDefaults' block is not found, we'll throw an error.
196+
// A more complete solution would involve adding the block if it's missing,
197+
// but that significantly increases complexity and fragility with string manipulation.
198+
throw new Error(
199+
'Could not find transformDefaults block in config file. Please ensure it exists and is correctly formatted.'
200+
);
201+
}
202+
203+
fs.writeFileSync(absPath, fileContent);
161204
console.log(`Updated ${transformName}.descriptions in ${configPath}`);
162205
}
163206

0 commit comments

Comments
 (0)