Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locat infrastructure improvments #907

Merged
merged 8 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions src/cmd/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
} from '../../steps';
import {prepareMapFile} from '../../steps/processMapFile';
import {copyFiles, logger} from '../../utils';
import {upload as publishFilesToS3} from '../../commands/publish/upload';

export const build = {
command: ['build', '$0'],
Expand Down Expand Up @@ -175,13 +174,13 @@
);
}

async function handler(args: Arguments<any>) {

Check warning on line 177 in src/cmd/build/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
const userOutputFolder = resolve(args.output);
const tmpInputFolder = resolve(args.output, TMP_INPUT_FOLDER);
const tmpOutputFolder = resolve(args.output, TMP_OUTPUT_FOLDER);

if (typeof VERSION !== 'undefined') {
console.log(`Using v${VERSION} version`);

Check warning on line 183 in src/cmd/build/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}

try {
Expand All @@ -192,12 +191,11 @@
output: tmpOutputFolder,
});
SearchService.init();
Includers.init([OpenapiIncluder as any]);

Check warning on line 194 in src/cmd/build/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type

const {
output: outputFolderPath,
outputFormat,
publish,
lintDisabled,
buildDisabled,
addMapFile,
Expand Down Expand Up @@ -245,30 +243,6 @@
if (glob.sync('.*', {cwd: tmpOutputFolder}).length) {
shell.cp('-r', join(tmpOutputFolder, '.*'), userOutputFolder);
}

if (publish) {
const DEFAULT_PREFIX = process.env.YFM_STORAGE_PREFIX ?? '';
const {
ignore = [],
storageRegion,
storageEndpoint: endpoint,
storageBucket: bucket,
storagePrefix: prefix = DEFAULT_PREFIX,
storageKeyId: accessKeyId,
storageSecretKey: secretAccessKey,
} = ArgvService.getConfig();

await publishFilesToS3({
input: userOutputFolder,
region: storageRegion,
ignore: [...ignore, TMP_INPUT_FOLDER, TMP_OUTPUT_FOLDER],
endpoint,
bucket,
prefix,
accessKeyId,
secretAccessKey,
});
}
}
} catch (err) {
logger.error('', err.message);
Expand Down
87 changes: 87 additions & 0 deletions src/commands/publish/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type {Run} from '../run';
import type {Mock} from 'vitest';
import type {PublishConfig} from '..';

import {expect, it, vi} from 'vitest';
import {Publish} from '..';
import {upload as originalUpload} from '../upload';

export const upload = originalUpload as Mock;

// eslint-disable-next-line no-var
var resolveConfig: Mock;

vi.mock('../upload');
vi.mock('~/config', async (importOriginal) => {
resolveConfig = vi.fn((_path, {defaults, fallback}) => {
return defaults || fallback;
});

return {
...((await importOriginal()) as {}),
resolveConfig,
};
});

export async function runPublish(args: string) {
const publish = new Publish();

publish.apply();

await publish.parse(['node', 'index'].concat(args.split(' ')));
}

type DeepPartial<T> = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[P in keyof T]?: T[P] extends Record<any, any> ? DeepPartial<T[P]> : T[P];
};

export function testConfig(name: string, args: string, result: DeepPartial<PublishConfig>): void;
export function testConfig(name: string, args: string, result: Error | string): void;
export function testConfig(
name: string,
args: string,
config: DeepPartial<PublishConfig>,
result: DeepPartial<PublishConfig>,
): void;
export function testConfig(
name: string,
args: string,
config: DeepPartial<PublishConfig>,
result: Error | string,
): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function testConfig(name: string, args: string, config: any, result?: any): void {
it(name, async () => {
if (!result) {
result = config;
config = {};
}

resolveConfig.mockImplementation((_path, {defaults}) => {
return {
...defaults,
...config,
};
});

upload.mockImplementation((run: Run) => {
expect(run.config).toMatchObject(result as Partial<PublishConfig>);
});

try {
await runPublish('--input ./input --access-key-id 1 --secret-access-key 1 ' + args);
expect(upload).toBeCalled();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
const message = error.message || error;
if (result instanceof Error) {
expect(message).toEqual(result.message);
} else if (typeof result === 'string') {
expect(message).toEqual(result);
} else {
throw error;
}
}
});
}
2 changes: 1 addition & 1 deletion src/commands/publish/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Run {
});

this.logger = new Logger(config, [
(level, message) => message.replace(new RegExp(this.root, 'ig'), ''),
(_level, message) => message.replace(new RegExp(this.root, 'ig'), ''),
]);
}

Expand Down
8 changes: 2 additions & 6 deletions src/commands/translate/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export async function runTranslate(args: string) {
return translate;
}

type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Record<any, any> ? DeepPartial<T[P]> : T[P];
};

export function testConfig<Config = TranslateConfig>(defaultArgs: string) {
function _testConfig(name: string, args: string, result: DeepPartial<Config>): void;
function _testConfig(name: string, args: string, result: Error | string): void;
Expand All @@ -48,6 +44,7 @@ export function testConfig<Config = TranslateConfig>(defaultArgs: string) {
config: DeepPartial<Config>,
result: Error | string,
): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _testConfig(name: string, args: string, config: any, result?: any): void {
it(name, async () => {
if (!result) {
Expand All @@ -63,9 +60,8 @@ export function testConfig<Config = TranslateConfig>(defaultArgs: string) {
});

if (result instanceof Error || typeof result === 'string') {
const message = result.message || result;
await expect(async () => runTranslate(defaultArgs + ' ' + args)).rejects.toThrow(
message,
result,
);
} else {
const instance = await runTranslate(defaultArgs + ' ' + args);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/translate/commands/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ export class Compose
this.logger.compose(file.path);
await configuredPipeline(file);
this.logger.composed(file.path);
} catch (error: any) {
} catch (error: unknown) {
if (error instanceof TranslateError) {
this.logger.composeError(file.path, `${error.message}`);

if (error.fatal) {
process.exit(1);
}
} else {
this.logger.error(file.path, error.message);
this.logger.error(file.path, error);
}
}
}),
Expand Down
10 changes: 5 additions & 5 deletions src/commands/translate/commands/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type ExtractArgs = ProgramArgs & {
target?: string | string[];
include?: string[];
exclude?: string[];
vars?: Record<string, any>;
vars?: Hash;
useExperimentalParser?: boolean;
};

Expand All @@ -45,7 +45,7 @@ export type ExtractConfig = Pick<ProgramConfig, 'input' | 'strict' | 'quiet'> &
exclude: string[];
files: string[];
skipped: [string, string][];
vars: Record<string, any>;
vars: Hash;
useExperimentalParser?: boolean;
};

Expand Down Expand Up @@ -154,7 +154,7 @@ export class Extract
this.logger.extract(file);
await configuredPipeline(file);
this.logger.extracted(file);
} catch (error: any) {
} catch (error: unknown) {
if (error instanceof TranslateError) {
if (error instanceof SkipTranslation) {
this.logger.skipped([[error.reason, file]]);
Expand All @@ -167,7 +167,7 @@ export class Extract
process.exit(1);
}
} else {
this.logger.error(file, error.message);
this.logger.error(file, error);
}
}
}),
Expand All @@ -181,7 +181,7 @@ export type PipelineParameters = {
output: string;
source: ExtractOptions['source'];
target: ExtractOptions['target'];
vars: Record<string, any>;
vars: Hash;
useExperimentalParser?: boolean;
};

Expand Down
12 changes: 9 additions & 3 deletions src/commands/translate/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('Translate command', () => {
);

test(
'should handle args with priority',
'should handle args with priority',
'--source ru',
{
// @ts-ignore
Expand Down Expand Up @@ -312,8 +312,14 @@ describe('Translate command', () => {
});

it('should call provider translate with config', async () => {
const instance = await run('-o output --folder 1');
const instance = await run('-o output --folder 1 --source ru --target en --auth y0_1');

expect(instance.provider?.translate).toBeCalledWith(expect.objectContaining({}));
expect(instance.provider?.translate).toBeCalledWith(
expect.anything(),
expect.objectContaining({
input: expect.stringMatching(/^(\/|[A-Z]:\\).*?/),
output: expect.stringMatching(/^(\/|[A-Z]:\\).*?/),
}),
);
});
});
4 changes: 2 additions & 2 deletions src/commands/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
target?: string | string[];
include?: string[];
exclude?: string[];
vars?: Record<string, any>;
vars?: Hash;
};

export type TranslateConfig = Pick<ProgramConfig, 'input' | 'strict' | 'quiet'> & {
Expand All @@ -55,7 +55,7 @@
exclude: string[];
files: string[];
skipped: [string, string][];
vars: Record<string, any>;
vars: Hash;
dryRun: boolean;
};

Expand Down Expand Up @@ -166,7 +166,7 @@
});
}

async action() {

Check warning on line 169 in src/commands/translate/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Expected to return a value at the end of async method 'action'
if (this.provider) {
await this.provider.skip(this.config.skipped, this.config);

Expand Down
19 changes: 10 additions & 9 deletions src/commands/translate/providers/yandex/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {cyan, gray} from 'chalk';
import {option, trim} from '~/config';
import {dedent} from 'ts-dedent';

import {option} from '~/config';

const folder = option({
flags: '--folder <value>',
Expand All @@ -9,14 +11,13 @@ const folder = option({
`,
});

const configExample = trim(
gray(`
glossaryPairs:
- sourceText: string
translatedText: string
- sourceText: string
translatedText: string`),
);
const configExample = gray(dedent`
glossaryPairs:
- sourceText: string
translatedText: string
- sourceText: string
translatedText: string
`);

const glossary = option({
flags: '--glossary <path>',
Expand Down
16 changes: 12 additions & 4 deletions src/commands/translate/providers/yandex/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {IProgram} from '~/program';
import type {TranslateArgs, TranslateConfig} from '~/commands/translate';
import {ok} from 'assert';
import {resolve} from 'node:path';
import {Translate} from '~/commands/translate';
import {defined, resolveConfig} from '~/config';
import {Provider} from './provider';
Expand Down Expand Up @@ -68,11 +69,18 @@ export class Extension {
ok(config.auth, 'Required param auth is not configured');
ok(config.folder, 'Required param folder is not configured');

const glossary = defined('glossary', args, config);
if (glossary) {
const glossaryConfig = await resolveConfig(config.glossary, {
if (defined('glossary', args, config)) {
let glossary: AbsolutePath;
if (defined('glossary', args)) {
glossary = resolve(args.input, args.glossary);
} else {
glossary = config.resolve(config.glossary);
}

const glossaryConfig = await resolveConfig(glossary, {
defaults: {glossaryPairs: []},
});

config.glossaryPairs = glossaryConfig.glossaryPairs || [];
} else {
config.glossaryPairs = [];
Expand All @@ -83,7 +91,7 @@ export class Extension {

const provider = new Provider(config);

provider.logger.pipe(program.logger);
provider.pipe(program.logger);

return provider;
});
Expand Down
4 changes: 4 additions & 0 deletions src/commands/translate/providers/yandex/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
this.logger = new TranslateLogger(config);
}

pipe(logger: Logger) {
this.logger.pipe(logger);
}

async skip(skipped: [string, string][]) {
this.logger.skipped(skipped);
}
Expand Down Expand Up @@ -95,7 +99,7 @@
output: string;
sourceLanguage: string;
targetLanguage: string;
vars: Record<string, any>;

Check warning on line 102 in src/commands/translate/providers/yandex/provider.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
// yandexCloudTranslateGlossaryPairs: YandexCloudTranslateGlossaryPair[];
};

Expand Down Expand Up @@ -203,7 +207,7 @@
}),
);

return data.translations.map(({text}) => text).forEach(resolve);

Check warning on line 210 in src/commands/translate/providers/yandex/provider.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Async function expected no return value
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error instanceof AxiosError) {
Expand All @@ -221,8 +225,8 @@
throw new RequestError(status, statusText, data);
}
} else {
console.error(error.code);

Check warning on line 228 in src/commands/translate/providers/yandex/provider.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
console.error(error.cause);

Check warning on line 229 in src/commands/translate/providers/yandex/provider.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/translate/providers/yandex/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TranslateError} from '../../../utils';

export class RequestError extends TranslateError {
static canRetry(error: any) {
static canRetry(error: unknown) {
if (error instanceof RequestError) {
switch (true) {
case error.status === 429:
Expand Down
2 changes: 1 addition & 1 deletion src/commands/translate/providers/yandex/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export {LimitExceed, RequestError, AuthError} from './errors';
export class Defer<T = string> {
resolve!: (text: T) => void;

reject!: (error: any) => void;
reject!: (error: unknown) => void;

promise: Promise<T>;

Expand Down
Loading
Loading