Skip to content

Commit

Permalink
feat: add format effected files
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe committed Jan 11, 2025
1 parent b3bf4a8 commit 64caf22
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 6 deletions.
18 changes: 17 additions & 1 deletion packages/codemod/src/actions/migrate-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import chalk from 'chalk';
import {confirmClack} from 'src/prompts/clack';

import {NEXTUI_PREFIX} from '../constants/prefix';
import {lintEffectedFiles} from '../helpers/actions/lint-effected-files';
import {migrateCssVariables} from '../helpers/actions/migrate/migrate-css-variables';
import {migrateImportPackageWithPaths} from '../helpers/actions/migrate/migrate-import';
import {migrateJson} from '../helpers/actions/migrate/migrate-json';
import {migrateNextuiProvider} from '../helpers/actions/migrate/migrate-nextui-provider';
import {migrateNpmrc} from '../helpers/actions/migrate/migrate-npmrc';
import {migrateTailwindcss} from '../helpers/actions/migrate/migrate-tailwindcss';
import {findFiles} from '../helpers/find-files';
import {getStore, storeParsedContent, storePathsRawContent} from '../helpers/store';
import {effectedFiles, getStore, storeParsedContent, storePathsRawContent} from '../helpers/store';
import {transformPaths} from '../helpers/transform';
import {getCanRunCodemod} from '../helpers/utils';

Expand Down Expand Up @@ -144,5 +145,20 @@ export async function migrateAction(projectPaths?: string[], options = {} as Mig
step++;
}

/** ======================== 7. Formatting effected files (Optional) ======================== */
const runFormatEffectedFiles = effectedFiles.size > 0;

if (runFormatEffectedFiles) {
p.log.step(`${step}. Formatting effected files (Optional)`);
const selectMigrateNpmrc = await confirmClack({
message: `Do you want to format effected files? (${effectedFiles.size})`
});

if (selectMigrateNpmrc) {
await lintEffectedFiles();
}
step++;
}

p.outro(chalk.green('✅ Migration completed!'));
}
10 changes: 10 additions & 0 deletions packages/codemod/src/helpers/actions/lint-effected-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {tryLintFile} from '../lint';
import {effectedFiles} from '../store';

export async function lintEffectedFiles() {
try {
await tryLintFile(Array.from(effectedFiles));
} catch (error) {
return;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {HEROUI_CSS_VARIABLES_PREFIX, NEXTUI_CSS_VARIABLES_PREFIX} from '../../../constants/prefix';
import {getStore, writeFileAndUpdateStore} from '../../store';
import {getStore, updateEffectedFiles, writeFileAndUpdateStore} from '../../store';

export function migrateCssVariables(files: string[]) {
for (const file of files) {
Expand All @@ -13,6 +13,7 @@ export function migrateCssVariables(files: string[]) {
);

writeFileAndUpdateStore(file, 'rawContent', content);
updateEffectedFiles(file);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import jscodeshift from 'jscodeshift';

import {HEROUI_PREFIX, NEXTUI_PREFIX} from '../../../constants/prefix';
import {type StoreObject, getStore, writeFileAndUpdateStore} from '../../store';
import {
type StoreObject,
getStore,
updateEffectedFiles,
writeFileAndUpdateStore
} from '../../store';

/**
* Migrate the import package will directly write the file
Expand All @@ -24,6 +29,7 @@ export function migrateImportPackageWithPaths(paths: string[]) {
if (dirtyFlag) {
// Write the modified content back to the file
writeFileAndUpdateStore(path, 'parsedContent', parsedContent);
updateEffectedFiles(path);
}
// eslint-disable-next-line no-empty
} catch {}
Expand Down
3 changes: 2 additions & 1 deletion packages/codemod/src/helpers/actions/migrate/migrate-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Logger} from '@helpers/logger';
import {HEROUI_PREFIX, NEXTUI_PREFIX} from '../../../constants/prefix';
import {fetchPackageLatestVersion} from '../../https';
import {safeParseJson} from '../../parse';
import {getStore, writeFileAndUpdateStore} from '../../store';
import {getStore, updateEffectedFiles, writeFileAndUpdateStore} from '../../store';

const DEFAULT_INDENT = 2;

Expand Down Expand Up @@ -51,6 +51,7 @@ export async function migrateJson(files: string[]) {
const indent = detectIndent(content);

writeFileAndUpdateStore(file, 'rawContent', JSON.stringify(json, null, indent));
updateEffectedFiles(file);
}
})
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {HEROUI_PROVIDER, NEXTUI_PROVIDER} from '../../../constants/prefix';
import {getStore, writeFileAndUpdateStore} from '../../store';
import {getStore, updateEffectedFiles, writeFileAndUpdateStore} from '../../store';

import {migrateImportName, migrateJSXElementName} from './migrate-common';

Expand Down Expand Up @@ -28,6 +28,7 @@ export function migrateNextuiProvider(paths: string[]) {

// Write the modified content back to the file
writeFileAndUpdateStore(path, 'parsedContent', parsedContent);
updateEffectedFiles(path);
}
// eslint-disable-next-line no-empty
} catch {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
NEXTUI_PLUGIN,
NEXTUI_PREFIX
} from '../../../constants/prefix';
import {getStore, writeFileAndUpdateStore} from '../../store';
import {getStore, updateEffectedFiles, writeFileAndUpdateStore} from '../../store';

import {migrateCallExpressionName, migrateImportName} from './migrate-common';
import {migrateImportPackage} from './migrate-import';
Expand Down Expand Up @@ -56,6 +56,7 @@ export function migrateTailwindcss(paths: string[]) {

if (dirtyFlag) {
writeFileAndUpdateStore(path, 'parsedContent', parsedContent);
updateEffectedFiles(path);
}
}
}
44 changes: 44 additions & 0 deletions packages/codemod/src/helpers/lint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {getStore, writeFileAndUpdateStore} from './store';

async function tryImportPackage(packageName: string) {
try {
return await import(packageName);
} catch {
return null;
}
}

/**
* Try linting a file with ESLint/Prettier
* First try ESLint, if it fails, try Prettier.
*/
export async function tryLintFile(filePaths: string[]) {
const eslintPkg = await tryImportPackage('eslint');

if (eslintPkg) {
const ESLint = eslintPkg.ESLint;
const eslint = new ESLint({
fix: true
});
const result = await eslint.lintFiles(filePaths);

await ESLint.outputFixes(result);
} else {
const prettier = await tryImportPackage('prettier');
const options = await prettier.resolveConfig(process.cwd());

if (prettier) {
await Promise.all(
filePaths.map(async (filePath) => {
const rawContent = getStore(filePath, 'rawContent');
const formattedContent = await prettier.format(rawContent, {
options,
parser: 'typescript'
});

writeFileAndUpdateStore(filePath, 'rawContent', formattedContent);
})
);
}
}
}
6 changes: 6 additions & 0 deletions packages/codemod/src/helpers/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,9 @@ export function writeFileAndUpdateStore<K extends ExcludeStoreKey>(
updateStore(path, key, value);
key === 'parsedContent' && updateStore(path, 'rawContent', data.rawContent);
}

export const effectedFiles = new Set<string>();

export function updateEffectedFiles(path: string) {
effectedFiles.add(path);
}

0 comments on commit 64caf22

Please sign in to comment.