|
1
|
import { describe, it, expect } from "vitest" |
|
2
|
import { npxToYarn } from "../npx-to-yarn.js" |
|
3
|
|
|
4
|
describe("npxToYarn", () => { |
|
5
|
describe("yarn conversion", () => { |
|
6
|
it("should convert basic npx command to yarn", () => { |
|
7
|
const result = npxToYarn("npx medusa db:migrate", "yarn") |
|
8
|
expect(result).toBe("yarn medusa db:migrate") |
|
9
|
}) |
|
10
|
|
|
11
|
it("should convert npx command with multiple arguments", () => { |
|
12
|
const result = npxToYarn("npx medusa develop --port 9000", "yarn") |
|
13
|
expect(result).toBe("yarn medusa develop --port 9000") |
|
14
|
}) |
|
15
|
|
|
16
|
it("should convert npx command with flags", () => { |
|
17
|
const result = npxToYarn("npx medusa user --email admin@test.com", "yarn") |
|
18
|
expect(result).toBe("yarn medusa user --email admin@test.com") |
|
19
|
}) |
|
20
|
|
|
21
|
it("should handle npx command with leading/trailing whitespace", () => { |
|
22
|
const result = npxToYarn(" npx medusa db:migrate ", "yarn") |
|
23
|
expect(result).toBe("yarn medusa db:migrate") |
|
24
|
}) |
|
25
|
|
|
26
|
it("should convert npx command to yarn dlx when isExecutable is true", () => { |
|
27
|
const result = npxToYarn("npx create-medusa-app@latest", "yarn", true) |
|
28
|
expect(result).toBe("yarn dlx create-medusa-app@latest") |
|
29
|
}) |
|
30
|
|
|
31
|
it("should convert npx command to yarn when isExecutable is false", () => { |
|
32
|
const result = npxToYarn("npx medusa db:migrate", "yarn", false) |
|
33
|
expect(result).toBe("yarn medusa db:migrate") |
|
34
|
}) |
|
35
|
}) |
|
36
|
|
|
37
|
describe("pnpm conversion", () => { |
|
38
|
it("should convert basic npx command to pnpm", () => { |
|
39
|
const result = npxToYarn("npx medusa db:migrate", "pnpm") |
|
40
|
expect(result).toBe("pnpm medusa db:migrate") |
|
41
|
}) |
|
42
|
|
|
43
|
it("should convert npx command with multiple arguments", () => { |
|
44
|
const result = npxToYarn("npx medusa develop --port 9000", "pnpm") |
|
45
|
expect(result).toBe("pnpm medusa develop --port 9000") |
|
46
|
}) |
|
47
|
|
|
48
|
it("should convert npx command with flags", () => { |
|
49
|
const result = npxToYarn("npx medusa user --email admin@test.com", "pnpm") |
|
50
|
expect(result).toBe("pnpm medusa user --email admin@test.com") |
|
51
|
}) |
|
52
|
|
|
53
|
it("should handle npx command with leading/trailing whitespace", () => { |
|
54
|
const result = npxToYarn(" npx medusa db:migrate ", "pnpm") |
|
55
|
expect(result).toBe("pnpm medusa db:migrate") |
|
56
|
}) |
|
57
|
|
|
58
|
it("should convert npx command to pnpm dlx when isExecutable is true", () => { |
|
59
|
const result = npxToYarn("npx create-medusa-app@latest", "pnpm", true) |
|
60
|
expect(result).toBe("pnpm dlx create-medusa-app@latest") |
|
61
|
}) |
|
62
|
|
|
63
|
it("should convert npx command to pnpm when isExecutable is false", () => { |
|
64
|
const result = npxToYarn("npx medusa db:migrate", "pnpm", false) |
|
65
|
expect(result).toBe("pnpm medusa db:migrate") |
|
66
|
}) |
|
67
|
}) |
|
68
|
|
|
69
|
describe("edge cases", () => { |
|
70
|
it("should return original command if it does not start with npx", () => { |
|
71
|
const result = npxToYarn("npm install medusa", "yarn") |
|
72
|
expect(result).toBe("npm install medusa") |
|
73
|
}) |
|
74
|
|
|
75
|
it("should handle command with only npx and package name", () => { |
|
76
|
const result = npxToYarn("npx medusa", "yarn") |
|
77
|
expect(result).toBe("yarn medusa") |
|
78
|
}) |
|
79
|
|
|
80
|
it("should preserve command structure with special characters", () => { |
|
81
|
const result = npxToYarn("npx medusa db:seed --file=./data.json", "pnpm") |
|
82
|
expect(result).toBe("pnpm medusa db:seed --file=./data.json") |
|
83
|
}) |
|
84
|
|
|
85
|
it("should handle command with path separators", () => { |
|
86
|
const result = npxToYarn("npx @medusajs/medusa-cli develop", "yarn") |
|
87
|
expect(result).toBe("yarn @medusajs/medusa-cli develop") |
|
88
|
}) |
|
89
|
|
|
90
|
it("should handle multi-line commands with backslash continuation", () => { |
|
91
|
const multiLineCommand = `npx create-medusa-app@latest \\ |
|
92
|
--db-url postgres://localhost/medusa \\ |
|
93
|
--skip-db` |
|
94
|
const result = npxToYarn(multiLineCommand, "yarn", true) |
|
95
|
expect(result).toBe(`yarn dlx create-medusa-app@latest \\ |
|
96
|
--db-url postgres://localhost/medusa \\ |
|
97
|
--skip-db`) |
|
98
|
}) |
|
99
|
|
|
100
|
it("should handle multi-line commands for pnpm", () => { |
|
101
|
const multiLineCommand = `npx medusa develop \\ |
|
102
|
--port 9000 \\ |
|
103
|
--verbose` |
|
104
|
const result = npxToYarn(multiLineCommand, "pnpm") |
|
105
|
expect(result).toBe(`pnpm medusa develop \\ |
|
106
|
--port 9000 \\ |
|
107
|
--verbose`) |
|
108
|
}) |
|
109
|
|
|
110
|
it("should handle commands with newlines", () => { |
|
111
|
const commandWithNewlines = "npx create-medusa-app@latest\n --db-url postgres://localhost/medusa" |
|
112
|
const result = npxToYarn(commandWithNewlines, "yarn", true) |
|
113
|
expect(result).toBe("yarn dlx create-medusa-app@latest\n --db-url postgres://localhost/medusa") |
|
114
|
}) |
|
115
|
|
|
116
|
it("should convert multiple npx commands on separate lines for yarn", () => { |
|
117
|
const multipleCommands = `npx medusa db:migrate |
|
118
|
npx medusa develop` |
|
119
|
const result = npxToYarn(multipleCommands, "yarn") |
|
120
|
expect(result).toBe(`yarn medusa db:migrate |
|
121
|
yarn medusa develop`) |
|
122
|
}) |
|
123
|
|
|
124
|
it("should convert multiple npx commands on separate lines for pnpm", () => { |
|
125
|
const multipleCommands = `npx medusa db:migrate |
|
126
|
npx medusa user --email admin@test.com |
|
127
|
npx medusa develop --port 9000` |
|
128
|
const result = npxToYarn(multipleCommands, "pnpm") |
|
129
|
expect(result).toBe(`pnpm medusa db:migrate |
|
130
|
pnpm medusa user --email admin@test.com |
|
131
|
pnpm medusa develop --port 9000`) |
|
132
|
}) |
|
133
|
|
|
134
|
it("should convert multiple npx commands with executable flag", () => { |
|
135
|
const multipleCommands = `npx create-medusa-app@latest |
|
136
|
npx @medusajs/medusa-cli init` |
|
137
|
const result = npxToYarn(multipleCommands, "yarn", true) |
|
138
|
expect(result).toBe(`yarn dlx create-medusa-app@latest |
|
139
|
yarn dlx @medusajs/medusa-cli init`) |
|
140
|
}) |
|
141
|
|
|
142
|
it("should preserve indentation when converting multiple commands", () => { |
|
143
|
const indentedCommands = `npx medusa db:migrate |
|
144
|
npx medusa develop |
|
145
|
npx medusa user` |
|
146
|
const result = npxToYarn(indentedCommands, "pnpm") |
|
147
|
expect(result).toBe(`pnpm medusa db:migrate |
|
148
|
pnpm medusa develop |
|
149
|
pnpm medusa user`) |
|
150
|
}) |
|
151
|
|
|
152
|
it("should handle mixed npx and non-npx lines", () => { |
|
153
|
const mixedCommands = `npx medusa db:migrate |
|
154
|
echo "Migration complete" |
|
155
|
npx medusa develop` |
|
156
|
const result = npxToYarn(mixedCommands, "yarn") |
|
157
|
expect(result).toBe(`yarn medusa db:migrate |
|
158
|
echo "Migration complete" |
|
159
|
yarn medusa develop`) |
|
160
|
}) |
|
161
|
}) |
|
162
|
}) |