From 2bade6153aad7cb05b981900894b29a4dde4a069 Mon Sep 17 00:00:00 2001 From: Kevin Ghadyani Date: Wed, 31 Jan 2024 15:03:37 -0600 Subject: [PATCH] build: updates properties-to-ts to use TypeScript --- lerna.json | 4 +- packages/odyssey-react-mui/package.json | 1 - ...roperties-to-ts.js => properties-to-ts.ts} | 79 +++++++++++-------- .../src/@types/properties.d.ts | 40 ++++++++++ .../src/icons.generated/index.ts | 3 +- 5 files changed, 91 insertions(+), 36 deletions(-) rename packages/odyssey-react-mui/scripts/{properties-to-ts.js => properties-to-ts.ts} (61%) create mode 100644 packages/odyssey-react-mui/src/@types/properties.d.ts diff --git a/lerna.json b/lerna.json index 3e40ddf5f9..46590e4c1c 100644 --- a/lerna.json +++ b/lerna.json @@ -2,9 +2,7 @@ "$schema": "node_modules/lerna/schemas/lerna-schema.json", "changelogPreset": "@okta/conventional-changelog-odyssey", "npmClient": "yarn", - "packages": [ - "packages/*" - ], + "packages": ["packages/*"], "useNx": false, "version": "1.12.2" } diff --git a/packages/odyssey-react-mui/package.json b/packages/odyssey-react-mui/package.json index 5535e36044..d2d71b7d42 100644 --- a/packages/odyssey-react-mui/package.json +++ b/packages/odyssey-react-mui/package.json @@ -96,7 +96,6 @@ "@types/jest-axe": "^3.5.1", "@types/react": "^17.0.30", "@types/react-dom": "^17.0.5", - "@types/yargs": "^17", "babel-plugin-import": "^1.13.5", "eslint": "^8.44.0", "jest": "^29.6.1", diff --git a/packages/odyssey-react-mui/scripts/properties-to-ts.js b/packages/odyssey-react-mui/scripts/properties-to-ts.ts similarity index 61% rename from packages/odyssey-react-mui/scripts/properties-to-ts.js rename to packages/odyssey-react-mui/scripts/properties-to-ts.ts index 0ae3724818..359b3bb855 100644 --- a/packages/odyssey-react-mui/scripts/properties-to-ts.js +++ b/packages/odyssey-react-mui/scripts/properties-to-ts.ts @@ -1,3 +1,4 @@ +#!/usr/bin/env node /*! * Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved. * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") @@ -10,29 +11,29 @@ * See the License for the specific language governing permissions and limitations under the License. */ -// Part of this has been copied over from @okta/ui-build-tools' own internal node script +// This was originally copied over from @okta/ui-build-tools' own internal node script: // https://github.com/okta/ui-build-tools/blob/main/packages/clis/i18n/properties-to-json.js -const { resolve, join, basename, extname } = require("node:path"); -const { - readFileSync, - writeFileSync, - rmSync, +import { basename, extname, join, resolve } from "node:path"; +import { existsSync, mkdirSync, -} = require("node:fs"); -const properties = require("properties"); -const readdir = require("recursive-readdir"); -const yargs = require("yargs"); -const { hideBin } = require("yargs/helpers"); + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { readdir } from "node:fs/promises"; +import properties from "properties"; +import { hideBin } from "yargs/helpers"; +import yargs from "yargs/yargs"; -const convert = (baseFiles, propertiesTargetDir) => { - for (const src of baseFiles) { +const convert = (baseFiles: string[], propertiesTargetDir: string) => { + baseFiles.forEach((src) => { const filename = basename(src); const extension = extname(src); const source = `${readFileSync(src)}`; - properties.parse(source, function (error, obj) { + properties.parse(source, function (error, propertiesJson) { if (error) { return console.error(error); } @@ -41,55 +42,71 @@ const convert = (baseFiles, propertiesTargetDir) => { propertiesTargetDir, filename.replace(extension, ".ts") ); + writeFileSync( targetFile, - `export const translation = ${JSON.stringify(obj)};` + `export const translation = ${JSON.stringify(propertiesJson)};` ); }); - } + }); }; -async function convertPropertiesToJson({ resourcePath, targetJsonPath }) { +async function convertPropertiesToJson({ + resourcePath, + targetJsonPath, +}: { + resourcePath: string; + targetJsonPath: string; +}) { const sourceDirectory = resolve(resourcePath); const propertiesTargetDirectory = resolve(targetJsonPath); if (!existsSync(sourceDirectory)) { mkdirSync(sourceDirectory); } + if (existsSync(propertiesTargetDirectory)) { rmSync(propertiesTargetDirectory, { recursive: true, force: true }); } + mkdirSync(propertiesTargetDirectory); - let baseFiles = await readdir(sourceDirectory); - convert(baseFiles, propertiesTargetDirectory); + const propertiesFilePaths = await readdir(sourceDirectory, { + recursive: true, + }); + + convert( + propertiesFilePaths + .filter((propertiesFilePath) => + propertiesFilePath.endsWith(".properties") + ) + .map((propertiesFilePath) => join(sourceDirectory, propertiesFilePath)), + propertiesTargetDirectory + ); } yargs(hideBin(process.argv)) .scriptName("properties-to-ts") .usage("$0 [args]") .command( - "bundle", - "Converts properties file to ts", - (yargs) => { + "bundle [resourcePath] [targetJsonPath]", + "Converts `properties` files to TypeScript types.", + (yargs) => yargs .positional("resourcePath", { - type: "string", default: "src/properties", describe: "A relative path to resources based on cwd.", + type: "string", }) .positional("targetJsonPath", { - type: "string", default: "src/properties/ts", describe: "A relative path to directory for ts file output", - }); - }, - function (argv) { - const { resourcePath, targetJsonPath } = argv; - + type: "string", + }), + (argv) => { convertPropertiesToJson({ - resourcePath, - targetJsonPath, + resourcePath: argv.resourcePath, + targetJsonPath: argv.targetJsonPath, }); } ) diff --git a/packages/odyssey-react-mui/src/@types/properties.d.ts b/packages/odyssey-react-mui/src/@types/properties.d.ts new file mode 100644 index 0000000000..faac11e76b --- /dev/null +++ b/packages/odyssey-react-mui/src/@types/properties.d.ts @@ -0,0 +1,40 @@ +/*! + * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved. + * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") + * + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and limitations under the License. + */ + +declare module "properties" { + type ParseCallback = (error: Error, propertiesJson: object) => void; + + type StringifyCallback = (error: Error, propertiesString: string) => void; + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + function createStringifier(): any; + + function parse( + data: string, + options: object | ParseCallback, + cb?: ParseCallback + ): void; + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + function stringify( + stringifier: string, + options: object | StringifyCallback, + cb?: StringifyCallback + ): void; + + // eslint-disable-next-line import/no-default-export + export default { + createStringifier, + parse, + stringify, + }; +} diff --git a/packages/odyssey-react-mui/src/icons.generated/index.ts b/packages/odyssey-react-mui/src/icons.generated/index.ts index fd4705cfa2..519c2a9742 100644 --- a/packages/odyssey-react-mui/src/icons.generated/index.ts +++ b/packages/odyssey-react-mui/src/icons.generated/index.ts @@ -1,5 +1,5 @@ /*! - * Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved. + * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved. * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") * * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. @@ -87,3 +87,4 @@ export * from "./User"; export * from "./Video"; export * from "./Warning"; export * from "./WarningFilled"; +export * from "./index";