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:
Adrien de Peretti
2023-06-28 16:37:25 +02:00
committed by GitHub
parent fe25c8a91f
commit 6f1fa244fa
32 changed files with 244 additions and 521 deletions

View File

@@ -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}&ltoken=${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&ltoken=${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
}
},
}

View File

@@ -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.")
}
},
}

View File

@@ -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,
},

View File

@@ -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}`)
}
},
}

View File

@@ -1,17 +1,15 @@
const path = require(`path`)
const resolveCwd = require(`resolve-cwd`)
import path from "path"
import resolveCwd from "resolve-cwd"
import { sync as existsSync } from "fs-exists-cached"
import { setTelemetryEnabled } from "medusa-telemetry"
import { getLocalMedusaVersion } from "./util/version"
import { didYouMean } from "./did-you-mean"
import reporter from "./reporter"
import { newStarter } from "./commands/new"
const yargs = require(`yargs`)
const existsSync = require(`fs-exists-cached`).sync
const { setTelemetryEnabled } = require("medusa-telemetry")
const { getLocalMedusaVersion } = require(`./util/version`)
const { didYouMean } = require(`./did-you-mean`)
const reporter = require("./reporter").default
const { newStarter } = require("./commands/new")
const { whoami } = require("./commands/whoami")
const { login } = require("./commands/login")
const { link } = require("./commands/link")
const handlerP =
(fn) =>
@@ -31,18 +29,10 @@ function buildLocalCommands(cli, isLocalProject) {
const useYarn = existsSync(path.join(directory, `yarn.lock`))
if (isLocalProject) {
const json = require(path.join(directory, `package.json`))
projectInfo.sitePackageJson = json
}
function getLocalMedusaMajorVersion() {
let version = getLocalMedusaVersion()
if (version) {
version = Number(version.split(`.`)[0])
}
return version
projectInfo["sitePackageJson"] = require(path.join(
directory,
`package.json`
))
}
function resolveLocalCommand(command) {
@@ -53,10 +43,10 @@ function buildLocalCommands(cli, isLocalProject) {
try {
const cmdPath = resolveCwd.silent(
`@medusajs/medusa/dist/commands/${command}`
)
)!
return require(cmdPath).default
} catch (err) {
if (process.env.NODE_ENV !== "production") {
if (!process.env.NODE_ENV?.startsWith("prod")) {
console.log("--------------- ERROR ---------------------")
console.log(err)
console.log("-------------------------------------------")
@@ -166,7 +156,7 @@ function buildLocalCommands(cli, isLocalProject) {
}),
handler: handlerP(
getCommandHandler(`seed`, (args, cmd) => {
process.env.NODE_ENV = process.env.NODE_ENV || `development`
process.env.NODE_ENV ??= `development`
return cmd(args)
})
),
@@ -187,41 +177,6 @@ function buildLocalCommands(cli, isLocalProject) {
})
),
})
.command({
command: `whoami`,
desc: `View the details of the currently logged in user.`,
handler: handlerP(whoami),
})
.command({
command: `link`,
desc: `Creates your Medusa Cloud user in your local database for local testing.`,
builder: (_) =>
_.option(`su`, {
alias: `skip-local-user`,
type: `boolean`,
default: false,
describe: `If set a user will not be created in the database.`,
}).option(`develop`, {
type: `boolean`,
default: false,
describe: `If set medusa develop will be run after successful linking.`,
}),
handler: handlerP((argv) => {
if (!isLocalProject) {
console.log("must be a local project")
cli.showHelp()
}
const args = { ...argv, ...projectInfo, useYarn }
return link(args)
}),
})
.command({
command: `login`,
desc: `Logs you into Medusa Cloud.`,
handler: handlerP(login),
})
.command({
command: `develop`,
desc: `Start development server. Watches file and rebuilds when something changes`,
@@ -316,17 +271,20 @@ function buildLocalCommands(cli, isLocalProject) {
function isLocalMedusaProject() {
let inMedusaProject = false
try {
const { dependencies, devDependencies } = require(path.resolve(
`./package.json`
))
inMedusaProject =
inMedusaProject = !!(
(dependencies && dependencies["@medusajs/medusa"]) ||
(devDependencies && devDependencies["@medusajs/medusa"])
)
} catch (err) {
/* ignore */
// ignore
}
return !!inMedusaProject
return inMedusaProject
}
function getVersionInfo() {
@@ -347,7 +305,7 @@ Medusa version: ${medusaVersion}
}
}
module.exports = (argv) => {
export default (argv) => {
const cli = yargs()
const isLocalProject = isLocalMedusaProject()
@@ -402,7 +360,7 @@ module.exports = (argv) => {
const arg = argv.slice(2)[0]
const suggestion = arg ? didYouMean(arg, availableCommands) : ``
if (process.env.NODE_ENV !== "production") {
if (!process.env.NODE_ENV?.startsWith("prod")) {
console.log("--------------- ERROR ---------------------")
console.log(err)
console.log("-------------------------------------------")

View File

@@ -1,7 +1,7 @@
import meant from "meant"
export function didYouMean(scmd, commands) {
const bestSimilarity = meant(scmd, commands).map(str => {
export function didYouMean(scmd, commands): string {
const bestSimilarity = meant(scmd, commands).map((str) => {
return ` ${str}`
})

View File

@@ -1,69 +0,0 @@
#!/usr/bin/env node
import "core-js/stable"
import "regenerator-runtime/runtime"
import os from "os"
import semver from "semver"
import util from "util"
import createCli from "./create-cli"
// import report from "./reporter"
import pkg from "../package.json"
// import updateNotifier from "update-notifier"
// import { ensureWindowsDriveLetterIsUppercase } from "./util/ensure-windows-drive-letter-is-uppercase"
const useJsonLogger = process.argv.slice(2).some(arg => arg.includes(`json`))
if (useJsonLogger) {
process.env.GATSBY_LOGGER = `json`
}
// Ensure stable runs on Windows when started from different shells (i.e. c:\dir vs C:\dir)
if (os.platform() === `win32`) {
// ensureWindowsDriveLetterIsUppercase()
}
// Check if update is available
// updateNotifier({ pkg }).notify({ isGlobal: true })
const MIN_NODE_VERSION = `10.13.0`
// const NEXT_MIN_NODE_VERSION = `10.13.0`
if (!semver.satisfies(process.version, `>=${MIN_NODE_VERSION}`)) {
//report.panic(
// report.stripIndent(`
// Gatsby requires Node.js ${MIN_NODE_VERSION} or higher (you have ${process.version}).
// Upgrade Node to the latest stable release: https://gatsby.dev/upgrading-node-js
// `)
//)
}
// if (!semver.satisfies(process.version, `>=${NEXT_MIN_NODE_VERSION}`)) {
// report.warn(
// report.stripIndent(`
// Node.js ${process.version} has reached End of Life status on 31 December, 2019.
// Gatsby will only actively support ${NEXT_MIN_NODE_VERSION} or higher and drop support for Node 8 soon.
// Please upgrade Node.js to a currently active LTS release: https://gatsby.dev/upgrading-node-js
// `)
// )
// }
process.on(`unhandledRejection`, reason => {
// This will exit the process in newer Node anyway so lets be consistent
// across versions and crash
// reason can be anything, it can be a message, an object, ANYTHING!
// we convert it to an error object so we don't crash on structured error validation
if (!(reason instanceof Error)) {
reason = new Error(util.format(reason))
}
console.log(reason)
// report.panic(`UNHANDLED REJECTION`, reason as Error)
})
process.on(`uncaughtException`, error => {
console.log(error)
// report.panic(`UNHANDLED EXCEPTION`, error)
})
createCli(process.argv)

View File

@@ -0,0 +1,44 @@
#!/usr/bin/env node
import "core-js/stable"
import "regenerator-runtime/runtime"
import os from "os"
import util from "util"
import createCli from "./create-cli"
const useJsonLogger = process.argv.slice(2).some((arg) => arg.includes(`json`))
if (useJsonLogger) {
process.env.GATSBY_LOGGER = `json`
}
// Ensure stable runs on Windows when started from different shells (i.e. c:\dir vs C:\dir)
if (os.platform() === `win32`) {
// ensureWindowsDriveLetterIsUppercase()
}
// Check if update is available
// updateNotifier({ pkg }).notify({ isGlobal: true })
const MIN_NODE_VERSION = `10.13.0`
process.on(`unhandledRejection`, (reason) => {
// This will exit the process in newer Node anyway so lets be consistent
// across versions and crash
// reason can be anything, it can be a message, an object, ANYTHING!
// we convert it to an error object, so we don't crash on structured error validation
if (!(reason instanceof Error)) {
reason = new Error(util.format(reason))
}
console.log(reason)
// report.panic(`UNHANDLED REJECTION`, reason as Error)
})
process.on(`uncaughtException`, (error) => {
console.log(error)
// report.panic(`UNHANDLED EXCEPTION`, error)
})
createCli(process.argv)

View File

@@ -5,12 +5,14 @@ import ora from "ora"
import { track } from "medusa-telemetry"
import { panicHandler } from "./panic-handler"
import * as Transport from "winston-transport"
const LOG_LEVEL = process.env.LOG_LEVEL || "silly"
const NODE_ENV = process.env.NODE_ENV || "development"
const IS_DEV = NODE_ENV === "development"
const IS_DEV = NODE_ENV.startsWith("dev")
let transports: Transport[] = []
const transports = []
if (!IS_DEV) {
transports.push(new winston.transports.Console())
} else {
@@ -39,8 +41,12 @@ const loggerInstance = winston.createLogger({
})
export class Reporter {
protected activities_: Record<string, any>
protected loggerInstance_: winston.Logger
protected ora_: typeof ora
constructor({ logger, activityLogger }) {
this.activities_ = []
this.activities_ = {}
this.loggerInstance_ = logger
this.ora_ = activityLogger
}
@@ -92,6 +98,7 @@ export class Reporter {
* Begin an activity. In development an activity is displayed as a spinner;
* in other environments it will log the activity at the info level.
* @param {string} message - the message to log the activity under
* @param {any} config
* @returns {string} the id of the activity; this should be passed to do
* further operations on the activity such as success, failure, progress.
*/
@@ -141,7 +148,7 @@ export class Reporter {
if (activity.activity) {
activity.text = message
} else {
toLog.activity_id = activityId
toLog["activity_id"] = activityId
this.loggerInstance_.log(toLog)
}
} else {
@@ -156,7 +163,7 @@ export class Reporter {
* message to log the error under; or an error object.
* @param {Error?} error - an error object to log message with
*/
error = (messageOrError, error) => {
error = (messageOrError, error = null) => {
let message = messageOrError
if (typeof messageOrError === "object") {
message = messageOrError.message
@@ -169,7 +176,7 @@ export class Reporter {
}
if (error) {
toLog.stack = stackTrace.parse(error)
toLog["stack"] = stackTrace.parse(error)
}
this.loggerInstance_.log(toLog)
@@ -200,8 +207,8 @@ export class Reporter {
if (activity.activity) {
activity.activity.fail(`${message} ${time - activity.start}`)
} else {
toLog.duration = time - activity.start
toLog.activity_id = activityId
toLog["duration"] = time - activity.start
toLog["activity_id"] = activityId
this.loggerInstance_.log(toLog)
}
} else {
@@ -225,7 +232,7 @@ export class Reporter {
* at the info level.
* @param {string} activityId - the id of the activity as returned by activity
* @param {string} message - the message to log
* @returns {object} data about the activity
* @returns {Record<string, any>} data about the activity
*/
success = (activityId, message) => {
const time = Date.now()
@@ -239,8 +246,8 @@ export class Reporter {
if (activity.activity) {
activity.activity.succeed(`${message} ${time - activity.start}ms`)
} else {
toLog.duration = time - activity.start
toLog.activity_id = activityId
toLog["duration"] = time - activity.start
toLog["activity_id"] = activityId
this.loggerInstance_.log(toLog)
}
} else {
@@ -295,6 +302,7 @@ export class Reporter {
* A wrapper around winston's log method.
*/
log = (...args) => {
// @ts-ignore
this.loggerInstance_.log(...args)
}
}

View File

@@ -1,14 +1,24 @@
export const panicHandler = (panicData = {}) => {
export type PanicData = {
id: string
context: {
rootPath: string
path: string
}
}
export enum PanicId {
InvalidProjectName = "10000",
InvalidPath = "10002",
AlreadyNodeProject = "10003",
}
export const panicHandler = (panicData: PanicData = {} as PanicData) => {
const { id, context } = panicData
switch (id) {
case "10000":
return {
message: `Looks like you provided a URL as your project name. Try "medusa new my-medusa-store ${context.rootPath}" instead.`,
}
case "10001":
return {
message: `Looks like you provided a URL as your project name. Try "medusa new my-medusa-store ${context.rootPath}" instead.`,
}
case "10002":
return {
message: `Could not create project because ${context.path} is not a valid path.`,

View File

@@ -1,22 +1,15 @@
import ConfigStore from "configstore"
import reporter from "../reporter"
let config
const config = new ConfigStore(`medusa`, {}, { globalConfigPath: true })
const packageMangerConfigKey = `cli.packageManager`
export const getPackageManager = () => {
if (!config) {
config = new ConfigStore(`medusa`, {}, { globalConfigPath: true })
}
return config.get(packageMangerConfigKey)
}
export const setPackageManager = packageManager => {
if (!config) {
config = new ConfigStore(`medusa`, {}, { globalConfigPath: true })
}
export const setPackageManager = (packageManager) => {
config.set(packageMangerConfigKey, packageManager)
reporter.info(`Preferred package manager set to "${packageManager}"`)
}

View File

@@ -1,20 +0,0 @@
const ConfigStore = require("configstore")
let config
module.exports = {
getToken: function() {
if (!config) {
config = new ConfigStore(`medusa`, {}, { globalConfigPath: true })
}
return config.get("cloud.login_token")
},
setToken: function(token) {
if (!config) {
config = new ConfigStore(`medusa`, {}, { globalConfigPath: true })
}
return config.set("cloud.login_token", token)
},
}

View File

@@ -1,6 +0,0 @@
import { getMedusaVersion } from "medusa-core-utils"
export const getLocalMedusaVersion = () => {
const version = getMedusaVersion()
return version
}

View File

@@ -0,0 +1,5 @@
import { getMedusaVersion } from "medusa-core-utils"
export const getLocalMedusaVersion = (): string => {
return getMedusaVersion()
}