Skip to content

Commit

Permalink
feat: add format to use prettier through option
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe committed Jan 14, 2025
1 parent 887b592 commit bb4efdf
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
7 changes: 5 additions & 2 deletions packages/codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion packages/codemod/src/actions/migrate-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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})`
Expand All @@ -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!'));
}
6 changes: 2 additions & 4 deletions packages/codemod/src/helpers/actions/lint-affected-files.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/codemod/src/helpers/lint.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {getOptionsValue} from './options';
import {getStore, writeFileAndUpdateStore} from './store';

async function tryImportPackage(packageName: string) {
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions packages/codemod/src/helpers/options.ts
Original file line number Diff line number Diff line change
@@ -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<T extends keyof typeof CODEMOD_OPTIONS>(key: T) {
return CODEMOD_OPTIONS[key];
}
5 changes: 5 additions & 0 deletions packages/codemod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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;
});
Expand Down

0 comments on commit bb4efdf

Please sign in to comment.