chore(medusa,medusa-cli): Clean up new command + fix CI (#4214)
* chore(workflows): Use default starter template in action * Use postgres by default in new * Add skip DB flag * Create thirty-tomatoes-hug.md * address PR comments
This commit is contained in:
committed by
GitHub
parent
f3920730dc
commit
260dc55b6f
6
.changeset/thirty-tomatoes-hug.md
Normal file
6
.changeset/thirty-tomatoes-hug.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/medusa-cli": patch
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
chore(medusa,medusa-cli): Clean up new command + fix CI
|
||||
17
.github/workflows/test-cli-with-database.yml
vendored
17
.github/workflows/test-cli-with-database.yml
vendored
@@ -8,11 +8,6 @@ jobs:
|
||||
NODE_ENV: CI
|
||||
REDIS_URL: redis://localhost:6379
|
||||
DATABASE_URL: "postgres://postgres:postgres@localhost/cli-test"
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
db: [postgres]
|
||||
|
||||
services:
|
||||
redis:
|
||||
image: redis
|
||||
@@ -56,7 +51,7 @@ jobs:
|
||||
|
||||
- name: Create Medusa project
|
||||
run: |
|
||||
medusa new cli-test 'https://github.com/medusajs/medusa-starter-default/tree/ci-test'
|
||||
medusa new cli-test --skip-db
|
||||
working-directory: ..
|
||||
|
||||
- name: run medusa dev
|
||||
@@ -64,28 +59,20 @@ jobs:
|
||||
working-directory: ../cli-test
|
||||
|
||||
- name: Run migrations
|
||||
env:
|
||||
DATABASE_TYPE: ${{ matrix.db }}
|
||||
run: medusa migrations run
|
||||
working-directory: ../cli-test
|
||||
|
||||
- name: Seed db
|
||||
env:
|
||||
DATABASE_TYPE: ${{ matrix.db }}
|
||||
run: yarn seed
|
||||
working-directory: ../cli-test
|
||||
|
||||
- name: Create admin user
|
||||
env:
|
||||
DATABASE_TYPE: ${{ matrix.db }}
|
||||
run: medusa user -e test@test.com -p password -i admin_123
|
||||
working-directory: ../cli-test
|
||||
|
||||
########################## Test medusa develop ###############################
|
||||
|
||||
- name: Run development server
|
||||
env:
|
||||
DATABASE_TYPE: ${{ matrix.db }}
|
||||
run: medusa develop &
|
||||
working-directory: ../cli-test
|
||||
|
||||
@@ -95,8 +82,6 @@ jobs:
|
||||
########################### Test medusa start ################################
|
||||
|
||||
- name: Starting medusa
|
||||
env:
|
||||
DATABASE_TYPE: ${{ matrix.db }}
|
||||
run: medusa start &
|
||||
working-directory: ../cli-test
|
||||
|
||||
|
||||
@@ -300,7 +300,9 @@ const verifyPgCreds = async (creds) => {
|
||||
|
||||
const interactiveDbCreds = async (dbName, dbCreds = {}) => {
|
||||
const credentials = Object.assign({}, defaultDBCreds, dbCreds)
|
||||
let collecting = true
|
||||
|
||||
const collecting = true
|
||||
|
||||
while (collecting) {
|
||||
const result = await inquirer
|
||||
.prompt([
|
||||
@@ -309,10 +311,9 @@ const interactiveDbCreds = async (dbName, dbCreds = {}) => {
|
||||
name: "continueWithDefault",
|
||||
message: `
|
||||
|
||||
Will attempt to setup database "${dbName}" with credentials:
|
||||
Will attempt to setup Postgres database "${dbName}" with credentials:
|
||||
user: ${credentials.user}
|
||||
password: ***
|
||||
database: ${credentials.database}
|
||||
port: ${credentials.port}
|
||||
host: ${credentials.host}
|
||||
Do you wish to continue with these credentials?
|
||||
@@ -352,14 +353,6 @@ Do you wish to continue with these credentials?
|
||||
default: credentials.host,
|
||||
message: `DB host`,
|
||||
},
|
||||
{
|
||||
type: "input",
|
||||
when: ({ continueWithDefault }) =>
|
||||
continueWithDefault === `Change credentials`,
|
||||
name: "database",
|
||||
default: credentials.database,
|
||||
message: `DB database`,
|
||||
},
|
||||
])
|
||||
.then(async (answers) => {
|
||||
const collectedCreds = Object.assign({}, credentials, {
|
||||
@@ -367,7 +360,6 @@ Do you wish to continue with these credentials?
|
||||
password: answers.password,
|
||||
host: answers.host,
|
||||
port: answers.port,
|
||||
database: answers.database,
|
||||
})
|
||||
|
||||
switch (answers.continueWithDefault) {
|
||||
@@ -428,32 +420,25 @@ const setupDB = async (dbName, dbCreds = {}) => {
|
||||
})
|
||||
}
|
||||
|
||||
const setupEnvVars = async (
|
||||
rootPath,
|
||||
dbName,
|
||||
dbCreds = {},
|
||||
isPostgres = true
|
||||
) => {
|
||||
const setupEnvVars = async (rootPath, dbName, dbCreds = {}) => {
|
||||
const templatePath = sysPath.join(rootPath, ".env.template")
|
||||
const destination = sysPath.join(rootPath, ".env")
|
||||
if (existsSync(templatePath)) {
|
||||
fs.renameSync(templatePath, destination)
|
||||
}
|
||||
|
||||
if (isPostgres) {
|
||||
const credentials = Object.assign({}, defaultDBCreds, dbCreds)
|
||||
let dbUrl = ""
|
||||
if (
|
||||
credentials.user !== defaultDBCreds.user ||
|
||||
credentials.password !== defaultDBCreds.password
|
||||
) {
|
||||
dbUrl = `postgres://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}/${dbName}`
|
||||
} else {
|
||||
dbUrl = `postgres://${credentials.host}:${credentials.port}/${dbName}`
|
||||
}
|
||||
|
||||
fs.appendFileSync(destination, `DATABASE_URL=${dbUrl}\n`)
|
||||
const credentials = Object.assign({}, defaultDBCreds, dbCreds)
|
||||
let dbUrl = ""
|
||||
if (
|
||||
credentials.user !== defaultDBCreds.user ||
|
||||
credentials.password !== defaultDBCreds.password
|
||||
) {
|
||||
dbUrl = `postgres://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}/${dbName}`
|
||||
} else {
|
||||
dbUrl = `postgres://${credentials.host}:${credentials.port}/${dbName}`
|
||||
}
|
||||
|
||||
fs.appendFileSync(destination, `DATABASE_URL=${dbUrl}\n`)
|
||||
}
|
||||
|
||||
const runMigrations = async (rootPath) => {
|
||||
@@ -611,29 +596,29 @@ medusa new ${rootPath} [url-to-starter]
|
||||
await copy(starterPath, rootPath)
|
||||
}
|
||||
|
||||
const medusaConfig = getMedusaConfig(rootPath)
|
||||
|
||||
let isPostgres = false
|
||||
|
||||
track("CLI_NEW_LAYOUT_COMPLETED")
|
||||
|
||||
let creds = dbCredentials
|
||||
|
||||
const dbName = `medusa-db-${Math.random().toString(36).substring(2, 7)}` // generate random 5 character string
|
||||
|
||||
if (!useDefaults && !skipDb && !skipEnv) {
|
||||
creds = await interactiveDbCreds(rootPath, dbCredentials)
|
||||
creds = await interactiveDbCreds(dbName, dbCredentials)
|
||||
}
|
||||
|
||||
if (creds === null) {
|
||||
reporter.info("Skipping automatic database setup")
|
||||
reporter.info(
|
||||
"Skipping automatic database setup. Please note that you need to create a database and run migrations before you can run your Medusa backend"
|
||||
)
|
||||
} else {
|
||||
if (!skipDb) {
|
||||
track("CLI_NEW_SETUP_DB")
|
||||
await setupDB(rootPath, creds)
|
||||
await setupDB(dbName, creds)
|
||||
}
|
||||
|
||||
if (!skipEnv) {
|
||||
track("CLI_NEW_SETUP_ENV")
|
||||
await setupEnvVars(rootPath, rootPath, creds)
|
||||
await setupEnvVars(rootPath, dbName, creds)
|
||||
}
|
||||
|
||||
if (!skipMigrations) {
|
||||
|
||||
Reference in New Issue
Block a user