-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a
fix
option for autofixing (#134)
- Loading branch information
Showing
11 changed files
with
241 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
import { getObjectValueByPath } from './utils.js'; | ||
|
||
/** | ||
* @param {string} content | ||
* @returns {string} | ||
*/ | ||
function getIndent (content) { | ||
const indentMatch = content.match(/^[\t ]+/m); | ||
|
||
return indentMatch ? indentMatch[0] : ' '; | ||
} | ||
|
||
/** | ||
* @typedef Fix | ||
* @property {string} childKey | ||
* @property {string} suggested | ||
* @property {string} topKey | ||
*/ | ||
|
||
/** | ||
* @param {string} cwd | ||
* @param {Fix[]} fixes | ||
* @returns {Promise<string[]>} | ||
*/ | ||
export async function fixPkg (cwd, fixes) { | ||
/** @type {string[]} */ | ||
const failures = []; | ||
|
||
if (fixes.length === 0) { | ||
return failures; | ||
} | ||
|
||
const pkgPath = path.join(cwd, 'package.json'); | ||
|
||
// eslint-disable-next-line security/detect-non-literal-fs-filename | ||
const raw = await readFile(pkgPath, { encoding: 'utf8' }); | ||
|
||
const indent = getIndent(raw); | ||
|
||
const data = JSON.parse(raw); | ||
|
||
for (const { childKey, suggested, topKey } of fixes) { | ||
const collection = getObjectValueByPath(data, topKey, true); | ||
|
||
if (!collection) { | ||
failures.push(`Failed to fix "${topKey}.${childKey}". Not an object at "${topKey}".`); | ||
continue; | ||
} else if (childKey === '__proto__' || childKey === 'constructor' || childKey === 'prototype') { | ||
failures.push(`Do not include "${childKey}" in your path`); | ||
continue; | ||
} | ||
|
||
collection[childKey] = suggested; | ||
} | ||
|
||
const newRaw = JSON.stringify(data, undefined, indent) + '\n'; | ||
|
||
// eslint-disable-next-line security/detect-non-literal-fs-filename | ||
await writeFile(pkgPath, newRaw); | ||
|
||
return failures; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.