Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add JSDoc with default values to nb.ts #3479

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions @navikt/core/react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scripts/*.backup
7 changes: 5 additions & 2 deletions @navikt/core/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,11 @@
"./package.json": "./package.json"
},
"scripts": {
"docgen": "yarn tsx ../../../scripts/docgen.ts",
"docgen": "tsx ../../../scripts/docgen.ts",
"i18n-jsdoc": "tsx ./scripts/addJsdocToLocales.ts",
"write-packagejson": "echo '{\"type\": \"module\"}' > esm/package.json",
"clean": "rimraf cjs esm",
"build": "concurrently \"tsc -p tsconfig.build.json\" \"tsc -p tsconfig.esm.json && tsc-alias -p tsconfig.esm.json && yarn write-packagejson\" ",
"build": "yarn i18n-jsdoc && concurrently \"tsc -p tsconfig.build.json\" \"tsc -p tsconfig.esm.json && tsc-alias -p tsconfig.esm.json && yarn write-packagejson\" && yarn i18n-jsdoc --cleanup",
"watch": "tsc --watch -p tsconfig.esm.json",
"test": "TZ=UTC vitest run -c tests/vitest.config.ts",
"test:watch": "vitest watch"
Expand All @@ -625,8 +626,10 @@
"@testing-library/jest-dom": "^5.16.0",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.2.0",
"@types/jscodeshift": "^0.11.11",
"concurrently": "9.0.1",
"fast-glob": "3.2.11",
"jscodeshift": "^0.15.1",
"jsdom": "24.0.0",
"react-dom": "^18.0.0",
"react-router-dom": "^6.3.0",
Expand Down
59 changes: 59 additions & 0 deletions @navikt/core/react/scripts/addJsdocToLocales.ts
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();
}
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3815,10 +3815,12 @@ __metadata:
"@testing-library/jest-dom": "npm:^5.16.0"
"@testing-library/react": "npm:^15.0.7"
"@testing-library/user-event": "npm:^14.2.0"
"@types/jscodeshift": "npm:^0.11.11"
clsx: "npm:^2.1.0"
concurrently: "npm:9.0.1"
date-fns: "npm:^3.0.0"
fast-glob: "npm:3.2.11"
jscodeshift: "npm:^0.15.1"
jsdom: "npm:24.0.0"
react-day-picker: "npm:8.10.1"
react-dom: "npm:^18.0.0"
Expand Down
Loading