|
1
|
import { setTelemetryEnabled } from "@medusajs/telemetry" |
|
2
|
import { sync as existsSync } from "fs-exists-cached" |
|
3
|
import path from "path" |
|
4
|
import resolveCwd from "resolve-cwd" |
|
5
|
import { getCodemod, listCodemods } from "./codemods/index" |
|
6
|
import { newStarter } from "./commands/new" |
|
7
|
import { didYouMean } from "./did-you-mean" |
|
8
|
import reporter from "./reporter" |
|
9
|
|
|
10
|
const yargs = require(`yargs`) |
|
11
|
|
|
12
|
const handlerP = |
|
13
|
(fn) => |
|
14
|
(...args) => { |
|
15
|
Promise.resolve(fn(...args)).then( |
|
16
|
() => process.exit(0), |
|
17
|
(err) => { |
|
18
|
console.error(err) |
|
19
|
process.exit(1) |
|
20
|
} |
|
21
|
) |
|
22
|
} |
|
23
|
|
|
24
|
/** |
|
25
|
* Whether `@medusajs/eslint-plugin` is installed in the consumer project. |
|
26
|
* Linting is enabled by default on `build` and `develop` only when this is true. |
|
27
|
*/ |
|
28
|
function isMedusaEslintPluginInstalled(cwd: string): boolean { |
|
29
|
try { |
|
30
|
require.resolve("@medusajs/eslint-plugin", { paths: [cwd] }) |
|
31
|
return true |
|
32
|
} catch { |
|
33
|
return false |
|
34
|
} |
|
35
|
} |
|
36
|
|
|
37
|
/** |
|
38
|
* Adds the shared `--lint` / `--fix` options to a command builder. Used by both |
|
39
|
* `build` and `develop`; the only difference is the `commandLabel` woven into |
|
40
|
* the describe text. |
|
41
|
*/ |
|
42
|
function addLintOptions(builder, commandLabel: string, directory: string) { |
|
43
|
const lintByDefault = isMedusaEslintPluginInstalled(directory) |
|
44
|
|
|
45
|
return builder |
|
46
|
.option("lint", { |
|
47
|
type: "boolean", |
|
48
|
describe: `Run the Medusa linter before ${commandLabel}. Enabled by default when @medusajs/eslint-plugin is installed. Use --no-lint to skip linting.`, |
|
49
|
default: lintByDefault, |
|
50
|
}) |
|
51
|
.option("fix", { |
|
52
|
type: "boolean", |
|
53
|
default: false, |
|
54
|
describe: `Auto-fix lint issues where possible before ${commandLabel}.`, |
|
55
|
}) |
|
56
|
} |
|
57
|
|
|
58
|
function buildLocalCommands(cli, isLocalProject) { |
|
59
|
const defaultPort = "9000" |
|
60
|
const directory = path.resolve(`.`) |
|
61
|
|
|
62
|
const projectInfo = { directory } |
|
63
|
const useYarn = existsSync(path.join(directory, `yarn.lock`)) |
|
64
|
|
|
65
|
if (isLocalProject) { |
|
66
|
projectInfo["sitePackageJson"] = require(path.join( |
|
67
|
directory, |
|
68
|
`package.json` |
|
69
|
)) |
|
70
|
} |
|
71
|
|
|
72
|
function resolveLocalCommand(command) { |
|
73
|
if (!isLocalProject && command !== "new") { |
|
74
|
console.error( |
|
75
|
`The "${command}" command must be run inside a Medusa project. Make sure you are in the root directory of a Medusa project and try again.` |
|
76
|
) |
|
77
|
process.exit(1) |
|
78
|
} |
|
79
|
|
|
80
|
try { |
|
81
|
const cmdPath = resolveCwd.silent(`@medusajs/medusa/commands/${command}`)! |
|
82
|
return require(cmdPath).default |
|
83
|
} catch (err) { |
|
84
|
console.error(err) |
|
85
|
cli.showHelp((s: string) => console.error(s)) |
|
86
|
} |
|
87
|
} |
|
88
|
|
|
89
|
function getCommandHandler(command, handler) { |
|
90
|
return (argv) => { |
|
91
|
const localCmd = resolveLocalCommand(command) |
|
92
|
const args = { ...argv, ...projectInfo, useYarn } |
|
93
|
|
|
94
|
return handler ? handler(args, localCmd) : localCmd(args) |
|
95
|
} |
|
96
|
} |
|
97
|
|
|
98
|
cli |
|
99
|
.command({ |
|
100
|
command: `new [root] [starter]`, |
|
101
|
builder: (_) => |
|
102
|
_.option(`seed`, { |
|
103
|
type: `boolean`, |
|
104
|
describe: `If flag is set the command will attempt to seed the database after setup.`, |
|
105
|
default: false, |
|
106
|
}) |
|
107
|
.option(`y`, { |
|
108
|
type: `boolean`, |
|
109
|
alias: "useDefaults", |
|
110
|
describe: `If flag is set the command will not interactively collect database credentials`, |
|
111
|
default: false, |
|
112
|
}) |
|
113
|
.option(`skip-db`, { |
|
114
|
type: `boolean`, |
|
115
|
describe: `If flag is set the command will not attempt to complete database setup`, |
|
116
|
default: false, |
|
117
|
}) |
|
118
|
.option(`skip-migrations`, { |
|
119
|
type: `boolean`, |
|
120
|
describe: `If flag is set the command will not attempt to complete database migration`, |
|
121
|
default: false, |
|
122
|
}) |
|
123
|
.option(`skip-env`, { |
|
124
|
type: `boolean`, |
|
125
|
describe: `If flag is set the command will not attempt to populate .env`, |
|
126
|
default: false, |
|
127
|
}) |
|
128
|
.option(`db-user`, { |
|
129
|
type: `string`, |
|
130
|
describe: `The database user to use for database setup and migrations.`, |
|
131
|
}) |
|
132
|
.option(`db-database`, { |
|
133
|
type: `string`, |
|
134
|
describe: `The database use for database setup and migrations.`, |
|
135
|
}) |
|
136
|
.option(`db-pass`, { |
|
137
|
type: `string`, |
|
138
|
describe: `The database password to use for database setup and migrations.`, |
|
139
|
}) |
|
140
|
.option(`db-port`, { |
|
141
|
type: `number`, |
|
142
|
describe: `The database port to use for database setup and migrations.`, |
|
143
|
}) |
|
144
|
.option(`db-host`, { |
|
145
|
type: `string`, |
|
146
|
describe: `The database host to use for database setup and migrations.`, |
|
147
|
}) |
|
148
|
.option(`v2`, { |
|
149
|
type: `boolean`, |
|
150
|
describe: `Install Medusa with the V2 feature flag enabled. WARNING: Medusa V2 is still in development and shouldn't be used in production.`, |
|
151
|
default: false, |
|
152
|
}) |
|
153
|
.option(`branch`, { |
|
154
|
type: `string`, |
|
155
|
describe: `The branch of the git repository to clone.`, |
|
156
|
}), |
|
157
|
desc: `Create a new Medusa project.`, |
|
158
|
handler: handlerP(newStarter), |
|
159
|
}) |
|
160
|
.command({ |
|
161
|
command: "db:setup", |
|
162
|
desc: "Create the database, run migrations and sync links", |
|
163
|
builder: (builder) => { |
|
164
|
builder.option("db", { |
|
165
|
type: "string", |
|
166
|
describe: "Specify the name of the database you want to create", |
|
167
|
}) |
|
168
|
builder.option("interactive", { |
|
169
|
type: "boolean", |
|
170
|
default: true, |
|
171
|
describe: |
|
172
|
"Display prompts. Use --no-interactive flag to run the command without prompts", |
|
173
|
}) |
|
174
|
builder.option("skip-links", { |
|
175
|
type: "boolean", |
|
176
|
describe: "Do not sync links", |
|
177
|
}) |
|
178
|
builder.option("skip-search", { |
|
179
|
type: "boolean", |
|
180
|
describe: "Do not create or alter search indexes", |
|
181
|
}) |
|
182
|
builder.option("execute-all-links", { |
|
183
|
type: "boolean", |
|
184
|
describe: |
|
185
|
"Skip prompts and execute all (including unsafe) actions from sync links", |
|
186
|
}) |
|
187
|
builder.option("execute-safe-links", { |
|
188
|
type: "boolean", |
|
189
|
describe: |
|
190
|
"Skip prompts and execute only safe actions from sync links", |
|
191
|
}) |
|
192
|
builder.option("execute-all-search", { |
|
193
|
type: "boolean", |
|
194
|
describe: |
|
195
|
"Skip prompts and execute all (including unsafe) actions on search indexes", |
|
196
|
}) |
|
197
|
builder.option("execute-safe-search", { |
|
198
|
type: "boolean", |
|
199
|
describe: |
|
200
|
"Skip prompts and execute only safe actions on search indexes", |
|
201
|
}) |
|
202
|
}, |
|
203
|
handler: handlerP( |
|
204
|
getCommandHandler("db/setup", (args, cmd) => { |
|
205
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
206
|
return cmd(args) |
|
207
|
}) |
|
208
|
), |
|
209
|
}) |
|
210
|
.command({ |
|
211
|
command: "db:create", |
|
212
|
desc: "Create the database used by your application", |
|
213
|
builder: (builder) => { |
|
214
|
builder.option("db", { |
|
215
|
type: "string", |
|
216
|
describe: "Specify the name of the database you want to create", |
|
217
|
}) |
|
218
|
builder.option("interactive", { |
|
219
|
type: "boolean", |
|
220
|
default: true, |
|
221
|
describe: |
|
222
|
"Display prompts. Use --no-interactive flag to run the command without prompts", |
|
223
|
}) |
|
224
|
}, |
|
225
|
handler: handlerP( |
|
226
|
getCommandHandler("db/create", (args, cmd) => { |
|
227
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
228
|
return cmd(args) |
|
229
|
}) |
|
230
|
), |
|
231
|
}) |
|
232
|
.command({ |
|
233
|
command: "db:migrate", |
|
234
|
desc: "Migrate the database by executing pending migrations", |
|
235
|
builder: (builder) => { |
|
236
|
builder.option("skip-scripts", { |
|
237
|
type: "boolean", |
|
238
|
describe: "Do not run migration scripts", |
|
239
|
}) |
|
240
|
builder.option("skip-links", { |
|
241
|
type: "boolean", |
|
242
|
describe: "Do not sync links", |
|
243
|
}) |
|
244
|
builder.option("skip-search", { |
|
245
|
type: "boolean", |
|
246
|
describe: "Do not create or alter search indexes", |
|
247
|
}) |
|
248
|
builder.option("execute-all-links", { |
|
249
|
type: "boolean", |
|
250
|
describe: |
|
251
|
"Skip prompts and execute all (including unsafe) actions from sync links", |
|
252
|
}) |
|
253
|
builder.option("execute-safe-links", { |
|
254
|
type: "boolean", |
|
255
|
describe: |
|
256
|
"Skip prompts and execute only safe actions from sync links", |
|
257
|
}) |
|
258
|
builder.option("execute-all-search", { |
|
259
|
type: "boolean", |
|
260
|
describe: |
|
261
|
"Skip prompts and execute all (including unsafe) actions on search indexes", |
|
262
|
}) |
|
263
|
builder.option("execute-safe-search", { |
|
264
|
type: "boolean", |
|
265
|
describe: |
|
266
|
"Skip prompts and execute only safe actions on search indexes", |
|
267
|
}) |
|
268
|
builder.option("concurrency", { |
|
269
|
type: "number", |
|
270
|
describe: "Number of concurrent migrations to run", |
|
271
|
}) |
|
272
|
builder.option("all-or-nothing", { |
|
273
|
type: "boolean", |
|
274
|
describe: |
|
275
|
"If set, the command will fail if any migration fails and revert the migrations that were applied so far", |
|
276
|
default: false, |
|
277
|
}) |
|
278
|
}, |
|
279
|
handler: handlerP( |
|
280
|
getCommandHandler("db/migrate", (args, cmd) => { |
|
281
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
282
|
return cmd(args) |
|
283
|
}) |
|
284
|
), |
|
285
|
}) |
|
286
|
.command({ |
|
287
|
command: "db:migrate:scripts", |
|
288
|
desc: "Run all migration scripts", |
|
289
|
handler: handlerP( |
|
290
|
getCommandHandler("db/run-scripts", (args, cmd) => { |
|
291
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
292
|
return cmd(args) |
|
293
|
}) |
|
294
|
), |
|
295
|
}) |
|
296
|
.command({ |
|
297
|
command: "db:migrate:search", |
|
298
|
desc: "Create and alter the search indexes declared under search/", |
|
299
|
builder: (builder) => { |
|
300
|
builder.option("execute-all-search", { |
|
301
|
type: "boolean", |
|
302
|
describe: |
|
303
|
"Skip prompts and execute all (including unsafe) actions on search indexes", |
|
304
|
}) |
|
305
|
builder.option("execute-safe-search", { |
|
306
|
type: "boolean", |
|
307
|
describe: |
|
308
|
"Skip prompts and execute only safe actions on search indexes", |
|
309
|
}) |
|
310
|
}, |
|
311
|
handler: handlerP( |
|
312
|
getCommandHandler("db/migrate-search", (args, cmd) => { |
|
313
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
314
|
return cmd(args) |
|
315
|
}) |
|
316
|
), |
|
317
|
}) |
|
318
|
.command({ |
|
319
|
command: "db:rollback [modules...]", |
|
320
|
desc: "Rollback last batch of executed migrations for a given module", |
|
321
|
builder: { |
|
322
|
modules: { |
|
323
|
type: "array", |
|
324
|
description: "Modules for which to rollback migrations", |
|
325
|
demand: true, |
|
326
|
}, |
|
327
|
}, |
|
328
|
handler: handlerP( |
|
329
|
getCommandHandler("db/rollback", (args, cmd) => { |
|
330
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
331
|
return cmd(args) |
|
332
|
}) |
|
333
|
), |
|
334
|
}) |
|
335
|
.command({ |
|
336
|
command: "db:generate [modules...]", |
|
337
|
desc: "Generate migrations for a given module", |
|
338
|
builder: { |
|
339
|
modules: { |
|
340
|
type: "array", |
|
341
|
description: "Modules for which to generate migration files", |
|
342
|
demand: true, |
|
343
|
}, |
|
344
|
}, |
|
345
|
handler: handlerP( |
|
346
|
getCommandHandler("db/generate", (args, cmd) => { |
|
347
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
348
|
return cmd(args) |
|
349
|
}) |
|
350
|
), |
|
351
|
}) |
|
352
|
.command({ |
|
353
|
command: "plugin:db:generate", |
|
354
|
desc: "Generate migrations for modules in a plugin", |
|
355
|
handler: handlerP( |
|
356
|
getCommandHandler("plugin/db/generate", (args, cmd) => { |
|
357
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
358
|
return cmd(args) |
|
359
|
}) |
|
360
|
), |
|
361
|
}) |
|
362
|
.command({ |
|
363
|
command: "db:sync-links", |
|
364
|
desc: "Sync database schema with the links defined by your application and Medusa core", |
|
365
|
builder: (builder) => { |
|
366
|
builder.option("execute-all", { |
|
367
|
type: "boolean", |
|
368
|
describe: "Skip prompts and execute all (including unsafe) actions", |
|
369
|
}) |
|
370
|
builder.option("execute-safe", { |
|
371
|
type: "boolean", |
|
372
|
describe: "Skip prompts and execute only safe actions", |
|
373
|
}) |
|
374
|
builder.option("concurrency", { |
|
375
|
type: "number", |
|
376
|
describe: "Number of concurrent migrations to run", |
|
377
|
}) |
|
378
|
}, |
|
379
|
handler: handlerP( |
|
380
|
getCommandHandler("db/sync-links", (args, cmd) => { |
|
381
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
382
|
return cmd(args) |
|
383
|
}) |
|
384
|
), |
|
385
|
}) |
|
386
|
.command({ |
|
387
|
command: "plugin:build", |
|
388
|
desc: "Build plugin source for publishing to a package registry", |
|
389
|
handler: handlerP( |
|
390
|
getCommandHandler("plugin/build", async (args, cmd) => { |
|
391
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
392
|
cmd(args) |
|
393
|
return new Promise((resolve) => {}) |
|
394
|
}) |
|
395
|
), |
|
396
|
}) |
|
397
|
.command({ |
|
398
|
command: "plugin:develop", |
|
399
|
desc: "Start plugin development process in watch mode. Changes will be re-published to the local packages registry", |
|
400
|
handler: handlerP( |
|
401
|
getCommandHandler("plugin/develop", async (args, cmd) => { |
|
402
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
403
|
cmd(args) |
|
404
|
return new Promise(() => {}) |
|
405
|
}) |
|
406
|
), |
|
407
|
}) |
|
408
|
.command({ |
|
409
|
command: "plugin:publish", |
|
410
|
desc: "Publish the plugin to the local packages registry", |
|
411
|
handler: handlerP( |
|
412
|
getCommandHandler("plugin/publish", async (args, cmd) => { |
|
413
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
414
|
cmd(args) |
|
415
|
return new Promise(() => {}) |
|
416
|
}) |
|
417
|
), |
|
418
|
}) |
|
419
|
.command({ |
|
420
|
command: "plugin:add [plugin_names...]", |
|
421
|
desc: "Add the specified plugin to the project from the local packages registry", |
|
422
|
builder: { |
|
423
|
plugin_names: { |
|
424
|
type: "array", |
|
425
|
description: "The name of the plugins to add", |
|
426
|
demand: true, |
|
427
|
}, |
|
428
|
}, |
|
429
|
handler: handlerP( |
|
430
|
getCommandHandler("plugin/add", async (args, cmd) => { |
|
431
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
432
|
cmd(args) |
|
433
|
return new Promise(() => {}) |
|
434
|
}) |
|
435
|
), |
|
436
|
}) |
|
437
|
.command({ |
|
438
|
command: `telemetry`, |
|
439
|
describe: `Enable or disable collection of anonymous usage data.`, |
|
440
|
builder: (yargs) => |
|
441
|
yargs |
|
442
|
.option(`enable`, { |
|
443
|
type: `boolean`, |
|
444
|
description: `Enable telemetry (default)`, |
|
445
|
}) |
|
446
|
.option(`disable`, { |
|
447
|
type: `boolean`, |
|
448
|
description: `Disable telemetry`, |
|
449
|
}), |
|
450
|
|
|
451
|
handler: handlerP(({ enable, disable }) => { |
|
452
|
const enabled = Boolean(enable) || !disable |
|
453
|
setTelemetryEnabled(enabled) |
|
454
|
reporter.info( |
|
455
|
`Telemetry collection ${enabled ? `enabled` : `disabled`}` |
|
456
|
) |
|
457
|
}), |
|
458
|
}) |
|
459
|
.command({ |
|
460
|
command: `codemod <codemod-name>`, |
|
461
|
desc: `Run automated code transformations`, |
|
462
|
builder: (yargs) => |
|
463
|
yargs |
|
464
|
.positional("codemod-name", { |
|
465
|
type: "string", |
|
466
|
describe: "Name of the codemod to run", |
|
467
|
demandOption: true, |
|
468
|
}) |
|
469
|
.option(`dry-run`, { |
|
470
|
type: `boolean`, |
|
471
|
description: `Preview changes without modifying files`, |
|
472
|
default: false, |
|
473
|
}), |
|
474
|
handler: handlerP(async ({ codemodName, dryRun }) => { |
|
475
|
const codemod = getCodemod(codemodName) |
|
476
|
|
|
477
|
if (!codemod) { |
|
478
|
const available = listCodemods() |
|
479
|
reporter.error(`Unknown codemod: ${codemodName}`) |
|
480
|
reporter.info( |
|
481
|
`\nAvailable codemods:\n${available |
|
482
|
.map((n) => ` - ${n}`) |
|
483
|
.join("\n")}` |
|
484
|
) |
|
485
|
process.exit(1) |
|
486
|
} |
|
487
|
|
|
488
|
reporter.info(`Running codemod: ${codemod.name}`) |
|
489
|
reporter.info(codemod.description) |
|
490
|
|
|
491
|
if (dryRun) { |
|
492
|
reporter.info(`\n DRY RUN MODE - No files will be modified\n`) |
|
493
|
} |
|
494
|
|
|
495
|
const result = await codemod.run({ dryRun }) |
|
496
|
|
|
497
|
reporter.info(`\n Summary:`) |
|
498
|
reporter.info(` Files scanned: ${result.filesScanned}`) |
|
499
|
reporter.info(` Files modified: ${result.filesModified}`) |
|
500
|
reporter.info(` Errors: ${result.errors}`) |
|
501
|
|
|
502
|
if (dryRun && result.filesModified > 0) { |
|
503
|
reporter.info(`\n Run without --dry-run to apply changes`) |
|
504
|
} else if (result.filesModified > 0) { |
|
505
|
reporter.info(`\n Codemod completed successfully!`) |
|
506
|
reporter.info(`\n Next steps:`) |
|
507
|
reporter.info(` 1. Review changes: git diff`) |
|
508
|
reporter.info(` 2. Run tests to verify`) |
|
509
|
reporter.info(` 3. Commit if satisfied`) |
|
510
|
} else { |
|
511
|
reporter.info(`\n No modifications needed`) |
|
512
|
} |
|
513
|
}), |
|
514
|
}) |
|
515
|
.command({ |
|
516
|
command: `develop`, |
|
517
|
desc: `Start development server. Watches file and rebuilds when something changes`, |
|
518
|
builder: (_) => |
|
519
|
addLintOptions( |
|
520
|
_.option("types", { |
|
521
|
type: "boolean", |
|
522
|
default: true, |
|
523
|
describe: |
|
524
|
"Generate automated types for modules inside the .medusa directory", |
|
525
|
}) |
|
526
|
.option(`H`, { |
|
527
|
alias: `host`, |
|
528
|
type: `string`, |
|
529
|
default: process.env.HOST, |
|
530
|
describe: process.env.HOST |
|
531
|
? `Set host. Defaults to ${process.env.HOST} (set by env.HOST)` |
|
532
|
: "", |
|
533
|
}) |
|
534
|
.option(`p`, { |
|
535
|
alias: `port`, |
|
536
|
type: `string`, |
|
537
|
default: process.env.PORT || defaultPort, |
|
538
|
describe: process.env.PORT |
|
539
|
? `Set port. Defaults to ${process.env.PORT} (set by env.PORT) (otherwise defaults ${defaultPort})` |
|
540
|
: `Set port. Defaults to ${defaultPort}`, |
|
541
|
}), |
|
542
|
"starting the dev server", |
|
543
|
directory |
|
544
|
), |
|
545
|
handler: handlerP( |
|
546
|
getCommandHandler(`develop`, async (args, cmd) => { |
|
547
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
548
|
|
|
549
|
cmd(args) |
|
550
|
// Return an empty promise to prevent handlerP from exiting early. |
|
551
|
// The development server shouldn't ever exit until the user directly |
|
552
|
// kills it so this is fine. |
|
553
|
return new Promise(() => {}) |
|
554
|
}) |
|
555
|
), |
|
556
|
}) |
|
557
|
.command({ |
|
558
|
command: `start`, |
|
559
|
desc: `Start production server.`, |
|
560
|
builder: (_) => |
|
561
|
_.option("types", { |
|
562
|
type: "boolean", |
|
563
|
default: false, |
|
564
|
describe: |
|
565
|
"Generate automated types for modules inside the .medusa directory", |
|
566
|
}) |
|
567
|
.option(`H`, { |
|
568
|
alias: `host`, |
|
569
|
type: `string`, |
|
570
|
default: process.env.HOST, |
|
571
|
describe: process.env.HOST |
|
572
|
? `Set host. Defaults to ${process.env.HOST} (set by env.HOST)` |
|
573
|
: ``, |
|
574
|
}) |
|
575
|
.option(`p`, { |
|
576
|
alias: `port`, |
|
577
|
type: `string`, |
|
578
|
default: process.env.PORT || defaultPort, |
|
579
|
describe: process.env.PORT |
|
580
|
? `Set port. Defaults to ${process.env.PORT} (set by env.PORT) (otherwise defaults ${defaultPort})` |
|
581
|
: `Set port. Defaults to ${defaultPort}`, |
|
582
|
}) |
|
583
|
.option(`cluster`, { |
|
584
|
type: `string`, |
|
585
|
describe: |
|
586
|
"Start the Node.js server in cluster mode. Specify the number of CPUs to use or a percentage (e.g., 50%). Defaults to the number of available CPUs.", |
|
587
|
}) |
|
588
|
.option("workers", { |
|
589
|
type: "string", |
|
590
|
default: "0", |
|
591
|
describe: |
|
592
|
"Number of worker processes in cluster mode or a percentage of cluster size (e.g., 25%).", |
|
593
|
}) |
|
594
|
.option("servers", { |
|
595
|
type: "string", |
|
596
|
default: "0", |
|
597
|
describe: |
|
598
|
"Number of server processes in cluster mode or a percentage of cluster size (e.g., 25%).", |
|
599
|
}), |
|
600
|
handler: handlerP( |
|
601
|
getCommandHandler(`start`, async (args, cmd) => { |
|
602
|
process.env.NODE_ENV = process.env.NODE_ENV || `production` |
|
603
|
cmd(args) |
|
604
|
// Return an empty promise to prevent handlerP from exiting early. |
|
605
|
// The development server shouldn't ever exit until the user directly |
|
606
|
// kills it so this is fine. |
|
607
|
return new Promise((resolve) => {}) |
|
608
|
}) |
|
609
|
), |
|
610
|
}) |
|
611
|
.command({ |
|
612
|
command: "build", |
|
613
|
desc: "Build your project.", |
|
614
|
builder: (_) => |
|
615
|
addLintOptions( |
|
616
|
_.option("admin-only", { |
|
617
|
default: false, |
|
618
|
type: "boolean", |
|
619
|
describe: |
|
620
|
"Only build the admin to serve it separately (outDir .medusa/admin)", |
|
621
|
}), |
|
622
|
"building", |
|
623
|
directory |
|
624
|
).option("quiet", { |
|
625
|
type: "boolean", |
|
626
|
default: false, |
|
627
|
describe: "Report lint errors only, suppressing warnings.", |
|
628
|
}), |
|
629
|
handler: handlerP( |
|
630
|
getCommandHandler(`build`, async (args, cmd) => { |
|
631
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
632
|
cmd(args) |
|
633
|
|
|
634
|
return new Promise((resolve) => {}) |
|
635
|
}) |
|
636
|
), |
|
637
|
}) |
|
638
|
.command({ |
|
639
|
command: "lint [paths...]", |
|
640
|
desc: "Lint your project using its ESLint configuration.", |
|
641
|
builder: (_) => |
|
642
|
_.positional("paths", { |
|
643
|
type: "string", |
|
644
|
array: true, |
|
645
|
describe: |
|
646
|
"Files or directories to lint. Defaults to the whole project.", |
|
647
|
}) |
|
648
|
.option("fix", { |
|
649
|
type: "boolean", |
|
650
|
default: false, |
|
651
|
describe: "Auto-fix lint issues where possible.", |
|
652
|
}) |
|
653
|
.option("quiet", { |
|
654
|
type: "boolean", |
|
655
|
default: false, |
|
656
|
describe: "Report lint errors only, suppressing warnings.", |
|
657
|
}), |
|
658
|
handler: handlerP( |
|
659
|
getCommandHandler(`lint`, async (args, cmd) => { |
|
660
|
process.env.NODE_ENV = process.env.NODE_ENV || `development` |
|
661
|
return cmd(args) |
|
662
|
}) |
|
663
|
), |
|
664
|
}) |
|
665
|
.command({ |
|
666
|
command: `user`, |
|
667
|
desc: `Create a user`, |
|
668
|
builder: (_) => |
|
669
|
_.option(`e`, { |
|
670
|
alias: `email`, |
|
671
|
type: `string`, |
|
672
|
describe: `User's email.`, |
|
673
|
}) |
|
674
|
.option(`p`, { |
|
675
|
alias: `password`, |
|
676
|
type: `string`, |
|
677
|
describe: `User's password.`, |
|
678
|
}) |
|
679
|
.option(`i`, { |
|
680
|
alias: `id`, |
|
681
|
type: `string`, |
|
682
|
describe: `User's id.`, |
|
683
|
}) |
|
684
|
.option(`invite`, { |
|
685
|
type: `boolean`, |
|
686
|
describe: `If flag is set, an invitation will be created instead of a new user and the invite token will be returned.`, |
|
687
|
default: false, |
|
688
|
}), |
|
689
|
handler: handlerP( |
|
690
|
getCommandHandler(`user`, async (args, cmd) => { |
|
691
|
cmd(args) |
|
692
|
// Return an empty promise to prevent handlerP from exiting early. |
|
693
|
// The development server shouldn't ever exit until the user directly |
|
694
|
// kills it so this is fine. |
|
695
|
return new Promise((resolve) => {}) |
|
696
|
}) |
|
697
|
), |
|
698
|
}) |
|
699
|
.command({ |
|
700
|
command: `exec [file] [args..]`, |
|
701
|
desc: `Run a function defined in a file.`, |
|
702
|
handler: handlerP( |
|
703
|
getCommandHandler(`exec`, async (args, cmd) => { |
|
704
|
cmd(args) |
|
705
|
// Return an empty promise to prevent handlerP from exiting early. |
|
706
|
// The development server shouldn't ever exit until the user directly |
|
707
|
// kills it so this is fine. |
|
708
|
return new Promise((resolve) => {}) |
|
709
|
}) |
|
710
|
), |
|
711
|
}) |
|
712
|
.command({ |
|
713
|
command: `mcloud [args..]`, |
|
714
|
desc: `Run mcloud CLI commands through Medusa CLI.`, |
|
715
|
builder: (builder) => |
|
716
|
builder.parserConfiguration({ |
|
717
|
"unknown-options-as-args": true, |
|
718
|
"halt-at-non-option": false, |
|
719
|
}), |
|
720
|
handler: handlerP( |
|
721
|
getCommandHandler(`mcloud`, async (args, cmd) => { |
|
722
|
cmd(args) |
|
723
|
return new Promise((resolve) => {}) |
|
724
|
}) |
|
725
|
), |
|
726
|
}) |
|
727
|
} |
|
728
|
|
|
729
|
function isLocalMedusaProject() { |
|
730
|
let inMedusaProject = false |
|
731
|
|
|
732
|
try { |
|
733
|
const { dependencies, devDependencies } = require(path.resolve( |
|
734
|
`./package.json` |
|
735
|
)) |
|
736
|
// Draft order plugin can't have @medusajs/medusa as dependency, |
|
737
|
// so we also check for @medusajs/cli |
|
738
|
inMedusaProject = !!( |
|
739
|
(dependencies && |
|
740
|
(dependencies["@medusajs/medusa"] || dependencies["@medusajs/cli"])) || |
|
741
|
(devDependencies && |
|
742
|
(devDependencies["@medusajs/medusa"] || |
|
743
|
devDependencies["@medusajs/cli"])) |
|
744
|
) |
|
745
|
} catch (err) { |
|
746
|
// ignore |
|
747
|
} |
|
748
|
|
|
749
|
return inMedusaProject |
|
750
|
} |
|
751
|
|
|
752
|
function getVersionInfo() { |
|
753
|
const { version } = require(`../package.json`) |
|
754
|
const isMedusaProject = isLocalMedusaProject() |
|
755
|
if (isMedusaProject) { |
|
756
|
let medusaVersion = "" |
|
757
|
try { |
|
758
|
medusaVersion = require(path.join( |
|
759
|
process.cwd(), |
|
760
|
`node_modules`, |
|
761
|
`@medusajs/medusa`, |
|
762
|
`package.json` |
|
763
|
)).version |
|
764
|
} catch (e) { |
|
765
|
/* noop */ |
|
766
|
} |
|
767
|
|
|
768
|
if (!medusaVersion) { |
|
769
|
medusaVersion = `unknown` |
|
770
|
} |
|
771
|
|
|
772
|
return `Medusa CLI version: ${version} |
|
773
|
Medusa version: ${medusaVersion} |
|
774
|
Note: this is the Medusa version for the site at: ${process.cwd()}` |
|
775
|
} else { |
|
776
|
return `Medusa CLI version: ${version}` |
|
777
|
} |
|
778
|
} |
|
779
|
|
|
780
|
export default (argv) => { |
|
781
|
const cli = yargs() |
|
782
|
const isLocalProject = isLocalMedusaProject() |
|
783
|
|
|
784
|
cli |
|
785
|
.scriptName(`medusa`) |
|
786
|
.usage(`Usage: $0 <command> [options]`) |
|
787
|
.alias(`h`, `help`) |
|
788
|
.alias(`v`, `version`) |
|
789
|
.option(`verbose`, { |
|
790
|
default: false, |
|
791
|
type: `boolean`, |
|
792
|
describe: `Turn on verbose output`, |
|
793
|
global: true, |
|
794
|
}) |
|
795
|
.option(`no-color`, { |
|
796
|
alias: `no-colors`, |
|
797
|
default: false, |
|
798
|
type: `boolean`, |
|
799
|
describe: `Turn off the color in output`, |
|
800
|
global: true, |
|
801
|
}) |
|
802
|
.option(`json`, { |
|
803
|
describe: `Turn on the JSON logger`, |
|
804
|
default: false, |
|
805
|
type: `boolean`, |
|
806
|
global: true, |
|
807
|
}) |
|
808
|
|
|
809
|
buildLocalCommands(cli, isLocalProject) |
|
810
|
|
|
811
|
try { |
|
812
|
cli.version( |
|
813
|
`version`, |
|
814
|
`Show the version of the Medusa CLI and the Medusa package in the current project`, |
|
815
|
getVersionInfo() |
|
816
|
) |
|
817
|
} catch (e) { |
|
818
|
// ignore |
|
819
|
} |
|
820
|
|
|
821
|
return cli |
|
822
|
.wrap(cli.terminalWidth()) |
|
823
|
.demandCommand(1, `Pass --help to see all available commands and options.`) |
|
824
|
.strict() |
|
825
|
.fail((msg, err, yargs) => { |
|
826
|
const availableCommands = yargs |
|
827
|
.getCommands() |
|
828
|
.map((commandDescription) => { |
|
829
|
const [command] = commandDescription |
|
830
|
return command.split(` `)[0] |
|
831
|
}) |
|
832
|
const arg = argv.slice(2)[0] |
|
833
|
const suggestion = arg ? didYouMean(arg, availableCommands) : `` |
|
834
|
|
|
835
|
if (msg) { |
|
836
|
reporter.error(msg) |
|
837
|
console.log() |
|
838
|
} |
|
839
|
if (suggestion) { |
|
840
|
reporter.info(suggestion) |
|
841
|
console.log() |
|
842
|
} |
|
843
|
|
|
844
|
if (err) { |
|
845
|
console.error("--------------- ERROR ---------------------") |
|
846
|
console.error(err) |
|
847
|
console.error("-------------------------------------------") |
|
848
|
} |
|
849
|
|
|
850
|
cli.showHelp((s: string) => console.error(s)) |
|
851
|
process.exit(1) |
|
852
|
}) |
|
853
|
.parse(argv.slice(2)) |
|
854
|
} |