Skip to content

Commit a043e1d

Browse files
bjohansebaskjugi
andauthored
fix relative path problem (#11)
* fix relative path problem * forget about using let in commands/transform * forget about using node:path library in commands/transform unit test * fix relative path problem * fix test for windows * remove redundant resolve in transform file * try to use system sep from node:path lib --------- Co-authored-by: Filip Kudła <[email protected]>
1 parent 30ff65a commit a043e1d

File tree

2 files changed

+82
-71
lines changed

2 files changed

+82
-71
lines changed
Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { join } from 'node:path'
1+
import { sep } from 'node:path'
22
import { run } from 'jscodeshift/src/Runner'
33
import prompts from 'prompts'
44
import { transform } from '../transform'
55

6+
const defaultOptions = {
7+
dry: true,
8+
silent: true,
9+
}
10+
11+
const getSystemPath = (inputPath: string) => inputPath.replaceAll('/', sep)
12+
613
jest.mock('jscodeshift/src/Runner', () => ({
714
run: jest.fn(),
815
}))
@@ -18,13 +25,13 @@ describe('interactive mode', () => {
1825
prompts.inject(['magic-redirect'])
1926
prompts.inject(['./transforms/__testfixtures__'])
2027

21-
await transform(undefined, undefined, { dry: true, silent: true })
28+
await transform(undefined, undefined, defaultOptions)
2229

2330
expect(spyOnConsole).not.toHaveBeenCalled()
2431
expect(run).toHaveBeenCalledTimes(1)
2532
expect(run).toHaveBeenCalledWith(
26-
join(__dirname, '../../', 'transforms/magic-redirect.js'),
27-
['./transforms/__testfixtures__'],
33+
expect.stringContaining(getSystemPath('/transforms/magic-redirect.js')),
34+
expect.arrayContaining([expect.stringContaining(getSystemPath('/transforms/__testfixtures__'))]),
2835
{
2936
babel: false,
3037
dry: true,
@@ -41,16 +48,13 @@ describe('interactive mode', () => {
4148

4249
prompts.inject(['magic-redirect'])
4350

44-
await transform('bad-codemod', './transforms/__testfixtures__', {
45-
dry: true,
46-
silent: true,
47-
})
51+
await transform('bad-codemod', './transforms/__testfixtures__', defaultOptions)
4852

4953
expect(spyOnConsole).not.toHaveBeenCalled()
5054
expect(run).toHaveBeenCalledTimes(1)
5155
expect(run).toHaveBeenCalledWith(
52-
join(__dirname, '../../', 'transforms/magic-redirect.js'),
53-
['./transforms/__testfixtures__'],
56+
expect.stringContaining(getSystemPath('/transforms/magic-redirect.js')),
57+
expect.arrayContaining([expect.stringContaining(getSystemPath('/transforms/__testfixtures__'))]),
5458
{
5559
babel: false,
5660
dry: true,
@@ -67,21 +71,22 @@ describe('interactive mode', () => {
6771

6872
prompts.inject(['__testfixtures__'])
6973

70-
await transform('magic-redirect', undefined, {
71-
dry: true,
72-
silent: true,
73-
})
74+
await transform('magic-redirect', undefined, defaultOptions)
7475

7576
expect(spyOnConsole).not.toHaveBeenCalled()
7677
expect(run).toHaveBeenCalledTimes(1)
77-
expect(run).toHaveBeenCalledWith(join(__dirname, '../../', 'transforms/magic-redirect.js'), ['__testfixtures__'], {
78-
babel: false,
79-
dry: true,
80-
extensions: 'cts,mts,ts,js,mjs,cjs',
81-
ignorePattern: '**/node_modules/**',
82-
silent: true,
83-
verbose: 0,
84-
})
78+
expect(run).toHaveBeenCalledWith(
79+
expect.stringContaining(getSystemPath('/transforms/magic-redirect.js')),
80+
expect.arrayContaining([expect.stringContaining('__testfixtures__')]),
81+
{
82+
babel: false,
83+
dry: true,
84+
extensions: 'cts,mts,ts,js,mjs,cjs',
85+
ignorePattern: '**/node_modules/**',
86+
silent: true,
87+
verbose: 0,
88+
},
89+
)
8590
})
8691
})
8792

@@ -93,20 +98,21 @@ describe('Non-Interactive Mode', () => {
9398
it('Transforms code with codemodName and source params provided', async () => {
9499
const spyOnConsole = jest.spyOn(console, 'log').mockImplementation()
95100

96-
await transform('magic-redirect', '__testfixtures__', {
97-
dry: true,
98-
silent: true,
99-
})
101+
await transform('magic-redirect', '__testfixtures__', defaultOptions)
100102

101103
expect(spyOnConsole).not.toHaveBeenCalled()
102104
expect(run).toHaveBeenCalledTimes(1)
103-
expect(run).toHaveBeenCalledWith(join(__dirname, '../../', 'transforms/magic-redirect.js'), ['__testfixtures__'], {
104-
babel: false,
105-
dry: true,
106-
extensions: 'cts,mts,ts,js,mjs,cjs',
107-
ignorePattern: '**/node_modules/**',
108-
silent: true,
109-
verbose: 0,
110-
})
105+
expect(run).toHaveBeenCalledWith(
106+
expect.stringContaining(getSystemPath('/transforms/magic-redirect.js')),
107+
expect.arrayContaining([expect.stringContaining('__testfixtures__')]),
108+
{
109+
babel: false,
110+
dry: true,
111+
extensions: 'cts,mts,ts,js,mjs,cjs',
112+
ignorePattern: '**/node_modules/**',
113+
silent: true,
114+
verbose: 0,
115+
},
116+
)
111117
})
112118
})

commands/transform.ts

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'node:path'
1+
import { join, resolve } from 'node:path'
22
import type { Options } from 'jscodeshift'
33
import { run as jscodeshift } from 'jscodeshift/src/Runner'
44
import { bold } from 'picocolors'
@@ -12,48 +12,53 @@ export function onCancel() {
1212

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

15-
export async function transform(codemodName?: string, source?: string, options?: Record<string, unknown>) {
16-
let codemodSelected = codemodName
17-
let sourceSelected = source
15+
const selectCodemod = async (): Promise<string> => {
16+
const res = await prompts(
17+
{
18+
type: 'select',
19+
name: 'transformer',
20+
message: 'Which codemod would you like to apply?',
21+
choices: TRANSFORM_OPTIONS.map(({ description, value, version }) => {
22+
return {
23+
title: `(${bold(`v${version}`)}) ${value}`,
24+
description,
25+
value,
26+
}
27+
}),
28+
},
29+
{ onCancel },
30+
)
1831

19-
const existCodemod = TRANSFORM_OPTIONS.find(({ value }) => value === codemodSelected)
32+
return res.transformer
33+
}
2034

21-
if (!codemodSelected || (codemodSelected && !existCodemod)) {
22-
const res = await prompts(
23-
{
24-
type: 'select',
25-
name: 'transformer',
26-
message: 'Which codemod would you like to apply?',
27-
choices: TRANSFORM_OPTIONS.map(({ description, value, version }) => {
28-
return {
29-
title: `(${bold(`v${version}`)}) ${value}`,
30-
description,
31-
value,
32-
}
33-
}),
34-
},
35-
{ onCancel },
36-
)
35+
const selectSource = async (): Promise<string> => {
36+
const res = await prompts(
37+
{
38+
type: 'text',
39+
name: 'path',
40+
message: 'Which files or directories should the codemods be applied to?',
41+
initial: '.',
42+
},
43+
{ onCancel },
44+
)
3745

38-
codemodSelected = res.transformer
39-
}
46+
return res.path
47+
}
4048

41-
if (!sourceSelected) {
42-
const res = await prompts(
43-
{
44-
type: 'text',
45-
name: 'path',
46-
message: 'Which files or directories should the codemods be applied to?',
47-
initial: '.',
48-
},
49-
{ onCancel },
50-
)
49+
export async function transform(codemodName?: string, source?: string, options?: Record<string, unknown>) {
50+
const existCodemod = TRANSFORM_OPTIONS.find(({ value }) => value === codemodName)
51+
const codemodSelected = !codemodName || (codemodName && !existCodemod) ? await selectCodemod() : codemodName
5152

52-
sourceSelected = res.path
53+
if (!codemodSelected) {
54+
console.info('> Codemod is not selected. Exits the program. \n')
55+
process.exit(1)
5356
}
5457

55-
if (!codemodSelected) {
56-
console.info('> Codemod is not selected. Exist the program. \n')
58+
const sourceSelected = source || (await selectSource())
59+
60+
if (!sourceSelected) {
61+
console.info('> Source path for project is not selected. Exits the program. \n')
5762
process.exit(1)
5863
}
5964

@@ -67,5 +72,5 @@ export async function transform(codemodName?: string, source?: string, options?:
6772
extensions: 'cts,mts,ts,js,mjs,cjs',
6873
}
6974

70-
await jscodeshift(transformerPath, [sourceSelected || ''], args)
75+
await jscodeshift(transformerPath, [resolve(sourceSelected)], args)
7176
}

0 commit comments

Comments
 (0)