From 1b83f9c08417c5be474730b9a018dae439cef274 Mon Sep 17 00:00:00 2001 From: yangchch6 Date: Fri, 5 Jun 2020 11:44:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=8F=8B=E4=BA=92=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/index.js | 7 ++++ src/cli/index.js | 4 ++- src/cli/login.js | 66 ++++++++++++++++++++++++++++++++++ src/utils/globalConfig.js | 4 ++- templates/Project/package.json | 1 - 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/cli/login.js diff --git a/bin/index.js b/bin/index.js index c74b834..6425171 100755 --- a/bin/index.js +++ b/bin/index.js @@ -52,6 +52,13 @@ const libra = () => { cli.set('yonui-cli') }) + program + .command('login') + .description('Log in to YonBuilder') + .action(() => { + cli.login() + }) + program .command('publish') .description('publish') diff --git a/src/cli/index.js b/src/cli/index.js index 2ef2791..1fce2d5 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -5,6 +5,7 @@ const start = require('./start') const create = require('./create') const compress = require('./compress') const publish = require('./publish') +const login = require('./login') module.exports = { init, build, @@ -12,5 +13,6 @@ module.exports = { create, set, publish, - compress + compress, + login } diff --git a/src/cli/login.js b/src/cli/login.js new file mode 100644 index 0000000..e084579 --- /dev/null +++ b/src/cli/login.js @@ -0,0 +1,66 @@ +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 login = async () => { + // 输入友互通账号密码 + const ans = await inquirer.prompt([ + { + type: 'input', + name: 'username', + message: 'username:', + validate: function (val) { + if (checkPhone(val) || checkEmail(val)) { // 校验位数 + return true + } + return 'Please input the correct mobile number or email' + } + }, + { + type: 'password', + name: 'password', + message: 'password:' + } + ]) + await setUserInfo(ans) +} + +function checkPhone (str) { + const reg = /^1[3456789]\d{9}$/ + if (reg.test(str)) { + return true + } else { + return false + } +} + +function checkEmail (str) { + const reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/ + if (reg.test(str)) { + return true + } else { + return false + } +} + +const setUserInfo = async (ans) => { + const form = new FormData() + form.append('username', ans.username) + form.append('password', ans.password) + fetch(LOGIN_URL, { + method: 'post', + body: form + }) + .then(async function (response) { + const res = await response.json() + if (res.status === 200) { + console.log(chalk.green(`${res.data}`)) + } + }).catch(function (error) { + console.error(error.message) + }) +} + +module.exports = login diff --git a/src/utils/globalConfig.js b/src/utils/globalConfig.js index 951f911..9008387 100644 --- a/src/utils/globalConfig.js +++ b/src/utils/globalConfig.js @@ -1,7 +1,9 @@ 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' module.exports = { CONFIG_FILE_NAME, - YNPM_PUBLISH_URL + YNPM_PUBLISH_URL, + LOGIN_URL } diff --git a/templates/Project/package.json b/templates/Project/package.json index 9e83429..f874b3a 100644 --- a/templates/Project/package.json +++ b/templates/Project/package.json @@ -15,7 +15,6 @@ }, "author": "", "license": "ISC", - "syncToMDD": true, "husky": { "hooks": { "pre-commit": "npm run lint:git",