首页 > 其他分享 >vue2 使用Markfile 命令创建新增组件模板

vue2 使用Markfile 命令创建新增组件模板

时间:2022-11-25 11:13:57浏览次数:35  
标签:const Markfile process componentname vue2 组件 path 模板

项目开发中封装了许多图表组件,每次新增组件或者组件迭代都要复制前后端的文件,为了避免重复操作,使用一键命令创建组件模板文件。

效果如下:

 

 

 

 

 

 

 

 

 

 在前端根目录创建Markfile 文件,里面是我们自定义的命令:

 

addcom:
    node build/addCom.js $(filter-out $@,$(MAKECMDGOALS))

editcom:
     node build/editCom.js $(filter-out $@,$(MAKECMDGOALS))

 具体新增组件实现逻辑如下:

"use strict";

process.on("exit", () => {
  console.log();
});

if (!process.argv[2]) {
  console.error(
    "请重新输入组件名"
  );
  process.exit(1);
}

const path = require("path");
const fs = require("fs");
const fileSave = require("file-save");
const uppercamelcase = require("uppercamelcase"); // 驼峰命名
const componentname = process.argv[2]; // 组件名
const chineseName = process.argv[3] || componentname; // 中文名
let componentType = process.argv[4] ||  'text'; // 类型
const ComponentName = uppercamelcase(componentname);
const comPath = '根据个人需求拼接'; // 组件vue路径
const jsonPath = "根据个人需求拼接"; // 组件json路径
const PackagePath = path.resolve(
  __dirname,
  "vue公共目录",
  comPath
);
// son路径
const comJsonPath = path.resolve(
  __dirname,
  "json公共目录",
  jsonPath
);
// 定义vue创建的文件模板
const vueFiles = [
  {
    filename: `${componentname}.vue`,
    content: `<template>
  <div >创建模板</div>
</template>

<script>
export default {
  name: '${ComponentName}',
  props: ["pageConfig"],
  data(){
    return {

    }
  },
  mounted(){

  },
  created(){

  },
  methods:{

  },
  watch:{

  }
};
</script>`
  }
];

  fileSave(path.join(PackagePath, vueFiles[0].filename))
    .write(vueFiles[0].content, "utf8")
    .end("\n");
// 定义后台json创建的文件模板
const jsonFile = [
  {
    filename: `${componentname}.json`,
    content: `{
    "name": "${chineseName}",
    "id": "id",
    "type": "${componentType}",
    "com_name": "${componentname}",
     ..................................
     "config": {}},
    "api_data": {
        "source": []
    },
    "events": {
        "allEvents": []
    }
}`
  }
];
// 创建后台文件
fileSave(path.join(comJsonPath, jsonFile[0].filename))
  .write(jsonFile[0].content, "utf8")
  .end("\n");
console.log("新增组件成功");

  具体操作还是看项目需求,如果需要创建的文件特别多,可以把要生成的文件统一放在一个变量进行处理。

     在Markfile 文件目录进入终端执行命令  Ubuntu 和 Mac 支持 make 命令   不支持可以用 node

 

标签:const,Markfile,process,componentname,vue2,组件,path,模板
From: https://www.cnblogs.com/hardmeng/p/16924496.html

相关文章

  • 数据结构初阶--单链表(讲解+类模板实现)
    单链表概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。值得注意的是:1.链表的在逻辑是连续的,物理上不一......
  • Vue2.0+3.0笔记
    生命周期 非单文件组件:全局事件时  脚手架文件结构  ├──node_modules├──public│├──favicon.ico:页签图标│└──index.htm......
  • wxlua scintilla 模板
    --wxluascintilla myph0="./?.dll;./?51.dll;E:/ProgramFiles/Lua/5.1/?.dll;E:/ProgramFiles/Lua/5.1/?51.dl\l;E:/ProgramFiles/Lua/5.1/clibs/?.dll;E:/Progra......
  • vue2 vue-cli12 vue2项目创建 创建自定义配置 严格模式 超详细的vue2项目创建
    设置全局的cli输入命令:vuei-g@vue/cli快速生成工程化项目:vuecreate项目名create后,选择最后一个自定义选择 第一个必选第二个是TS第三个是渐进式框架......
  • Android Studio 自定义模板内容(注释、代码)
        ......
  • 用例技术(二)——几种用例模板(格式)
    1.完整正式用例格式<名字:应该是一个动词短语来表示目标>使用的环境:<目标较长的描述,如果需要,还包括触发条件>范围:<设计范围,设计时将系统作为黑盒考虑>层次:<概要,用户目......
  • c#按键的拖拽模板代码
    publicvoidBtnMove(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){if(!mAllowDragKey)......
  • c#自定义控件模板代码
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSyste......
  • 算法基础:单链表图解及模板总结
    ⭐写在前面的话:本系列文章旨在复习算法刷题中常用的基础算法与数据结构,配以详细的图例解释,总结相应的代码模板,同时结合例题以达到最佳的学习效果。本专栏面向算法零基础但有......
  • c++ 代码模板
    #include<bits/stdc++.h>usingnamespacestd;typedefunsignedlonglongull;typedeflongdoubledoubleL;typedeflonglongll;#define_SILENCE_CXX20_CISO646......