forked from benkeen/generatedata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgruntfile.js
89 lines (77 loc) · 2.65 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
const fs = require('fs');
const path = require('path');
const result = require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
if (result.error) {
console.error("\nMissing .env file.... Please see the documentation about setting up your environment.\n", result);
return;
}
const BASE_PATH = path.join(__dirname, '../client');
const locales = process.env.GD_LOCALES.split(',');
module.exports = function (grunt) {
const dataTypesFolder = `${BASE_PATH}/src/plugins/dataTypes`;
const exportTypesFolder = `${BASE_PATH}/src/plugins/exportTypes`;
const countriesFolder = `${BASE_PATH}/src/plugins/countries`;
const createI18nFolder = () => {
const i18nFolder = path.join(__dirname, '/src/_i18n');
if (!fs.existsSync(i18nFolder)) {
fs.mkdirSync(i18nFolder);
}
};
const generateI18nBundles = () => {
locales.forEach((locale) => {
const coreLocaleStrings = JSON.parse(fs.readFileSync(`${BASE_PATH}/src/i18n/${locale}.json`, 'utf8'));
const dtImports = getPluginLocaleFiles(grunt, locale, dataTypesFolder);
const etImports = getPluginLocaleFiles(grunt, locale, exportTypesFolder);
const countryImports = getPluginLocaleFiles(grunt, locale, countriesFolder);
generateLocaleFileTemplate(locale, coreLocaleStrings, dtImports, etImports, countryImports)
});
};
const getPluginLocaleFiles = (grunt, locale, pluginTypeFolder) => {
const plugins = fs.readdirSync(pluginTypeFolder);
const imports = {};
plugins.forEach((folder) => {
const localeFile = `${pluginTypeFolder}/${folder}/i18n/${locale}.json`;
if (fs.existsSync(localeFile)) {
try {
imports[folder] = JSON.parse(fs.readFileSync(localeFile, 'utf8'));
} catch (e) {
grunt.fail.fatal('problem parsing i18n file: ' + localeFile);
}
}
});
return imports;
};
const generateLocaleFileTemplate = (locale, coreLocaleStrings, dtImports, etImports, countryImports) => {
const template = {
core: coreLocaleStrings,
dataTypes: dtImports,
exportTypes: etImports,
countries: countryImports
};
const filename = `./src/_i18n/${locale}.json`;
fs.writeFileSync(filename, JSON.stringify(template));
};
grunt.initConfig({
clean: {
dist: ['dist']
},
copy: {
i18n: {
files: [
{
expand: true,
cwd: 'src/_i18n',
src: ['*'],
dest: 'dist/cli/src/_i18n'
}
]
},
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask('default', ['clean', 'createEmptyDist', 'copyI18nFiles', 'i18n']);
grunt.registerTask('i18n', generateI18nBundles);
grunt.registerTask('createEmptyDist', createI18nFolder);
grunt.registerTask('copyI18nFiles', ['copy:i18n'])
};