html项目cli脚手架,前端脚手架(cli)实现

背景

整合前端团队代码规范, 以及统一整体架构

使用库

本文借鉴vue-cli思路,用到的库有:

t.insert("four","three");list.insert("five","four");//console.log(list.showlist());//onetwothreefour

commander.js,可以自动的解析命令和参数,用于处理用户输入的命令。

download-git-repo,下载并提取 git 仓库,用于下载项目模板。

inquirer.js,通用的命令行用户界面集合,用于和用户进行交互。

handlebars.js,模板引擎,将用户提交的信息动态填充到文件中。

ora,下载过程久的话,可以用于显示下载中的动画效果。

chalk,可以给终端的字体加上颜色。

log-symbols,可以在终端上显示出 √ 或 × 等的图标。

代码实现

下载依赖

npm init -y

npm i commander download-git-repo inquirer handlebars ora chalk log-symbols -S

package.json

{

"name": "lcq-cli",

"version": "1.0.0",

"description": "lcq-cli",

"bin": {

"lcq": "index.js"

},

...

}

index.js

const fs = require("fs");

const program = require("commander");

const download = require("download-git-repo");

const handlebars = require("handlebars");

const inquirer = require("inquirer");

const ora = require("ora");

const chalk = require("chalk");

const symbols = require("log-symbols");

program.version("1.0.0", "-v, --version")

.command("init ")

.action((name) => {

if(!fs.existsSync(name)){

inquirer.prompt([

{

name: "description",

message: "请输入项目描述"

},

{

name: "author",

message: "请输入作者名称"

}

]).then((answers) => {

const spinner = ora("download...");

spinner.start();

download("https://github.com:bear-new/latest-webpack-cli#master", name, {clone: true}, (err) => {

if(err){

spinner.fail();

console.log(chalk.red(err));

}else{

spinner.succeed();

const fileName = `${name}/package.json`;

const meta = {

name,

description: answers.description,

author: answers.author

}

if(fs.existsSync(fileName)){

const content = fs.readFileSync(fileName).toString();

const result = handlebars.compile(content)(meta);

fs.writeFileSync(fileName, result);

}

console.log(chalk.green("success"));

}

})

})

}else{

console.log(symbols.error, chalk.red("项目已存在"));

}

})

program.parse(process.argv);

执行

node ./index.js init demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Vue 3.0和CLI脚手架实现登录的代码示例: 1. 在Vue 3.0项目中创建一个名为“Login”的组件,代码如下: ``` <template> <div> <h2>登录</h2> <label>用户名:</label> <input type="text" v-model="username"> <label>密码:</label> <input type="password" v-model="password"> <button v-on:click="login">登录</button> </div> </template> <script> export default { data() { return { username: '', password: '' } }, methods: { login() { // 在这里实现登录逻辑 } } } </script> ``` 2. 使用Vue Router创建一个名为“Login”的路由,指向上述组件。 ``` import { createRouter, createWebHistory } from 'vue-router' import Login from './components/Login.vue' const routes = [ { path: '/login', name: 'Login', component: Login } ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` 3. 在Vue 3.0项目中安装并配置axios,并在上述Login组件中使用axios发送POST请求实现登录逻辑。 ```javascript import axios from 'axios'; methods: { async login() { const response = await axios.post('http://localhost:3000/login', { username: this.username, password: this.password }); const token = response.data.token; // 在这里将token保存至Vuex或localStorage等介质中,以供其他需要登录状态的路由使用 this.$router.push('/dashboard'); } } ``` 4. 在CLI脚手架(如Vue CLI)中创建一个名为“Login”的组件和对应的路由,然后重复上述步骤3中的内容实现登录逻辑。 希望这个代码示例对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值