nodejs 如何批量替换接口文件里面的信息?

接口文件是自动生成的,生成的method为DELETE的data需要修改为[data]
如何能批量的替换掉,一个文件里会有多个export async function

export async function deleteE (
  data: {
    app: any;
  },
  options?: Record<string, any>,
): Promise<CommonResult<any>> {
  return axios({
    method: 'DELETE',
    url: `/api/app/`,
    data: {
      ...data,
    },
    ...(options || {}),
  })
}

替换成

export async function deleteE (
  data: {
    app: any;
  },
  options?: Record<string, any>,
): Promise<CommonResult<any>> {
  return axios({
    method: 'DELETE',
    url: `/api/app/`,
    data: [data],
    ...(options || {}),
  })
}
阅读 1.4k
1 个回答
const fs = require('fs');

let content = fs.readFileSync('yourfile.js', 'utf8');

// 用正则表达式替换内容
let newContent = content.replace(/data: \{\n\s+\.\.\.data,\n\s+\}/g, 'data: [data]');


fs.writeFileSync('yourfile.js', newContent);
推荐问题
logo
Microsoft
子站问答
访问
宣传栏