-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathng-translation-gen
executable file
·257 lines (235 loc) · 6.57 KB
/
ng-translation-gen
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const ArgumentParser = require('argparse').ArgumentParser;
/**
* JSON configuration file read by the generator (if present).
*/
const CONFIG = "ng-translation-gen.json";
/**
* JSON Schema used to define the JSON configuration file.
*/
const SCHEMA = "ng-translation-gen-schema.json";
/**
* Read the package configuration.
*/
const pkg = parseJSON(path.join(__dirname, "package.json"));
/**
* Read the configuration schema.
*/
const schema = parseJSON(path.join(__dirname, SCHEMA));
/******************************************************************************/
/********************************* MAIN ***************************************/
/******************************************************************************/
var argParser = new ArgumentParser({
version: pkg.version,
addHelp: true,
description: "Translation message accessors generator for Angular 2+ projects. " +
"Either a file named " + CONFIG + " should exist in the current directory " +
"or arguments can be specified. Also, a configuration file can be generated " +
"with all default values if --gen-config is specified."
});
argParser.addArgument(
["-i", "--input"],
{
help: schema.properties.input.description,
defaultValue: schema.properties.input.default,
dest: "input",
}
);
argParser.addArgument(
["-o", "--output"],
{
help: schema.properties.output.description,
defaultValue: schema.properties.output.default,
dest: "output"
}
);
argParser.addArgument(
["-m", "--mapping"],
{
help: schema.properties.mapping.description,
defaultValue: schema.properties.mapping.default,
dest: "mapping"
}
);
argParser.addArgument(
["--gen-config"],
{
help: "Generates the configuration file " + CONFIG +
" in the current directory. No message accessor classes are generated." +
" If input and output are specified, their values are stored in the" +
" generated file as well.",
action: "storeTrue",
dest: "genConfig"
}
);
argParser.addArgument(
["-w", "--watch"],
{
help: "Watches for changes in the input directory, re-generating files" +
" when source translations change",
action: "storeTrue",
dest: "watch"
}
);
argParser.addArgument(
["--merge"],
{
help: "Merges the translation files for additional locales, from the" +
" source locale",
action: "storeTrue",
dest: "merge"
}
);
var args = argParser.parseArgs();
// The mapping is specified as file1=Class1:file2=Class2:...
if (args.mapping) {
var mapping = {};
for (let part of args.mapping.split(':')) {
let subparts = part.split('=');
let name = subparts[0].trim();
let value = (subparts[1] || '').trim();
mapping[name] = value;
}
args.mapping = mapping;
}
// Check the action
var configExists = fse.existsSync(CONFIG);
if (args.genConfig) {
if (configExists) {
// Ask for confirmation
askThenGenerateConfig();
} else {
// Write the configuration file
generateConfig();
}
} else {
const config = parseJSON(CONFIG);
if (args.merge) {
// Merge translations
merge(config);
} else {
// Main execution
if (configExists) {
// The configuration file exists, so read it
run(config, args.watch);
} else {
// No configuration file. Show the usage and exit.
argParser.parseArgs(["--help"]);
}
}
}
/******************************************************************************/
/*************************** Utility functions ********************************/
/******************************************************************************/
/**
* Generates a configuration file in the current directory
*/
function generateConfig() {
var options = {
"$schema": "./node_modules/ng-translation-gen/ng-translation-gen-schema.json"
};
if (args.input) {
options.input = args.input;
}
if (args.output) {
options.output = args.output;
}
if (args.mapping) {
options.mapping = args.mapping;
}
if (args.separator) {
options.separator = args.separator;
}
setDefaults(options, schema);
var json = JSON.stringify(options, null, 2);
fse.writeFileSync(CONFIG, json, { encoding: "utf8" });
console.info("Wrote configuration file " + CONFIG);
}
/**
* Asks if the configuration file should be overridden, then, if so, do it
*/
function askThenGenerateConfig() {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("The configuration file " + CONFIG + " already exists.\n" +
"Do you want to override it? [y/N]", (answer) => {
if (answer == 'y' || answer == 'Y') {
generateConfig();
}
rl.close();
});
}
/**
* Sets the default options for those properties not set.
*/
function setDefaults(options, schema) {
// apply the default values for those properties not defined
var properties = schema.properties;
for (var propName in properties) {
var propDef = properties[propName];
if (propDef.default && !options.hasOwnProperty(propName)) {
options[propName] = propDef.default;
}
}
if (!options.hasOwnProperty("mapping")) {
options.mapping = {};
}
}
function preprocess(options) {
// sets the default values for those properties not set in the options
setDefaults(options, schema);
if (!fse.existsSync(options.input)) {
console.log("Error: missing input folder: " + options.input);
process.exit(1);
}
}
/**
* Runs the ng-translation-gen generation
*/
function run(options, watch) {
preprocess(options);
var ngTranslationGen = require("./ng-translation-gen.js");
try {
ngTranslationGen(options);
} catch (e) {
console.error("Error generating translations");
console.dir(e);
console.error(e.stack);
}
if (watch) {
console.info("Watching " + options.input + " for changes");
let fsWait = false;
fs.watch(options.input, (_event, filename) => {
if (filename) {
if (fsWait) {
return;
}
fsWait = setTimeout(() => {
fsWait = false;
try {
ngTranslationGen(options);
} catch (e) {
console.error("Error generating translations");
console.dir(e);
console.error(e.stack);
}
}, 100);
}
});
}
}
function merge(options) {
preprocess(options);
var mergeTranslations = require("./merge-translations.js");
mergeTranslations(options);
}
function parseJSON(file) {
return JSON.parse(fse.readFileSync(file, "utf8"));
}