chore(medusa-cli): Cleanup plugin setup (#4420)
* chore(medusa-cli): Cleanup plugin setup * fix: logger types * fix event bus local * fix event bus redis * Create late-dragons-collect.md * move to ts * remove unused command * env * fix
This commit is contained in:
committed by
GitHub
parent
fe25c8a91f
commit
6f1fa244fa
@@ -1,139 +0,0 @@
|
||||
const axios = require("axios").default
|
||||
const inquirer = require("inquirer")
|
||||
const open = require("open")
|
||||
const execa = require("execa")
|
||||
const resolveCwd = require(`resolve-cwd`)
|
||||
const { track } = require("medusa-telemetry")
|
||||
|
||||
const { getToken } = require("../util/token-store")
|
||||
const logger = require("../reporter").default
|
||||
|
||||
const MEDUSA_CLI_DEBUG = process.env.MEDUSA_CLI_DEBUG || false
|
||||
|
||||
module.exports = {
|
||||
link: async argv => {
|
||||
track("CLI_LINK", { args: argv })
|
||||
const port = process.env.PORT || 9000
|
||||
const appHost =
|
||||
process.env.MEDUSA_APP_HOST || "https://app.medusa-commerce.com"
|
||||
|
||||
const apiHost =
|
||||
process.env.MEDUSA_API_HOST || "https://api.medusa-commerce.com"
|
||||
|
||||
// Checks if there is already a token from a previous log in; this is
|
||||
// necessary to redirect the customer to the page where local linking is
|
||||
// done
|
||||
const tok = getToken()
|
||||
if (!tok) {
|
||||
console.log(
|
||||
"You must login to Medusa Cloud first. Please run medusa login."
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Get the currently logged in user; we will be using the Cloud user id to
|
||||
// create a user in the local DB with the same user id; allowing you to
|
||||
// authenticate to the local API.
|
||||
const { data: auth } = await axios
|
||||
.get(`${apiHost}/auth`, {
|
||||
headers: {
|
||||
authorization: `Bearer ${tok}`,
|
||||
},
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err)
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
const linkActivity = logger.activity("Linking local project")
|
||||
|
||||
// Create the user with the user id
|
||||
if (!argv.skipLocalUser && auth.user) {
|
||||
let proc
|
||||
try {
|
||||
proc = execa(
|
||||
`./node_modules/@medusajs/medusa/cli.js`,
|
||||
[`user`, `--id`, auth.user.id, `--email`, auth.user.email],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: "command",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (MEDUSA_CLI_DEBUG) {
|
||||
proc.stderr.pipe(process.stderr)
|
||||
proc.stdout.pipe(process.stdout)
|
||||
}
|
||||
|
||||
const res = await proc
|
||||
if (res.stderr) {
|
||||
const err = new Error("stderr error")
|
||||
err.stderr = res.stderr
|
||||
throw err
|
||||
}
|
||||
} catch (error) {
|
||||
logger.failure(linkActivity, "Failed to perform local linking")
|
||||
if (error.stderr) {
|
||||
console.error(error.stderr)
|
||||
} else if (error.code === "ENOENT") {
|
||||
logger.error(
|
||||
`Couldn't find the Medusa CLI - please make sure that you have installed it globally`
|
||||
)
|
||||
}
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
logger.success(linkActivity, "Local project linked")
|
||||
track("CLI_LINK_COMPLETED")
|
||||
|
||||
console.log()
|
||||
console.log(
|
||||
"Link Medusa Cloud to your local server. This will open the browser"
|
||||
)
|
||||
console.log()
|
||||
|
||||
const prompts = [
|
||||
{
|
||||
type: "input",
|
||||
name: "open",
|
||||
message: "Press enter key to open browser for linking or n to exit",
|
||||
},
|
||||
]
|
||||
|
||||
await inquirer.prompt(prompts).then(async a => {
|
||||
if (a.open === "n") {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const params = `lurl=http://localhost:${port}<oken=${auth.user.id}`
|
||||
|
||||
// This step sets the Cloud link by opening a browser
|
||||
const browserOpen = await open(
|
||||
`${appHost}/local-link?${encodeURI(params)}`,
|
||||
{
|
||||
app: "browser",
|
||||
wait: false,
|
||||
}
|
||||
)
|
||||
|
||||
browserOpen.on("error", err => {
|
||||
console.warn(err)
|
||||
console.log(
|
||||
`Could not open browser go to: ${appHost}/local-link?lurl=http://localhost:9000<oken=${auth.user.id}`
|
||||
)
|
||||
})
|
||||
|
||||
track("CLI_LINK_BROWSER_OPENED")
|
||||
})
|
||||
|
||||
if (argv.develop) {
|
||||
const proc = execa(`./node_modules/@medusajs/medusa/cli.js`, [`develop`])
|
||||
proc.stdout.pipe(process.stdout)
|
||||
proc.stderr.pipe(process.stderr)
|
||||
await proc
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
const axios = require("axios").default
|
||||
const open = require("open")
|
||||
const inquirer = require("inquirer")
|
||||
const { track } = require("medusa-telemetry")
|
||||
|
||||
const logger = require("../reporter").default
|
||||
const { setToken } = require("../util/token-store")
|
||||
|
||||
/**
|
||||
* The login command allows the CLI to keep track of Cloud users; the command
|
||||
* makes a cli-login request to the cloud server and keeps an open connection
|
||||
* until the user has authenticated via the Medusa Cloud website.
|
||||
*/
|
||||
module.exports = {
|
||||
login: async _ => {
|
||||
track("CLI_LOGIN")
|
||||
const apiHost =
|
||||
process.env.MEDUSA_API_HOST || "https://api.medusa-commerce.com"
|
||||
|
||||
const authHost = process.env.MEDUSA_AUTH_HOST || `${apiHost}/cli-auth`
|
||||
|
||||
const loginHost =
|
||||
process.env.MEDUSA_APP_HOST || "https://app.medusa-commerce.com"
|
||||
|
||||
const { data: urls } = await axios.post(authHost)
|
||||
|
||||
const loginUri = `${loginHost}${urls.browser_url}`
|
||||
|
||||
const prompts = [
|
||||
{
|
||||
type: "input",
|
||||
name: "open",
|
||||
message: "Press enter key to open browser for login or n to exit",
|
||||
},
|
||||
]
|
||||
|
||||
console.log()
|
||||
console.log("Login to Medusa Cloud")
|
||||
console.log()
|
||||
|
||||
await inquirer.prompt(prompts).then(async a => {
|
||||
if (a.open === "n") {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const browserOpen = await open(loginUri, {
|
||||
app: "browser",
|
||||
wait: false,
|
||||
})
|
||||
browserOpen.on("error", err => {
|
||||
console.warn(err)
|
||||
console.log(`Could not open browser go to: ${loginUri}`)
|
||||
})
|
||||
})
|
||||
|
||||
const spinner = logger.activity(`Waiting for login at ${loginUri}`)
|
||||
const fetchAuth = async (retries = 3) => {
|
||||
try {
|
||||
const { data: auth } = await axios.get(`${authHost}${urls.cli_url}`, {
|
||||
headers: { authorization: `Bearer ${urls.cli_token}` },
|
||||
})
|
||||
return auth
|
||||
} catch (err) {
|
||||
if (retries > 0 && err.http && err.http.statusCode > 500)
|
||||
return fetchAuth(retries - 1)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
const auth = await fetchAuth()
|
||||
|
||||
// This is kept alive for several seconds until the user has authenticated
|
||||
// in the browser.
|
||||
const { data: user } = await axios
|
||||
.get(`${apiHost}/auth`, {
|
||||
headers: {
|
||||
authorization: `Bearer ${auth.password}`,
|
||||
},
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err)
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
if (user) {
|
||||
track("CLI_LOGIN_SUCCEEDED")
|
||||
logger.success(spinner, "Log in succeeded.")
|
||||
setToken(auth.password)
|
||||
} else {
|
||||
track("CLI_LOGIN_FAILED")
|
||||
logger.failure(spinner, "Log in failed.")
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -19,12 +19,13 @@ import inquirer from "inquirer"
|
||||
import reporter from "../reporter"
|
||||
import { getPackageManager, setPackageManager } from "../util/package-manager"
|
||||
import { clearProject } from "@medusajs/utils"
|
||||
import { PanicId } from "../reporter/panic-handler"
|
||||
|
||||
const removeUndefined = (obj) => {
|
||||
return Object.fromEntries(
|
||||
Object.entries(obj)
|
||||
.filter(([_, v]) => v != null)
|
||||
.map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
|
||||
.map(([k, v]) => [k, v === Object(v) ? removeUndefined(v) : v])
|
||||
)
|
||||
}
|
||||
|
||||
@@ -119,10 +120,10 @@ const install = async (rootPath) => {
|
||||
}
|
||||
if (getPackageManager() === `yarn` && checkForYarn()) {
|
||||
await fs.remove(`package-lock.json`)
|
||||
await spawn(`yarnpkg`)
|
||||
await spawn(`yarnpkg`, {})
|
||||
} else {
|
||||
await fs.remove(`yarn.lock`)
|
||||
await spawn(`npm install`)
|
||||
await spawn(`npm install`, {})
|
||||
}
|
||||
} finally {
|
||||
process.chdir(prevDir)
|
||||
@@ -389,6 +390,8 @@ Do you wish to continue with these credentials?
|
||||
|
||||
console.log("\n\nCould not verify DB credentials - please try again\n\n")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const setupDB = async (dbName, dbCreds = {}) => {
|
||||
@@ -553,7 +556,7 @@ medusa new ${rootPath} [url-to-starter]
|
||||
|
||||
if (/medusa-starter/gi.test(rootPath) && isStarterAUrl) {
|
||||
reporter.panic({
|
||||
id: `10000`,
|
||||
id: PanicId.InvalidProjectName,
|
||||
context: {
|
||||
starter,
|
||||
rootPath,
|
||||
@@ -561,8 +564,9 @@ medusa new ${rootPath} [url-to-starter]
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
reporter.panic({
|
||||
id: `10001`,
|
||||
id: PanicId.InvalidProjectName,
|
||||
context: {
|
||||
rootPath,
|
||||
},
|
||||
@@ -572,7 +576,7 @@ medusa new ${rootPath} [url-to-starter]
|
||||
|
||||
if (!isValid(rootPath)) {
|
||||
reporter.panic({
|
||||
id: `10002`,
|
||||
id: PanicId.InvalidPath,
|
||||
context: {
|
||||
path: sysPath.resolve(rootPath),
|
||||
},
|
||||
@@ -582,7 +586,7 @@ medusa new ${rootPath} [url-to-starter]
|
||||
|
||||
if (existsSync(sysPath.join(rootPath, `package.json`))) {
|
||||
reporter.panic({
|
||||
id: `10003`,
|
||||
id: PanicId.AlreadyNodeProject,
|
||||
context: {
|
||||
rootPath,
|
||||
},
|
||||
@@ -1,48 +0,0 @@
|
||||
const axios = require("axios").default
|
||||
const { getToken } = require("../util/token-store")
|
||||
const logger = require("../reporter").default
|
||||
|
||||
/**
|
||||
* Fetches the locally logged in user.
|
||||
*/
|
||||
module.exports = {
|
||||
whoami: async argv => {
|
||||
const apiHost =
|
||||
process.env.MEDUSA_API_HOST || "https://api.medusa-commerce.com"
|
||||
|
||||
const tok = getToken()
|
||||
|
||||
if (!tok) {
|
||||
console.log(
|
||||
"You are not logged into Medusa Cloud. Please run medusa login."
|
||||
)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const activity = logger.activity("checking login details")
|
||||
|
||||
const { data: auth } = await axios
|
||||
.get(`${apiHost}/auth`, {
|
||||
headers: {
|
||||
authorization: `Bearer ${tok}`,
|
||||
},
|
||||
})
|
||||
.catch(err => {
|
||||
logger.failure(activity, "Couldn't gather login details")
|
||||
logger.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
if (auth.user) {
|
||||
logger.success(
|
||||
activity,
|
||||
`Hi, ${auth.user.first_name}! Here are your details:`
|
||||
)
|
||||
|
||||
console.log(`id: ${auth.user.id}`)
|
||||
console.log(`email: ${auth.user.email}`)
|
||||
console.log(`first_name: ${auth.user.first_name}`)
|
||||
console.log(`last_name: ${auth.user.last_name}`)
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user