|
1
|
import fs from "fs" |
|
2
|
import inquirer from "inquirer" |
|
3
|
import path from "path" |
|
4
|
import slugifyType from "slugify" |
|
5
|
import logMessage from "../log-message.js" |
|
6
|
import { |
|
7
|
getNodeVersion, |
|
8
|
isNodeVersionSupported, |
|
9
|
MIN_SUPPORTED_NODE_VERSION, |
|
10
|
} from "../node-version.js" |
|
11
|
import { ProjectCreator, ProjectOptions } from "./creator.js" |
|
12
|
import { PluginProjectCreator } from "./medusa-plugin-creator.js" |
|
13
|
import { MedusaProjectCreator } from "./medusa-project-creator.js" |
|
14
|
import terminalLink from "terminal-link" |
|
15
|
|
|
16
|
const slugify = slugifyType.default |
|
17
|
|
|
18
|
export class ProjectCreatorFactory { |
|
19
|
static async create( |
|
20
|
args: string[], |
|
21
|
options: ProjectOptions |
|
22
|
): Promise<ProjectCreator> { |
|
23
|
ProjectCreatorFactory.validateNodeVersion() |
|
24
|
|
|
25
|
const projectName = await ProjectCreatorFactory.getProjectName( |
|
26
|
args, |
|
27
|
options.directoryPath, |
|
28
|
options.plugin |
|
29
|
) |
|
30
|
|
|
31
|
return options.plugin |
|
32
|
? new PluginProjectCreator(projectName, options, args) |
|
33
|
: new MedusaProjectCreator(projectName, options, args) |
|
34
|
} |
|
35
|
|
|
36
|
private static validateNodeVersion(): void { |
|
37
|
const nodeVersion = getNodeVersion() |
|
38
|
if (!isNodeVersionSupported(nodeVersion)) { |
|
39
|
logMessage({ |
|
40
|
message: `Medusa requires at least v${MIN_SUPPORTED_NODE_VERSION} of Node.js. You're using v${nodeVersion}. Please ${terminalLink( |
|
41
|
"install Node.js", |
|
42
|
"https://nodejs.org/en/download" |
|
43
|
)} v${MIN_SUPPORTED_NODE_VERSION} or later and try again.`, |
|
44
|
type: "error", |
|
45
|
}) |
|
46
|
} |
|
47
|
} |
|
48
|
|
|
49
|
private static async getProjectName( |
|
50
|
args: string[], |
|
51
|
directoryPath?: string, |
|
52
|
isPlugin?: boolean |
|
53
|
): Promise<string> { |
|
54
|
let askProjectName = args.length === 0 |
|
55
|
if (args.length > 0) { |
|
56
|
const projectPath = path.join(directoryPath || "", args[0]) |
|
57
|
if ( |
|
58
|
fs.existsSync(projectPath) && |
|
59
|
fs.lstatSync(projectPath).isDirectory() |
|
60
|
) { |
|
61
|
logMessage({ |
|
62
|
message: `A directory already exists with the name ${ |
|
63
|
args[0] |
|
64
|
}. Please enter a different ${isPlugin ? "plugin" : "project"} name.`, |
|
65
|
type: "warn", |
|
66
|
}) |
|
67
|
askProjectName = true |
|
68
|
} else if (args[0].includes(".")) { |
|
69
|
// We don't allow projects to have a dot in the name, as this causes issues for |
|
70
|
// for MikroORM path resolutions. |
|
71
|
logMessage({ |
|
72
|
message: `Project names cannot contain a dot (.) character. Please enter a different ${ |
|
73
|
isPlugin ? "plugin" : "project" |
|
74
|
} name.`, |
|
75
|
type: "error", |
|
76
|
}) |
|
77
|
askProjectName = true |
|
78
|
} |
|
79
|
} |
|
80
|
|
|
81
|
return askProjectName |
|
82
|
? await askForProjectName(directoryPath, isPlugin) |
|
83
|
: args[0] |
|
84
|
} |
|
85
|
} |
|
86
|
|
|
87
|
async function askForProjectName( |
|
88
|
directoryPath?: string, |
|
89
|
isPlugin?: boolean |
|
90
|
): Promise<string> { |
|
91
|
const { projectName } = await inquirer.prompt([ |
|
92
|
{ |
|
93
|
type: "input", |
|
94
|
name: "projectName", |
|
95
|
message: `What's the name of your ${isPlugin ? "plugin" : "project"}?`, |
|
96
|
default: isPlugin ? "my-medusa-plugin" : "my-medusa-store", |
|
97
|
filter: (input) => { |
|
98
|
return slugify(input).toLowerCase() |
|
99
|
}, |
|
100
|
validate: (input) => { |
|
101
|
// We don't allow projects to have a dot in the name, as this causes issues for |
|
102
|
// for MikroORM path resolutions. |
|
103
|
if (input.includes(".")) { |
|
104
|
return `Project names cannot contain a dot (.) character. Please enter a different ${ |
|
105
|
isPlugin ? "plugin" : "project" |
|
106
|
} name.` |
|
107
|
} |
|
108
|
|
|
109
|
if (!input.length) { |
|
110
|
return `Please enter a ${isPlugin ? "plugin" : "project"} name` |
|
111
|
} |
|
112
|
const projectPath = path.join(directoryPath || "", input) |
|
113
|
return fs.existsSync(projectPath) && |
|
114
|
fs.lstatSync(projectPath).isDirectory() |
|
115
|
? `A directory already exists with the same name. Please enter a different ${ |
|
116
|
isPlugin ? "plugin" : "project" |
|
117
|
} name.` |
|
118
|
: true |
|
119
|
}, |
|
120
|
}, |
|
121
|
]) |
|
122
|
return projectName |
|
123
|
} |