From bb4efdf3152f561a8bb97df8524f09296dd9f0e9 Mon Sep 17 00:00:00 2001 From: winches <329487092@qq.com> Date: Tue, 14 Jan 2025 09:51:26 +0800 Subject: [PATCH] feat: add format to use prettier through option --- packages/codemod/README.md | 7 +++++-- packages/codemod/src/actions/migrate-action.ts | 10 +++++++++- .../src/helpers/actions/lint-affected-files.ts | 6 ++---- packages/codemod/src/helpers/lint.ts | 7 ++++--- packages/codemod/src/helpers/options.ts | 17 +++++++++++++++++ packages/codemod/src/index.ts | 5 +++++ 6 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 packages/codemod/src/helpers/options.ts diff --git a/packages/codemod/README.md b/packages/codemod/README.md index 73589da..cab565e 100644 --- a/packages/codemod/README.md +++ b/packages/codemod/README.md @@ -19,6 +19,8 @@ The CLI provides a comprehensive suite of tools to migrate your codebase from Ne ## Quick Start > **Note**: The heroui CLI requires [Node.js](https://nodejs.org/en) _18.17.x_ or later +> +> **Note**: If running in monorepo, you need to run the command in the root of your monorepo You can start using @heroui/codemod in one of the following ways: @@ -49,9 +51,10 @@ Options: -v, --version Output the current version -d, --debug Enable debug mode -h, --help Display help for command + -f, --format Format the affected files with Prettier Commands: - migrate [projectPath] Migrate your codebase to use heroui + migrate [projectPath] Migrate your codebase to use heroui ``` ## Codemod Arguments @@ -141,7 +144,7 @@ Example: Migrate your entire codebase from NextUI to heroui. You can choose which codemods to run during the migration process. ```bash -heroui-codemod migrate [projectPath] +heroui-codemod migrate [projectPath] [--format] ``` Example: diff --git a/packages/codemod/src/actions/migrate-action.ts b/packages/codemod/src/actions/migrate-action.ts index 48e93fb..496213d 100644 --- a/packages/codemod/src/actions/migrate-action.ts +++ b/packages/codemod/src/actions/migrate-action.ts @@ -14,6 +14,7 @@ import {migrateNextuiProvider} from '../helpers/actions/migrate/migrate-nextui-p import {migrateNpmrc} from '../helpers/actions/migrate/migrate-npmrc'; import {migrateTailwindcss} from '../helpers/actions/migrate/migrate-tailwindcss'; import {findFiles} from '../helpers/find-files'; +import {getOptionsValue} from '../helpers/options'; import {affectedFiles, getStore, storeParsedContent, storePathsRawContent} from '../helpers/store'; import {transformPaths} from '../helpers/transform'; import {getCanRunCodemod} from '../helpers/utils'; @@ -145,10 +146,12 @@ export async function migrateAction(projectPaths?: string[], options = {} as Mig step++; } + const format = getOptionsValue('format'); /** ======================== 7. Formatting affected files (Optional) ======================== */ const runFormatAffectedFiles = affectedFiles.size > 0; - if (runFormatAffectedFiles) { + // If user using format option, we don't need to use eslint + if (runFormatAffectedFiles && !format) { p.log.step(`${step}. Formatting affected files (Optional)`); const selectMigrateNpmrc = await confirmClack({ message: `Do you want to format affected files? (${affectedFiles.size})` @@ -160,5 +163,10 @@ export async function migrateAction(projectPaths?: string[], options = {} as Mig step++; } + // Directly linting affected files don't need to ask user + if (format) { + await lintAffectedFiles(); + } + p.outro(chalk.green('✅ Migration completed!')); } diff --git a/packages/codemod/src/helpers/actions/lint-affected-files.ts b/packages/codemod/src/helpers/actions/lint-affected-files.ts index 53bf106..e331d78 100644 --- a/packages/codemod/src/helpers/actions/lint-affected-files.ts +++ b/packages/codemod/src/helpers/actions/lint-affected-files.ts @@ -1,11 +1,9 @@ import {tryLintFile} from '../lint'; import {affectedFiles} from '../store'; -export async function lintAffectedFiles(params?: {format: boolean}) { - const {format} = params || {}; - +export async function lintAffectedFiles() { try { - await tryLintFile(Array.from(affectedFiles), format); + await tryLintFile(Array.from(affectedFiles)); } catch (error) { return; } diff --git a/packages/codemod/src/helpers/lint.ts b/packages/codemod/src/helpers/lint.ts index 808c784..a015d58 100644 --- a/packages/codemod/src/helpers/lint.ts +++ b/packages/codemod/src/helpers/lint.ts @@ -1,3 +1,4 @@ +import {getOptionsValue} from './options'; import {getStore, writeFileAndUpdateStore} from './store'; async function tryImportPackage(packageName: string) { @@ -42,11 +43,11 @@ export async function lintWithPrettier(filePaths: string[]) { } /** - * Try linting a file with ESLint + * Try linting a file with ESLint or Prettier */ -export async function tryLintFile(filePaths: string[], format = false) { +export async function tryLintFile(filePaths: string[]) { try { - if (format) { + if (getOptionsValue('format')) { await lintWithPrettier(filePaths); } else { await lintWithESLint(filePaths); diff --git a/packages/codemod/src/helpers/options.ts b/packages/codemod/src/helpers/options.ts new file mode 100644 index 0000000..01ce731 --- /dev/null +++ b/packages/codemod/src/helpers/options.ts @@ -0,0 +1,17 @@ +export const CODEMOD_OPTIONS = { + format: false +}; + +export function initOptions(options: {format: boolean}) { + const {format} = options; + + setOptionsValue('format', format); +} + +export function setOptionsValue(key: keyof typeof CODEMOD_OPTIONS, value: boolean) { + CODEMOD_OPTIONS[key] = value; +} + +export function getOptionsValue(key: T) { + return CODEMOD_OPTIONS[key]; +} diff --git a/packages/codemod/src/index.ts b/packages/codemod/src/index.ts index bd925a0..90d8402 100644 --- a/packages/codemod/src/index.ts +++ b/packages/codemod/src/index.ts @@ -9,6 +9,7 @@ import pkg from '../package.json'; import {codemodAction} from './actions/codemod-action'; import {migrateAction} from './actions/migrate-action'; import {DEBUG} from './helpers/debug'; +import {initOptions} from './helpers/options'; import {codemods} from './types'; const nextui = new Command(); @@ -22,6 +23,7 @@ nextui .argument('[codemod]', `Specify which codemod to run\nCodemods: ${codemods.join(', ')}`) .allowUnknownOption() .option('-d, --debug', 'Enable debug mode') + .option('-f, --format', 'Format the affected files with Prettier') .action(codemodAction); nextui @@ -33,6 +35,9 @@ nextui nextui.hook('preAction', async (command) => { const options = (command as SAFE_ANY).rawArgs.slice(2); const debug = options.includes('--debug') || options.includes('-d'); + const format = options.includes('--format') || options.includes('-f'); + + initOptions({format}); DEBUG.enabled = debug; });