From c8722047ddd92a24030cd23b7ebe27cb39955978 Mon Sep 17 00:00:00 2001 From: UnrefinedBrain Date: Mon, 30 Sep 2024 00:16:59 +0000 Subject: [PATCH 1/2] add --list-plugins and --plugins options to cli --- package.json | 2 ++ pnpm-lock.yaml | 26 ++++++++++++++++++++++---- src/cli.ts | 18 ++++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index e6fc04f..4dd9503 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "@types/cli-progress": "^3.11.5", "@types/deep-diff": "^1.0.5", "@types/lodash-es": "^4.17.12", + "@types/micromatch": "^4.0.9", "@types/node": "^22.0.0", "@typescript-eslint/eslint-plugin": "^7.13.1", "@typescript-eslint/parser": "^7.13.1", @@ -77,6 +78,7 @@ "glob": "^11.0.0", "lodash-es": "^4.17.21", "magic-string": "^0.30.10", + "micromatch": "^4.0.8", "node-html-parser": "^6.1.13", "postcss": "^8.4.38", "postcss-less": "^6.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8d0740a..4c297cb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ importers: magic-string: specifier: ^0.30.10 version: 0.30.11 + micromatch: + specifier: ^4.0.8 + version: 4.0.8 node-html-parser: specifier: ^6.1.13 version: 6.1.13 @@ -87,6 +90,9 @@ importers: '@types/lodash-es': specifier: ^4.17.12 version: 4.17.12 + '@types/micromatch': + specifier: ^4.0.9 + version: 4.0.9 '@types/node': specifier: ^22.0.0 version: 22.7.3 @@ -763,6 +769,9 @@ packages: '@types/argparse@1.0.38': resolution: {integrity: sha512-ebDJ9b0e702Yr7pWgB0jzm+CX4Srzz8RcXtLJDJB+BSccqMa36uyH/zUsSYao5+BD1ytv3k3rPYCq4mAE1hsXA==} + '@types/braces@3.0.4': + resolution: {integrity: sha512-0WR3b8eaISjEW7RpZnclONaLFDf7buaowRHdqLp4vLj54AsSAYWfh3DRbfiYJY9XDxMgx1B4sE1Afw2PGpuHOA==} + '@types/cli-progress@3.11.6': resolution: {integrity: sha512-cE3+jb9WRlu+uOSAugewNpITJDt1VF8dHOopPO4IABFc3SXYL5WE/+PTz/FCdZRRfIujiWW3n3aMbv1eIGVRWA==} @@ -802,6 +811,9 @@ packages: '@types/mdurl@2.0.0': resolution: {integrity: sha512-RGdgjQUZba5p6QEFAVx2OGb8rQDL/cPRG7GiedRzMcJ1tYnUANBncjbSB1NRGwbvjcPeikRABz2nshyPk1bhWg==} + '@types/micromatch@4.0.9': + resolution: {integrity: sha512-7V+8ncr22h4UoYRLnLXSpTxjQrNUXtWHGeMPRJt1nULXI57G9bIcpyrHlmrQ7QK24EyyuXvYcSSWAM8GA9nqCg==} + '@types/ms@0.7.34': resolution: {integrity: sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==} @@ -2174,8 +2186,8 @@ packages: micromark@4.0.0: resolution: {integrity: sha512-o/sd0nMof8kYff+TqcDx3VSrgBTcZpSvYcAHIfHhv5VAuNmisCxjhx6YmxS8PFEpb9z5WKWKPdzf0jM23ro3RQ==} - micromatch@4.0.7: - resolution: {integrity: sha512-LPP/3KorzCwBxfeUuZmaR6bG2kdeHSbe0P2tY3FLRU4vYrjYz5hI4QZwV0njUx3jeuKe67YukQ1LSPZBKDqO/Q==} + micromatch@4.0.8: + resolution: {integrity: sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==} engines: {node: '>=8.6'} mimic-fn@2.1.0: @@ -3643,6 +3655,8 @@ snapshots: '@types/argparse@1.0.38': {} + '@types/braces@3.0.4': {} + '@types/cli-progress@3.11.6': dependencies: '@types/node': 22.7.3 @@ -3682,6 +3696,10 @@ snapshots: '@types/mdurl@2.0.0': {} + '@types/micromatch@4.0.9': + dependencies: + '@types/braces': 3.0.4 + '@types/ms@0.7.34': {} '@types/node@22.7.3': @@ -4714,7 +4732,7 @@ snapshots: '@nodelib/fs.walk': 1.2.8 glob-parent: 5.1.2 merge2: 1.4.1 - micromatch: 4.0.7 + micromatch: 4.0.8 fast-json-stable-stringify@2.1.0: {} @@ -5430,7 +5448,7 @@ snapshots: transitivePeerDependencies: - supports-color - micromatch@4.0.7: + micromatch@4.0.8: dependencies: braces: 3.0.3 picomatch: 2.3.1 diff --git a/src/cli.ts b/src/cli.ts index 5cfc6db..ae23c3a 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,6 +1,7 @@ import { Command } from 'commander'; import { globSync } from 'glob'; import { promises as fs } from 'fs'; +import micromatch from 'micromatch'; import type { CodemodPlugin, ManualMigrationPlugin, Plugin } from './types'; import { transform } from './transform'; import { ManualMigrationReport, findManualMigrations } from './manual'; @@ -100,6 +101,8 @@ export interface CreateVueMetamorphCliOptions { type ProgramOptions = { files: string; + plugins: string[]; + listPlugins: boolean; }; /** @@ -111,7 +114,9 @@ export function createVueMetamorphCli(options: CreateVueMetamorphCliOptions) { const defaultCliProgressHandler = createDefaultCliProgressHandler(console); program - .requiredOption('--files ', 'Run transforms against these files', '**/src/**/*'); + .requiredOption('--files ', 'Run transforms against these files', '**/src/**/*') + .requiredOption('--plugins ', 'Run only these plugins using micromatch queries', '*') + .option('--list-plugins', 'Print a list of plugins.'); options.additionalCliOptions?.(program); @@ -122,6 +127,11 @@ export function createVueMetamorphCli(options: CreateVueMetamorphCliOptions) { const opts = program.opts(); const stats: Record = {}; + if (opts.listPlugins) { + process.stdout.write(`${options.plugins.flat().map((plugin) => plugin.name).join('\n')}\n`); + process.exit(0); + } + const files = globSync(opts.files, { absolute: true, nodir: true, @@ -140,7 +150,11 @@ export function createVueMetamorphCli(options: CreateVueMetamorphCliOptions) { }, }); - const plugins = options.plugins.flat(); + const plugins = options + .plugins + .flat() + .filter((plugin) => micromatch.isMatch(plugin.name, opts.plugins)); + const codemodPlugins = plugins.filter((plugin): plugin is CodemodPlugin => plugin.type === 'codemod'); const manualMigrationPlugins = plugins.filter((plugin): plugin is ManualMigrationPlugin => plugin.type === 'manual'); const manualMigrationReports: ManualMigrationReport[] = []; From 6e0361ab213eb867b3288995723e3e4ac8ecd77a Mon Sep 17 00:00:00 2001 From: UnrefinedBrain Date: Mon, 30 Sep 2024 00:22:46 +0000 Subject: [PATCH 2/2] add new options to docs --- docs/guide/cli.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/guide/cli.md b/docs/guide/cli.md index f1d2617..2232ce3 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -7,7 +7,9 @@ vue-metamorph provides a CLI codemod runner to faciliate running codemods agains | Option | Description | Default | | - | - | - | | --help | Print available options | N/A | +| --list-plugins | Lists all registered plugins and exists | N/A | | --files <glob> | Run transforms against these files using a [glob](https://www.npmjs.com/package/glob) pattern | `'**/src/**/*'` | +| --plugins <glob> | Only run plugins matching these [micromatch](https://github.com/micromatch/micromatch) patterns. This option can be passed multiple times to specify multiple patterns | `'*'` | ## API @@ -54,9 +56,11 @@ Options will be passed to the CodemodPlugin `transform()` and ManualMigrationPlu const myCodemod: CodemodPlugin = { name: 'myCodemod', type: 'codemod', - transform(scriptASTs, sfcAST, filename, utils, opts) { + transform({ opts }) { if (opts.myCustomOption) { - // behave differently + // do something + } else { + // do something else } } }