@tolgee/cli 1.1.0 → 1.1.2

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.
Files changed (43) hide show
  1. package/README.md +11 -8
  2. package/dist/client/errors.js +1 -5
  3. package/dist/client/export.js +1 -4
  4. package/dist/client/import.js +5 -11
  5. package/dist/client/index.js +17 -23
  6. package/dist/client/internal/requester.js +14 -20
  7. package/dist/client/internal/schema.generated.js +1 -2
  8. package/dist/client/internal/schema.utils.js +1 -2
  9. package/dist/client/languages.js +1 -4
  10. package/dist/client/project.js +1 -4
  11. package/dist/commands/extract/check.js +11 -13
  12. package/dist/commands/extract/print.js +10 -12
  13. package/dist/commands/extract.js +8 -13
  14. package/dist/commands/login.js +16 -22
  15. package/dist/commands/pull.js +12 -14
  16. package/dist/commands/push.js +28 -30
  17. package/dist/commands/sync/compare.js +18 -23
  18. package/dist/commands/sync/sync.js +34 -39
  19. package/dist/commands/sync/syncUtils.js +10 -18
  20. package/dist/config/credentials.js +16 -25
  21. package/dist/config/tolgeerc.js +11 -14
  22. package/dist/constants.js +11 -18
  23. package/dist/extractor/extractor.js +13 -19
  24. package/dist/extractor/index.js +1 -2
  25. package/dist/extractor/machines/comments.js +10 -15
  26. package/dist/extractor/machines/react.js +36 -41
  27. package/dist/extractor/machines/shared/comments.js +3 -6
  28. package/dist/extractor/machines/shared/properties.js +13 -15
  29. package/dist/extractor/machines/svelte.js +43 -48
  30. package/dist/extractor/runner.js +8 -16
  31. package/dist/extractor/tokenizer.js +20 -21
  32. package/dist/extractor/warnings.js +9 -14
  33. package/dist/extractor/worker.js +23 -28
  34. package/dist/index.js +53 -58
  35. package/dist/options.js +14 -17
  36. package/dist/utils/ask.js +4 -12
  37. package/dist/utils/configPath.js +10 -10
  38. package/dist/utils/deferred.js +1 -5
  39. package/dist/utils/logger.js +8 -19
  40. package/dist/utils/moduleLoader.js +7 -15
  41. package/dist/utils/overwriteDir.js +13 -17
  42. package/dist/utils/zip.js +11 -16
  43. package/package.json +33 -30
@@ -1,13 +1,8 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const xstate_1 = require("xstate");
7
- const properties_1 = __importDefault(require("./shared/properties"));
8
- const comments_1 = __importDefault(require("./shared/comments"));
1
+ import { createMachine, assign, send, forwardTo } from 'xstate';
2
+ import propertiesMachine from './shared/properties.js';
3
+ import commentsService from './shared/comments.js';
9
4
  const VOID_KEY = { keyName: '', line: -1 };
10
- exports.default = (0, xstate_1.createMachine)({
5
+ export default createMachine({
11
6
  predictableActionArguments: true,
12
7
  id: 'svelteExtractor',
13
8
  type: 'parallel',
@@ -15,7 +10,7 @@ exports.default = (0, xstate_1.createMachine)({
15
10
  children: '',
16
11
  line: 0,
17
12
  key: VOID_KEY,
18
- useTranslate: null,
13
+ getTranslate: null,
19
14
  ignore: null,
20
15
  keys: [],
21
16
  warnings: [],
@@ -24,7 +19,7 @@ exports.default = (0, xstate_1.createMachine)({
24
19
  comments: {
25
20
  invoke: {
26
21
  id: 'comments',
27
- src: () => comments_1.default,
22
+ src: () => commentsService,
28
23
  },
29
24
  on: {
30
25
  // Service messages
@@ -43,21 +38,21 @@ exports.default = (0, xstate_1.createMachine)({
43
38
  },
44
39
  // Code messages
45
40
  'comment.line.double-slash.ts': {
46
- actions: (0, xstate_1.send)((_ctx, evt) => ({
41
+ actions: send((_ctx, evt) => ({
47
42
  type: 'COMMENT',
48
43
  data: evt.token,
49
44
  line: evt.line,
50
45
  }), { to: 'comments' }),
51
46
  },
52
47
  'comment.block.ts': {
53
- actions: (0, xstate_1.send)((_ctx, evt) => ({
48
+ actions: send((_ctx, evt) => ({
54
49
  type: 'COMMENT',
55
50
  data: evt.token,
56
51
  line: evt.line,
57
52
  }), { to: 'comments' }),
58
53
  },
59
54
  'comment.block.svelte': {
60
- actions: (0, xstate_1.send)((_ctx, evt) => ({
55
+ actions: send((_ctx, evt) => ({
61
56
  type: 'COMMENT',
62
57
  data: evt.token,
63
58
  line: evt.line,
@@ -69,7 +64,7 @@ exports.default = (0, xstate_1.createMachine)({
69
64
  },
70
65
  },
71
66
  },
72
- useTranslate: {
67
+ getTranslate: {
73
68
  initial: 'idle',
74
69
  states: {
75
70
  idle: {
@@ -77,7 +72,7 @@ exports.default = (0, xstate_1.createMachine)({
77
72
  'entity.name.function.ts': {
78
73
  target: 'func',
79
74
  actions: 'storeLine',
80
- cond: (_ctx, evt) => evt.token === 'useTranslate',
75
+ cond: (_ctx, evt) => evt.token === 'getTranslate',
81
76
  },
82
77
  },
83
78
  },
@@ -108,12 +103,12 @@ exports.default = (0, xstate_1.createMachine)({
108
103
  'punctuation.definition.string.template.begin.ts': 'namespace',
109
104
  'variable.other.readwrite.ts': {
110
105
  target: 'idle',
111
- actions: ['storeUseTranslate', 'markUseTranslateAsDynamic'],
106
+ actions: ['storeGetTranslate', 'markGetTranslateAsDynamic'],
112
107
  },
113
108
  'meta.brace.round.ts': {
114
109
  target: 'idle',
115
110
  cond: (_ctx, evt) => evt.token === ')',
116
- actions: 'storeUseTranslate',
111
+ actions: 'storeGetTranslate',
117
112
  },
118
113
  },
119
114
  },
@@ -121,7 +116,7 @@ exports.default = (0, xstate_1.createMachine)({
121
116
  on: {
122
117
  '*': {
123
118
  target: 'namespace_end',
124
- actions: 'storeNamespacedUseTranslate',
119
+ actions: 'storeNamespacedGetTranslate',
125
120
  },
126
121
  },
127
122
  },
@@ -131,11 +126,11 @@ exports.default = (0, xstate_1.createMachine)({
131
126
  'meta.brace.round.ts': 'idle',
132
127
  'punctuation.definition.template-expression.begin.ts': {
133
128
  target: 'idle',
134
- actions: 'markUseTranslateAsDynamic',
129
+ actions: 'markGetTranslateAsDynamic',
135
130
  },
136
131
  'keyword.operator.arithmetic.ts': {
137
132
  target: 'idle',
138
- actions: 'markUseTranslateAsDynamic',
133
+ actions: 'markGetTranslateAsDynamic',
139
134
  },
140
135
  },
141
136
  },
@@ -174,7 +169,7 @@ exports.default = (0, xstate_1.createMachine)({
174
169
  props: {
175
170
  invoke: {
176
171
  id: 'propertiesMachine',
177
- src: properties_1.default,
172
+ src: propertiesMachine,
178
173
  onDone: [
179
174
  {
180
175
  target: 'idle',
@@ -196,7 +191,7 @@ exports.default = (0, xstate_1.createMachine)({
196
191
  },
197
192
  on: {
198
193
  '*': {
199
- actions: (0, xstate_1.forwardTo)('propertiesMachine'),
194
+ actions: forwardTo('propertiesMachine'),
200
195
  },
201
196
  },
202
197
  },
@@ -209,7 +204,7 @@ exports.default = (0, xstate_1.createMachine)({
209
204
  on: {
210
205
  'punctuation.definition.variable.svelte': {
211
206
  target: 'dollar',
212
- cond: (ctx, evt) => ctx.useTranslate !== null && evt.token === '$',
207
+ cond: (ctx, evt) => ctx.getTranslate !== null && evt.token === '$',
213
208
  },
214
209
  },
215
210
  },
@@ -328,7 +323,7 @@ exports.default = (0, xstate_1.createMachine)({
328
323
  param_object: {
329
324
  invoke: {
330
325
  id: 'propertiesMachine',
331
- src: properties_1.default,
326
+ src: propertiesMachine,
332
327
  data: {
333
328
  depth: 1,
334
329
  },
@@ -346,7 +341,7 @@ exports.default = (0, xstate_1.createMachine)({
346
341
  },
347
342
  on: {
348
343
  '*': {
349
- actions: (0, xstate_1.forwardTo)('propertiesMachine'),
344
+ actions: forwardTo('propertiesMachine'),
350
345
  },
351
346
  },
352
347
  },
@@ -358,35 +353,35 @@ exports.default = (0, xstate_1.createMachine)({
358
353
  isPropertiesDataDynamic: (_ctx, evt) => evt.data.keyName === false || evt.data.namespace === false,
359
354
  },
360
355
  actions: {
361
- storeLine: (0, xstate_1.assign)({
356
+ storeLine: assign({
362
357
  line: (_ctx, evt) => evt.line,
363
358
  }),
364
- ignoreNextLine: (0, xstate_1.assign)({
359
+ ignoreNextLine: assign({
365
360
  ignore: (_ctx, evt) => ({ type: 'ignore', line: evt.line + 1 }),
366
361
  }),
367
- consumeIgnoredLine: (0, xstate_1.assign)({
362
+ consumeIgnoredLine: assign({
368
363
  ignore: (_ctx, _evt) => null,
369
364
  }),
370
- warnUnusedIgnore: (0, xstate_1.assign)({
365
+ warnUnusedIgnore: assign({
371
366
  warnings: (ctx, evt) => [
372
367
  ...ctx.warnings,
373
368
  { warning: 'W_UNUSED_IGNORE', line: evt.line - 1 },
374
369
  ],
375
370
  }),
376
- storeUseTranslate: (0, xstate_1.assign)({
377
- useTranslate: (_ctx, _evt) => '',
371
+ storeGetTranslate: assign({
372
+ getTranslate: (_ctx, _evt) => '',
378
373
  }),
379
- storeNamespacedUseTranslate: (0, xstate_1.assign)({
380
- useTranslate: (_ctx, evt) => evt.token,
374
+ storeNamespacedGetTranslate: assign({
375
+ getTranslate: (_ctx, evt) => evt.token,
381
376
  }),
382
- markUseTranslateAsDynamic: (0, xstate_1.assign)({
383
- useTranslate: (_ctx, _evt) => false,
377
+ markGetTranslateAsDynamic: assign({
378
+ getTranslate: (_ctx, _evt) => false,
384
379
  warnings: (ctx, _evt) => [
385
380
  ...ctx.warnings,
386
381
  { warning: 'W_DYNAMIC_NAMESPACE', line: ctx.line },
387
382
  ],
388
383
  }),
389
- consumeParameters: (0, xstate_1.assign)({
384
+ consumeParameters: assign({
390
385
  key: (ctx, evt) => ({
391
386
  keyName: ctx.key.keyName || evt.data.keyName,
392
387
  defaultValue: ctx.key.defaultValue || evt.data.defaultValue || undefined,
@@ -402,7 +397,7 @@ exports.default = (0, xstate_1.createMachine)({
402
397
  ];
403
398
  },
404
399
  }),
405
- emitWarningFromParameters: (0, xstate_1.assign)({
400
+ emitWarningFromParameters: assign({
406
401
  warnings: (ctx, evt) => [
407
402
  ...ctx.warnings,
408
403
  {
@@ -414,40 +409,40 @@ exports.default = (0, xstate_1.createMachine)({
414
409
  ],
415
410
  key: (_ctx, _evt) => VOID_KEY,
416
411
  }),
417
- storeKeyName: (0, xstate_1.assign)({
412
+ storeKeyName: assign({
418
413
  key: (ctx, evt) => ({ ...ctx.key, keyName: evt.token }),
419
414
  }),
420
- storeKeyDefault: (0, xstate_1.assign)({
415
+ storeKeyDefault: assign({
421
416
  key: (ctx, evt) => ({ ...ctx.key, defaultValue: evt.token }),
422
417
  }),
423
- storeKeyCurrentNamespace: (0, xstate_1.assign)({
418
+ storeKeyCurrentNamespace: assign({
424
419
  key: (ctx, _evt) => ({
425
420
  ...ctx.key,
426
- namespace: ctx.useTranslate !== null ? ctx.useTranslate : undefined,
421
+ namespace: ctx.getTranslate !== null ? ctx.getTranslate : undefined,
427
422
  }),
428
423
  }),
429
- dynamicKeyName: (0, xstate_1.assign)({
424
+ dynamicKeyName: assign({
430
425
  warnings: (ctx, _evt) => [
431
426
  ...ctx.warnings,
432
427
  { warning: 'W_DYNAMIC_KEY', line: ctx.line },
433
428
  ],
434
429
  key: (_ctx, _evt) => VOID_KEY,
435
430
  }),
436
- dynamicKeyDefault: (0, xstate_1.assign)({
431
+ dynamicKeyDefault: assign({
437
432
  key: (ctx, _evt) => ({ ...ctx.key, defaultValue: undefined }),
438
433
  warnings: (ctx, _evt) => [
439
434
  ...ctx.warnings,
440
435
  { warning: 'W_DYNAMIC_DEFAULT_VALUE', line: ctx.line },
441
436
  ],
442
437
  }),
443
- dynamicOptions: (0, xstate_1.assign)({
438
+ dynamicOptions: assign({
444
439
  key: (_ctx, _evt) => VOID_KEY,
445
440
  warnings: (ctx, _evt) => [
446
441
  ...ctx.warnings,
447
442
  { warning: 'W_DYNAMIC_OPTIONS', line: ctx.line },
448
443
  ],
449
444
  }),
450
- pushKey: (0, xstate_1.assign)({
445
+ pushKey: assign({
451
446
  warnings: (ctx, _evt) => {
452
447
  if (!ctx.key.keyName || ctx.key.namespace !== false)
453
448
  return ctx.warnings;
@@ -473,7 +468,7 @@ exports.default = (0, xstate_1.createMachine)({
473
468
  },
474
469
  key: (_ctx, _evt) => ({ keyName: '', line: 0 }),
475
470
  }),
476
- pushImmediateKey: (0, xstate_1.assign)({
471
+ pushImmediateKey: assign({
477
472
  ignore: (_ctx, evt) => ({ type: 'key', line: evt.line + 1 }),
478
473
  keys: (ctx, evt) => [
479
474
  ...ctx.keys,
@@ -485,7 +480,7 @@ exports.default = (0, xstate_1.createMachine)({
485
480
  },
486
481
  ],
487
482
  }),
488
- pushWarning: (0, xstate_1.assign)({
483
+ pushWarning: assign({
489
484
  warnings: (ctx, evt) => [
490
485
  ...ctx.warnings,
491
486
  { warning: evt.kind, line: evt.line },
@@ -1,19 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.filterExtractionResult = exports.extractKeysOfFiles = exports.extractKeysFromFile = exports.NullNamespace = void 0;
4
- const glob_1 = require("glob");
5
- const worker_1 = require("./worker");
6
- const util_1 = require("util");
7
- const glob = (0, util_1.promisify)(glob_1.glob);
8
- exports.NullNamespace = Symbol('namespace.null');
9
- async function extractKeysFromFile(file, extractor) {
10
- return (0, worker_1.callWorker)({
1
+ import { glob } from 'glob';
2
+ import { callWorker } from './worker.js';
3
+ export const NullNamespace = Symbol('namespace.null');
4
+ export async function extractKeysFromFile(file, extractor) {
5
+ return callWorker({
11
6
  extractor: extractor,
12
7
  file: file,
13
8
  });
14
9
  }
15
- exports.extractKeysFromFile = extractKeysFromFile;
16
- async function extractKeysOfFiles(filesPattern, extractor) {
10
+ export async function extractKeysOfFiles(filesPattern, extractor) {
17
11
  const files = await glob(filesPattern, { nodir: true });
18
12
  const result = new Map();
19
13
  await Promise.all(files.map(async (file) => {
@@ -22,12 +16,11 @@ async function extractKeysOfFiles(filesPattern, extractor) {
22
16
  }));
23
17
  return result;
24
18
  }
25
- exports.extractKeysOfFiles = extractKeysOfFiles;
26
- function filterExtractionResult(data) {
19
+ export function filterExtractionResult(data) {
27
20
  const result = Object.create(null);
28
21
  for (const { keys } of data.values()) {
29
22
  for (const key of keys) {
30
- const namespace = key.namespace || exports.NullNamespace;
23
+ const namespace = key.namespace || NullNamespace;
31
24
  if (!(namespace in result)) {
32
25
  result[namespace] = new Map();
33
26
  }
@@ -36,4 +29,3 @@ function filterExtractionResult(data) {
36
29
  }
37
30
  return result;
38
31
  }
39
- exports.filterExtractionResult = filterExtractionResult;
@@ -1,36 +1,36 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const path_1 = require("path");
4
- const promises_1 = require("fs/promises");
5
- const vscode_textmate_1 = require("vscode-textmate");
6
- const vscode_oniguruma_1 = require("vscode-oniguruma");
7
- const GRAMMAR_PATH = (0, path_1.join)(__dirname, '..', '..', 'textmate');
1
+ import { extname } from 'path';
2
+ import { readFile } from 'fs/promises';
3
+ import { createRequire } from 'module';
4
+ import TextMate from 'vscode-textmate';
5
+ import Oniguruma from 'vscode-oniguruma';
6
+ const GRAMMAR_PATH = new URL('../../textmate/', import.meta.url);
8
7
  const GrammarFiles = {
9
- ["source.ts" /* Grammar.TYPESCRIPT */]: (0, path_1.join)(GRAMMAR_PATH, 'TypeScript.tmLanguage'),
10
- ["source.tsx" /* Grammar.TYPESCRIPT_TSX */]: (0, path_1.join)(GRAMMAR_PATH, 'TypeScriptReact.tmLanguage'),
11
- ["source.svelte" /* Grammar.SVELTE */]: (0, path_1.join)(GRAMMAR_PATH, 'Svelte.tmLanguage'),
8
+ ["source.ts" /* Grammar.TYPESCRIPT */]: new URL('TypeScript.tmLanguage', GRAMMAR_PATH),
9
+ ["source.tsx" /* Grammar.TYPESCRIPT_TSX */]: new URL('TypeScriptReact.tmLanguage', GRAMMAR_PATH),
10
+ ["source.svelte" /* Grammar.SVELTE */]: new URL('Svelte.tmLanguage', GRAMMAR_PATH),
12
11
  };
13
12
  let oniguruma;
14
13
  let registry;
15
14
  async function initializeOniguruma() {
15
+ const require = createRequire(import.meta.url);
16
16
  const wasmBlobPath = require
17
17
  .resolve('vscode-oniguruma')
18
18
  .replace('main.js', 'onig.wasm');
19
- const wasmBlob = await (0, promises_1.readFile)(wasmBlobPath);
20
- await (0, vscode_oniguruma_1.loadWASM)(wasmBlob);
19
+ const wasmBlob = await readFile(wasmBlobPath);
20
+ await Oniguruma.loadWASM(wasmBlob);
21
21
  return {
22
- createOnigScanner: (patterns) => new vscode_oniguruma_1.OnigScanner(patterns),
23
- createOnigString: (s) => new vscode_oniguruma_1.OnigString(s),
22
+ createOnigScanner: (patterns) => new Oniguruma.OnigScanner(patterns),
23
+ createOnigString: (s) => new Oniguruma.OnigString(s),
24
24
  };
25
25
  }
26
26
  async function loadGrammar(scope) {
27
27
  const file = GrammarFiles[scope];
28
28
  if (!file)
29
29
  return null;
30
- const grammar = await (0, promises_1.readFile)(file, 'utf8');
30
+ const grammar = await readFile(file, 'utf8');
31
31
  return grammar.startsWith('{')
32
32
  ? JSON.parse(grammar)
33
- : (0, vscode_textmate_1.parseRawGrammar)(grammar);
33
+ : TextMate.parseRawGrammar(grammar);
34
34
  }
35
35
  function extnameToGrammar(extname) {
36
36
  switch (extname) {
@@ -49,7 +49,7 @@ function extnameToGrammar(extname) {
49
49
  }
50
50
  }
51
51
  function tokenize(code, grammar) {
52
- let stack = vscode_textmate_1.INITIAL;
52
+ let stack = TextMate.INITIAL;
53
53
  let linePtr = 0;
54
54
  const lines = code.split('\n');
55
55
  const tokens = [];
@@ -84,16 +84,16 @@ function tokenize(code, grammar) {
84
84
  }
85
85
  return tokens;
86
86
  }
87
- async function default_1(code, fileName) {
87
+ export default async function (code, fileName) {
88
88
  if (!oniguruma) {
89
89
  // Lazily initialize the WebAssembly runtime
90
90
  oniguruma = initializeOniguruma();
91
- registry = new vscode_textmate_1.Registry({
91
+ registry = new TextMate.Registry({
92
92
  onigLib: oniguruma,
93
93
  loadGrammar: loadGrammar,
94
94
  });
95
95
  }
96
- const fileType = (0, path_1.extname)(fileName);
96
+ const fileType = extname(fileName);
97
97
  const grammarName = extnameToGrammar(fileType);
98
98
  if (!grammarName) {
99
99
  throw new Error(`Cannot find grammar for file type ${fileType}`);
@@ -104,4 +104,3 @@ async function default_1(code, fileName) {
104
104
  }
105
105
  return tokenize(code, grammar);
106
106
  }
107
- exports.default = default_1;
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.emitGitHubWarning = exports.dumpWarnings = exports.WarningMessages = void 0;
4
- const path_1 = require("path");
5
- exports.WarningMessages = {
1
+ import { relative } from 'path';
2
+ export const WarningMessages = {
6
3
  W_DYNAMIC_KEY: {
7
4
  name: 'Dynamic key',
8
5
  description: 'The key not static and cannot be extracted automatically. This key will be ignored.\n\nUse @tolgee-key or @tolgee-ignore to suppress this warning.',
@@ -42,7 +39,7 @@ exports.WarningMessages = {
42
39
  * @param extractionResult Extraction result to dump warnings from.
43
40
  * @returns Count of emitted warnings in the extraction.
44
41
  */
45
- function dumpWarnings(extractionResult) {
42
+ export function dumpWarnings(extractionResult) {
46
43
  let warningCount = 0;
47
44
  for (const [file, { warnings }] of extractionResult.entries()) {
48
45
  if (warnings.length) {
@@ -51,8 +48,8 @@ function dumpWarnings(extractionResult) {
51
48
  }
52
49
  console.error(file);
53
50
  for (const warning of warnings) {
54
- const warnText = warning.warning in exports.WarningMessages
55
- ? exports.WarningMessages[warning.warning].name
51
+ const warnText = warning.warning in WarningMessages
52
+ ? WarningMessages[warning.warning].name
56
53
  : warning.warning;
57
54
  console.error('\tline %d: %s', warning.line, warnText);
58
55
  emitGitHubWarning(warning.warning, file, warning.line);
@@ -65,13 +62,12 @@ function dumpWarnings(extractionResult) {
65
62
  }
66
63
  return warningCount;
67
64
  }
68
- exports.dumpWarnings = dumpWarnings;
69
- function emitGitHubWarning(warning, file, line) {
65
+ export function emitGitHubWarning(warning, file, line) {
70
66
  if (!process.env.GITHUB_ACTIONS)
71
67
  return;
72
- file = (0, path_1.relative)(process.env.GITHUB_WORKSPACE ?? process.cwd(), file);
73
- if (warning in exports.WarningMessages) {
74
- const { name, description } = exports.WarningMessages[warning];
68
+ file = relative(process.env.GITHUB_WORKSPACE ?? process.cwd(), file);
69
+ if (warning in WarningMessages) {
70
+ const { name, description } = WarningMessages[warning];
75
71
  const encodedDescription = description
76
72
  .replaceAll('%', '%25')
77
73
  .replaceAll('\r', '%0D')
@@ -84,4 +80,3 @@ function emitGitHubWarning(warning, file, line) {
84
80
  }
85
81
  console.log(`::warning file=${file},line=${line}::${warning}`);
86
82
  }
87
- exports.emitGitHubWarning = emitGitHubWarning;
@@ -1,16 +1,10 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.callWorker = void 0;
7
- const path_1 = require("path");
8
- const worker_threads_1 = require("worker_threads");
9
- const promises_1 = require("fs/promises");
10
- const extractor_1 = __importDefault(require("./extractor"));
11
- const moduleLoader_1 = require("../utils/moduleLoader");
12
- const deferred_1 = require("../utils/deferred");
13
- const IS_TS_NODE = (0, path_1.extname)(__filename) === '.ts';
1
+ import { resolve, extname } from 'path';
2
+ import { Worker, isMainThread, parentPort } from 'worker_threads';
3
+ import { readFile } from 'fs/promises';
4
+ import internalExtractor from './extractor.js';
5
+ import { loadModule } from '../utils/moduleLoader.js';
6
+ import { createDeferred } from '../utils/deferred.js';
7
+ const IS_TS_NODE = extname(import.meta.url) === '.ts';
14
8
  // --- Worker functions
15
9
  let loadedExtractor = Symbol('unloaded');
16
10
  let extractor;
@@ -18,30 +12,32 @@ async function handleJob(args) {
18
12
  if (loadedExtractor !== args.extractor) {
19
13
  loadedExtractor = args.extractor;
20
14
  extractor = args.extractor
21
- ? await (0, moduleLoader_1.loadModule)(args.extractor).then((mdl) => mdl.default)
22
- : extractor_1.default;
15
+ ? await loadModule(args.extractor).then((mdl) => mdl.default)
16
+ : internalExtractor;
23
17
  }
24
- const file = (0, path_1.resolve)(args.file);
25
- const code = await (0, promises_1.readFile)(file, 'utf8');
18
+ const file = resolve(args.file);
19
+ const code = await readFile(file, 'utf8');
26
20
  return extractor(code, file);
27
21
  }
28
22
  async function workerInit() {
29
- worker_threads_1.parentPort.on('message', (params) => {
23
+ parentPort.on('message', (params) => {
30
24
  handleJob(params)
31
- .then((res) => worker_threads_1.parentPort.postMessage({ data: res }))
32
- .catch((e) => worker_threads_1.parentPort.postMessage({ err: e }));
25
+ .then((res) => parentPort.postMessage({ data: res }))
26
+ .catch((e) => parentPort.postMessage({ err: e }));
33
27
  });
34
28
  }
35
- if (!worker_threads_1.isMainThread)
29
+ if (!isMainThread)
36
30
  workerInit();
37
31
  // --- Main thread functions
38
32
  let worker;
39
33
  const jobQueue = [];
40
34
  function createWorker() {
41
- const worker = new worker_threads_1.Worker(__filename, {
42
- // ts-node workaround
43
- execArgv: IS_TS_NODE ? ['--require', 'ts-node/register'] : undefined,
44
- });
35
+ const worker = IS_TS_NODE
36
+ ? new Worker(new URL(import.meta.url).pathname.replace('.ts', '.js'), {
37
+ // ts-node workaround
38
+ execArgv: ['--require', 'ts-node/register'],
39
+ })
40
+ : new Worker(new URL(import.meta.url).pathname);
45
41
  let timeout;
46
42
  let currentDeferred;
47
43
  function workOrDie() {
@@ -70,12 +66,11 @@ function createWorker() {
70
66
  workOrDie();
71
67
  return worker;
72
68
  }
73
- async function callWorker(params) {
74
- const deferred = (0, deferred_1.createDeferred)();
69
+ export async function callWorker(params) {
70
+ const deferred = createDeferred();
75
71
  jobQueue.push([params, deferred]);
76
72
  if (!worker) {
77
73
  worker = createWorker();
78
74
  }
79
75
  return deferred.promise;
80
76
  }
81
- exports.callWorker = callWorker;