Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions commands/__test__/transform.spec.ts
Original file line number Diff line number Diff line change
@@ -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(),
}))
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
},
)
})
})

Expand All @@ -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,
},
)
})
})
79 changes: 42 additions & 37 deletions commands/transform.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -12,48 +12,53 @@ export function onCancel() {

const transformerDirectory = join(__dirname, '../', 'transforms')

export async function transform(codemodName?: string, source?: string, options?: Record<string, unknown>) {
let codemodSelected = codemodName
let sourceSelected = source
const selectCodemod = async (): Promise<string> => {
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<string> => {
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<string, unknown>) {
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)
}

Expand All @@ -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)
}
Loading