@diplodoc/cli 4.13.7 → 4.13.8

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/build/index.js CHANGED
@@ -3812,9 +3812,6 @@ function normalizeInput(params, language, exts) {
3812
3812
  function resolveSchemas(_path) {
3813
3813
  return null;
3814
3814
  }
3815
- function flat(parts) {
3816
- return [].concat(...parts);
3817
- }
3818
3815
  var Defer = class {
3819
3816
  constructor() {
3820
3817
  this.promise = new Promise((resolve20, reject) => {
@@ -3841,8 +3838,7 @@ function builder3(argv) {
3841
3838
  }).option("output", {
3842
3839
  alias: "o",
3843
3840
  describe: "output folder where translated markdown will be stored",
3844
- type: "string",
3845
- default: process.cwd()
3841
+ type: "string"
3846
3842
  }).option("source", {
3847
3843
  alias: ["sll", "source-language-locale"],
3848
3844
  describe: "source language and locale",
@@ -3924,8 +3920,7 @@ function builder4(argv) {
3924
3920
  }).option("output", {
3925
3921
  alias: "o",
3926
3922
  describe: "output folder where translated markdown will be stored",
3927
- type: "string",
3928
- default: process.cwd()
3923
+ type: "string"
3929
3924
  }).option("use-source", {
3930
3925
  describe: "for debug",
3931
3926
  type: "boolean"
@@ -3980,45 +3975,39 @@ function pipeline3(input, output, { useSource }) {
3980
3975
  }
3981
3976
 
3982
3977
  // src/cmd/translate/handler.ts
3978
+ var import_axios = __toESM(require("axios"));
3983
3979
  var import_assert3 = require("assert");
3984
3980
  var import_path34 = require("path");
3985
3981
  var import_promises6 = require("fs/promises");
3986
3982
 
3987
3983
  // src/cmd/translate/yandex/auth.ts
3988
3984
  var import_fs14 = require("fs");
3985
+ var resolveKey = (data) => {
3986
+ switch (true) {
3987
+ case data.startsWith("y0_"):
3988
+ return "Bearer " + data;
3989
+ case data.startsWith("t1."):
3990
+ return "Bearer " + data;
3991
+ case data.startsWith("AQVN"):
3992
+ return "Api-Key " + data;
3993
+ default:
3994
+ return null;
3995
+ }
3996
+ };
3989
3997
  function getYandexAuth(path) {
3990
- if (path.startsWith("y0_")) {
3991
- return {
3992
- oauthToken: path
3993
- };
3998
+ let auth = resolveKey(path);
3999
+ if (auth !== null) {
4000
+ return auth;
3994
4001
  }
3995
- const data = (0, import_fs14.readFileSync)(path, "utf8");
3996
- try {
3997
- const json = JSON.parse(data);
3998
- if (isServeseAccount(json)) {
3999
- return {
4000
- serviceAccountJson: {
4001
- serviceAccountId: json.service_account_id,
4002
- accessKeyId: json.id,
4003
- privateKey: json.private_key
4004
- }
4005
- };
4006
- }
4007
- } catch (e) {
4002
+ auth = resolveKey((0, import_fs14.readFileSync)(path, "utf8"));
4003
+ if (auth === null) {
4004
+ throw new Error("No Auth");
4008
4005
  }
4009
- return {
4010
- oauthToken: data
4011
- };
4012
- }
4013
- function isServeseAccount(json) {
4014
- return "private_key" in json;
4006
+ return auth;
4015
4007
  }
4016
4008
 
4017
4009
  // src/cmd/translate/handler.ts
4018
4010
  var import_async5 = require("async");
4019
- var import_session = require("@yandex-cloud/nodejs-sdk/dist/session");
4020
- var import_service_clients = require("@yandex-cloud/nodejs-sdk/dist/generated/yandex/cloud/service_clients");
4021
- var import_translation_service = require("@yandex-cloud/nodejs-sdk/dist/generated/yandex/cloud/ai/translate/v2/translation_service");
4022
4011
  var REQUESTS_LIMIT = 20;
4023
4012
  var BYTES_LIMIT = 1e4;
4024
4013
  var RETRY_LIMIT = 3;
@@ -4084,12 +4073,10 @@ function handler5(args) {
4084
4073
  }
4085
4074
  function requester(params, cache) {
4086
4075
  const { auth, folderId, sourceLanguage, targetLanguage, dryRun } = params;
4087
- const session = new import_session.Session(auth);
4088
- const client = session.client(import_service_clients.TranslationServiceClient);
4089
4076
  const resolve20 = (text, index, texts) => {
4090
4077
  const defer = cache.get(texts[index]);
4091
4078
  if (defer) {
4092
- defer.resolve([text]);
4079
+ defer.resolve(text);
4093
4080
  }
4094
4081
  return text;
4095
4082
  };
@@ -4101,21 +4088,28 @@ function requester(params, cache) {
4101
4088
  if (dryRun) {
4102
4089
  return texts.map(resolve20);
4103
4090
  }
4104
- return client.translate(
4105
- import_translation_service.TranslateRequest.fromPartial({
4106
- texts,
4091
+ return (0, import_axios.default)({
4092
+ method: "POST",
4093
+ url: "https://translate.api.cloud.yandex.net/translate/v2/translate",
4094
+ headers: {
4095
+ "Content-Type": "application/json",
4096
+ Authorization: auth
4097
+ },
4098
+ data: {
4107
4099
  folderId,
4100
+ texts,
4108
4101
  sourceLanguageCode: sourceLanguage,
4109
4102
  targetLanguageCode: targetLanguage,
4110
- // glossaryConfig: {
4111
- // glossaryData: {
4112
- // glossaryPairs: yandexCloudTranslateGlossaryPairs,
4113
- // },
4114
- // },
4115
- format: import_translation_service.TranslateRequest_Format.HTML
4116
- })
4117
- ).then((results) => {
4118
- return results.translations.map(({ text }, index) => {
4103
+ format: "HTML"
4104
+ }
4105
+ }).then(({ data, status }) => {
4106
+ if (status === 200) {
4107
+ return data;
4108
+ } else {
4109
+ throw new Error(data.message);
4110
+ }
4111
+ }).then((result) => {
4112
+ return result.translations.map(({ text }, index) => {
4119
4113
  return resolve20(text, index, texts);
4120
4114
  });
4121
4115
  }).catch((error) => {
@@ -4133,13 +4127,15 @@ function requester(params, cache) {
4133
4127
  }
4134
4128
  function translator(params, split) {
4135
4129
  const { input, output, sourceLanguage, targetLanguage } = params;
4130
+ const inputRoot = (0, import_path34.resolve)(input);
4131
+ const outputRoot = (0, import_path34.resolve)(output);
4136
4132
  return (path) => __async(this, null, function* () {
4137
4133
  const ext = (0, import_path34.extname)(path);
4138
4134
  if (![".yaml", ".json", ".md"].includes(ext)) {
4139
4135
  return;
4140
4136
  }
4141
- const inputPath = (0, import_path34.resolve)(input, path);
4142
- const outputPath = (0, import_path34.resolve)(output, path);
4137
+ const inputPath = (0, import_path34.join)(inputRoot, path);
4138
+ const outputPath = (0, import_path34.join)(outputRoot, path.replace(sourceLanguage, targetLanguage));
4143
4139
  const content = yield loadFile(inputPath);
4144
4140
  yield (0, import_promises6.mkdir)((0, import_path34.dirname)(outputPath), { recursive: true });
4145
4141
  if (!content) {
@@ -4160,7 +4156,7 @@ function translator(params, split) {
4160
4156
  yield dumpFile(outputPath, content);
4161
4157
  return;
4162
4158
  }
4163
- const parts = flat(yield Promise.all(split(path, units)));
4159
+ const parts = yield Promise.all(split(path, units));
4164
4160
  const composed = compose(skeleton, parts, { useSource: true });
4165
4161
  yield dumpFile(outputPath, composed);
4166
4162
  });
@@ -4171,28 +4167,29 @@ function splitter(request, cache) {
4171
4167
  let buffer = [];
4172
4168
  let bufferSize = 0;
4173
4169
  const release = () => {
4174
- promises.push(backoff(request(buffer)));
4170
+ backoff(request(buffer));
4175
4171
  buffer = [];
4176
4172
  bufferSize = 0;
4177
4173
  };
4178
4174
  for (const text of texts) {
4179
- const defer = cache.get(text);
4180
- if (defer) {
4181
- promises.push(defer.promise);
4182
- } else if (text.length >= BYTES_LIMIT) {
4175
+ if (text.length >= BYTES_LIMIT) {
4183
4176
  logger.warn(path, "Skip document part for translation. Part is too big.");
4184
- promises.push(Promise.resolve([text]));
4177
+ promises.push(Promise.resolve(text));
4185
4178
  } else {
4186
- if (bufferSize + text.length > BYTES_LIMIT) {
4187
- release();
4179
+ const defer = cache.get(text) || new Defer();
4180
+ promises.push(defer.promise);
4181
+ if (!cache.get(text)) {
4182
+ if (bufferSize + text.length > BYTES_LIMIT) {
4183
+ release();
4184
+ }
4185
+ buffer.push(text);
4186
+ bufferSize += text.length;
4188
4187
  }
4189
- buffer.push(text);
4190
- bufferSize += text.length;
4191
- cache.set(text, new Defer());
4188
+ cache.set(text, defer);
4192
4189
  }
4193
4190
  }
4194
4191
  if (bufferSize) {
4195
- promises.push(backoff(request(buffer)));
4192
+ release();
4196
4193
  }
4197
4194
  return promises;
4198
4195
  };
@@ -4268,7 +4265,7 @@ import_yargs.default.command(build).command(publish).command(translate).option("
4268
4265
  default: false,
4269
4266
  describe: "Run in quiet mode. Don't write logs to stdout",
4270
4267
  type: "boolean"
4271
- }).group(["config", "strict", "quiet", "help", "version"], "Common options:").version(true ? "4.13.7" : "").help().parse((0, import_helpers.hideBin)(process.argv), {}, (err, { strict }, output) => {
4268
+ }).group(["config", "strict", "quiet", "help", "version"], "Common options:").version(true ? "4.13.8" : "").help().parse((0, import_helpers.hideBin)(process.argv), {}, (err, { strict }, output) => {
4272
4269
  console.timeEnd(MAIN_TIMER_ID);
4273
4270
  if (err) {
4274
4271
  console.error(err);