写个node脚本自动全局复制文件
Opened this issue · 0 comments
aermin commented
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');
});