Skip to content

Commit

Permalink
Merge pull request #186 from UnrefinedBrain/allow-running-plugin-subset
Browse files Browse the repository at this point in the history
add --list-plugins and --plugins options to cli
  • Loading branch information
UnrefinedBrain authored Sep 30, 2024
2 parents 70075a8 + 6e0361a commit 424d6ef
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
8 changes: 6 additions & 2 deletions docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
26 changes: 22 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -100,6 +101,8 @@ export interface CreateVueMetamorphCliOptions {

type ProgramOptions = {
files: string;
plugins: string[];
listPlugins: boolean;
};

/**
Expand All @@ -111,7 +114,9 @@ export function createVueMetamorphCli(options: CreateVueMetamorphCliOptions) {
const defaultCliProgressHandler = createDefaultCliProgressHandler(console);

program
.requiredOption('--files <glob>', 'Run transforms against these files', '**/src/**/*');
.requiredOption('--files <glob>', 'Run transforms against these files', '**/src/**/*')
.requiredOption('--plugins <glob...>', 'Run only these plugins using micromatch queries', '*')
.option('--list-plugins', 'Print a list of plugins.');

options.additionalCliOptions?.(program);

Expand All @@ -122,6 +127,11 @@ export function createVueMetamorphCli(options: CreateVueMetamorphCliOptions) {
const opts = program.opts<ProgramOptions>();
const stats: Record<string, number> = {};

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,
Expand All @@ -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[] = [];
Expand Down

0 comments on commit 424d6ef

Please sign in to comment.