|
1
|
/** |
|
2
|
* Converts an npx command to its yarn or pnpm equivalent |
|
3
|
* Assumes the package is installed locally in node_modules |
|
4
|
* @param npxCommand - The npx command to convert (e.g., "npx medusa db:migrate") |
|
5
|
* @param packageManager - The target package manager ("yarn" or "pnpm") |
|
6
|
* @param isExecutable - Whether the command is being run as an executable (default: false) |
|
7
|
* @returns The converted command |
|
8
|
* |
|
9
|
* @example |
|
10
|
* npxToYarn("npx medusa db:migrate", "yarn") // "yarn medusa db:migrate" |
|
11
|
* npxToYarn("npx medusa db:migrate", "pnpm") // "pnpm medusa db:migrate" |
|
12
|
* npxToYarn("npx create-medusa-app@latest \\\n --db-url postgres://localhost/medusa", "yarn", true) |
|
13
|
* // "yarn dlx create-medusa-app@latest \\\n --db-url postgres://localhost/medusa" |
|
14
|
* npxToYarn("npx medusa db:migrate\nnpx medusa develop", "yarn") |
|
15
|
* // "yarn medusa db:migrate\nyarn medusa develop" |
|
16
|
*/ |
|
17
|
export function npxToYarn( |
|
18
|
npxCommand: string, |
|
19
|
packageManager: "yarn" | "pnpm", |
|
20
|
isExecutable: boolean = false |
|
21
|
): string { |
|
22
|
// Remove leading/trailing whitespace |
|
23
|
const trimmed = npxCommand.trim() |
|
24
|
|
|
25
|
// Split by lines to handle multiple commands |
|
26
|
const lines = trimmed.split("\n") |
|
27
|
|
|
28
|
const convertedLines = lines.map((line) => { |
|
29
|
const trimmedLine = line.trim() |
|
30
|
|
|
31
|
// Check if line starts with npx |
|
32
|
if (!trimmedLine.startsWith("npx ")) { |
|
33
|
return line |
|
34
|
} |
|
35
|
|
|
36
|
// Remove "npx " prefix and replace with the target package manager |
|
37
|
const command = trimmedLine.slice(4) |
|
38
|
const leadingWhitespace = line.match(/^(\s*)/)?.[1] || "" |
|
39
|
|
|
40
|
let converted: string |
|
41
|
if (packageManager === "yarn") { |
|
42
|
converted = isExecutable ? `yarn dlx ${command}` : `yarn ${command}` |
|
43
|
} else if (packageManager === "pnpm") { |
|
44
|
converted = isExecutable ? `pnpm dlx ${command}` : `pnpm ${command}` |
|
45
|
} else { |
|
46
|
converted = trimmedLine |
|
47
|
} |
|
48
|
|
|
49
|
return leadingWhitespace + converted |
|
50
|
}) |
|
51
|
|
|
52
|
return convertedLines.join("\n") |
|
53
|
} |