diff --git a/commands/__test__/transform.spec.ts b/commands/__test__/transform.spec.ts index 7866cb3..71e2c72 100644 --- a/commands/__test__/transform.spec.ts +++ b/commands/__test__/transform.spec.ts @@ -1,8 +1,15 @@ -import { join } from 'node:path' +import { sep } from 'node:path' import { run } from 'jscodeshift/src/Runner' import prompts from 'prompts' import { transform } from '../transform' +const defaultOptions = { + dry: true, + silent: true, +} + +const getSystemPath = (inputPath: string) => inputPath.replaceAll('/', sep) + jest.mock('jscodeshift/src/Runner', () => ({ run: jest.fn(), })) @@ -18,13 +25,13 @@ describe('interactive mode', () => { prompts.inject(['magic-redirect']) prompts.inject(['./transforms/__testfixtures__']) - await transform(undefined, undefined, { dry: true, silent: true }) + await transform(undefined, undefined, defaultOptions) expect(spyOnConsole).not.toHaveBeenCalled() expect(run).toHaveBeenCalledTimes(1) expect(run).toHaveBeenCalledWith( - join(__dirname, '../../', 'transforms/magic-redirect.js'), - ['./transforms/__testfixtures__'], + expect.stringContaining(getSystemPath('/transforms/magic-redirect.js')), + expect.arrayContaining([expect.stringContaining(getSystemPath('/transforms/__testfixtures__'))]), { babel: false, dry: true, @@ -41,16 +48,13 @@ describe('interactive mode', () => { prompts.inject(['magic-redirect']) - await transform('bad-codemod', './transforms/__testfixtures__', { - dry: true, - silent: true, - }) + await transform('bad-codemod', './transforms/__testfixtures__', defaultOptions) expect(spyOnConsole).not.toHaveBeenCalled() expect(run).toHaveBeenCalledTimes(1) expect(run).toHaveBeenCalledWith( - join(__dirname, '../../', 'transforms/magic-redirect.js'), - ['./transforms/__testfixtures__'], + expect.stringContaining(getSystemPath('/transforms/magic-redirect.js')), + expect.arrayContaining([expect.stringContaining(getSystemPath('/transforms/__testfixtures__'))]), { babel: false, dry: true, @@ -67,21 +71,22 @@ describe('interactive mode', () => { prompts.inject(['__testfixtures__']) - await transform('magic-redirect', undefined, { - dry: true, - silent: true, - }) + await transform('magic-redirect', undefined, defaultOptions) expect(spyOnConsole).not.toHaveBeenCalled() expect(run).toHaveBeenCalledTimes(1) - expect(run).toHaveBeenCalledWith(join(__dirname, '../../', 'transforms/magic-redirect.js'), ['__testfixtures__'], { - babel: false, - dry: true, - extensions: 'cts,mts,ts,js,mjs,cjs', - ignorePattern: '**/node_modules/**', - silent: true, - verbose: 0, - }) + expect(run).toHaveBeenCalledWith( + expect.stringContaining(getSystemPath('/transforms/magic-redirect.js')), + expect.arrayContaining([expect.stringContaining('__testfixtures__')]), + { + babel: false, + dry: true, + extensions: 'cts,mts,ts,js,mjs,cjs', + ignorePattern: '**/node_modules/**', + silent: true, + verbose: 0, + }, + ) }) }) @@ -93,20 +98,21 @@ describe('Non-Interactive Mode', () => { it('Transforms code with codemodName and source params provided', async () => { const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() - await transform('magic-redirect', '__testfixtures__', { - dry: true, - silent: true, - }) + await transform('magic-redirect', '__testfixtures__', defaultOptions) expect(spyOnConsole).not.toHaveBeenCalled() expect(run).toHaveBeenCalledTimes(1) - expect(run).toHaveBeenCalledWith(join(__dirname, '../../', 'transforms/magic-redirect.js'), ['__testfixtures__'], { - babel: false, - dry: true, - extensions: 'cts,mts,ts,js,mjs,cjs', - ignorePattern: '**/node_modules/**', - silent: true, - verbose: 0, - }) + expect(run).toHaveBeenCalledWith( + expect.stringContaining(getSystemPath('/transforms/magic-redirect.js')), + expect.arrayContaining([expect.stringContaining('__testfixtures__')]), + { + babel: false, + dry: true, + extensions: 'cts,mts,ts,js,mjs,cjs', + ignorePattern: '**/node_modules/**', + silent: true, + verbose: 0, + }, + ) }) }) diff --git a/commands/transform.ts b/commands/transform.ts index 9ac5659..4317660 100644 --- a/commands/transform.ts +++ b/commands/transform.ts @@ -1,4 +1,4 @@ -import { join } from 'node:path' +import { join, resolve } from 'node:path' import type { Options } from 'jscodeshift' import { run as jscodeshift } from 'jscodeshift/src/Runner' import { bold } from 'picocolors' @@ -12,48 +12,53 @@ export function onCancel() { const transformerDirectory = join(__dirname, '../', 'transforms') -export async function transform(codemodName?: string, source?: string, options?: Record) { - let codemodSelected = codemodName - let sourceSelected = source +const selectCodemod = async (): Promise => { + const res = await prompts( + { + type: 'select', + name: 'transformer', + message: 'Which codemod would you like to apply?', + choices: TRANSFORM_OPTIONS.map(({ description, value, version }) => { + return { + title: `(${bold(`v${version}`)}) ${value}`, + description, + value, + } + }), + }, + { onCancel }, + ) - const existCodemod = TRANSFORM_OPTIONS.find(({ value }) => value === codemodSelected) + return res.transformer +} - if (!codemodSelected || (codemodSelected && !existCodemod)) { - const res = await prompts( - { - type: 'select', - name: 'transformer', - message: 'Which codemod would you like to apply?', - choices: TRANSFORM_OPTIONS.map(({ description, value, version }) => { - return { - title: `(${bold(`v${version}`)}) ${value}`, - description, - value, - } - }), - }, - { onCancel }, - ) +const selectSource = async (): Promise => { + const res = await prompts( + { + type: 'text', + name: 'path', + message: 'Which files or directories should the codemods be applied to?', + initial: '.', + }, + { onCancel }, + ) - codemodSelected = res.transformer - } + return res.path +} - if (!sourceSelected) { - const res = await prompts( - { - type: 'text', - name: 'path', - message: 'Which files or directories should the codemods be applied to?', - initial: '.', - }, - { onCancel }, - ) +export async function transform(codemodName?: string, source?: string, options?: Record) { + const existCodemod = TRANSFORM_OPTIONS.find(({ value }) => value === codemodName) + const codemodSelected = !codemodName || (codemodName && !existCodemod) ? await selectCodemod() : codemodName - sourceSelected = res.path + if (!codemodSelected) { + console.info('> Codemod is not selected. Exits the program. \n') + process.exit(1) } - if (!codemodSelected) { - console.info('> Codemod is not selected. Exist the program. \n') + const sourceSelected = source || (await selectSource()) + + if (!sourceSelected) { + console.info('> Source path for project is not selected. Exits the program. \n') process.exit(1) } @@ -67,5 +72,5 @@ export async function transform(codemodName?: string, source?: string, options?: extensions: 'cts,mts,ts,js,mjs,cjs', } - await jscodeshift(transformerPath, [sourceSelected || ''], args) + await jscodeshift(transformerPath, [resolve(sourceSelected)], args) }