forked from stellar/stellar-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyIgnoredFiles.mjs
123 lines (107 loc) · 3.85 KB
/
copyIgnoredFiles.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import yaml from 'js-yaml';
// Define the list of language codes
const languages = ['es'];
/**
* Reads and parses the crowdin.yml file.
* @param {string} filePath - The path to the crowdin.yml file.
* @returns {Object} - The parsed configuration object.
*/
function readCrowdinConfig(filePath) {
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
return yaml.load(fileContent);
} catch (err) {
logMessage(`Error reading or parsing the file ${filePath}: ${err.message}`);
process.exit(1);
}
}
/**
* Extracts the sources array from the configuration object.
* @param {Object} config - The parsed configuration object.
* @returns {string[]} - The array of source paths.
*/
function extractSources(config) {
return config.files.map(file => file.source.replace(/\/\*\*\/\*$/, '').replace(/^\//, ''));
}
/**
* Logs a message to the console with a timestamp.
* @param {string} message - The message to log.
*/
function logMessage(message) {
console.log(`[${new Date().toISOString()}] ${message}`);
}
/**
* Copies a file or directory from source to destination.
* @param {string} src - The source path.
* @param {string} dest - The destination path.
*/
function copyFileOrDirectory(src, dest) {
const destDir = path.dirname(dest);
fs.mkdirSync(destDir, { recursive: true });
const stat = fs.statSync(src);
if (stat.isDirectory()) {
fs.cpSync(src, dest, { recursive: true });
logMessage(`Successfully copied directory ${src} to ${dest}`);
} else {
fs.copyFileSync(src, dest);
logMessage(`Successfully copied file ${src} to ${dest}`);
}
}
/**
* Replaces placeholders in the translation path with actual values.
* @param {string} translationPath - The translation path template.
* @param {string} languageCode - The language code to replace placeholders.
* @param {string} srcPath - The source file path.
* @param {string[]} sources - The array of source paths.
* @returns {string} - The translation path with placeholders replaced.
*/
function getTranslationPath(translationPath, languageCode, srcPath, sources) {
let relativePath = srcPath;
for (const source of sources) {
if (srcPath.startsWith(source)) {
relativePath = srcPath.replace(`${source}/`, '');
break;
}
}
const destPath = translationPath
.replace('%two_letters_code%', languageCode)
.replace('%original_file_name%', path.basename(srcPath))
.replace('**', path.dirname(relativePath).replace(/\\/g, '/'));
return path.join(process.cwd(), destPath);
}
/**
* Copies ignored files for each language based on the configuration.
*/
function copyIgnoredFilesForLanguages(config) {
const sources = extractSources(config);
config.files.forEach(({ source, translation, ignore = [] }) => {
ignore.forEach(ignorePattern => {
const srcPattern = ignorePattern.replace(/^\//, ''); // Strip the leading slash for the source path
glob(srcPattern, (err, files) => {
if (err) {
logMessage(`Error processing pattern ${srcPattern}: ${err.message}`);
return;
}
files.forEach(srcPath => {
languages.forEach(languageCode => {
const destPath = getTranslationPath(translation, languageCode, srcPath, sources);
copyFileOrDirectory(srcPath, destPath);
});
});
});
});
});
}
/**
* Main function to execute the workflow.
*/
function main() {
const config = readCrowdinConfig('crowdin.yml');
copyIgnoredFilesForLanguages(config);
logMessage('Ignored files copied for all specified languages.');
}
// Execute the main function
main();