-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically add JSDoc with default values to nb.ts (#3479)
* Automatically add JSDoc with default values to nb.ts * Added jscodeshift to devDeps * 🐛 Use same jscodeshift versions * 🐛 Use same types for jscodeshift --------- Co-authored-by: Ken <ken.aleksander@gmail.com>
- Loading branch information
1 parent
1fb7224
commit 137b4c4
Showing
4 changed files
with
67 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
scripts/*.backup |
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,59 @@ | ||
import fs from "fs"; | ||
import jscodeshift from "jscodeshift"; | ||
|
||
const j = jscodeshift.withParser("ts"); | ||
const file = "src/util/i18n/locales/nb.ts"; | ||
const backup = "scripts/nb.backup"; | ||
|
||
function addJsdoc() { | ||
if (fs.existsSync(backup)) { | ||
// If last build failed, we need to restore the file so that we don't create duplicate JSDoc | ||
console.info( | ||
`WARNING: Restoring backup of ${file} - Uncommitted changes will be lost!`, | ||
); | ||
fs.copyFileSync(backup, file); | ||
} | ||
|
||
fs.copyFileSync(file, backup); // Create a copy of the original file that we can restore afterwards | ||
|
||
const code = fs.readFileSync(file, "utf-8"); | ||
const parsedCode = j(code); | ||
const defaultExport = parsedCode.find(j.ExportDefaultDeclaration); | ||
const keys = defaultExport.find(j.ObjectProperty); | ||
keys | ||
.filter((key) => key.value.value.type === "StringLiteral") | ||
.forEach((key) => { | ||
// @ts-expect-error It works, doesn't it? | ||
const value = key.value.value.value; | ||
const foundCommentBlock = key.value.comments?.find( | ||
(comment) => comment.type === "CommentBlock", | ||
); | ||
if (foundCommentBlock) { | ||
foundCommentBlock.value = `${foundCommentBlock.value}\n* @default "${value}" `; | ||
} else { | ||
key.value.comments = [ | ||
...(key.value.comments || []), | ||
{ | ||
type: "CommentBlock", | ||
value: `* @default "${value}" `, | ||
leading: true, | ||
trailing: false, | ||
}, | ||
]; | ||
} | ||
}); | ||
|
||
const modifiedCode = parsedCode.toSource(); | ||
fs.writeFileSync(file, modifiedCode); | ||
} | ||
|
||
function cleanup() { | ||
fs.copyFileSync(backup, file); | ||
fs.unlinkSync(backup); | ||
} | ||
|
||
if (process.argv.includes("--cleanup")) { | ||
cleanup(); | ||
} else { | ||
addJsdoc(); | ||
} |
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