forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmerge-i18n-files.ts
More file actions
116 lines (102 loc) · 4.01 KB
/
Copy pathmerge-i18n-files.ts
File metadata and controls
116 lines (102 loc) · 4.01 KB
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
import {
existsSync,
lstatSync,
readdirSync,
readFileSync,
writeFileSync,
} from 'node:fs';
import {
Presets,
SingleBar,
} from 'cli-progress';
import { Command } from 'commander';
import {
parse,
stringify,
} from 'json5';
import { projectRoot } from '../webpack/helpers';
const program = new Command();
program.version('1.0.0', '-v, --version');
const LANGUAGE_FILES_LOCATION = 'src/assets/i18n';
parseCliInput();
/**
* Purpose: Allows customization of i18n labels from within themes
* e.g. Customize the label "menu.section.browse_global" to display "Browse DSpace" rather than "All of DSpace"
*
* This script uses the i18n files found in a source directory to override settings in files with the same
* name in a destination directory. Only the i18n labels to be overridden need be in the source files.
*
* Example execution (using custom theme):
* ```
* npm run merge-i18n -- -s src/themes/custom/assets/i18n
* ```
*
* Input parameters:
* * Output directory: The directory in which the original i18n files are stored
* - Defaults to src/assets/i18n (the default i18n file location)
* - This is where the final output files will be written
* * Source directory: The directory with override files
* - Required
* - Recommended to place override files in the theme directory under assets/i18n (but this is not required)
* - Files must have matching names in both source and destination directories, for example:
* en.json5 in the source directory will be merged with en.json5 in the destination directory
* fr.json5 in the source directory will be merged with fr.json5 in the destination directory
*/
function parseCliInput() {
program
.option('-d, --output-dir <output-dir>', 'output dir when running script on all language files', projectRoot(LANGUAGE_FILES_LOCATION))
.option('-s, --source-dir <source-dir>', 'source dir of translations to be merged')
.usage('(-s <source-dir> [-d <output-dir>])')
.parse(process.argv);
const source = program.opts().sourceDir;
const destination = program.opts().outputDir;
if (destination && source) {
if (!existsSync(destination) || !lstatSync(destination).isDirectory() ) {
console.error('Output does not exist or is not a directory.');
console.info(program.outputHelp());
process.exit(1);
}
if (!existsSync(source) || !lstatSync(source).isDirectory() ) {
console.error('Source does not exist or is not a directory.');
console.info(program.outputHelp());
process.exit(1);
}
readdirSync(projectRoot(source)).forEach(file => {
if (existsSync(destination + '/' + file) ) {
console.info('Merging: ' + destination + '/' + file + ' with ' + source + '/' + file);
mergeFileWithSource(source + '/' + file, destination + '/' + file);
}
});
} else {
console.error('Source or Output parameter is missing.');
console.info(program.outputHelp());
process.exit(1);
}
}
/**
* Reads source file and output file to merge the contents
* > Iterates over the source file keys
* > Updates values for each key and adds new keys as needed
* > Updates the output file with the new merged json
* @param pathToSourceFile Valid path to source file to merge from
* @param pathToOutputFile Valid path to merge and write output
*/
function mergeFileWithSource(pathToSourceFile, pathToOutputFile) {
const progressBar = new SingleBar({}, Presets.shades_classic);
progressBar.start(100, 0);
const sourceFile = readFileSync(pathToSourceFile, 'utf8');
progressBar.update(10);
const outputFile = readFileSync(pathToOutputFile, 'utf8');
progressBar.update(20);
const parsedSource = parse(sourceFile);
progressBar.update(30);
const parsedOutput = parse(outputFile);
progressBar.update(40);
for (const key of Object.keys(parsedSource)) {
parsedOutput[key] = parsedSource[key];
}
progressBar.update(80);
writeFileSync(pathToOutputFile,stringify(parsedOutput,{ space:'\n ', quote: '"' }), { encoding:'utf8' });
progressBar.update(100);
progressBar.stop();
}