Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(medusa,medusa-cli): Clean up new command + fix CI #4214

Merged
merged 7 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thirty-tomatoes-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/medusa-cli": patch
"@medusajs/medusa": patch
---

chore(medusa,medusa-cli): Clean up new command + fix CI
17 changes: 1 addition & 16 deletions .github/workflows/test-cli-with-database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,36 +51,28 @@ 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
run: medusa-dev --force-install
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 [email protected] -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

Expand All @@ -95,8 +82,6 @@ jobs:
########################### Test medusa start ################################

- name: Starting medusa
env:
DATABASE_TYPE: ${{ matrix.db }}
run: medusa start &
working-directory: ../cli-test

Expand Down
63 changes: 24 additions & 39 deletions packages/medusa-cli/src/commands/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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?
Expand Down Expand Up @@ -352,22 +353,13 @@ 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, {
user: answers.user,
password: answers.password,
host: answers.host,
port: answers.port,
database: answers.database,
})

switch (answers.continueWithDefault) {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
Expand Down