Skip to content

Commit

Permalink
tech: change Readme + change api
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackySoul committed Dec 8, 2023
1 parent 8a30a32 commit 7aefd47
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/codemods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Options:
-V, --version output the version number
-l --list list available codemods
--all apply all available codemods
-g --glob <glob> glob for files upon which to apply the codemods (default: "**/*.tsx?")
-p --path <paths> path to files in which to apply the codemods (default: current directory)
--dry-run no changes are made to files
--ignore-config <config> ignore files if they match patterns sourced from a configuration file (e.g. a .gitignore)
--debug all logs are shown
Expand All @@ -45,7 +45,7 @@ npx @vkontakte/vkui-codemods --all
Если вы хотите исключить некоторые файлы или директории из обработки, то временно создайте файл (по примеру .gitignore) с перечисленными исключениями:

```shell
npx @vkontakte/vkui-codemods --all --glob "./examples" --ignore-config "./.codemodignore"
npx @vkontakte/vkui-codemods --all --path "./examples" --ignore-config "./.codemodignore"
```

```
Expand Down
4 changes: 2 additions & 2 deletions packages/codemods/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import pkg from '../package.json';

export interface CliOptions {
list: boolean;
glob: string;
path: string;
dryRun: boolean;
ignoreConfig: string;
debug: boolean;
Expand Down Expand Up @@ -49,7 +49,7 @@ export const runCli = async (): Promise<Cli> => {
.usage(`${chalk.green('[codemod-name]')}`)
.option('-l --list', 'list available codemods')
.option('--all', 'apply all available codemods')
.option('-g --glob <glob>', 'glob for files upon which to apply the codemods', '**/*.tsx?')
.option('-p --path <paths>', 'path to files in which to apply the codemods')
.option('--dry-run', 'no changes are made to files')
.option(
'--ignore-config <config>',
Expand Down
21 changes: 10 additions & 11 deletions packages/codemods/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CliOptions, runCli } from './cli.js';
import getAvailableCodemods, { TRANSFORM_DIR } from './getAvailableCodemods.js';
import logger from './helpers/logger.js';

function runJSCodeShift(codemodName: string, flags: CliOptions) {
function runJSCodeShift(codemodName: string, workingDirectory: string, flags: CliOptions) {
const args = ['--parser=tsx', '--extensions=tsx,ts', `--alias=${flags.alias}`];
if (flags.dryRun) {
args.push('--dry');
Expand All @@ -27,7 +27,7 @@ function runJSCodeShift(codemodName: string, flags: CliOptions) {
'-t',
`${TRANSFORM_DIR}/${codemodName}.js`,
...args,
flags.glob,
workingDirectory,
],
{
stdio: 'inherit',
Expand Down Expand Up @@ -76,28 +76,27 @@ async function verifyConfiguration(workingDirectory: string, codemodName?: strin
const run = async () => {
const { flags, codemodName } = await runCli();

if (codemodName && flags.glob) {
const workingDirectory = flags.path ? flags.path : process.cwd();
if (codemodName && workingDirectory) {
const codemodes = getAvailableCodemods();
if (codemodes.includes(codemodName)) {
await verifyConfiguration(flags.glob, codemodName);
await verifyConfiguration(workingDirectory, codemodName);
logger.info("\n 🚀 Let's go!");
runJSCodeShift(codemodName, flags);
runJSCodeShift(codemodName, workingDirectory, flags);
} else {
logger.error(
`Codemod ${codemodName} doesn't exist. Please check the available codemods by running with --list option`,
);
process.exit(0);
}
}
if (flags.all && flags.glob) {
await verifyConfiguration(flags.glob);
if (flags.all && workingDirectory) {
await verifyConfiguration(workingDirectory);
logger.info("\n 🚀 Let's go!");
const codemodes = getAvailableCodemods();
codemodes.forEach((codemod) => {
if (flags.debug) {
logger.info(`Codemod ${codemod} in process...`);
}
runJSCodeShift(codemod, flags);
logger.info(`Codemod ${codemod} in process...`);
runJSCodeShift(codemod, workingDirectory, flags);
});
}

Expand Down

0 comments on commit 7aefd47

Please sign in to comment.