-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
169 lines (149 loc) · 5.02 KB
/
index.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 onesky = require("@brainly/onesky-utils");
const fs = require("fs-extra");
const path = require("path");
const ora = require("ora");
const chalk = require("chalk");
function validationError(message) {
console.error(chalk.bold.red(`✖ ${message}`));
process.exit(1);
}
function oneSkyErrorMessageHandler(spinner, code, message) {
spinner.fail(code ? chalk.bold.red(`${code}: `) : "" + chalk.red(message));
process.exit(1);
}
module.exports = function(command, files = [], _options) {
if (!_options.apiKey) {
validationError(
"--api-key is required. You can obtain it on Site Settings page on OneSky"
);
}
if (!_options.secret) {
validationError(
"--secret is required. You can obtain it on Site Settings page on OneSky"
);
}
if (!_options.projectId) {
validationError(
"--project-id is required. You will found it in the URL `admin/project/dashboard/project/:project-id` on OneSky"
);
}
({
upload() {
if (!files.length) {
validationError("Path to uploading file is required");
}
files.forEach(file => {
const options = Object.assign(
{
language: "en",
fileName: `${path.parse(file).base}`,
format: "HIERARCHICAL_JSON",
content: fs.readFileSync(file).toString(),
keepStrings: false
},
_options
);
const spinner = ora(`Uploading \`${options.fileName}\``).start();
onesky
.postFile(options)
.then(data => {
const { meta, data: { name, language: { code } } } = JSON.parse(
data
);
if (meta.status === 201) {
spinner.succeed(
`File \`${name}\` for \`${code}\` locale was uploaded successfully`
);
}
})
.catch(({ message, code }) => {
oneSkyErrorMessageHandler(spinner, code, message);
});
});
},
download() {
if (!files.length) {
validationError("Path to output directory is required");
}
if (files.length > 1) {
validationError("Too many directories. Expected 1");
}
if (!_options.fileName) {
validationError("--file-name is required for `download`");
}
const options = Object.assign({}, _options);
const file = files[0];
const langsListPromise = Promise.resolve()
.then(() => {
if (!options.language) {
return Promise.reject();
}
return [options.language];
})
.catch(() => {
const downloadingLanguages = ora(
`Getting language list for \`${options.fileName}\``
).start();
return onesky
.getLanguages({
apiKey: options.apiKey,
secret: options.secret,
projectId: options.projectId
})
.then(response => {
const { data } = JSON.parse(response);
downloadingLanguages.succeed();
return data
.filter(lang => !lang.is_base_language)
.map(lang => lang.code);
})
.catch(({ message, code }) => {
oneSkyErrorMessageHandler(downloadingLanguages, code, message);
});
});
Promise.resolve()
.then(() => langsListPromise)
.then(langsList => {
const downloadingSpinner = ora(
`Downloading translations for \`${options.fileName}\``
).start();
return onesky
.getMultilingualFile(options)
.then(response => {
downloadingSpinner.succeed();
const translations = JSON.parse(response);
const translationCodes = Object.keys(translations);
langsList.forEach(code => {
const lang = translations[code];
const ext = path.parse(options.fileName).ext;
const filename = `${code}${ext}`;
const filepath = path.join(file, filename);
const savingSpinner = ora(`Saving \`${filepath}\``).start();
if (!lang || !lang.translation) {
savingSpinner.fail(
`There is no translation for \`${code}\` code. Found \`${translationCodes.join(
", "
)}\` instead`
);
return;
}
fs
.ensureFile(filepath)
.then(() => {
return fs.writeJson(filepath, lang.translation, {
spaces: 2
});
})
.catch(({ message }) => {
oneSkyErrorMessageHandler(savingSpinner, null, message);
});
savingSpinner.succeed();
});
})
.catch(({ message, code }) => {
oneSkyErrorMessageHandler(downloadingSpinner, code, message);
});
});
}
}[command]());
};