fix: better cli
This commit is contained in:
@@ -40,6 +40,7 @@
|
||||
"@hapi/joi": "^16.1.8",
|
||||
"axios": "^0.21.1",
|
||||
"chalk": "^4.0.0",
|
||||
"configstore": "5.0.1",
|
||||
"core-js": "^3.6.5",
|
||||
"dotenv": "^8.2.0",
|
||||
"fs-exists-cached": "^1.0.0",
|
||||
|
||||
@@ -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<oken=${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}`
|
||||
)
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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")
|
||||
|
||||
20
packages/medusa-cli/src/util/token-store.js
Normal file
20
packages/medusa-cli/src/util/token-store.js
Normal 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)
|
||||
},
|
||||
}
|
||||
@@ -1704,6 +1704,18 @@ concat-map@0.0.1:
|
||||
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
|
||||
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
|
||||
|
||||
configstore@5.0.1:
|
||||
version "5.0.1"
|
||||
resolved "https://registry.yarnpkg.com/configstore/-/configstore-5.0.1.tgz#d365021b5df4b98cdd187d6a3b0e3f6a7cc5ed96"
|
||||
integrity sha512-aMKprgk5YhBNyH25hj8wGt2+D52Sw1DRRIzqBwLp2Ya9mFmY8KPvvtvmna8SxVR9JMZ4kzMD68N22vlaRpkeFA==
|
||||
dependencies:
|
||||
dot-prop "^5.2.0"
|
||||
graceful-fs "^4.1.2"
|
||||
make-dir "^3.0.0"
|
||||
unique-string "^2.0.0"
|
||||
write-file-atomic "^3.0.0"
|
||||
xdg-basedir "^4.0.0"
|
||||
|
||||
convert-source-map@^1.1.0, convert-source-map@^1.4.0, convert-source-map@^1.6.0, convert-source-map@^1.7.0:
|
||||
version "1.7.0"
|
||||
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.7.0.tgz#17a2cb882d7f77d3490585e2ce6c524424a3a442"
|
||||
@@ -1766,6 +1778,11 @@ cross-spawn@^7.0.0:
|
||||
shebang-command "^2.0.0"
|
||||
which "^2.0.1"
|
||||
|
||||
crypto-random-string@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-2.0.0.tgz#ef2a7a966ec11083388369baa02ebead229b30d5"
|
||||
integrity sha512-v1plID3y9r/lPhviJ1wrXpLeyUIGAZ2SHNYTEapm7/8A9nLPoyvVp3RK/EPFqn5kEznyWgYZNsRtYYIWbuG8KA==
|
||||
|
||||
cssom@^0.4.1:
|
||||
version "0.4.4"
|
||||
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.4.4.tgz#5a66cf93d2d0b661d80bf6a44fb65f5c2e4e0a10"
|
||||
@@ -1903,6 +1920,13 @@ domexception@^1.0.1:
|
||||
dependencies:
|
||||
webidl-conversions "^4.0.2"
|
||||
|
||||
dot-prop@^5.2.0:
|
||||
version "5.3.0"
|
||||
resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-5.3.0.tgz#90ccce708cd9cd82cc4dc8c3ddd9abdd55b20e88"
|
||||
integrity sha512-QM8q3zDe58hqUqjraQOmzZ1LIH9SWQJTlEKCH4kJ2oQvLZk7RbQXvtDM2XEq3fwkV9CCvvH4LA0AV+ogFsBM2Q==
|
||||
dependencies:
|
||||
is-obj "^2.0.0"
|
||||
|
||||
dotenv@^8.2.0:
|
||||
version "8.2.0"
|
||||
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
|
||||
@@ -2439,6 +2463,11 @@ graceful-fs@^4.1.11, graceful-fs@^4.2.4:
|
||||
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb"
|
||||
integrity sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==
|
||||
|
||||
graceful-fs@^4.1.2:
|
||||
version "4.2.6"
|
||||
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
|
||||
integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==
|
||||
|
||||
growly@^1.3.0:
|
||||
version "1.3.0"
|
||||
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
|
||||
@@ -2768,6 +2797,11 @@ is-number@^7.0.0:
|
||||
resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b"
|
||||
integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==
|
||||
|
||||
is-obj@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/is-obj/-/is-obj-2.0.0.tgz#473fb05d973705e3fd9620545018ca8e22ef4982"
|
||||
integrity sha512-drqDG3cbczxxEJRoOXcOjtdp1J/lyp1mNn0xaznRs8+muBhgQcrnbspox5X5fOw0HnMnbfDzvnEMEtqDEJEo8w==
|
||||
|
||||
is-plain-object@^2.0.3, is-plain-object@^2.0.4:
|
||||
version "2.0.4"
|
||||
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677"
|
||||
@@ -4827,6 +4861,13 @@ union-value@^1.0.0:
|
||||
is-extendable "^0.1.1"
|
||||
set-value "^2.0.1"
|
||||
|
||||
unique-string@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-2.0.0.tgz#39c6451f81afb2749de2b233e3f7c5e8843bd89d"
|
||||
integrity sha512-uNaeirEPvpZWSgzwsPGtU2zVSTrn/8L5q/IexZmH0eH6SA73CmAA5U4GwORTxQAZs95TAXLNqeLoPPNO5gZfWg==
|
||||
dependencies:
|
||||
crypto-random-string "^2.0.0"
|
||||
|
||||
unset-value@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/unset-value/-/unset-value-1.0.0.tgz#8376873f7d2335179ffb1e6fc3a8ed0dfc8ab559"
|
||||
@@ -5007,6 +5048,11 @@ ws@^7.0.0:
|
||||
resolved "https://registry.yarnpkg.com/ws/-/ws-7.2.5.tgz#abb1370d4626a5a9cd79d8de404aa18b3465d10d"
|
||||
integrity sha512-C34cIU4+DB2vMyAbmEKossWq2ZQDr6QEyuuCzWrM9zfw1sGc0mYiJ0UnG9zzNykt49C2Fi34hvr2vssFQRS6EA==
|
||||
|
||||
xdg-basedir@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-4.0.0.tgz#4bc8d9984403696225ef83a1573cbbcb4e79db13"
|
||||
integrity sha512-PSNhEJDejZYV7h50BohL09Er9VaIefr2LMAf3OEmpCkjOi34eYyQYAXUTjEQtZJTKcF0E2UKTh+osDLsgNim9Q==
|
||||
|
||||
xml-name-validator@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"
|
||||
|
||||
Reference in New Issue
Block a user