如何使用Node编写开发小工具

news/2024/7/8 1:14:43

在做有些项目的时候有时候会遇到要重复创建内容大概相同的文件,但是命名和文件夹存放不一样的业务。比如说组件参考文档框架的Storybook的编写,就是需要大量的拷贝相同的文件代码,其目录大概如下

|--|
| src
|  |--Buttom
|  |    |--Button.component.tsx
|  |    |--Button.stories.ts
|  |--Input
|  |    |--Input.component.tsx
|  |    |--Input.stories.ts
|  |--.....很多文件夹
|  |......很多文件

上面文件目录里面的内容都相差不大,就是文件名和组件名不一样, 那么我们可以使用Node.js创建文件夹文件减少复制粘贴的繁琐工作,其代码如下:

/* 
 * initFile.js
 * 使用js生产新的Story文件
 * 执行 npm run init [your file name] [project]
 * npm run init button mui
 */
import fs from "fs";
const args = process.argv.slice(2);

const getProjectType = () => {
  let type = false;
  if (args?.length) type = args.some((item) => item?.toLowerCase() === "mui");
  return type;
};

const firstCharsToUpperCase = () => {
  if (args?.length) {
    const name = args?.[0] ?? "";
    return `${name.toLowerCase().slice(0, 1).toUpperCase()}${name.slice(1)}`;
  } else {
    return "Test";
  }
};

const isMui = getProjectType();
const fileName = firstCharsToUpperCase();

const storyContent = `
  import type { Meta, StoryObj } from '@storybook/react'

  import { ${fileName} } from './${fileName}.component'

  const meta = {
    title: '${isMui ? "Mui" : "Antd"}/${fileName}',
    component: ${fileName},
    parameters: {
      layout: 'centered',
    },
    tags: ['autodocs'],
    argTypes: {},
  } satisfies Meta<typeof ${fileName}>

  export default meta
  type Story = StoryObj<typeof meta>

  export const Primary: Story = {
    args: {},
  }
`;

const componentContent = `
  import { ${fileName} as Mui${fileName} } from '@Xcloud/${
  isMui ? "mui" : "antd"
}';
  import type { ${fileName}Props as Mui${fileName}Props } from '@ocloud/${
  isMui ? "mui" : "antd"
}';
  import type { ReactNode } from "react";

  export interface ${fileName}Props extends Mui${fileName}Props {
    children?: ReactNode;
  }

  export const ${fileName} = ({ children, ...rest }: ${fileName}Props) => (
    <Mui${fileName} {...rest}>{children}</Mui${fileName}>
  );
`;

((storyContent, componentContent) => {
  const storyName = `${firstCharsToUpperCase()}.stories.ts`;
  const componentName = `${firstCharsToUpperCase()}.component.tsx`;
  const filePath = isMui
    ? `./src/stories/Mui/${fileName}`
    : `./src/stories/Antd/${fileName}`;
  fs.mkdir(filePath, { recursive: true }, (err) => {
    if (err) return callback(err);
    fs.writeFile(`${filePath}/${storyName}`, storyContent, (error) => {
      if (error) return console.error(error);
      console.log("Create story success!");
    });
    fs.writeFile(`${filePath}/${componentName}`, componentContent, (error) => {
      if (error) return console.error(error);
      console.log("Create component success!");
    });
  });
})(storyContent, componentContent);

编写好上面的Node脚本之后,就可以执行了,在项目的pageage.json中的scripts属性中增加如下配置

  "scripts": {
    "init": "node initFile",
    "dev": "storybook dev -p 6006",
  },

然后在编辑器控制台或者CMD窗口中执行命令npm run init button mui,就会在src/Mui文件下生成文件夹Button和两个文件Button.stories.tsButton.component.tsx;如果是使用拷贝文件夹然后再修改,一个文件夹最少要花费三五分钟,现在大概不要5秒;这样就可以在这两个文件在愉快的写文档了。


http://lihuaxi.xjx100.cn/news/1764829.html

相关文章

聊聊芯片制造中的金属杂质

在半导体制造过程中&#xff0c;杂质控制至关重要。杂质可以影响半导体的电导率&#xff0c;导致性能降低或者失效。在这些杂质中&#xff0c;金属杂质是最主要的来源之一&#xff0c;这些金属杂质可以通过扩散或者电迁移的方式在芯片中迅速扩散&#xff0c;因此控制金属杂质的…

lightdb 普通用户拥有XMLTYPE类型的访问权限

文章目录 概述示例总结 概述 在信创移植的SQL语句中&#xff0c;有来源于Oracle数据库的SQL语句。 在Oracle中存在getClobVal函数&#xff0c;这个函数是Oracle中sys.XMLType的成员方法。 因此在LightDB23.3版本中实现了TYPE支持定义成员方法并且在新定义的XMLType类型中实现…

易点易动设备管理系统:提升设备巡检和维修效率,延长设备使用寿命的利器

在现代企业中&#xff0c;设备管理是一个至关重要的环节。然而&#xff0c;许多企业在设备巡检和维修方面面临挑战&#xff0c;如效率低下、信息不透明等问题。为了帮助企业提升设备巡检和维修效率&#xff0c;并延长设备的使用寿命&#xff0c;易点易动设备管理系统应运而生。…

Python+Selenium+Unittest 之selenium12--WebDriver操作方法2-鼠标操作1(ActionChains类简介)

在我们平时的使用过程中&#xff0c;会使用鼠标去进行很多操作&#xff0c;比如鼠标左键点击、双击、鼠标右键点击&#xff0c;鼠标指针悬浮、拖拽等操作。在selenium中&#xff0c;我们也可以去实现常用的这些鼠标操作&#xff0c;这时候就需要用到selenium中的ActionChains类…

windows cmake x86 x64 下载与安装

cmake 下载路径&#xff1a;cmake 下载选择&#xff1a; 界面下拉选取适合自己的版本 这里是windows x86 x64 &#xff08;x86是32位系统&#xff1b;x64是64位系统&#xff09; 安装&#xff1a; 点击安装。 此处选择添加环境变量 命令提示符 验证查看 cmake 桌面可以…

平面和射线交点

设平面 A x B y C z D 0 ( A 2 B 2 C 2 ≠ 0 ) AxByCz D0\left(A^2B^2C^2\neq 0\right) AxByCzD0(A2B2C20), 点 P ( P x , P y , P z ) P(P_x, P_y, P_z) P(Px​,Py​,Pz​) (1)求点 P P P到平面距离 (2)过点 P P P作直线&#xff0c;方向为 ( D x , D y , D z ) \lef…

cad打开后好多圈圈

原因&#xff1a;标注比例出问题 解决&#xff1a;d——修改标注样式——把自己的标注样式【置为当前】即可解决

【洛谷 P1591】阶乘数码 题解(模拟+高精度)

阶乘数码 题目描述 求 n ! n! n! 中某个数码出现的次数。 输入格式 第一行为 t ( t ≤ 10 ) t(t \leq 10) t(t≤10)&#xff0c;表示数据组数。接下来 t t t 行&#xff0c;每行一个正整数 n ( n ≤ 1000 ) n(n \leq 1000) n(n≤1000) 和数码 a a a。 输出格式 对于…