|
1
|
import fs from "fs" |
|
2
|
import { Ora } from "ora" |
|
3
|
import path from "path" |
|
4
|
import { isAbortError } from "./create-abort-controller.js" |
|
5
|
import execute from "./execute.js" |
|
6
|
import logMessage from "./log-message.js" |
|
7
|
import { execFileSync } from "child_process" |
|
8
|
|
|
9
|
type CloneRepoOptions = { |
|
10
|
directoryName?: string |
|
11
|
repoUrl?: string |
|
12
|
abortController?: AbortController |
|
13
|
verbose?: boolean |
|
14
|
isPlugin?: boolean |
|
15
|
} |
|
16
|
|
|
17
|
const DEFAULT_REPO = "https://github.com/medusajs/dtc-starter" |
|
18
|
const DEFAULT_PLUGIN_REPO = "https://github.com/medusajs/medusa-starter-plugin" |
|
19
|
const BRANCH = "main" |
|
20
|
const PLUGIN_BRANCH = "main" |
|
21
|
|
|
22
|
export default async function cloneRepo({ |
|
23
|
directoryName = "", |
|
24
|
repoUrl, |
|
25
|
abortController, |
|
26
|
verbose = false, |
|
27
|
isPlugin = false, |
|
28
|
}: CloneRepoOptions) { |
|
29
|
const defaultRepo = isPlugin ? DEFAULT_PLUGIN_REPO : DEFAULT_REPO |
|
30
|
const branch = isPlugin ? PLUGIN_BRANCH : BRANCH |
|
31
|
|
|
32
|
await execute( |
|
33
|
[ |
|
34
|
`git clone ${ |
|
35
|
repoUrl || defaultRepo |
|
36
|
} -b ${branch} ${directoryName} --depth 1`, |
|
37
|
{ |
|
38
|
signal: abortController?.signal, |
|
39
|
}, |
|
40
|
], |
|
41
|
{ verbose } |
|
42
|
) |
|
43
|
} |
|
44
|
|
|
45
|
export async function runCloneRepo({ |
|
46
|
projectName, |
|
47
|
repoUrl, |
|
48
|
abortController, |
|
49
|
spinner, |
|
50
|
verbose = false, |
|
51
|
isPlugin = false, |
|
52
|
}: { |
|
53
|
projectName: string |
|
54
|
repoUrl: string |
|
55
|
abortController: AbortController |
|
56
|
spinner: Ora |
|
57
|
verbose?: boolean |
|
58
|
isPlugin?: boolean |
|
59
|
}) { |
|
60
|
try { |
|
61
|
await cloneRepo({ |
|
62
|
directoryName: projectName, |
|
63
|
repoUrl, |
|
64
|
abortController, |
|
65
|
verbose, |
|
66
|
isPlugin, |
|
67
|
}) |
|
68
|
|
|
69
|
deleteGitDirectory(projectName) |
|
70
|
} catch (e) { |
|
71
|
if (isAbortError(e)) { |
|
72
|
process.exit() |
|
73
|
} |
|
74
|
|
|
75
|
spinner.stop() |
|
76
|
logMessage({ |
|
77
|
message: `An error occurred while setting up your project: ${e}`, |
|
78
|
type: "error", |
|
79
|
}) |
|
80
|
} |
|
81
|
} |
|
82
|
|
|
83
|
function deleteGitDirectory(projectDirectory: string) { |
|
84
|
try { |
|
85
|
fs.rmSync(path.join(projectDirectory, ".git"), { |
|
86
|
recursive: true, |
|
87
|
force: true, |
|
88
|
}) |
|
89
|
} catch (error) { |
|
90
|
deleteWithCommand(projectDirectory, ".git") |
|
91
|
} |
|
92
|
|
|
93
|
try { |
|
94
|
fs.rmSync(path.join(projectDirectory, ".github"), { |
|
95
|
recursive: true, |
|
96
|
force: true, |
|
97
|
}) |
|
98
|
} catch (error) { |
|
99
|
deleteWithCommand(projectDirectory, ".github") |
|
100
|
} |
|
101
|
} |
|
102
|
|
|
103
|
/** |
|
104
|
* Useful for deleting directories when fs methods fail (e.g., with Yarn v3) |
|
105
|
*/ |
|
106
|
function deleteWithCommand(projectDirectory: string, dirName: string) { |
|
107
|
const dirPath = path.normalize(path.join(projectDirectory, dirName)) |
|
108
|
if (!fs.existsSync(dirPath)) { |
|
109
|
return |
|
110
|
} |
|
111
|
|
|
112
|
if (process.platform === "win32") { |
|
113
|
execFileSync("cmd", ["/c", "rmdir", "/s", "/q", dirPath]) |
|
114
|
} else { |
|
115
|
execFileSync("rm", ["-rf", dirPath]) |
|
116
|
} |
|
117
|
} |