Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

写个node脚本自动全局复制文件 #57

Open
aermin opened this issue Nov 1, 2018 · 0 comments
Open

写个node脚本自动全局复制文件 #57

aermin opened this issue Nov 1, 2018 · 0 comments

Comments

@aermin
Copy link
Owner

aermin commented Nov 1, 2018

PM给的userstory:

Zendesk should support zh-HK. (notice that zh-HK is same as zh-TW)

也就是我们集成在Zendesk(一个产品)上的应用需要支持香港版的翻译(用户在浏览器选择香港语言,我们的应用的语言需要跟着变成香港的),
然而台湾和香港都是繁体,所以在每个翻译文件夹直接拷贝已有的台湾翻译内容穿件新的zh-HK文件即可,作为程序员,手动去一个一个创建文件是不可能的,一辈都不可能的😂所以写个脚本解放双手。

代码:

import fs from 'fs';

import path from 'path';

async function readFiles(
  dir: string,
  originFileName: string,
  targetFileName: string,
  alsoReplaceExistFile: boolean = false,
) {
  const files = fs.readdirSync(dir);
  await Promise.all(
    files.map(async (filePath) => {
      // if this is a folder, Recursive call it to handle the files from this folder;
      const isFolder = fs.lstatSync(path.resolve(dir, filePath)).isDirectory();
      if (isFolder) {
        await readFiles(
          path.resolve(dir, filePath),
          originFileName,
          targetFileName,
          alsoReplaceExistFile,
        );
        // 是否存在被拷贝的文件 && (是否要忽略已存在的文件 || 要生成的文件是否已存在(存在就不会覆盖)
      } else if (
        new RegExp(originFileName).test(filePath.toString()) &&
        (alsoReplaceExistFile ||
          !fs.existsSync(path.resolve(dir, targetFileName)))
      ) {
        fs.readFile(path.resolve(dir, filePath), 'utf8', (err, content) => {
          if (err) throw err;
          fs.writeFile(`${dir}/${targetFileName}`, content, (err) => {
            if (err) throw err;
          });
        });
      }
    }),
  );
}

export { readFiles };

// use with gulp
gulp.task('copy-en-AU-as-en-GB', async () => {
  await readFiles(path.resolve(__dirname, ''), 'en-AU.ts', 'en-GB.ts');
});

完成的源码repo地址

@aermin aermin changed the title 写个脚本自动全局复制文件 写个node脚本自动全局复制文件 Nov 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant