diff --git a/package.json b/package.json index 6c67356c476..95df8f08268 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "changelog:update": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run changelog:update", "changelog:validate": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run changelog:validate", "create-package": "tsx scripts/create-package", - "generate-method-action-types": "tsx scripts/generate-method-action-types.ts", + "generate-method-action-types": "yarn workspaces foreach --all --parallel --interlaced --verbose run generate-method-action-types", "lint": "yarn lint:eslint && echo && yarn lint:misc --check && yarn constraints && yarn lint:dependencies && yarn lint:teams && yarn generate-method-action-types --check", "lint:dependencies": "depcheck && yarn dedupe --check", "lint:dependencies:fix": "depcheck && yarn dedupe", diff --git a/packages/analytics-controller/package.json b/packages/analytics-controller/package.json index 822a2043028..16c857b8017 100644 --- a/packages/analytics-controller/package.json +++ b/packages/analytics-controller/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/analytics-controller", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/analytics-controller", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -59,6 +60,7 @@ "deepmerge": "^4.2.2", "jest": "^29.7.0", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/analytics-data-regulation-controller/package.json b/packages/analytics-data-regulation-controller/package.json index 68428c7b58f..5f5b90a5b0b 100644 --- a/packages/analytics-data-regulation-controller/package.json +++ b/packages/analytics-data-regulation-controller/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/analytics-data-regulation-controller", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/analytics-data-regulation-controller", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -61,6 +62,7 @@ "jest": "^29.7.0", "nock": "^13.3.1", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/assets-controller/package.json b/packages/assets-controller/package.json index 4b4db942e33..68d530fd148 100644 --- a/packages/assets-controller/package.json +++ b/packages/assets-controller/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/assets-controller", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/assets-controller", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -82,6 +83,7 @@ "deepmerge": "^4.2.2", "jest": "^29.7.0", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/client-controller/package.json b/packages/client-controller/package.json index a93784be46d..c288fddcf7e 100644 --- a/packages/client-controller/package.json +++ b/packages/client-controller/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/client-controller", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/client-controller", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -59,6 +60,7 @@ "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/compliance-controller/package.json b/packages/compliance-controller/package.json index 7c534c32ffd..fd1e411c058 100644 --- a/packages/compliance-controller/package.json +++ b/packages/compliance-controller/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/compliance-controller", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/compliance-controller", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -63,6 +64,7 @@ "jest": "^29.7.0", "nock": "^13.3.1", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/connectivity-controller/package.json b/packages/connectivity-controller/package.json index e8d76a0f4ad..87b15ab337e 100644 --- a/packages/connectivity-controller/package.json +++ b/packages/connectivity-controller/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/connectivity-controller", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/connectivity-controller", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -58,6 +59,7 @@ "deepmerge": "^4.2.2", "jest": "^29.7.0", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/core-backend/package.json b/packages/core-backend/package.json index fe2ddecc23c..fa64608fb9a 100644 --- a/packages/core-backend/package.json +++ b/packages/core-backend/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/core-backend", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/core-backend", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -65,6 +66,7 @@ "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/profile-metrics-controller/package.json b/packages/profile-metrics-controller/package.json index 19911979d38..42f1eb42d43 100644 --- a/packages/profile-metrics-controller/package.json +++ b/packages/profile-metrics-controller/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/profile-metrics-controller", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/profile-metrics-controller", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -68,6 +69,7 @@ "jest": "^29.7.0", "nock": "^13.3.1", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/ramps-controller/package.json b/packages/ramps-controller/package.json index da9f373834e..f91b8d64c00 100644 --- a/packages/ramps-controller/package.json +++ b/packages/ramps-controller/package.json @@ -41,6 +41,7 @@ "changelog:update": "../../scripts/update-changelog.sh @metamask/ramps-controller", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/ramps-controller", "dev": "node dev-watch.js", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -61,6 +62,7 @@ "jest": "^29.7.0", "nock": "^13.3.1", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/sample-controllers/package.json b/packages/sample-controllers/package.json index eca34b378f6..d731f96448e 100644 --- a/packages/sample-controllers/package.json +++ b/packages/sample-controllers/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/sample-controllers", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/sample-controllers", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -62,6 +63,7 @@ "jest": "^29.7.0", "nock": "^13.3.1", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/packages/storage-service/package.json b/packages/storage-service/package.json index 16ed3d774ee..e5d74798e41 100644 --- a/packages/storage-service/package.json +++ b/packages/storage-service/package.json @@ -40,6 +40,7 @@ "build:docs": "typedoc", "changelog:update": "../../scripts/update-changelog.sh @metamask/storage-service", "changelog:validate": "../../scripts/validate-changelog.sh @metamask/storage-service", + "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts", "publish:preview": "yarn npm publish --tag preview", "since-latest-release": "../../scripts/since-latest-release.sh", "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", @@ -58,6 +59,7 @@ "deepmerge": "^4.2.2", "jest": "^29.7.0", "ts-jest": "^29.2.5", + "tsx": "^4.20.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", "typescript": "~5.3.3" diff --git a/scripts/generate-method-action-types.ts b/scripts/generate-method-action-types.ts index 82939606b01..ff4d59a5cfd 100755 --- a/scripts/generate-method-action-types.ts +++ b/scripts/generate-method-action-types.ts @@ -32,6 +32,10 @@ type CommandLineArguments = { * Whether to fix the action types files. */ fix: boolean; + /** + * Optional path to a specific controller to process. + */ + controllerPath: string; }; /** @@ -40,7 +44,22 @@ type CommandLineArguments = { * @returns The command line arguments. */ async function parseCommandLineArguments(): Promise { - const { check, fix } = await yargs(process.argv.slice(2)) + const { + check, + fix, + path: controllerPath, + } = await yargs(process.argv.slice(2)) + .command( + '$0 [path]', + 'Generate method action types for a controller messenger', + (yargsInstance) => { + yargsInstance.positional('path', { + type: 'string', + description: 'Path to the folder where controllers are located', + default: 'src', + }); + }, + ) .option('check', { type: 'boolean', description: 'Check if generated action type files are up to date', @@ -59,7 +78,12 @@ async function parseCommandLineArguments(): Promise { return true; }).argv; - return { check, fix }; + return { + check, + fix, + // TypeScript doesn't narrow the type of `controllerPath` even though we defined it as a string in yargs, so we need to cast it here. + controllerPath: controllerPath as string, + }; } /** @@ -181,11 +205,11 @@ async function checkActionTypesFiles( * Main entry point for the script. */ async function main(): Promise { - const { fix } = await parseCommandLineArguments(); + const { fix, controllerPath } = await parseCommandLineArguments(); console.log('🔍 Searching for controllers with MESSENGER_EXPOSED_METHODS...'); - const controllers = await findControllersWithExposedMethods(); + const controllers = await findControllersWithExposedMethods(controllerPath); if (controllers.length === 0) { console.log('⚠️ No controllers found with MESSENGER_EXPOSED_METHODS'); @@ -237,43 +261,33 @@ async function isDirectory(pathValue: string): Promise { /** * Finds all controller files that have MESSENGER_EXPOSED_METHODS constants. * + * @param controllerPath - Path to the folder where controllers are located. * @returns A list of controller information objects. */ -async function findControllersWithExposedMethods(): Promise { - const packagesDir = path.resolve(__dirname, '../packages'); +async function findControllersWithExposedMethods( + controllerPath: string, +): Promise { + const srcPath = path.resolve(process.cwd(), controllerPath); const controllers: ControllerInfo[] = []; - const packageDirs = await fs.promises.readdir(packagesDir, { - withFileTypes: true, - }); - - for (const packageDir of packageDirs) { - if (!packageDir.isDirectory()) { - continue; - } + if (!(await isDirectory(srcPath))) { + throw new Error(`The specified path is not a directory: ${srcPath}`); + } - const packagePath = path.join(packagesDir, packageDir.name); - const srcPath = path.join(packagePath, 'src'); + const srcFiles = await fs.promises.readdir(srcPath); - if (!(await isDirectory(srcPath))) { + for (const file of srcFiles) { + if (!file.endsWith('.ts') || file.endsWith('.test.ts')) { continue; } - const srcFiles = await fs.promises.readdir(srcPath); - - for (const file of srcFiles) { - if (!file.endsWith('.ts') || file.endsWith('.test.ts')) { - continue; - } - - const filePath = path.join(srcPath, file); - const content = await fs.promises.readFile(filePath, 'utf8'); + const filePath = path.join(srcPath, file); + const content = await fs.promises.readFile(filePath, 'utf8'); - if (content.includes('MESSENGER_EXPOSED_METHODS')) { - const controllerInfo = await parseControllerFile(filePath); - if (controllerInfo) { - controllers.push(controllerInfo); - } + if (content.includes('MESSENGER_EXPOSED_METHODS')) { + const controllerInfo = await parseControllerFile(filePath); + if (controllerInfo) { + controllers.push(controllerInfo); } } } diff --git a/yarn.lock b/yarn.lock index f4a33091a01..8c93c138e4b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2415,6 +2415,7 @@ __metadata: deepmerge: "npm:^4.2.2" jest: "npm:^29.7.0" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -2436,6 +2437,7 @@ __metadata: jest: "npm:^29.7.0" nock: "npm:^13.3.1" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -2542,6 +2544,7 @@ __metadata: jest: "npm:^29.7.0" lodash: "npm:^4.17.21" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -2871,6 +2874,7 @@ __metadata: jest: "npm:^29.7.0" jest-environment-jsdom: "npm:^29.7.0" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -2894,6 +2898,7 @@ __metadata: nock: "npm:^13.3.1" reselect: "npm:^5.1.1" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -2932,6 +2937,7 @@ __metadata: deepmerge: "npm:^4.2.2" jest: "npm:^29.7.0" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -3012,6 +3018,7 @@ __metadata: jest: "npm:^29.7.0" jest-environment-jsdom: "npm:^29.7.0" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -4488,6 +4495,7 @@ __metadata: jest: "npm:^29.7.0" nock: "npm:^13.3.1" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -4570,6 +4578,7 @@ __metadata: jest: "npm:^29.7.0" nock: "npm:^13.3.1" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -4652,6 +4661,7 @@ __metadata: jest: "npm:^29.7.0" nock: "npm:^13.3.1" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3" @@ -4960,6 +4970,7 @@ __metadata: deepmerge: "npm:^4.2.2" jest: "npm:^29.7.0" ts-jest: "npm:^29.2.5" + tsx: "npm:^4.20.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" typescript: "npm:~5.3.3"