-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
284 lines (252 loc) · 9.49 KB
/
index.js
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
const inquirer = require('inquirer');
const program = require('commander');
const DistGenerator = require('./generators/distGenerator');
const SrcGenerator = require('./generators/srcGenerator');
const PathUtility = require('./utils/pathUtility');
const LogUtility = require('./utils/logUtility');
const Markup = require('./utils/markupUtility');
const initModule = ({
tabSize,
srcFolder,
distFolder,
resxPrefix,
jsNamespace,
languages,
defaultLang,
currentLangNS,
}) => {
/* utilities initialization */
const pathUtility = new PathUtility();
pathUtility.init(srcFolder, distFolder, defaultLang, resxPrefix);
const markupUtility = new Markup();
markupUtility.init(tabSize);
const srcGenerator = new SrcGenerator(languages, defaultLang, srcFolder);
const distGenerator = new DistGenerator(jsNamespace, languages, defaultLang, resxPrefix, srcFolder, currentLangNS);
/* END */
const generateAll = () => {
srcGenerator.generateAll()
.then(() => distGenerator.generateAll())
.then(() => LogUtility.logSuccess())
.catch(LogUtility.logErr);
};
const yesNo = {
yes: 'Yes',
no: 'No',
};
const yesNoList = [
{ name: yesNo.yes },
{ name: yesNo.no },
];
const askForRecursiveActions = () => {
inquirer
.prompt({
type: 'list',
name: 'newKey',
message: 'Would you like to do something else?',
choices: yesNoList,
})
.then(a => {
if (a.newKey === yesNo.yes) {
beginInteraction();
}
});
};
const isValidJSName = name => name.trim().length && !(/^[^a-zA-Z_]+|[^a-zA-Z_0-9]+/).test(name);
const beginInteraction = () => {
const actions = {
create: 'create',
add: 'add',
regenerateAll: 'regenerateAll',
};
const actonsList = [
{ name: 'Do everything GOOD', value: actions.regenerateAll },
{ name: 'Create new resx', value: actions.create },
{ name: 'Add keys to existing one', value: actions.add },
];
const startupQuestions = [
{
type: 'list', name: 'action', message: 'Select operation?', choices: actonsList,
},
{
type: 'input',
name: 'resxName',
message: 'Give it a name: ',
when: a => a.action === actions.create,
validate: resxName => {
const exists = SrcGenerator.checkChunkExistance(resxName);
const isValidName = isValidJSName(resxName);
if (exists || !isValidName) {
return exists ? 'Resource file already exists' : 'Resource file name isn\'t valid';
}
return true;
},
},
];
const defaultSelectedLangs = [defaultLang, 'ru'];
const langList = languages.map(l => ({ name: l }));
const doLangKeyValQuestions = (lang, keyName) => ({
type: 'input',
name: 'val',
message: `'${lang}' value for '${keyName}'?`,
validate: a => (a ? true : 'Can\'t add empty value'),
});
const doAddScenarioQuestions = resxName => [
{
type: 'input',
name: 'keyName',
message: 'Key name? ',
validate: name => {
const fileContent = SrcGenerator.readDefaultLangChunk(resxName);
const isValidName = isValidJSName(name);
const exists = name in fileContent;
if (exists || !isValidName) {
return exists ? 'This key is already exists' : 'Key name isn\'t valid';
}
return true;
},
},
{
type: 'checkbox',
name: 'keyLangs',
message: 'Select languages:',
choices: langList,
default: defaultSelectedLangs,
validate: list => {
const isDefaultLangSelected = list.includes(defaultLang);
return isDefaultLangSelected ? true : `Default language (${defaultLang}) must be selected`;
},
},
];
const doAdd = (chunkName, keyName, langValPairs) => {
SrcGenerator.addKey(chunkName, keyName, langValPairs)
.then(() => srcGenerator.processChunk(chunkName))
.then(() => distGenerator.generateChunk(chunkName, 'updated'))
.then(() => {
inquirer
.prompt({
type: 'list',
name: 'newKey',
message: 'add one more key?',
choices: yesNoList,
})
.then(a => {
switch (a.newKey) {
case yesNo.yes:
return addScenario(chunkName);
default:
return askForRecursiveActions();
}
});
});
};
const addScenario = resxName => {
const askForValues = (keyName, keyLangs) => {
const langValPairs = [];
let iteration = 0;
const askForValue = () => {
const currLang = keyLangs[iteration];
if (langValPairs.length < keyLangs.length) {
const question = doLangKeyValQuestions(currLang, keyName);
inquirer
.prompt(question)
.then(a => {
langValPairs.push({ [currLang]: a.val });
iteration += 1;
askForValue();
});
}
else {
const langData = langValPairs.reduce((acc, val) => {
const key = Object.keys(val)[0];
acc[key] = val[key];
return acc;
}, {});
doAdd(resxName, keyName, langData);
}
};
askForValue();
};
const askForKey = () => {
inquirer
.prompt(doAddScenarioQuestions(resxName))
.then(a => {
askForValues(a.keyName, a.keyLangs);
});
};
askForKey();
};
const askForAddKeys = chunkName => {
inquirer
.prompt({
type: 'list',
name: 'addKey',
message: 'add keys??',
choices: yesNoList,
})
.then(a => {
switch (a.addKey) {
case yesNo.yes:
return addScenario(chunkName);
default:
return askForRecursiveActions();
}
});
};
const createScenario = resxName => {
srcGenerator.generateEmptyChunk(resxName)
.then(() => distGenerator.generateChunk(resxName, 'created'))
.then(() => askForAddKeys(resxName))
.catch(LogUtility.logErr);
};
const createSelectChunkQuestion = chunkNames => {
const chunkList = chunkNames.map(chunkName => ({ name: chunkName }));
return {
type: 'list',
name: 'addKey',
message: 'Select resource: ',
choices: chunkList,
};
};
const readChunksAndAsk = () => {
pathUtility.readChunksNames()
.then(chunkNames => {
if (!chunkNames.length) {
LogUtility.logErr(`NO RESOURCES FOUND IN ${srcFolder}`);
askForRecursiveActions();
return;
}
const question = createSelectChunkQuestion(chunkNames);
inquirer
.prompt(question)
.then(a => {
addScenario(a.addKey);
});
})
.catch(LogUtility.logErr);
};
inquirer
.prompt(startupQuestions)
.then(a => {
if (a.action === actions.add) {
readChunksAndAsk();
}
if (a.action === actions.create) {
createScenario(a.resxName);
}
if (a.action === actions.regenerateAll) {
generateAll();
}
});
};
program
.option('-d, --dogood', 'Doing everything GOOD')
.parse(process.argv);
if (program.dogood) {
generateAll();
}
else {
beginInteraction();
}
};
module.exports = initModule;
// todo: Move all questions to its own utility to make index.js clean and simple for understanding;