diff --git a/@navikt/core/react/.gitignore b/@navikt/core/react/.gitignore new file mode 100644 index 0000000000..86ad5d77f4 --- /dev/null +++ b/@navikt/core/react/.gitignore @@ -0,0 +1 @@ +scripts/*.backup \ No newline at end of file diff --git a/@navikt/core/react/package.json b/@navikt/core/react/package.json index 9e1d443b22..bc7504ec29 100644 --- a/@navikt/core/react/package.json +++ b/@navikt/core/react/package.json @@ -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" @@ -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", diff --git a/@navikt/core/react/scripts/addJsdocToLocales.ts b/@navikt/core/react/scripts/addJsdocToLocales.ts new file mode 100644 index 0000000000..f96699cc04 --- /dev/null +++ b/@navikt/core/react/scripts/addJsdocToLocales.ts @@ -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(); +} diff --git a/yarn.lock b/yarn.lock index 0ed03eb3e4..10e0a79714 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"