zoukankan      html  css  js  c++  java
  • nodejs生成模板文件

      之前为了学习,用vue-cli搭了一个前端架子,但是每次新建组件的时候都需要新建文件夹和.vue文件,感觉很繁琐。本着“懒惰是程序员的第一美德”的宗旨,用node写了一个简单的自动生成组件名字和内容的脚本。

    /**
     * node c componentName
     * @componentName {String} 组件名
     */
    
    const fs = require('fs');
    const path = require('path');
    
    // 获取到组件名
    const args = process.argv.splice(2);
    const componentName = args[0];
    
    console.log('prepare to creat a ' + componentName + ' component');
    
    let root = './src/components/';
    
    // 读取模板文件,并修改内容
    let template = fs.readFileSync(path.join(__dirname, './template.vue'), 'utf8');
    let content = template.replace(/componentName/g, componentName); // target file content
    
    // 目标文件夹和目标文件的路径
    let targetDirPath = path.join(__dirname, root, componentName);
    let targetFilePath = path.join(__dirname, root, componentName, componentName + '.vue');
    
    // mkdirSync
    if (!fs.existsSync(targetDirPath)) {
        fs.mkdirSync(targetDirPath);
        console.log('The ' + targetDirPath + ' folder has been created!');
    }
    
    // writeFile async
    if (!fs.existsSync(targetFilePath)) {
        fs.writeFile(targetFilePath, content, (err) => {
            if (err) throw err;
            console.log('The ' + targetFilePath + ' has been created!');
        });
    } else {
        console.error('error!
    ' + targetFilePath + ' has already been existed!');
    }

    template.vue的内容如下:

    <template>
      <div class="componentName">
        
      </div>
    </template>
    
    <script>
    export default {
      name: 'componentName',
      data () {
        return {
    
        }
      }
    }
    </script>
    
    <style lang="scss" rel="stylesheet/scss">
    
    </style>
  • 相关阅读:
    k8s的chart学习(上)
    k8s的应用打包工具Helm
    k8s通过configmap管理应用配置信息
    k8s通过secret管理敏感信息
    k8s的持久化存储PV&&PVC
    k8s的存储Volume
    使用python获取整月每一天的系统监控数据生成报表
    NGUI的UISprite动态染色的一种方法
    VS生成后事件对文件的copy以及更换扩展名
    【转】搞清楚脚本中这些函数的调用规律
  • 原文地址:https://www.cnblogs.com/xiaohaifengke/p/7693185.html
Copyright © 2011-2022 走看看