Vue 自动编写注释

最近快要交付项目了,有个要求是30%的注释量,所以用node.js写了一个脚本自动生成。接下来看代码吧。

一、新建文件夹

我这里是新建scripts,跟src同级,在里面放了一个js文件

二、 add-comment.js内容

这里是匹配src下的views里的文件夹和文件里的.vue后缀,在el-button上新增一行注释;

const fs = require("fs");
const path = require("path");

const viewsDir = path.resolve(__dirname, "../src/views"); // views 文件夹路径
const commentText = "这是一个功能按钮;"; // 要添加的注释内容

// 遍历 views 文件夹下的所有文件和文件夹
fs.readdir(viewsDir, { withFileTypes: true }, (err, entries) => {
  if (err) {
    console.error(err);
    return;
  }

  entries.forEach((entry) => {
    const entryPath = path.join(viewsDir, entry.name);

    if (entry.isFile() && path.extname(entry.name) === ".vue") {
      // 如果是 .vue 文件
      const filePath = entryPath;

      // 读取文件内容
      fs.readFile(filePath, "utf8", (err, data) => {
        if (err) {
          console.error(err);
          return;
        }

        // 在 <el-button> 标签上方添加注释
        const regex = /(<el-button[\s\S]*?>)/g;
        const lines = data.split("\n");
        let updatedData = data;
        let offset = 0;
        let lastLineIndex = -1;

        for (let i = 0; i < lines.length; i++) {
          const line = lines[i];
          const match = regex.exec(line);
          if (match && !/<!--.*?-->/.test(line)) {
            lastLineIndex = i;
            break;
          }
        }

        if (lastLineIndex !== -1) {
          const insertPos = lines.slice(0, lastLineIndex + 1).join("\n").length;

          updatedData = data.slice(0, insertPos) + `\n<!-- ${commentText} -->` + data.slice(insertPos);

          // 将注释添加到文件中
          fs.writeFile(filePath, updatedData, (err) => {
            if (err) {
              console.error(err);
              return;
            }

            console.log(`已为 ${filePath} 添加注释`);
          });
        }
      });
    } else if (entry.isDirectory()) {
      // 如果是文件夹,则遍历文件夹内的 .vue 文件
      fs.readdir(entryPath, (err, files) => {
        if (err) {
          console.error(err);
          return;
        }

        files.forEach((file) => {
          if (path.extname(file) === ".vue") {
            const filePath = path.join(entryPath, file);

            // 读取文件内容
            fs.readFile(filePath, "utf8", (err, data) => {
              if (err) {
                console.error(err);
                return;
              }

              // 在 <el-button> 标签上方添加注释
              const regex = /(<el-button[\s\S]*?>)/g;
              const lines = data.split("\n");
              let updatedData = data;
              let offset = 0;
              let lastLineIndex = -1;

              for (let i = 0; i < lines.length; i++) {
                const line = lines[i];
                const match = regex.exec(line);
                if (match && !/<!--.*?-->/.test(line)) {
                  lastLineIndex = i;
                  break;
                }
              }

              if (lastLineIndex !== -1) {
                const insertPos = lines.slice(0, lastLineIndex + 1).join("\n").length;

                updatedData = data.slice(0, insertPos) + `\n<!-- ${commentText} -->` + data.slice(insertPos);

                // 将注释添加到文件中
                fs.writeFile(filePath, updatedData, (err) => {
                  if (err) {
                    console.error(err);
                    return;
                  }

                  console.log(`已为 ${filePath} 添加注释`);
                });
              }
            });
          }
        });
      });
    }
  });
});

三、配置命令 

在package.json里

最后,在控制台输入

npm run add-comment 

 就能得到注释啦。

你还可以任意发挥

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用 Vue3+Element-plus 编写图片上传的代码及注释: ```html <template> <div> <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false" :on-success="handleSuccess" :before-upload="beforeAvatarUpload" > <!-- 上传按钮 --> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </div> </template> <script> import { ref } from 'vue' export default { name: 'AvatarUpload', setup() { const imageUrl = ref('') // 上传前的校验 const beforeAvatarUpload = (file) => { const isJPG = file.type === 'image/jpeg' const isPNG = file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG && !isPNG) { this.$message.error('上传头像图片只能是 JPG/PNG 格式!') return false } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!') return false } return true } // 上传成功的回调 const handleSuccess = (res) => { if (res.code === '200') { imageUrl.value = res.data.url this.$message.success('上传成功!') } else { this.$message.error('上传失败,请重试!') } } return { imageUrl, beforeAvatarUpload, handleSuccess } } } </script> <style scoped> .avatar-uploader { display: flex; justify-content: center; align-items: center; width: 120px; height: 120px; border-radius: 50%; border: 1px dashed #ccc; background-color: #f9fafc; cursor: pointer; } .avatar { width: 120px; height: 120px; border-radius: 50%; object-fit: cover; } .avatar-uploader-icon { font-size: 28px; color: #8c939d; } </style> ``` 注释: - `el-upload` 是 Element-plus 中的上传组件,`action` 属性指定上传接口地址,`show-file-list` 属性指定是否显示上传文件列表,`on-success` 属性指定上传成功的回调函数,`before-upload` 属性指定上传前的校验函数; - `img` 标签用于展示上传成功后的图片,属性 `v-if="imageUrl"` 判断 `imageUrl` 是否存在,如果存在则显示图片,否则显示上传按钮; - `i` 标签用于显示上传按钮,属性 `v-else` 表示如果 `imageUrl` 不存在,则显示按钮; - `ref` 是 Vue3 中用于创建响应式数据的方法; - `beforeAvatarUpload` 方法用于上传前的校验,判断文件类型和大小是否符合要求; - `handleSuccess` 方法用于上传成功的回调,在回调中将上传成功的图片地址赋值给 `imageUrl`,并提示上传成功; - `setup` 函数是 Vue3 中的新特性,用于组件的选项设置,返回一个对象,包含组件中需要用到的数据和方法; - `return` 中的数据和方法,会被 Vue3 自动注入到组件的模板中使用; - `style` 标签的 `scoped` 属性表示样式仅作用于当前组件中的元素,不会影响到其他组件的样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值