forked from locize/locize-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.js
162 lines (142 loc) · 4.09 KB
/
migrate.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
const fs = require('fs');
const path = require('path');
const flatten = require('flat');
const async = require('async');
const colors = require('colors');
const request = require('./request');
const getDirectories = (srcpath) => {
return fs.readdirSync(srcpath).filter(function(file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
});
};
const getFiles = (srcpath) => {
return fs.readdirSync(srcpath).filter(function(file) {
return !fs.statSync(path.join(srcpath, file)).isDirectory();
});
};
const load = (namespaces, cb) => {
async.each(namespaces, (ns, done) => {
fs.readFile(ns.path, 'utf8', (err, data) => {
if (err) return done(err);
try {
ns.value = flatten(JSON.parse(data));
} catch (err) {
console.error(colors.red(err.stack));
ns.value = {};
}
done();
});
}, (err) => cb(err, namespaces));
};
const parseLanguage = (p, cb) => {
const dirs = getDirectories(p);
const namespaces = [];
dirs.forEach((lng) => {
const files = getFiles(path.join(p, lng));
files.forEach((file) => {
if (path.extname(file) !== '.json') return;
namespaces.push({
language: lng,
namespace: path.basename(file, '.json'),
path: path.join(p, lng, file)
});
});
});
load(namespaces, cb);
};
const transfer = (opt, ns, cb) => {
var url = opt.addPath
.replace('{{projectId}}', opt.projectId)
.replace('{{ver}}', opt.version)
.replace('{{version}}', opt.version)
.replace('{{language}}', ns.language)
.replace('{{lng}}', ns.language)
.replace('{{ns}}', ns.namespace)
.replace('{{namespace}}', ns.namespace);
console.log(colors.yellow(`transfering ${opt.version}/${ns.language}/${ns.namespace}...`));
request(url + `?replace=${!!opt.replace}`, {
method: 'post',
body: ns.value,
headers: {
'Authorization': opt.apiKey
}
}, (err, res, obj) => {
if (err || (obj && (obj.errorMessage || obj.message))) {
console.log(colors.red(`transfer failed for ${opt.version}/${ns.language}/${ns.namespace}...`));
if (err) return cb(err);
if (obj && (obj.errorMessage || obj.message)) return cb(new Error((obj.errorMessage || obj.message)));
}
if (res.status >= 300 && res.status !== 412) return cb(new Error(res.statusText + ' (' + res.status + ')'));
console.log(colors.green(`transfered ${opt.version}/${ns.language}/${ns.namespace}...`));
cb(null);
});
};
const upload = (opt, nss, cb) => {
async.eachLimit(
nss,
require('os').cpus().length,
(ns, done) => transfer(opt, ns, done),
cb
);
};
const migrate = (opt, cb) => {
if (opt.format !== 'json') {
var err = new Error(`Format ${opt.format} is not accepted!`);
if (!cb) throw err;
if (cb) cb(err);
return;
}
if (opt.language) {
const files = getFiles(opt.path);
const namespaces = files.map((file) => {
return {
language: opt.language,
namespace: path.basename(file, '.json'),
path: path.join(opt.path, file)
};
});
load(namespaces, (err, nss) => {
if (err) {
if (!cb) { console.error(colors.red(err.stack)); process.exit(1); }
if (cb) cb(err);
return;
}
upload(opt, nss, (err) => {
if (err) {
if (!cb) {
console.error(colors.red(err.stack));
process.exit(1);
}
if (cb) cb(err);
return;
}
if (!cb) console.log(colors.green('FINISHED'));
if (cb) cb(null);
});
});
return;
}
if (opt.parseLanguage) {
parseLanguage(opt.path, (err, nss) => {
if (err) {
if (!cb) console.error(colors.red(err.stack)); process.exit(1);
if (cb) cb(err);
return;
}
upload(opt, nss, (err) => {
if (err) {
if (!cb) {
console.error(colors.red(err.stack));
process.exit(1);
}
if (cb) cb(err);
return;
}
if (!cb) console.log(colors.green('FINISHED'));
if (cb) cb(null);
});
});
return;
}
};
module.exports = migrate;