t-comm
Version:
专业、稳定、纯粹的工具库
52 lines (49 loc) • 1.6 kB
JavaScript
import { isDirectory } from '../fs/fs.mjs';
import { getFs } from '../nodejs/fs.mjs';
import { getPath } from '../nodejs/path.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/os.mjs';
/**
* 获取复制目标路径
* @param copySourceDir 复制源目录,注意,这里是绝对路径
* @param domainDir 域名目录,注意,这里是绝对路径
* @returns 要复制的列表,注意,这里是相对路径,如 ['os-x/en', 'os-x/zh-hant']
* @example
* ```ts
* // 假设 /tmp/dist/os-x 下有 en、zh-hans、zh-hant 三个语言目录
* getCopyTargetDirs(
* '/tmp/dist/os-x/zh-hans',
* '/tmp/dist',
* );
* // ['os-x/en', 'os-x/zh-hant']
*
* // 仅保留指定前缀
* getCopyTargetDirs(
* '/tmp/dist/os-x/zh-hans',
* '/tmp/dist',
* { copyTargetPrefix: 'os-x/zh-' },
* );
* // ['os-x/zh-hant']
* ```
*/
function getCopyTargetDirs(copySourceDir, domainDir, options) {
var baseDir = getPath().dirname(copySourceDir);
var list = (getFs().readdirSync(baseDir) || []).map(function (item) {
return getPath().resolve(baseDir, item);
}).filter(function (item) {
return isDirectory(item);
});
var copyTargetDirs = list.filter(function (item) {
return item !== copySourceDir;
});
var result = copyTargetDirs.map(function (item) {
return item.replace(domainDir, '').replace(/^\//, '');
});
if (options === null || options === void 0 ? void 0 : options.copyTargetPrefix) {
result = result.filter(function (item) {
return item.startsWith(options.copyTargetPrefix);
});
}
return result;
}
export { getCopyTargetDirs };