@diplodoc/cli 4.13.2 → 4.13.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/vendor.css +16 -1
- package/assets/vendor.js +1 -1
- package/build/index.js +105 -83
- package/build/index.js.map +3 -3
- package/build/linter.js +66 -87
- package/build/linter.js.map +4 -4
- package/package.json +5 -8
- package/src/cmd/translate/index.ts +131 -92
package/build/index.js
CHANGED
|
@@ -3975,14 +3975,15 @@ function builder5(argv) {
|
|
|
3975
3975
|
}
|
|
3976
3976
|
|
|
3977
3977
|
// src/cmd/translate/index.ts
|
|
3978
|
+
var import_glob4 = __toESM(require("glob"));
|
|
3979
|
+
var import_assert = require("assert");
|
|
3978
3980
|
var import_async5 = require("async");
|
|
3979
3981
|
var import_path33 = require("path");
|
|
3980
3982
|
var import_promises4 = require("fs/promises");
|
|
3981
|
-
var import_fast_xml_parser = require("fast-xml-parser");
|
|
3982
3983
|
var import_session = require("@yandex-cloud/nodejs-sdk/dist/session");
|
|
3983
3984
|
var import_service_clients = require("@yandex-cloud/nodejs-sdk/dist/generated/yandex/cloud/service_clients");
|
|
3984
3985
|
var import_translation_service = require("@yandex-cloud/nodejs-sdk/dist/generated/yandex/cloud/ai/translate/v2/translation_service");
|
|
3985
|
-
var import_markdown_translation3 =
|
|
3986
|
+
var import_markdown_translation3 = require("@diplodoc/markdown-translation");
|
|
3986
3987
|
|
|
3987
3988
|
// src/packages/credentials/yandex-oauth.ts
|
|
3988
3989
|
var import_promises3 = require("fs/promises");
|
|
@@ -4024,13 +4025,14 @@ var translate = {
|
|
|
4024
4025
|
handler: handler5,
|
|
4025
4026
|
builder: builder6
|
|
4026
4027
|
};
|
|
4027
|
-
var MD_GLOB3 = "**/*.md";
|
|
4028
4028
|
var REQUESTS_LIMIT = 20;
|
|
4029
4029
|
var BYTES_LIMIT = 1e4;
|
|
4030
4030
|
var RETRY_LIMIT = 3;
|
|
4031
|
-
var MTRANS_LOCALE = "MTRANS";
|
|
4032
4031
|
function builder6(argv) {
|
|
4033
|
-
return argv.option("
|
|
4032
|
+
return argv.option("folder-id", {
|
|
4033
|
+
describe: "folder id",
|
|
4034
|
+
type: "string"
|
|
4035
|
+
}).option("source-language", {
|
|
4034
4036
|
alias: "sl",
|
|
4035
4037
|
describe: "source language code",
|
|
4036
4038
|
type: "string"
|
|
@@ -4038,10 +4040,7 @@ function builder6(argv) {
|
|
|
4038
4040
|
alias: "tl",
|
|
4039
4041
|
describe: "target language code",
|
|
4040
4042
|
type: "string"
|
|
4041
|
-
}).
|
|
4042
|
-
["source-language", "target-language"],
|
|
4043
|
-
"command requires to specify source and target languages"
|
|
4044
|
-
);
|
|
4043
|
+
}).check(argvValidator);
|
|
4045
4044
|
}
|
|
4046
4045
|
var TranslatorError = class extends Error {
|
|
4047
4046
|
constructor(message, path) {
|
|
@@ -4051,34 +4050,47 @@ var TranslatorError = class extends Error {
|
|
|
4051
4050
|
};
|
|
4052
4051
|
function handler5(args) {
|
|
4053
4052
|
return __async(this, null, function* () {
|
|
4054
|
-
argv_default.init(__spreadValues({}, args));
|
|
4053
|
+
argv_default.init(__spreadValues(__spreadValues({}, args.translate || {}), args));
|
|
4055
4054
|
const {
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
} =
|
|
4055
|
+
folderId,
|
|
4056
|
+
// yandexCloudTranslateGlossaryPairs,
|
|
4057
|
+
sourceLanguage,
|
|
4058
|
+
targetLanguage,
|
|
4059
|
+
exclude = []
|
|
4060
|
+
} = argv_default.getConfig();
|
|
4061
|
+
let { input, output, include = [] } = argv_default.getConfig();
|
|
4063
4062
|
logger.info(
|
|
4064
4063
|
input,
|
|
4065
4064
|
`translating documentation from ${sourceLanguage} to ${targetLanguage} language`
|
|
4066
4065
|
);
|
|
4066
|
+
output = output || input;
|
|
4067
|
+
(0, import_assert.ok)(input, "Required param input is not configured");
|
|
4068
|
+
(0, import_assert.ok)(sourceLanguage, "Required param sourceLanguage is not configured");
|
|
4069
|
+
(0, import_assert.ok)(targetLanguage, "Required param targetLanguage is not configured");
|
|
4067
4070
|
try {
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
}
|
|
4072
|
-
|
|
4073
|
-
}
|
|
4071
|
+
if (input.endsWith(".md")) {
|
|
4072
|
+
include = [(0, import_path33.basename)(input)];
|
|
4073
|
+
input = (0, import_path33.dirname)(input);
|
|
4074
|
+
} else if (!include.length) {
|
|
4075
|
+
include.push("**/*");
|
|
4076
|
+
}
|
|
4077
|
+
const files = [].concat(
|
|
4078
|
+
...include.map(
|
|
4079
|
+
(match) => import_glob4.default.sync(match, {
|
|
4080
|
+
cwd: (0, import_path33.join)(input, sourceLanguage),
|
|
4081
|
+
ignore: exclude
|
|
4082
|
+
})
|
|
4083
|
+
)
|
|
4084
|
+
);
|
|
4085
|
+
const found = [...new Set(files)];
|
|
4074
4086
|
const oauthToken = yield getYandexOAuthToken();
|
|
4075
4087
|
const translatorParams = {
|
|
4076
4088
|
input,
|
|
4077
4089
|
output,
|
|
4078
4090
|
sourceLanguage,
|
|
4079
4091
|
targetLanguage,
|
|
4080
|
-
yandexCloudTranslateGlossaryPairs,
|
|
4081
|
-
folderId
|
|
4092
|
+
// yandexCloudTranslateGlossaryPairs,
|
|
4093
|
+
folderId,
|
|
4082
4094
|
oauthToken
|
|
4083
4095
|
};
|
|
4084
4096
|
const translateFn = translator(translatorParams);
|
|
@@ -4103,45 +4115,69 @@ function translator(params) {
|
|
|
4103
4115
|
input,
|
|
4104
4116
|
output,
|
|
4105
4117
|
sourceLanguage,
|
|
4106
|
-
targetLanguage
|
|
4107
|
-
yandexCloudTranslateGlossaryPairs
|
|
4118
|
+
targetLanguage
|
|
4119
|
+
// yandexCloudTranslateGlossaryPairs,
|
|
4108
4120
|
} = params;
|
|
4121
|
+
const tmap = /* @__PURE__ */ new Map();
|
|
4109
4122
|
const session = new import_session.Session({ oauthToken });
|
|
4110
4123
|
const client = session.client(import_service_clients.TranslationServiceClient);
|
|
4111
|
-
const request = (texts) => () =>
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4115
|
-
|
|
4116
|
-
|
|
4117
|
-
|
|
4118
|
-
|
|
4119
|
-
|
|
4124
|
+
const request = (texts) => () => {
|
|
4125
|
+
return client.translate(
|
|
4126
|
+
import_translation_service.TranslateRequest.fromPartial({
|
|
4127
|
+
texts,
|
|
4128
|
+
folderId,
|
|
4129
|
+
sourceLanguageCode: sourceLanguage,
|
|
4130
|
+
targetLanguageCode: targetLanguage,
|
|
4131
|
+
// glossaryConfig: {
|
|
4132
|
+
// glossaryData: {
|
|
4133
|
+
// glossaryPairs: yandexCloudTranslateGlossaryPairs,
|
|
4134
|
+
// },
|
|
4135
|
+
// },
|
|
4136
|
+
format: import_translation_service.TranslateRequest_Format.HTML
|
|
4137
|
+
})
|
|
4138
|
+
).then((results) => {
|
|
4139
|
+
return results.translations.map(({ text }, index) => {
|
|
4140
|
+
const defer = tmap.get(texts[index]);
|
|
4141
|
+
if (defer) {
|
|
4142
|
+
defer.resolve([text]);
|
|
4120
4143
|
}
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
})
|
|
4124
|
-
|
|
4144
|
+
return text;
|
|
4145
|
+
});
|
|
4146
|
+
});
|
|
4147
|
+
};
|
|
4125
4148
|
return (mdPath) => __async(this, null, function* () {
|
|
4149
|
+
if (!mdPath.endsWith(".md")) {
|
|
4150
|
+
return;
|
|
4151
|
+
}
|
|
4126
4152
|
try {
|
|
4127
4153
|
logger.info(mdPath, "translating");
|
|
4128
|
-
const
|
|
4129
|
-
const
|
|
4154
|
+
const inputPath = (0, import_path33.resolve)(input, sourceLanguage, mdPath);
|
|
4155
|
+
const outputPath = (0, import_path33.resolve)(output, targetLanguage, mdPath);
|
|
4156
|
+
const md = yield (0, import_promises4.readFile)(inputPath, { encoding: "utf-8" });
|
|
4157
|
+
yield (0, import_promises4.mkdir)((0, import_path33.dirname)(outputPath), { recursive: true });
|
|
4158
|
+
if (!md) {
|
|
4159
|
+
yield (0, import_promises4.writeFile)(outputPath, md);
|
|
4160
|
+
return;
|
|
4161
|
+
}
|
|
4162
|
+
const { units, skeleton } = (0, import_markdown_translation3.extract)({
|
|
4130
4163
|
source: {
|
|
4131
4164
|
language: sourceLanguage,
|
|
4132
|
-
locale:
|
|
4165
|
+
locale: "RU"
|
|
4133
4166
|
},
|
|
4134
4167
|
target: {
|
|
4135
4168
|
language: targetLanguage,
|
|
4136
|
-
locale:
|
|
4169
|
+
locale: "US"
|
|
4137
4170
|
},
|
|
4138
4171
|
markdown: md,
|
|
4139
4172
|
markdownPath: mdPath,
|
|
4140
4173
|
skeletonPath: ""
|
|
4141
4174
|
});
|
|
4142
|
-
|
|
4175
|
+
if (!units.length) {
|
|
4176
|
+
yield (0, import_promises4.writeFile)(outputPath, md);
|
|
4177
|
+
return;
|
|
4178
|
+
}
|
|
4143
4179
|
const parts = yield Promise.all(
|
|
4144
|
-
|
|
4180
|
+
units.reduce(
|
|
4145
4181
|
({
|
|
4146
4182
|
promises,
|
|
4147
4183
|
buffer,
|
|
@@ -4155,13 +4191,23 @@ function translator(params) {
|
|
|
4155
4191
|
promises.push(Promise.resolve([text]));
|
|
4156
4192
|
return { promises, buffer, bufferSize };
|
|
4157
4193
|
}
|
|
4158
|
-
|
|
4194
|
+
const defer = tmap.get(text);
|
|
4195
|
+
if (defer) {
|
|
4196
|
+
console.log("SKIPPED", text);
|
|
4197
|
+
promises.push(defer.promise);
|
|
4198
|
+
return { promises, buffer, bufferSize };
|
|
4199
|
+
}
|
|
4200
|
+
if (bufferSize + text.length > BYTES_LIMIT) {
|
|
4159
4201
|
promises.push(backoff(request(buffer)));
|
|
4160
4202
|
buffer = [];
|
|
4161
4203
|
bufferSize = 0;
|
|
4162
4204
|
}
|
|
4163
4205
|
buffer.push(text);
|
|
4164
4206
|
bufferSize += text.length;
|
|
4207
|
+
tmap.set(text, new Defer());
|
|
4208
|
+
if (index === units.length - 1) {
|
|
4209
|
+
promises.push(backoff(request(buffer)));
|
|
4210
|
+
}
|
|
4165
4211
|
return { promises, buffer, bufferSize };
|
|
4166
4212
|
},
|
|
4167
4213
|
{
|
|
@@ -4172,18 +4218,11 @@ function translator(params) {
|
|
|
4172
4218
|
).promises
|
|
4173
4219
|
);
|
|
4174
4220
|
const translations = [].concat(...parts);
|
|
4175
|
-
const
|
|
4176
|
-
|
|
4177
|
-
|
|
4178
|
-
sources: texts,
|
|
4179
|
-
targets: translations
|
|
4180
|
-
});
|
|
4181
|
-
const composed = yield import_markdown_translation3.default.compose({
|
|
4182
|
-
xlf: translatedXLIFF,
|
|
4221
|
+
const composed = yield (0, import_markdown_translation3.compose)({
|
|
4222
|
+
useSource: true,
|
|
4223
|
+
units: translations,
|
|
4183
4224
|
skeleton
|
|
4184
4225
|
});
|
|
4185
|
-
const outputPath = mdPath.replace(input, output);
|
|
4186
|
-
yield (0, import_promises4.mkdir)((0, import_path33.dirname)(outputPath), { recursive: true });
|
|
4187
4226
|
yield (0, import_promises4.writeFile)(outputPath, composed);
|
|
4188
4227
|
logger.info(outputPath, "finished translating");
|
|
4189
4228
|
} catch (err) {
|
|
@@ -4204,31 +4243,14 @@ function backoff(action) {
|
|
|
4204
4243
|
(0, import_async5.asyncify)(action)
|
|
4205
4244
|
);
|
|
4206
4245
|
}
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
}
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
const unit = (text, i) => `
|
|
4216
|
-
<trans-unit id="${i + 1}">
|
|
4217
|
-
<source xml:lang="${sourceLanguage}">${sources[i]}</source>
|
|
4218
|
-
<target xml:lang="${targetLanguage}">${text}</target>
|
|
4219
|
-
</trans-unit>`;
|
|
4220
|
-
const doc = `
|
|
4221
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
4222
|
-
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
|
|
4223
|
-
<file original="" source-language="${sourceLanguage}" target-language="${targetLanguage}">
|
|
4224
|
-
<header>
|
|
4225
|
-
<skl><external-file href="" /></skl>
|
|
4226
|
-
</header>
|
|
4227
|
-
<body>${targets.map(unit)}</body>
|
|
4228
|
-
</file>
|
|
4229
|
-
</xliff>`;
|
|
4230
|
-
return doc;
|
|
4231
|
-
}
|
|
4246
|
+
var Defer = class {
|
|
4247
|
+
constructor() {
|
|
4248
|
+
this.promise = new Promise((resolve18, reject) => {
|
|
4249
|
+
this.resolve = resolve18;
|
|
4250
|
+
this.reject = reject;
|
|
4251
|
+
});
|
|
4252
|
+
}
|
|
4253
|
+
};
|
|
4232
4254
|
|
|
4233
4255
|
// src/index.ts
|
|
4234
4256
|
console.time(MAIN_TIMER_ID);
|
|
@@ -4246,7 +4268,7 @@ import_yargs.default.command(build).command(publish).command(xliff).command(tran
|
|
|
4246
4268
|
default: false,
|
|
4247
4269
|
describe: "Run in quiet mode. Don't write logs to stdout",
|
|
4248
4270
|
type: "boolean"
|
|
4249
|
-
}).group(["config", "strict", "quiet", "help", "version"], "Common options:").version(true ? "4.13.
|
|
4271
|
+
}).group(["config", "strict", "quiet", "help", "version"], "Common options:").version(true ? "4.13.4" : "").help().parse((0, import_helpers.hideBin)(process.argv), {}, (err, { strict }, output) => {
|
|
4250
4272
|
console.timeEnd(MAIN_TIMER_ID);
|
|
4251
4273
|
if (err) {
|
|
4252
4274
|
console.error(err);
|