create-medusa-app 2.22.5 → 2.22.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/commands/create.js +10 -0
- package/dist/index.js +0 -0
- package/dist/utils/claude-code-plugin.js +76 -0
- package/dist/utils/clone-repo.js +77 -0
- package/dist/utils/create-abort-controller.js +11 -0
- package/dist/utils/create-db.js +188 -0
- package/dist/utils/execute.js +51 -0
- package/dist/utils/facts.js +87 -0
- package/dist/utils/format-connection-string.js +17 -0
- package/dist/utils/get-current-os.js +10 -0
- package/dist/utils/log-message.js +21 -0
- package/dist/utils/logger.js +8 -0
- package/dist/utils/nextjs-utils.js +55 -0
- package/dist/utils/node-version.js +15 -0
- package/dist/utils/package-manager.js +354 -0
- package/dist/utils/postgres-client.js +9 -0
- package/dist/utils/prepare-project.js +195 -0
- package/dist/utils/process-manager.js +50 -0
- package/dist/utils/project-creator/creator.js +46 -0
- package/dist/utils/project-creator/index.js +4 -0
- package/dist/utils/project-creator/medusa-plugin-creator.js +96 -0
- package/dist/utils/project-creator/medusa-project-creator.js +215 -0
- package/dist/utils/project-creator/project-creator-factory.js +83 -0
- package/dist/utils/project-paths.js +7 -0
- package/dist/utils/start-medusa.js +13 -0
- package/dist/utils/update-package-versions.js +36 -0
- package/package.json +4 -4
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import execute from "./execute.js";
|
|
3
|
+
import logMessage from "./log-message.js";
|
|
4
|
+
import { existsSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
5
|
+
import { parse as parseYaml } from "yaml";
|
|
6
|
+
export default class PackageManager {
|
|
7
|
+
packageManager;
|
|
8
|
+
packageManagerVersion;
|
|
9
|
+
processManager;
|
|
10
|
+
verbose;
|
|
11
|
+
chosenPackageManager;
|
|
12
|
+
constructor(processManager, options = {}) {
|
|
13
|
+
this.processManager = processManager;
|
|
14
|
+
this.verbose = options.verbose || false;
|
|
15
|
+
if (options.useNpm) {
|
|
16
|
+
this.chosenPackageManager = "npm";
|
|
17
|
+
}
|
|
18
|
+
else if (options.usePnpm) {
|
|
19
|
+
this.chosenPackageManager = "pnpm";
|
|
20
|
+
}
|
|
21
|
+
else if (options.useYarn) {
|
|
22
|
+
this.chosenPackageManager = "yarn";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
detectFromUserAgent() {
|
|
26
|
+
const userAgent = process.env.npm_config_user_agent;
|
|
27
|
+
if (!userAgent) {
|
|
28
|
+
return { manager: "npm" };
|
|
29
|
+
}
|
|
30
|
+
// Extract package manager and version (e.g., "yarn/4.9.0" -> ["yarn", "4.9.0"])
|
|
31
|
+
const match = userAgent.match(/(pnpm|pnpx|yarn|npm|nub)\/(\d+\.\d+\.\d+)/);
|
|
32
|
+
if (match) {
|
|
33
|
+
const [, manager, version] = match;
|
|
34
|
+
if (this.verbose) {
|
|
35
|
+
logMessage({
|
|
36
|
+
type: "info",
|
|
37
|
+
message: `Detected from user agent: ${manager}@${version}`,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
// pnpx is an alias for pnpm
|
|
41
|
+
if (manager === "pnpx") {
|
|
42
|
+
return { manager: "pnpm", version };
|
|
43
|
+
}
|
|
44
|
+
return { manager: manager, version };
|
|
45
|
+
}
|
|
46
|
+
// Fallback detection without version
|
|
47
|
+
if (userAgent.includes("pnpm") || userAgent.includes("pnpx")) {
|
|
48
|
+
return { manager: "pnpm" };
|
|
49
|
+
}
|
|
50
|
+
if (userAgent.includes("yarn")) {
|
|
51
|
+
return { manager: "yarn" };
|
|
52
|
+
}
|
|
53
|
+
if (userAgent.includes("nub")) {
|
|
54
|
+
return { manager: "nub" };
|
|
55
|
+
}
|
|
56
|
+
return { manager: "npm" };
|
|
57
|
+
}
|
|
58
|
+
async getVersion(pm, execOptions) {
|
|
59
|
+
const commands = {
|
|
60
|
+
yarn: "yarn -v",
|
|
61
|
+
pnpm: "pnpm -v",
|
|
62
|
+
npm: "npm -v",
|
|
63
|
+
nub: "nub -v",
|
|
64
|
+
};
|
|
65
|
+
try {
|
|
66
|
+
const result = await execute([commands[pm], execOptions], {
|
|
67
|
+
verbose: false,
|
|
68
|
+
});
|
|
69
|
+
const version = result.stdout?.trim();
|
|
70
|
+
if (this.verbose) {
|
|
71
|
+
logMessage({
|
|
72
|
+
type: "info",
|
|
73
|
+
message: `Detected ${pm} version: ${version}`,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return version;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
if (this.verbose) {
|
|
80
|
+
logMessage({
|
|
81
|
+
type: "info",
|
|
82
|
+
message: `Failed to get version for package manager: ${pm}`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async setPackageManager(execOptions) {
|
|
89
|
+
if (this.packageManager) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
// check whether package manager is available and get version
|
|
93
|
+
await this.processManager.runProcess({
|
|
94
|
+
process: async () => {
|
|
95
|
+
if (this.chosenPackageManager) {
|
|
96
|
+
const version = await this.getVersion(this.chosenPackageManager, execOptions);
|
|
97
|
+
if (version) {
|
|
98
|
+
this.packageManager = this.chosenPackageManager;
|
|
99
|
+
// Store version if we don't have it from user agent
|
|
100
|
+
if (!this.packageManagerVersion) {
|
|
101
|
+
this.packageManagerVersion = version;
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
// Error logs exit the process, so command execution will stop here
|
|
106
|
+
logMessage({
|
|
107
|
+
type: "error",
|
|
108
|
+
message: `The specified package manager "${this.chosenPackageManager}" is not available. Please install it or choose another package manager.`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const detectedResult = this.detectFromUserAgent();
|
|
112
|
+
// fallback to npm if detection fails
|
|
113
|
+
this.packageManager = detectedResult.manager || "npm";
|
|
114
|
+
this.packageManagerVersion = detectedResult.version;
|
|
115
|
+
if (!this.packageManagerVersion) {
|
|
116
|
+
// get version for the detected package manager (or npm fallback)
|
|
117
|
+
this.packageManagerVersion = await this.getVersion(this.packageManager, execOptions);
|
|
118
|
+
if (this.verbose) {
|
|
119
|
+
logMessage({
|
|
120
|
+
type: "info",
|
|
121
|
+
message: `Falling back to ${this.packageManager} as the package manager.`,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
if (this.verbose) {
|
|
127
|
+
logMessage({
|
|
128
|
+
type: "info",
|
|
129
|
+
message: `Using detected package manager "${this.packageManager}".`,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
ignoreERESOLVE: true,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
applyYarnWorkspaceChanges(directory) {
|
|
138
|
+
writeFileSync(path.join(directory, "yarn.lock"), "");
|
|
139
|
+
writeFileSync(path.join(directory, ".yarnrc.yml"), "nodeLinker: node-modules\n");
|
|
140
|
+
}
|
|
141
|
+
applyNpmWorkspaceChanges(packageJson) {
|
|
142
|
+
// npm's flat hoisting can only keep a single ajv major at the workspace
|
|
143
|
+
// root, but Medusa's stack needs two: the backend's migration tooling
|
|
144
|
+
// (@mikro-orm/migrations -> umzug -> @rushstack/node-core-library ->
|
|
145
|
+
// ajv-draft-04) does `require('ajv/dist/core')`, which only exists in ajv
|
|
146
|
+
// v8+, while ESLint (`eslint` core and `@eslint/eslintrc`) needs ajv v6.
|
|
147
|
+
// Both `ajv-draft-04` and `eslint` hoist to the root and resolve `ajv`
|
|
148
|
+
// from there, so whichever major loses the root slot breaks. We pin v8 to
|
|
149
|
+
// the root with a direct devDependency (this is what the hoisted
|
|
150
|
+
// `ajv-draft-04` resolves against) and scope v6 back under ESLint via
|
|
151
|
+
// overrides, which npm then nests. This lets both the backend
|
|
152
|
+
// (`medusa db:migrate`/`build`) and the storefront (`next lint`) resolve a
|
|
153
|
+
// compatible ajv from a single npm install.
|
|
154
|
+
packageJson.devDependencies = {
|
|
155
|
+
...packageJson.devDependencies,
|
|
156
|
+
ajv: "^8.13.0",
|
|
157
|
+
};
|
|
158
|
+
packageJson.overrides = {
|
|
159
|
+
...packageJson.overrides,
|
|
160
|
+
eslint: {
|
|
161
|
+
...(packageJson.overrides?.eslint ?? {}),
|
|
162
|
+
ajv: "^6.12.4",
|
|
163
|
+
},
|
|
164
|
+
"@eslint/eslintrc": {
|
|
165
|
+
...(packageJson.overrides?.["@eslint/eslintrc"] ?? {}),
|
|
166
|
+
ajv: "^6.12.4",
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
async transformWorkspaceConfig(directory) {
|
|
171
|
+
if (this.packageManager === "pnpm") {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const workspaceYamlPath = path.join(directory, "pnpm-workspace.yaml");
|
|
175
|
+
if (!existsSync(workspaceYamlPath)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const yamlContent = readFileSync(workspaceYamlPath, "utf-8");
|
|
179
|
+
const workspacePackages = parseYaml(yamlContent)?.packages ?? [];
|
|
180
|
+
const packageJsonPath = path.join(directory, "package.json");
|
|
181
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
182
|
+
packageJson.workspaces = workspacePackages;
|
|
183
|
+
if (packageJson.scripts) {
|
|
184
|
+
const pm = this.packageManager;
|
|
185
|
+
packageJson.scripts = Object.fromEntries(Object.entries(packageJson.scripts).map(([key, value]) => [
|
|
186
|
+
key,
|
|
187
|
+
value
|
|
188
|
+
.replace(/\bpnpm\s+-r\s+(\S+)/g, "turbo $1")
|
|
189
|
+
.replace(/\bpnpm\b/g, pm),
|
|
190
|
+
]));
|
|
191
|
+
}
|
|
192
|
+
if (this.packageManager === "yarn") {
|
|
193
|
+
this.applyYarnWorkspaceChanges(directory);
|
|
194
|
+
}
|
|
195
|
+
else if (this.packageManager === "npm") {
|
|
196
|
+
this.applyNpmWorkspaceChanges(packageJson);
|
|
197
|
+
}
|
|
198
|
+
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
199
|
+
rmSync(workspaceYamlPath, { force: true });
|
|
200
|
+
}
|
|
201
|
+
async removeLockFiles(directory) {
|
|
202
|
+
const lockFiles = {
|
|
203
|
+
npm: ["yarn.lock", "pnpm-lock.yaml", ".yarn"],
|
|
204
|
+
yarn: ["package-lock.json", "pnpm-lock.yaml"],
|
|
205
|
+
pnpm: ["yarn.lock", "package-lock.json", ".yarn"],
|
|
206
|
+
nub: ["yarn.lock", "package-lock.json", "pnpm-lock.yaml", ".yarn"],
|
|
207
|
+
};
|
|
208
|
+
if (!this.packageManager) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const filesToRemove = lockFiles[this.packageManager] || [];
|
|
212
|
+
for (const file of filesToRemove) {
|
|
213
|
+
const filePath = path.join(directory, file);
|
|
214
|
+
if (existsSync(filePath)) {
|
|
215
|
+
rmSync(filePath, {
|
|
216
|
+
force: true,
|
|
217
|
+
recursive: true,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async installDependencies(execOptions, installOptions) {
|
|
223
|
+
if (!this.packageManager) {
|
|
224
|
+
await this.setPackageManager(execOptions);
|
|
225
|
+
}
|
|
226
|
+
if (execOptions.cwd && typeof execOptions.cwd === "string") {
|
|
227
|
+
await this.transformWorkspaceConfig(execOptions.cwd);
|
|
228
|
+
await this.removeLockFiles(execOptions.cwd);
|
|
229
|
+
}
|
|
230
|
+
const commands = {
|
|
231
|
+
yarn: "yarn",
|
|
232
|
+
pnpm: "pnpm install",
|
|
233
|
+
npm: `npm install${installOptions?.installLegacyPeerDeps ? " --legacy-peer-deps" : ""}`,
|
|
234
|
+
nub: "nub install",
|
|
235
|
+
};
|
|
236
|
+
const command = commands[this.packageManager || "npm"];
|
|
237
|
+
await this.processManager.runProcess({
|
|
238
|
+
process: async () => {
|
|
239
|
+
try {
|
|
240
|
+
await execute([command, execOptions], {
|
|
241
|
+
verbose: this.verbose,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
const errorStr = typeof error === "string"
|
|
246
|
+
? error
|
|
247
|
+
: error?.stderr ||
|
|
248
|
+
error?.message ||
|
|
249
|
+
String(error);
|
|
250
|
+
const packageManagerConflictMatch = errorStr.match(/This project is configured to use (\w+) because (.+) has a "packageManager" field/);
|
|
251
|
+
if (this.packageManager === "pnpm" && packageManagerConflictMatch) {
|
|
252
|
+
const conflictingFile = packageManagerConflictMatch[2];
|
|
253
|
+
throw new Error(`Cannot install with pnpm because ${conflictingFile} has a "packageManager" field that specifies a different package manager. Consider removing that field from ${conflictingFile}, or create your Medusa project in a different directory.`);
|
|
254
|
+
}
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
// For npm, run npm ci after npm install to validate installation
|
|
258
|
+
if (this.packageManager === "npm") {
|
|
259
|
+
try {
|
|
260
|
+
await execute(["npm ci", execOptions], {
|
|
261
|
+
verbose: this.verbose,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
// If npm ci fails, re-run npm install
|
|
266
|
+
if (this.verbose) {
|
|
267
|
+
logMessage({
|
|
268
|
+
type: "info",
|
|
269
|
+
message: "npm ci validation failed, re-running npm install...",
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
await execute(["npm install", execOptions], {
|
|
273
|
+
verbose: this.verbose,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
ignoreERESOLVE: true,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
async runCommand(command, execOptions, verboseOptions = {}) {
|
|
282
|
+
if (!this.packageManager) {
|
|
283
|
+
await this.setPackageManager(execOptions);
|
|
284
|
+
}
|
|
285
|
+
const commandStr = this.getCommandStr(command);
|
|
286
|
+
return await this.processManager.runProcess({
|
|
287
|
+
process: async () => {
|
|
288
|
+
return await execute([commandStr, execOptions], {
|
|
289
|
+
verbose: this.verbose,
|
|
290
|
+
...verboseOptions,
|
|
291
|
+
});
|
|
292
|
+
},
|
|
293
|
+
ignoreERESOLVE: true,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
async runMedusaCommand(command, execOptions, verboseOptions = {}) {
|
|
297
|
+
if (!this.packageManager) {
|
|
298
|
+
await this.setPackageManager(execOptions);
|
|
299
|
+
}
|
|
300
|
+
const formats = {
|
|
301
|
+
yarn: `yarn medusa ${command}`,
|
|
302
|
+
pnpm: `pnpm medusa ${command}`,
|
|
303
|
+
npm: `npx medusa ${command}`,
|
|
304
|
+
nub: `nubx medusa ${command}`,
|
|
305
|
+
};
|
|
306
|
+
const commandStr = formats[this.packageManager || "npm"];
|
|
307
|
+
return await this.processManager.runProcess({
|
|
308
|
+
process: async () => {
|
|
309
|
+
return await execute([commandStr, execOptions], {
|
|
310
|
+
verbose: this.verbose,
|
|
311
|
+
...verboseOptions,
|
|
312
|
+
});
|
|
313
|
+
},
|
|
314
|
+
ignoreERESOLVE: true,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
getCommandStr(command) {
|
|
318
|
+
if (!this.packageManager) {
|
|
319
|
+
throw new Error("Package manager not set");
|
|
320
|
+
}
|
|
321
|
+
const formats = {
|
|
322
|
+
yarn: `yarn ${command}`,
|
|
323
|
+
pnpm: `pnpm ${command}`,
|
|
324
|
+
npm: `npm run ${command}`,
|
|
325
|
+
nub: `nub run ${command}`,
|
|
326
|
+
};
|
|
327
|
+
return formats[this.packageManager];
|
|
328
|
+
}
|
|
329
|
+
getPackageManager() {
|
|
330
|
+
return this.packageManager;
|
|
331
|
+
}
|
|
332
|
+
async getPackageManagerString() {
|
|
333
|
+
if (!this.packageManager) {
|
|
334
|
+
await this.setPackageManager({});
|
|
335
|
+
}
|
|
336
|
+
if (!this.packageManagerVersion) {
|
|
337
|
+
if (this.verbose) {
|
|
338
|
+
logMessage({
|
|
339
|
+
type: "info",
|
|
340
|
+
message: `No version detected for package manager: ${this.packageManager}`,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
const result = `${this.packageManager}@${this.packageManagerVersion}`;
|
|
346
|
+
if (this.verbose) {
|
|
347
|
+
logMessage({
|
|
348
|
+
type: "info",
|
|
349
|
+
message: `Package manager string: ${result}`,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
return result;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { randomBytes } from "crypto";
|
|
4
|
+
import { EOL } from "os";
|
|
5
|
+
import { displayFactBox } from "./facts.js";
|
|
6
|
+
import { updatePackageVersions } from "./update-package-versions.js";
|
|
7
|
+
import { getBackendDirectory } from "./project-paths.js";
|
|
8
|
+
const ADMIN_EMAIL = "admin@medusa-test.com";
|
|
9
|
+
let STORE_CORS = "http://localhost:8000";
|
|
10
|
+
let ADMIN_CORS = "http://localhost:5173,http://localhost:9000";
|
|
11
|
+
const DOCS_CORS = "https://docs.medusajs.com";
|
|
12
|
+
const AUTH_CORS = [ADMIN_CORS, STORE_CORS, DOCS_CORS].join(",");
|
|
13
|
+
STORE_CORS += `,${DOCS_CORS}`;
|
|
14
|
+
ADMIN_CORS += `,${DOCS_CORS}`;
|
|
15
|
+
const DEFAULT_REDIS_URL = "redis://localhost:6379";
|
|
16
|
+
const AUTH_MFA_ENCRYPTION_KEY = "AUTH_MFA_ENCRYPTION_KEY";
|
|
17
|
+
export default async (prepareOptions) => {
|
|
18
|
+
if (prepareOptions.isPlugin) {
|
|
19
|
+
return preparePlugin(prepareOptions);
|
|
20
|
+
}
|
|
21
|
+
return prepareProject(prepareOptions);
|
|
22
|
+
};
|
|
23
|
+
async function preparePlugin({ directory, projectName, spinner, processManager, abortController, verbose = false, packageManager, version, }) {
|
|
24
|
+
// initialize execution options
|
|
25
|
+
const execOptions = {
|
|
26
|
+
cwd: directory,
|
|
27
|
+
signal: abortController?.signal,
|
|
28
|
+
};
|
|
29
|
+
const factBoxOptions = {
|
|
30
|
+
interval: null,
|
|
31
|
+
spinner,
|
|
32
|
+
processManager,
|
|
33
|
+
message: "",
|
|
34
|
+
title: "",
|
|
35
|
+
verbose,
|
|
36
|
+
};
|
|
37
|
+
// Update package.json
|
|
38
|
+
const packageJsonPath = path.join(directory, "package.json");
|
|
39
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
|
|
40
|
+
// Update name
|
|
41
|
+
packageJson.name = projectName;
|
|
42
|
+
// Add packageManager field to ensure consistent version usage
|
|
43
|
+
const packageManagerString = await packageManager.getPackageManagerString();
|
|
44
|
+
if (packageManagerString) {
|
|
45
|
+
packageJson.packageManager = packageManagerString;
|
|
46
|
+
}
|
|
47
|
+
// Update medusa dependencies versions
|
|
48
|
+
if (version) {
|
|
49
|
+
updatePackageVersions(packageJson, version);
|
|
50
|
+
}
|
|
51
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
52
|
+
factBoxOptions.interval = displayFactBox({
|
|
53
|
+
...factBoxOptions,
|
|
54
|
+
spinner,
|
|
55
|
+
title: "Installing dependencies...",
|
|
56
|
+
processManager,
|
|
57
|
+
});
|
|
58
|
+
await packageManager.installDependencies(execOptions);
|
|
59
|
+
factBoxOptions.interval = displayFactBox({
|
|
60
|
+
...factBoxOptions,
|
|
61
|
+
message: "Installed Dependencies",
|
|
62
|
+
});
|
|
63
|
+
displayFactBox({ ...factBoxOptions, message: "Finished Preparation" });
|
|
64
|
+
}
|
|
65
|
+
async function prepareProject({ directory, projectName, dbName, dbConnectionString, seed, spinner, processManager, abortController, skipDb, migrations, onboardingType = "default", nextjsDirectory = "", client, verbose = false, packageManager, version, }) {
|
|
66
|
+
const backendDirectory = getBackendDirectory(directory);
|
|
67
|
+
// initialize execution options
|
|
68
|
+
const execOptions = {
|
|
69
|
+
cwd: directory,
|
|
70
|
+
signal: abortController?.signal,
|
|
71
|
+
};
|
|
72
|
+
const backendExecOptions = {
|
|
73
|
+
cwd: backendDirectory,
|
|
74
|
+
signal: abortController?.signal,
|
|
75
|
+
};
|
|
76
|
+
const npxOptions = {
|
|
77
|
+
...backendExecOptions,
|
|
78
|
+
env: {
|
|
79
|
+
...process.env,
|
|
80
|
+
npm_config_yes: "yes",
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
const factBoxOptions = {
|
|
84
|
+
interval: null,
|
|
85
|
+
spinner,
|
|
86
|
+
processManager,
|
|
87
|
+
message: "",
|
|
88
|
+
title: "",
|
|
89
|
+
verbose,
|
|
90
|
+
};
|
|
91
|
+
// Add packageManager field to ensure consistent version usage
|
|
92
|
+
const packageManagerString = await packageManager.getPackageManagerString();
|
|
93
|
+
// Update root package.json name and packageManager
|
|
94
|
+
const packageJsonPath = path.join(directory, "package.json");
|
|
95
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
|
|
96
|
+
packageJson.name = projectName;
|
|
97
|
+
if (packageManagerString) {
|
|
98
|
+
packageJson.packageManager = packageManagerString;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
delete packageJson.packageManager;
|
|
102
|
+
}
|
|
103
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
104
|
+
// Update backend package.json
|
|
105
|
+
const backendPackageJsonPath = path.join(backendDirectory, "package.json");
|
|
106
|
+
const backendPackageJson = JSON.parse(fs.readFileSync(backendPackageJsonPath, "utf-8"));
|
|
107
|
+
if (packageManagerString) {
|
|
108
|
+
backendPackageJson.packageManager = packageManagerString;
|
|
109
|
+
}
|
|
110
|
+
// Update medusa dependencies versions
|
|
111
|
+
if (version) {
|
|
112
|
+
updatePackageVersions(backendPackageJson, version);
|
|
113
|
+
}
|
|
114
|
+
fs.writeFileSync(backendPackageJsonPath, JSON.stringify(backendPackageJson, null, 2));
|
|
115
|
+
// initialize the invite token to return
|
|
116
|
+
let inviteToken = undefined;
|
|
117
|
+
// add environment variables
|
|
118
|
+
let env = `MEDUSA_ADMIN_ONBOARDING_TYPE=${onboardingType}${EOL}STORE_CORS=${STORE_CORS}${EOL}ADMIN_CORS=${ADMIN_CORS}${EOL}AUTH_CORS=${AUTH_CORS}${EOL}REDIS_URL=${DEFAULT_REDIS_URL}${EOL}JWT_SECRET=supersecret${EOL}COOKIE_SECRET=supersecret${EOL}${AUTH_MFA_ENCRYPTION_KEY}=${randomBytes(32).toString("hex")}`;
|
|
119
|
+
if (!skipDb) {
|
|
120
|
+
if (dbName) {
|
|
121
|
+
env += `${EOL}DB_NAME=${dbName}`;
|
|
122
|
+
dbConnectionString = dbConnectionString.replace(dbName, "$DB_NAME");
|
|
123
|
+
}
|
|
124
|
+
env += `${EOL}DATABASE_URL=${dbConnectionString}`;
|
|
125
|
+
}
|
|
126
|
+
if (nextjsDirectory) {
|
|
127
|
+
env += `${EOL}MEDUSA_ADMIN_ONBOARDING_NEXTJS_DIRECTORY=${nextjsDirectory}`;
|
|
128
|
+
}
|
|
129
|
+
fs.appendFileSync(path.join(backendDirectory, `.env`), env);
|
|
130
|
+
factBoxOptions.interval = displayFactBox({
|
|
131
|
+
...factBoxOptions,
|
|
132
|
+
spinner,
|
|
133
|
+
title: "Installing dependencies...",
|
|
134
|
+
processManager,
|
|
135
|
+
});
|
|
136
|
+
await packageManager.installDependencies(execOptions, {
|
|
137
|
+
installLegacyPeerDeps: true,
|
|
138
|
+
});
|
|
139
|
+
factBoxOptions.interval = displayFactBox({
|
|
140
|
+
...factBoxOptions,
|
|
141
|
+
message: "Installed Dependencies",
|
|
142
|
+
});
|
|
143
|
+
if (!skipDb && migrations) {
|
|
144
|
+
factBoxOptions.interval = displayFactBox({
|
|
145
|
+
...factBoxOptions,
|
|
146
|
+
title: "Running Migrations...",
|
|
147
|
+
});
|
|
148
|
+
// run migrations
|
|
149
|
+
const migrationExecResult = await packageManager.runMedusaCommand("db:migrate", npxOptions, {
|
|
150
|
+
verbose,
|
|
151
|
+
needOutput: true,
|
|
152
|
+
});
|
|
153
|
+
if (client) {
|
|
154
|
+
// check the migrations table is in the database
|
|
155
|
+
// to ensure that migrations ran
|
|
156
|
+
let errorOccurred = false;
|
|
157
|
+
try {
|
|
158
|
+
const migrations = await client.query(`SELECT count(tablename) from pg_tables WHERE tablename = 'mikro_orm_migrations'`);
|
|
159
|
+
errorOccurred = migrations.rowCount == 0;
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
// avoid error thrown if the migrations table
|
|
163
|
+
// doesn't exist
|
|
164
|
+
errorOccurred = true;
|
|
165
|
+
}
|
|
166
|
+
// ensure that migrations actually ran in case of an uncaught error
|
|
167
|
+
if (errorOccurred &&
|
|
168
|
+
(migrationExecResult.stderr || migrationExecResult.stdout)) {
|
|
169
|
+
throw new Error(`An error occurred while running migrations: ${migrationExecResult.stderr || migrationExecResult.stdout}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
factBoxOptions.interval = displayFactBox({
|
|
173
|
+
...factBoxOptions,
|
|
174
|
+
message: "Ran Migrations",
|
|
175
|
+
});
|
|
176
|
+
const userExecResult = (await packageManager.runMedusaCommand(`user -e ${ADMIN_EMAIL} --invite`, npxOptions, { verbose, needOutput: true }));
|
|
177
|
+
// get invite token from stdout
|
|
178
|
+
const match = userExecResult.stdout.match(/Invite token: (?<token>.+)/);
|
|
179
|
+
inviteToken = match?.groups?.token;
|
|
180
|
+
}
|
|
181
|
+
// if installation includes Next.js, retrieve the publishable API key
|
|
182
|
+
// from the backend and add it as an enviornment variable
|
|
183
|
+
if (nextjsDirectory && client) {
|
|
184
|
+
const apiKeys = await client.query(`SELECT * FROM "api_key" WHERE type = 'publishable'`);
|
|
185
|
+
if (apiKeys.rowCount) {
|
|
186
|
+
const nextjsEnvPath = path.join(nextjsDirectory, fs.existsSync(path.join(nextjsDirectory, ".env.local"))
|
|
187
|
+
? ".env.local"
|
|
188
|
+
: ".env.template");
|
|
189
|
+
const originalContent = fs.readFileSync(nextjsEnvPath, "utf-8");
|
|
190
|
+
fs.writeFileSync(nextjsEnvPath, originalContent.replace("NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=", `NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=${apiKeys.rows[0].token}`));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
displayFactBox({ ...factBoxOptions, message: "Finished Preparation" });
|
|
194
|
+
return inviteToken;
|
|
195
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export default class ProcessManager {
|
|
2
|
+
intervals = [];
|
|
3
|
+
static MAX_RETRIES = 3;
|
|
4
|
+
constructor() {
|
|
5
|
+
this.onTerminated(() => {
|
|
6
|
+
this.intervals.forEach((interval) => {
|
|
7
|
+
clearInterval(interval);
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
onTerminated(fn) {
|
|
12
|
+
process.on("SIGTERM", async () => fn());
|
|
13
|
+
process.on("SIGINT", async () => fn());
|
|
14
|
+
}
|
|
15
|
+
addInterval(interval) {
|
|
16
|
+
this.intervals.push(interval);
|
|
17
|
+
}
|
|
18
|
+
// when running commands with npx or npm sometimes they
|
|
19
|
+
// terminate with EAGAIN error unexpectedly
|
|
20
|
+
// this utility function allows retrying the process if
|
|
21
|
+
// EAGAIN occurs, or otherwise throw the error that occurs
|
|
22
|
+
async runProcess({ process, ignoreERESOLVE }) {
|
|
23
|
+
let processError = false;
|
|
24
|
+
let retries = 0;
|
|
25
|
+
do {
|
|
26
|
+
++retries;
|
|
27
|
+
try {
|
|
28
|
+
return await process();
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (typeof error === "object" &&
|
|
32
|
+
error !== null &&
|
|
33
|
+
"code" in error &&
|
|
34
|
+
error?.code === "EAGAIN") {
|
|
35
|
+
processError = true;
|
|
36
|
+
}
|
|
37
|
+
else if (ignoreERESOLVE &&
|
|
38
|
+
typeof error === "object" &&
|
|
39
|
+
error !== null &&
|
|
40
|
+
"code" in error &&
|
|
41
|
+
error?.code === "ERESOLVE") {
|
|
42
|
+
// ignore error
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
} while (processError && retries <= ProcessManager.MAX_RETRIES);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import ora from "ora";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import createAbortController from "../create-abort-controller.js";
|
|
4
|
+
import ProcessManager from "../process-manager.js";
|
|
5
|
+
import PackageManager from "../package-manager.js";
|
|
6
|
+
// Base class for common project functionality
|
|
7
|
+
export class BaseProjectCreator {
|
|
8
|
+
options;
|
|
9
|
+
args;
|
|
10
|
+
spinner;
|
|
11
|
+
processManager;
|
|
12
|
+
packageManager;
|
|
13
|
+
abortController;
|
|
14
|
+
factBoxOptions;
|
|
15
|
+
projectName;
|
|
16
|
+
projectPath;
|
|
17
|
+
isProjectCreated = false;
|
|
18
|
+
printedMessage = false;
|
|
19
|
+
constructor(projectName, options, args) {
|
|
20
|
+
this.options = options;
|
|
21
|
+
this.args = args;
|
|
22
|
+
this.spinner = ora();
|
|
23
|
+
this.processManager = new ProcessManager();
|
|
24
|
+
this.packageManager = new PackageManager(this.processManager, {
|
|
25
|
+
verbose: options.verbose,
|
|
26
|
+
useNpm: options.useNpm,
|
|
27
|
+
usePnpm: options.usePnpm,
|
|
28
|
+
useYarn: options.useYarn,
|
|
29
|
+
});
|
|
30
|
+
this.abortController = createAbortController(this.processManager);
|
|
31
|
+
this.projectName = projectName;
|
|
32
|
+
const basePath = typeof options.directoryPath === "string" ? options.directoryPath : "";
|
|
33
|
+
this.projectPath = path.join(basePath, projectName);
|
|
34
|
+
this.factBoxOptions = {
|
|
35
|
+
interval: null,
|
|
36
|
+
spinner: this.spinner,
|
|
37
|
+
processManager: this.processManager,
|
|
38
|
+
message: "",
|
|
39
|
+
title: "",
|
|
40
|
+
verbose: options.verbose || false,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
getProjectPath(projectName) {
|
|
44
|
+
return path.join(this.options.directoryPath ?? "", projectName);
|
|
45
|
+
}
|
|
46
|
+
}
|