-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60f69d6
commit 5950126
Showing
5 changed files
with
122 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { run } from 'jscodeshift/src/Runner' | ||
import { upgrade } from '../upgrade' | ||
|
||
jest.mock('jscodeshift/src/Runner', () => ({ | ||
run: jest.fn(), | ||
})) | ||
|
||
describe('interactive mode', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it.todo('runs without source params provided and select is true') | ||
}) | ||
|
||
describe('Non-Interactive Mode', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('Transforms code with source params provided', async () => { | ||
const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() | ||
|
||
await upgrade('__testfixtures__') | ||
|
||
expect(spyOnConsole).toHaveBeenCalled() | ||
expect(run).toHaveBeenCalledTimes(4) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { join, resolve } from 'node:path' | ||
import type { Options } from 'jscodeshift' | ||
import { run as jscodeshift } from 'jscodeshift/src/Runner' | ||
import prompts from 'prompts' | ||
import { TRANSFORM_OPTIONS } from '../config' | ||
import { onCancel, promptSource } from '../utils/prompts' | ||
|
||
const transformerDirectory = join(__dirname, '../', 'transforms') | ||
|
||
export async function upgrade(source?: string, options?: Record<string, unknown>) { | ||
const sourceSelected = source || (await promptSource('Which directory should the codemods be applied to?')) | ||
|
||
if (!sourceSelected) { | ||
console.info('> Source path for project is not selected. Exits the program. \n') | ||
process.exit(1) | ||
} | ||
let codemods: string[] = [] | ||
|
||
if (options?.select) { | ||
const { codemodsSelected } = await prompts( | ||
{ | ||
type: 'multiselect', | ||
name: 'codemodsSelected', | ||
message: `The following 'codemods' are recommended for your upgrade. Select the ones to apply.`, | ||
choices: TRANSFORM_OPTIONS.map(({ description, value, version }) => { | ||
return { | ||
title: `(v${version}) ${value}`, | ||
description, | ||
value, | ||
selected: true, | ||
} | ||
}), | ||
}, | ||
{ onCancel }, | ||
) | ||
codemods = codemodsSelected | ||
} else { | ||
codemods = TRANSFORM_OPTIONS.map(({ value }) => value) | ||
} | ||
|
||
const args: Options = { | ||
dry: false, | ||
babel: false, | ||
silent: true, | ||
ignorePattern: '**/node_modules/**', | ||
extensions: 'cts,mts,ts,js,mjs,cjs', | ||
} | ||
|
||
for (const codemod of codemods) { | ||
const transformerPath = join(transformerDirectory, `${codemod}.js`) | ||
|
||
console.log(`> Applying codemod: ${codemod}`) | ||
|
||
try { | ||
await jscodeshift(transformerPath, [resolve(sourceSelected)], args) | ||
} catch (error) { | ||
console.error(`> Error applying codemod: ${codemod}`) | ||
console.error(error) | ||
} | ||
} | ||
|
||
console.log('\n> All codemods have been applied successfully. \n') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import prompts from 'prompts' | ||
|
||
export function onCancel() { | ||
console.info('> Cancelled process. Program will stop now without any actions. \n') | ||
process.exit(1) | ||
} | ||
|
||
export const promptSource = async (message: string): Promise<string> => { | ||
const res = await prompts( | ||
{ | ||
type: 'text', | ||
name: 'path', | ||
message, | ||
initial: '.', | ||
}, | ||
{ onCancel }, | ||
) | ||
|
||
return res.path | ||
} |