-
Notifications
You must be signed in to change notification settings - Fork 97
/
document.ts
293 lines (235 loc) · 8.51 KB
/
document.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Vendor
import * as FormData from 'form-data';
import {CLIError} from '@oclif/errors';
import * as fs from 'fs-extra';
import * as mkdirp from 'mkdirp';
import * as chalk from 'chalk';
import * as path from 'path';
import fetch, {Response} from 'node-fetch';
// Services
import {fetchFromRevision} from './revision-slug-fetcher';
import Tree from './tree';
// Types
import {Config} from '../types/config';
import {DocumentConfig, NamePattern} from '../types/document-config';
import {OperationResponse} from '../types/operation-response';
import {Project} from '../types/project';
const SERVER_INTERNAL_ERROR_THRESHOLD_STATUS_CODE = 400;
const throwOnServerError = async (response: Response) => {
if (response.status >= SERVER_INTERNAL_ERROR_THRESHOLD_STATUS_CODE) {
throw new CLIError(chalk.red(`${await response.text()}`), {exit: 1});
}
};
interface FormatOptions {
'order-by': string;
}
export default class Document {
paths: string[];
readonly apiKey: string;
readonly apiUrl: string;
readonly projectId: string | null | undefined;
readonly config: DocumentConfig;
readonly target: string;
constructor(documentConfig: DocumentConfig, config: Config) {
this.config = this.resolveNamePattern(documentConfig);
this.apiKey = config.apiKey;
this.apiUrl = config.apiUrl;
this.projectId = config.project;
this.target = this.config.target;
this.paths = new Tree(this.config).list();
}
refreshPaths() {
this.paths = new Tree(this.config).list();
}
async format(file: string, language: string, options: FormatOptions) {
const formData = new FormData();
formData.append('file', fs.createReadStream(file));
formData.append('document_path', this.parseDocumentName(file, this.config));
formData.append('document_format', this.config.format);
formData.append('language', language);
if (this.projectId) formData.append('project_id', this.projectId);
if (options['order-by']) formData.append('order_by', options['order-by']);
const url = `${this.apiUrl}/format`;
const response = await fetch(url, {
body: formData,
headers: this.authorizationHeader(),
method: 'POST'
});
await throwOnServerError(response);
return this.writeResponseToFile(response, file);
}
async lint(file: string, language: string) {
const formData = new FormData();
formData.append('file', fs.createReadStream(file));
formData.append('document_path', this.parseDocumentName(file, this.config));
formData.append('document_format', this.config.format);
formData.append('language', language);
if (this.projectId) formData.append('project_id', this.projectId);
const url = `${this.apiUrl}/lint`;
const response = await fetch(url, {
body: formData,
headers: this.authorizationHeader(),
method: 'POST'
});
await throwOnServerError(response);
return await response.json();
}
async sync(project: Project, file: string, options: any) {
const masterLanguage = fetchFromRevision(project.masterRevision);
const formData = new FormData();
formData.append('file', fs.createReadStream(file));
formData.append('document_path', this.parseDocumentName(file, this.config));
formData.append('document_format', this.config.format);
formData.append('language', masterLanguage);
if (this.projectId) formData.append('project_id', this.projectId);
let url = `${this.apiUrl}/sync`;
if (options['dry-run']) url = `${url}/peek`;
if (options.version) formData.append('version', options.version);
if (options['sync-type']) {
formData.append('sync_type', options['sync-type']);
}
const response = await fetch(url, {
body: formData,
headers: this.authorizationHeader(),
method: 'POST'
});
await throwOnServerError(response);
return this.handleResponse(response, options, {file});
}
async addTranslations(
file: string,
language: string,
documentPath: string,
options: any
) {
const formData = new FormData();
formData.append('file', fs.createReadStream(file));
formData.append('document_path', documentPath);
formData.append('document_format', this.config.format);
formData.append('language', language);
if (this.projectId) formData.append('project_id', this.projectId);
let url = `${this.apiUrl}/add-translations`;
if (options['dry-run']) url = `${url}/peek`;
if (options.version) formData.append('version', options.version);
if (options['merge-type']) {
formData.append('merge_type', options['merge-type']);
}
const response = await fetch(url, {
body: formData,
headers: this.authorizationHeader(),
method: 'POST'
});
await throwOnServerError(response);
return this.handleResponse(response, options, {file, documentPath});
}
fetchLocalFile(documentPath: string, localPath: string) {
return this.paths.reduce((memo: string, path: string) => {
if (this.parseDocumentName(path, this.config) === documentPath) {
return localPath;
} else {
return memo;
}
}, localPath);
}
async export(
file: string,
language: string,
documentPath: string,
options: any
) {
const query = [
['document_path', documentPath],
['document_format', this.config.format],
['order_by', options['order-by']],
['language', language]
];
if (options.version) query.push(['version', options.version]);
if (this.projectId) query.push(['project_id', this.projectId]);
const url = `${this.apiUrl}/export?${this.encodeQuery(query)}`;
const response = await fetch(url, {
headers: this.authorizationHeader()
});
await throwOnServerError(response);
return this.writeResponseToFile(response, file);
}
async exportJipt(file: string, documentPath: string) {
const query = [
['document_path', documentPath],
['document_format', this.config.format]
];
if (this.projectId) query.push(['project_id', this.projectId]);
const url = `${this.apiUrl}/jipt-export?${this.encodeQuery(query)}`;
const response = await fetch(url, {
headers: this.authorizationHeader()
});
await throwOnServerError(response);
return this.writeResponseToFile(response, file);
}
parseDocumentName(file: string, config: DocumentConfig): string {
if (config.namePattern === NamePattern.parentDirectory) {
const targetPrefixMatch = config.target.match(/(\w+\/)+/);
if (targetPrefixMatch) {
return path.dirname(file).replace(targetPrefixMatch[0], '');
} else {
return path.dirname(file);
}
}
const basename = path.basename(file).replace(path.extname(file), '');
if (config.namePattern === NamePattern.fileWithParentDirectory) {
const languageIndex = config.target.split(path.sep).indexOf('%slug%') + 1;
const pathParts = file.split(path.sep);
const resultPath = pathParts.splice(
languageIndex,
pathParts.length - languageIndex - 1
);
return resultPath.length > 0
? resultPath.join(path.sep).concat(path.sep).concat(basename)
: basename;
}
if (config.namePattern === NamePattern.fileWithSlugSuffix) {
return basename.split('.')[0];
}
return basename;
}
private encodeQuery(params: string[][]) {
return params
.map(([name, value]) => `${name}=${encodeURIComponent(value)}`)
.join('&');
}
private authorizationHeader() {
return {authorization: `Bearer ${this.apiKey}`};
}
private resolveNamePattern(config: DocumentConfig) {
if (config.namePattern) return config;
let pattern = NamePattern.parentDirectory;
if (config.target.match(/%slug%\//) || !config.source.match(/\//)) {
pattern = NamePattern.file;
}
if (config.target.match(/\.%slug%\./)) {
pattern = NamePattern.fileWithSlugSuffix;
}
config.namePattern = pattern;
return config;
}
private async writeResponseToFile(response: Response, file: string) {
return new Promise((resolve, reject) => {
mkdirp.sync(path.dirname(file));
const fileStream = fs.createWriteStream(file, {autoClose: true});
response.body?.pipe(fileStream);
response.body?.on('error', reject);
fileStream.on('finish', resolve);
});
}
private async handleResponse(
response: Response,
options: any,
info: object
): Promise<OperationResponse> {
if (!options['dry-run']) {
return {peek: false, ...info};
} else {
const {data} = (await response.json()) as {data: any};
return {peek: data, ...info};
}
}
}