fix: better cli

This commit is contained in:
Sebastian Rindom
2021-05-02 23:20:35 +02:00
parent df5018669c
commit 953747f3d2
7 changed files with 112 additions and 39 deletions

View File

@@ -1,13 +1,15 @@
const axios = require("axios").default
const open = require("open")
const resolveCwd = require(`resolve-cwd`)
const Netrc = require("netrc-parser").default
const { getToken } = require("../util/token-store")
module.exports = {
link: async argv => {
const appHost =
process.env.MEDUSA_APP_HOST || "https://app.medusa-commerce.com"
const apiHost =
process.env.MEDUSA_API_HOST || "https://api.medusa-commerce.com"
const hostMachine =
process.env.MEDUSA_HOST_MACHINE || "api.medusa-commerce.com"
function resolveLocalCommand(command) {
try {
@@ -21,22 +23,18 @@ module.exports = {
}
}
await Netrc.load()
if (!Netrc.machines[hostMachine]) {
const tok = getToken()
if (!tok) {
console.log(
"You must login to Medusa Cloud first. Please run medusa login."
)
process.exit(1)
}
const { login, password } = Netrc.machines[hostMachine]
const { data: auth } = await axios
.get(`${apiHost}/auth`, {
auth: {
username: login,
password: password,
headers: {
authorization: `Bearer ${tok}`,
},
})
.catch(err => {
@@ -44,13 +42,29 @@ module.exports = {
process.exit(1)
})
if (auth.user) {
if (!argv.skipLocalUser && auth.user) {
const localCmd = resolveLocalCommand(`user`)
return localCmd({
await localCmd({
directory: argv.directory,
id: auth.user.id,
email: auth.user.email,
})
}
console.log(auth.user)
const bo = await open(
`${appHost}/local-link?lurl=http://localhost:4000&ltoken=${auth.user.id}`,
{
app: "browser",
wait: false,
}
)
bo.on("error", err => {
console.warn(err)
console.log(
`Could not open browser go to: ${loginHost}${urls.browser_url}`
)
})
},
}

View File

@@ -1,8 +1,9 @@
const axios = require("axios").default
const open = require("open")
const Netrc = require("netrc-parser").default
const inquirer = require("inquirer")
const { setToken } = require("../util/token-store")
module.exports = {
login: async _ => {
const apiHost =
@@ -11,7 +12,7 @@ module.exports = {
const authHost = process.env.MEDUSA_AUTH_HOST || `${apiHost}/cli-auth`
const loginHost =
process.env.MEDUSA_LOGIN_HOST || "https://admin.medusa-commerce.com"
process.env.MEDUSA_APP_HOST || "https://app.medusa-commerce.com"
const { data: urls } = await axios.post(authHost)
@@ -55,9 +56,8 @@ module.exports = {
const { data: user } = await axios
.get(`${apiHost}/auth`, {
auth: {
username: auth.username,
password: auth.password,
headers: {
authorization: `Bearer ${auth.password}`,
},
})
.catch(err => {
@@ -65,18 +65,9 @@ module.exports = {
process.exit(1)
})
await Netrc.load()
if (user) {
const hostMachine =
process.env.MEDUSA_HOST_MACHINE || "api.medusa-commerce.com"
if (!Netrc.machines[hostMachine]) {
Netrc.machines[hostMachine] = {}
}
Netrc.machines[hostMachine].login = auth.username
Netrc.machines[hostMachine].password = auth.password
setToken(auth.password)
}
await Netrc.save()
})
},
}

View File

@@ -1,30 +1,24 @@
const axios = require("axios").default
const resolveCwd = require(`resolve-cwd`)
const Netrc = require("netrc-parser").default
const { getToken } = require("../util/token-store")
module.exports = {
whoami: async argv => {
const apiHost =
process.env.MEDUSA_API_HOST || "https://api.medusa-commerce.com"
const hostMachine =
process.env.MEDUSA_HOST_MACHINE || "api.medusa-commerce.com"
await Netrc.load()
const tok = getToken()
if (!Netrc.machines[hostMachine]) {
if (!tok) {
console.log(
"You are not logged into Medusa Cloud. Please run medusa login."
)
process.exit(0)
}
const { login, password } = Netrc.machines[hostMachine]
const { data: auth } = await axios
.get(`${apiHost}/auth`, {
auth: {
username: login,
password: password,
headers: {
authorization: `Bearer ${tok}`,
},
})
.catch(err => {

View File

@@ -90,6 +90,13 @@ function buildLocalCommands(cli, isLocalProject) {
.command({
command: `link`,
desc: `Creates your Medusa Cloud user in your local database for local testing.`,
builder: _ =>
_.option(`skip-local-user`, {
alias: `skipLocalUser`,
type: `boolean`,
default: false,
describe: `If set a user will not be created in the database.`,
}),
handler: handlerP(argv => {
if (!isLocalProject) {
console.log("must be a local project")

View File

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