Skip to content

Commit

Permalink
feat: login自动配置userId、privateKey
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchch6 committed Jun 6, 2020
1 parent 1b83f9c commit b997029
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 36 deletions.
9 changes: 6 additions & 3 deletions src/cli/login.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fetch = require('node-fetch')
const FormData = require('form-data')
const inquirer = require('inquirer')
const chalk = require('chalk')
const { LOGIN_URL } = require('../utils/globalConfig')
const { set } = require('./set')
const { BASE_URL, LOGIN_URL, CONFIG_FILE_NAME } = require('../utils/globalConfig')

const login = async () => {
// 输入友互通账号密码
Expand Down Expand Up @@ -56,7 +56,10 @@ const setUserInfo = async (ans) => {
.then(async function (response) {
const res = await response.json()
if (res.status === 200) {
console.log(chalk.green(`${res.data}`))
const { userId, privateKey } = res.data
set(CONFIG_FILE_NAME, { userId, privateKey })
console.log(res)
console.log(`Logged in as ${ans.username} on ${BASE_URL}.`)
}
}).catch(function (error) {
console.error(error.message)
Expand Down
11 changes: 6 additions & 5 deletions src/cli/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ const { CONFIG_FILE_NAME, YNPM_PUBLISH_URL } = require('../utils/globalConfig')
const publish = () => {
const userConfig = getRc(CONFIG_FILE_NAME)
if (userConfig === null) {
console.error(chalk.red('Error: Can not Find User, Please Use `yonui set email=xxx && yonui set privateKey=xxx` !'))
console.error(chalk.red('Error: Can not Find User, Please Use `yonui login` !'))
process.exit(0)
}
const { email, privateKey } = userConfig
if (!email || !privateKey) {
console.error(chalk.red('Error: Can not Find User, Please Use `yonui set email=xxx && yonui set privateKey=xxx` !'))
const { username, privateKey, userId } = userConfig
if (!(userId || username) && !privateKey) {
console.error(chalk.red('Error: Can not Find User, Please Use `yonui login` !'))
process.exit(0)
}
const packageJson = replacePackageName(getPackageJson(), getManifestJson())
Expand All @@ -24,7 +24,8 @@ const publish = () => {
form.append('readme', fs.createReadStream(path.resolve('./README.md')))
form.append('api', fs.createReadStream(path.resolve('./api.md')))
form.append('packageJson', JSON.stringify(packageJson))
form.append('email', email)
userId && form.append('userId', userId)
username && form.append('username', username)
form.append('privateKey', privateKey)
fetch(YNPM_PUBLISH_URL, {
method: 'post',
Expand Down
35 changes: 10 additions & 25 deletions src/cli/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs')
const propertiesParser = require('properties-parser')
const objectAssign = require('object-assign')
const userPath = process.env.HOME || process.env.USERPROFILE
function getCommands (fileName) {
function getConfig (fileName) {
let config = {}
const argvs = process.argv
try {
Expand All @@ -23,38 +23,23 @@ function getCommands (fileName) {
return null
}
}
function set (fileName) {
function set (fileName, property = {}) {
const path = getRcFile(fileName)
try {
const valida = getValidateRc(fileName)
if (!valida) {
const comm = getCommands(fileName)
const editor = propertiesParser.createEditor()
for (const item in comm) {
editor.set(item, comm[item])
}
fs.writeFileSync(path, editor.toString())
// comm?fs.writeFileSync(path,JSON.stringify(comm)):"";
} else {
const comm = getCommands(fileName)
let config = propertiesParser.read(path)
if (comm) {
config = config || {}
config = objectAssign(config, comm)
const editor = propertiesParser.createEditor()
for (const item in config) {
editor.set(item, config[item])
}
fs.writeFileSync(path, editor.toString())
};
const comm = getConfig(fileName)
const config = objectAssign(property, comm)
const editor = propertiesParser.createEditor()
for (const item in config) {
editor.set(item, config[item])
}
fs.writeFileSync(path, editor.toString())
} catch (e) {

console.log(e)
}
}

/**
* 获取文件
* 获取文件内容
* @param {any} fileName
* @returns
*/
Expand Down
8 changes: 5 additions & 3 deletions src/utils/globalConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const CONFIG_FILE_NAME = 'yonui-cli'
const YNPM_PUBLISH_URL = 'http://libraui-backend.test.app.yyuap.com/package/cliPublish'
const LOGIN_URL = 'http://libraui-backend.test.app.yyuap.com/package/cliLogin'
const BASE_URL = 'http://libraui-backend.test.app.yyuap.com/'
const YNPM_PUBLISH_URL = `${BASE_URL}package/cliPublish`
const LOGIN_URL = `${BASE_URL}user/cliLogin`

module.exports = {
CONFIG_FILE_NAME,
YNPM_PUBLISH_URL,
LOGIN_URL
LOGIN_URL,
BASE_URL
}
Binary file modified templates/project.tgz
Binary file not shown.

0 comments on commit b997029

Please sign in to comment.