1 |
|
2 | import * as fs from 'fs';
|
3 | import * as p from 'path';
|
4 | import {sync as mkdirpSync} from 'mkdirp';
|
5 |
|
6 | function writeFile(filename, contents) {
|
7 | return new Promise((resolve, reject) => {
|
8 | fs.writeFile(filename, contents, (err) => {
|
9 | if (err) {
|
10 | reject(err);
|
11 | } else {
|
12 | resolve(p.resolve(filename));
|
13 | }
|
14 | });
|
15 | });
|
16 | }
|
17 |
|
18 | function mergeData(...sources) {
|
19 | return sources.reduce((data, source) => {
|
20 | Object.keys(source || {}).forEach((locale) => {
|
21 | data[locale] = Object.assign(data[locale] || {}, source[locale]);
|
22 | });
|
23 |
|
24 | return data;
|
25 | }, {});
|
26 | }
|
27 |
|
28 | function reviver (k, v) {
|
29 | let idx;
|
30 |
|
31 | if (k === 'locale')
|
32 | return v;
|
33 |
|
34 | else if (typeof v === 'string') {
|
35 | idx = prims.indexOf(v);
|
36 | valCount++;
|
37 |
|
38 | if (idx === -1)
|
39 | idx += prims.push(v);
|
40 |
|
41 | return '###prims['+ idx +']###';
|
42 | }
|
43 |
|
44 | else if (typeof v === 'object' && v !== null) {
|
45 | const str = JSON.stringify(v);
|
46 | objCount++;
|
47 |
|
48 | if (objStrs.hasOwnProperty(str))
|
49 | return objStrs[str];
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | let depth;
|
55 | let objDepths = [0];
|
56 |
|
57 | for (let key in v) {
|
58 | if (typeof v[key] === 'string' && (depth = v[key].match(/^###objs\[(\d+)/)))
|
59 | objDepths.push(+depth[1] + 1);
|
60 | }
|
61 |
|
62 | depth = Math.max.apply(Math, objDepths);
|
63 |
|
64 | if (!Array.isArray(objs[depth]))
|
65 | objs[depth] = [];
|
66 |
|
67 | idx = objs[depth].push(v) - 1;
|
68 | objStrs[str] = '###objs['+ depth +']['+ idx +']###';
|
69 |
|
70 | return objStrs[str];
|
71 | }
|
72 |
|
73 | return v;
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 | mkdirpSync('locale-data/');
|
79 | mkdirpSync('locale-data/json/');
|
80 | mkdirpSync('locale-data/jsonp/');
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | const jsonpExp = /"(?!default)([\w$][\w\d$]+)":/g;
|
87 |
|
88 | import reduceLocaleData from './utils/reduce';
|
89 |
|
90 | import extractCalendars from './utils/extract-calendars';
|
91 | import extractNumbersFields from './utils/extract-numbers';
|
92 | import {getAllLocales} from './utils/locales';
|
93 |
|
94 |
|
95 | const locales = getAllLocales();
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | let locData = mergeData(
|
101 | extractCalendars(locales),
|
102 | extractNumbersFields(locales)
|
103 | );
|
104 |
|
105 | let locStringData = {};
|
106 |
|
107 | Object.keys(locData).forEach((locale) => {
|
108 |
|
109 | if (locale.toLowerCase() === 'en-us-posix') {
|
110 | return;
|
111 | }
|
112 |
|
113 | const obj = reduceLocaleData(locale, locData[locale]);
|
114 | locStringData[locale] = JSON.stringify(obj, null, 4);
|
115 | const jsonpContent = `IntlPolyfill.__addLocaleData(${JSON.stringify(obj).replace(jsonpExp, '$1:')});`;
|
116 | writeFile('locale-data/json/' + locale + '.json', locStringData[locale]);
|
117 | writeFile('locale-data/jsonp/' + locale + '.js', jsonpContent);
|
118 | });
|
119 |
|
120 | console.log('Total number of locales is ' + Object.keys(locData).length);
|
121 |
|
122 |
|
123 |
|
124 | function replacer($0, type, loc) {
|
125 | return (type === 'prims' ? 'a' : 'b') + loc;
|
126 | }
|
127 |
|
128 | let
|
129 | objStrs = {},
|
130 | objs = [],
|
131 | prims = [],
|
132 |
|
133 | valCount = 0,
|
134 | objCount = 0,
|
135 |
|
136 | fileData = '';
|
137 |
|
138 | fileData += '(function(addLocaleData){\n';
|
139 |
|
140 | let locReducedData = {};
|
141 | Object.keys(locStringData).forEach((k) => {
|
142 | const c = locStringData[k];
|
143 | locReducedData[k] = JSON.parse(c, reviver);
|
144 | });
|
145 |
|
146 | fileData += `var a=${JSON.stringify(prims)},b=[];`;
|
147 | objs.forEach((val, idx) => {
|
148 | const ref = JSON.stringify(val).replace(/"###(objs|prims)(\[[^#]+)###"/g, replacer);
|
149 |
|
150 | fileData += `b[${idx}]=${ref};`;
|
151 | });
|
152 |
|
153 | for (let k in locReducedData) {
|
154 | fileData += `addLocaleData(${locReducedData[k].replace(/###(objs|prims)(\[[^#]+)###/, replacer)});
|
155 | `;
|
156 | }
|
157 |
|
158 | fileData += `})(IntlPolyfill.__addLocaleData);`;
|
159 |
|
160 |
|
161 | writeFile('locale-data/complete.js', fileData);
|
162 |
|
163 | console.log('Total number of reused strings is ' + prims.length + ' (reduced from ' + valCount + ')');
|
164 | console.log('Total number of reused objects is ' + Object.keys(objStrs).length + ' (reduced from ' + objCount + ')');
|
165 |
|
166 | process.on('unhandledRejection', (reason) => {throw reason;});
|
167 | console.log('Writing locale data files...');
|