forked from locize/locize-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
missing.js
169 lines (141 loc) · 5.31 KB
/
missing.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
const colors = require('colors');
const async = require('async');
const path = require('path');
const request = require('./request');
const formats = require('./formats');
const reversedFileExtensionsMap = formats.reversedFileExtensionsMap;
const getRemoteLanguages = require('./getRemoteLanguages');
const parseLocalReference = require('./parseLocalReference');
const parseLocalLanguages = require('./parseLocalLanguages');
const getRemoteNamespace = require('./getRemoteNamespace');
const compareNamespace = (local, remote) => {
const diff = {
toAdd: []
};
local = local || {};
remote = remote || {};
Object.keys(local).forEach((k) => {
if (remote[k] === '' && local[k] === '') return;
if (!remote[k]) {
diff.toAdd.push(k);
}
});
return diff;
};
const compareNamespaces = (opt, localNamespaces, cb) => {
async.map(localNamespaces, (ns, clb) => {
getRemoteNamespace(opt, ns.language, ns.namespace, (err, remoteNamespace) => {
if (err) return clb(err);
const diff = compareNamespace(ns.content, remoteNamespace);
ns.diff = diff;
ns.remoteContent = remoteNamespace;
clb(null, ns);
});
}, cb);
};
const saveMissing = (opt, lng, ns, cb) => {
var data = {};
ns.diff.toAdd.forEach((k) => data[k] = ns.content[k]);
if (Object.keys(data).length === 0) return cb(null);
if (opt.dry) return cb(null);
var payloadKeysLimit = 1000;
function send(d, clb, isRetrying) {
request(opt.apiPath + '/missing/' + opt.projectId + '/' + opt.version + '/' + lng + '/' + ns.namespace, {
method: 'post',
body: d,
headers: {
'Authorization': opt.apiKey
}
}, (err, res, obj) => {
if (err) return clb(err);
if (res.status === 504 && !isRetrying) {
return setTimeout(() => send(d, clb, true), 3000);
}
if (res.status >= 300 && res.status !== 412) {
if (obj && (obj.errorMessage || obj.message)) {
return clb(new Error((obj.errorMessage || obj.message)));
}
return clb(new Error(res.statusText + ' (' + res.status + ')'));
}
setTimeout(() => clb(null), 1000);
});
}
if (Object.keys(data).length > payloadKeysLimit) {
var tasks = [];
var keysInObj = Object.keys(data);
while (keysInObj.length > payloadKeysLimit) {
(function() {
var pagedData = {};
keysInObj.splice(0, payloadKeysLimit).forEach((k) => pagedData[k] = data[k]);
tasks.push((c) => send(pagedData, c));
})();
}
if (keysInObj.length === 0) return cb(null);
var finalPagedData = {};
keysInObj.splice(0, keysInObj.length).forEach((k) => finalPagedData[k] = data[k]);
tasks.push((c) => send(finalPagedData, c));
async.series(tasks, cb);
return;
}
send(data, cb);
};
const handleError = (err, cb) => {
if (!cb && err) {
console.error(colors.red(err.stack));
process.exit(1);
}
if (cb) cb(err);
};
const handleMissing = (opt, localNamespaces, cb) => {
if (!localNamespaces || localNamespaces.length === 0) {
return handleError(new Error('No local namespaces found!'));
}
compareNamespaces(opt, localNamespaces, (err, compared) => {
if (err) return handleError(err);
async.eachLimit(compared, Math.round(require('os').cpus().length / 2), (ns, clb) => {
if (!cb) {
if (ns.diff.toAdd.length > 0) {
console.log(colors.green(`adding ${ns.diff.toAdd.length} keys in ${ns.language}/${ns.namespace}...`));
if (opt.dry) console.log(colors.green(`would add ${ns.diff.toAdd.join(', ')} in ${ns.language}/${ns.namespace}...`));
}
}
saveMissing(opt, ns.language, ns, clb);
}, (err) => {
if (err) return handleError(err);
if (!cb) console.log(colors.green('FINISHED'));
if (cb) cb(null);
});
});
};
const missing = (opt, cb) => {
if (!reversedFileExtensionsMap[opt.format]) {
return handleError(new Error(`${opt.format} is not a valid format!`));
}
if (opt.namespace && opt.namespace.indexOf(',') > 0) {
opt.namespaces = opt.namespace.split(',');
delete opt.namespace;
}
opt.pathMaskInterpolationPrefix = opt.pathMaskInterpolationPrefix || '{{';
opt.pathMaskInterpolationSuffix = opt.pathMaskInterpolationSuffix || '}}';
opt.pathMask = opt.pathMask || `${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}${path.sep}${opt.pathMaskInterpolationPrefix}namespace${opt.pathMaskInterpolationSuffix}`;
opt.languageFolderPrefix = opt.languageFolderPrefix || '';
opt.pathMask = opt.pathMask.replace(`${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}`, `${opt.languageFolderPrefix}${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}`);
getRemoteLanguages(opt, (err, remoteLanguages) => {
if (err) return handleError(err);
if (opt.referenceLanguageOnly && opt.language && opt.referenceLanguage !== opt.language) {
opt.referenceLanguage = opt.language;
}
if (opt.referenceLanguageOnly) {
parseLocalReference(opt, (err, localNamespaces) => {
if (err) return handleError(err);
handleMissing(opt, localNamespaces, cb);
});
return;
}
parseLocalLanguages(opt, remoteLanguages, (err, localNamespaces) => {
if (err) return handleError(err);
handleMissing(opt, localNamespaces, cb);
});
});
};
module.exports = missing;