-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathgruntfile.js
467 lines (392 loc) · 11.5 KB
/
gruntfile.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
const fs = require('fs');
const path = require('path');
const lineByLine = require('n-readlines');
const sass = require('sass');
const pkg = require('./package.json');
const child_process = require('child_process');
const release_folder = 'formtools_releases';
const formtools_releases_folder = path.resolve(__dirname, '..', release_folder);
// i18n string keys that aren't used in the Core, but useful for multiple other modules, so placed here. These allow
// the `grunt i18n` task to pass
const globalI18nStrings = [
'word_help',
'word_wysiwyg',
'phrase_update_order',
'phrase_access_type',
'notify_module_already_installed'
];
let active_modules = [];
let active_themes = [];
let active_api = null;
if (fs.existsSync('./local.dev.js')) {
const local_dev = require('./local.dev.js');
active_modules = local_dev.modules;
active_themes = local_dev.themes;
active_api = local_dev.hasOwnProperty('api') ? local_dev.api : null;
}
const walk = (dir) => {
let results = [];
const list = fs.readdirSync(dir);
list.forEach((file) => {
file = dir + '/' + file;
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(walk(file));
} else {
results.push(file);
}
});
return results;
};
// files that get copied over when they change.
const syncSrc = [
'**',
'!**/*.scss', // sass files are built separately
'!react/**',
'!themes/default/sass/**',
];
// contains the task details for sass for
const sassFiles = [
{
src: 'src/themes/default/sass/index.scss',
dest: 'dist/themes/default/css/styles.css'
},
{
src: ['swatch_*.scss'],
expand: true,
cwd: 'src/themes/default/sass/',
dest: 'dist/themes/default/css/',
ext: '.css'
}
];
const sassLint = [
'src/themes/default/sass/\*.scss'
];
const sassFileWatchers = [
'**/*.scss'
];
active_themes.forEach((folder) => {
sassFiles.push({
src: `../${folder}/sass/index.scss`,
dest: `../${folder}/css/styles.css`
});
sassLint.push(`../${folder}/sass/\*.scss`);
sassFileWatchers.push(`../${folder}/sass/\*.scss`);
});
const config = {
sass: {
options: {
outputStyle: 'compressed',
implementation: sass,
sourceMap: false
},
dist: {
files: sassFiles
}
},
sasslint: {
target: sassLint
},
watch: {
css: {
files: sassFileWatchers,
tasks: ['sass', 'sasslint']
},
src: {
files: ['src/**'],
tasks: ['sync']
},
modules: {
files: active_modules.map((folder) => path.resolve(__dirname, '..', folder) + '/**'),
tasks: ['sync']
}
},
sync: {
main: {
files: [{
cwd: 'src',
src: syncSrc,
dest: 'dist',
}],
verbose: true
},
modules: {
files: active_modules.map((folder) => ({
cwd: path.resolve(__dirname, '..', folder),
src: ['**'],
dest: `dist/modules/${folder.replace(/^module-/, '')}`,
}))
},
themes: {
files: active_themes.map((folder) => ({
cwd: path.resolve(__dirname, '..', folder),
src: ['**'],
dest: `dist/themes/${folder.replace(/^theme-/, '')}`,
}))
},
api: {
// there's actually only a single folder, but convenient to return empty array
files: active_api !== null ? [{
cwd: path.resolve(__dirname, '..', active_api),
src: ['**'],
dest: `dist/global/api/`
}] : []
}
},
run: {
webpack_dev: {
cmd: 'yarn',
args: [
'dev'
]
},
webpack_prod: {
cmd: 'yarn',
args: [
'prod'
]
}
},
concurrent: {
watchers: {
options: {
logConcurrentOutput: true
},
tasks: [
'watch',
'run:webpack_dev'
]
}
}
};
module.exports = function (grunt) {
grunt.initConfig(config);
require('load-grunt-tasks')(grunt);
const removeKeyFromI18nFiles = (key) => {
pkg.locales.forEach((locale) => {
const file = `./src/global/lang/${locale}.php`;
const lines = new lineByLine(file);
let line;
let regex = /^\$LANG\["(.*)"\]\s*=/;
let updatedFileLines = [];
while (line = lines.next()) {
const lineString = line.toString('utf8');
let match = lineString.match(regex);
if (match === null || match[1] !== key) {
updatedFileLines.push(lineString);
}
}
fs.writeFileSync(file, updatedFileLines.join('\n'));
});
};
const addKeyToI18nFiles = (key) => {
const en = getLocaleFileStrings('en_us');
if (!en[key]) {
grunt.fail.fatal('First add the key to the en_us.php file then re-run this command.');
}
pkg.locales.filter((locale) => locale !== 'en_us').forEach((locale) => {
const file = `./src/global/lang/${locale}.php`;
const lines = new lineByLine(file);
let line;
let regex = new RegExp(`^\\$LANG\\["${key}"\\]`);
let found = false;
let updatedLines = [];
while (line = lines.next()) {
const lineString = line.toString('utf8');
if (regex.test(lineString)) {
found = true;
}
updatedLines.push(lineString);
}
if (!found) {
updatedLines.push(`$LANG["${key}"] = "${en[key]}";`);
fs.writeFileSync(file, updatedLines.join('\n'));
grunt.log.writeln(`${key} added to ${locale} file.`);
} else {
grunt.log.writeln(`${key} already found in ${locale} file.`);
}
});
};
const sortI18nFiles = () => {
pkg.locales.forEach((locale) => {
const data = getLocaleFileStrings(locale);
const file = `./src/global/lang/${locale}.php`;
const sortedKeys = Object.keys(data).sort();
let lines = [];
sortedKeys.forEach((key) => {
lines.push(`$LANG["${key}"] = "${data[key]}";`);
});
fs.writeFileSync(file, '<?php\n\n$LANG = array();\n\n' + lines.join('\n'));
});
};
const getLocaleFileStrings = (locale) => {
const en_us_lines = new lineByLine(`./src/global/lang/${locale}.php`);
let line;
let regex = /^\$LANG\["(.*)"\]\s*=\s*"(.*)";\s*$/;
const map = {};
while (line = en_us_lines.next()) {
const lineString = line.toString('utf8');
if (/^\$LANG\[/.test(lineString)) {
let match = lineString.match(regex);
if (match !== null) {
map[match[1]] = match[2];
}
}
}
return map;
};
const findStringsInEnFileMissingFromOtherLangFiles = (results, stringsByLocale) => {
const langs = Object.keys(stringsByLocale);
let count = 0;
results.lines.push('\nEnglish strings missing from other lang files:\n-------------------------------------------');
Object.keys(stringsByLocale['en_us']).forEach((key) => {
const missing = [];
langs.forEach((locale) => {
if (!stringsByLocale[locale][key]) {
missing.push(locale);
}
});
if (missing.length > 0) {
count++;
results.lines.push(`${key}\n -missing from: ${missing.join(', ')}`);
}
});
if (count > 0) {
results.error = true;
results.lines.push(`-- MISSING ${count}`);
} else {
results.lines.push('All good!\n');
}
return results;
};
const findStringsInOtherFilesNotInEnFile = (results, stringsByLocale) => {
const langs = Object.keys(stringsByLocale);
const en = stringsByLocale['en_us'];
langs.filter((lang) => lang !== 'en_us').forEach((lang) => {
const extra = [];
Object.keys(stringsByLocale[lang]).forEach((key) => {
if (!en[key]) {
extra.push(key);
}
});
if (extra.length) {
results.error = true;
results.lines.push(`${lang} file contains unused strings: \n-- ${extra.join('\n-- ')}`);
}
});
return results;
};
const parseCoreToFindUnusedStrings = (results, en) => {
let missingKeys = Object.keys(en);
const ignoreFolders = [
'src/global/lang/',
'src/global/vendor/',
'src/global/codemirror/',
'src/global/fancybox/',
'src/global/images/',
'dist/',
'node_modules/',
'src/modules/'
];
const files = walk('./src');
files.forEach((file) => {
for (let i=0; i<ignoreFolders.length; i++) {
const re = new RegExp(ignoreFolders[i]);
if (re.test(file)) {
return;
}
}
if (!(/[php|txt|tpl|html|js]$/.test(file))) {
return;
}
const lines = new lineByLine(file);
let line;
while (line = lines.next()) {
line.toString('ascii');
// loop through all keys that still haven't been found yet and remove any that are found on the row
let updatedKeys = [];
missingKeys.forEach((key) => {
const regex = new RegExp(key);
// very kludgy, but the only place Form Tools uses dynamic keys is for dates: ignore all those keys.
// We also ignore any i18n keys flagged for global use across FT modules
if (!(/^date_/.test(key)) && !regex.test(line) && globalI18nStrings.indexOf(key) === -1) {
updatedKeys.push(key);
}
});
missingKeys = updatedKeys;
}
});
if (missingKeys.length > 0) {
results.error = true;
results.lines.push(`\nUNUSED KEYS:\n${missingKeys.join('\n --')}`);
}
};
const publishReleaseBranch = () => {
const version = grunt.option('release') || null;
if (!version) {
console.log('Missing `release` param. Usage: `grunt publish --release=1.2.3`');
return;
}
// git co master
// git branch -D auto_3.0.14
// git push origin --delete auto_3.0.14
child_process.execSync('git checkout releases', { cwd: formtools_releases_folder });
child_process.execSync('git rm -rf .', { cwd: formtools_releases_folder });
child_process.execSync('git reset --hard origin/releases', { cwd: formtools_releases_folder });
child_process.execSync(`cp -R . ../../${release_folder}`, { cwd: path.resolve(__dirname, 'dist') });
child_process.execSync(`git checkout -b auto_${version}`, { cwd: formtools_releases_folder });
child_process.execSync(`git add -A`, { cwd: formtools_releases_folder });
child_process.execSync(`git commit -m "auto publishing ${version}"`, { cwd: formtools_releases_folder });
child_process.execSync(`git push origin head`, { cwd: formtools_releases_folder });
console.log(`${version} branch published on github. Now need to manually publish release.`);
};
grunt.registerTask('publishReleaseBranch', publishReleaseBranch);
grunt.registerTask('i18n', () => {
const stringsByLocale = {};
pkg.locales.forEach((locale) => {
stringsByLocale[locale] = getLocaleFileStrings(locale);
});
let results = {
error: false,
lines: []
};
findStringsInEnFileMissingFromOtherLangFiles(results, stringsByLocale);
findStringsInOtherFilesNotInEnFile(results, stringsByLocale);
parseCoreToFindUnusedStrings(results, stringsByLocale['en_us']);
// actually halt the grunt process if there are errors
const output = results.lines.join('\n');
if (results.error) {
grunt.fail.fatal(output);
} else {
grunt.log.write(output);
}
});
// helper methods to operate on all lang files at once
grunt.registerTask('removeI18nKey', () => {
const key = grunt.option('key') || null;
if (!key) {
grunt.fail.fatal("Please enter a key to remove. Format: `grunt removeI18nKey --key=word_goodbye");
}
removeKeyFromI18nFiles(grunt.option('key'));
});
// assumes the key already exists in en_us.php
grunt.registerTask('addI18nKey', () => {
const key = grunt.option('key') || null;
if (!key) {
grunt.fail.fatal("Please enter a key to add. Format: `grunt addI18nKey --key=word_hello");
}
addKeyToI18nFiles(key);
});
grunt.registerTask('sortI18nFiles', sortI18nFiles);
// grunt publish --release=3.0.17
grunt.registerTask('publish', [
//'prod',
'publishReleaseBranch'
]);
// for local dev work. All you need to do is run `grunt`: that creates a dist/ folder containing all the built code,
// sets up watchers to copy over changed files and generate the CSS from Sass. It also boots up webpack to handle
// any JS conversion. Be sure to load up the dist/ subfolder in your browser
grunt.registerTask('default', ['sync', 'sasslint', 'sass', 'concurrent:watchers']);
// builds everything into the dist folder
grunt.registerTask('prod', ['i18n', 'sync', 'sass', 'run:webpack_prod']);
};