Skip to content

Commit

Permalink
fix: glob pattern in push + tests (#134)
Browse files Browse the repository at this point in the history
* fix: glob pattern in push + tests

* fix: glob pattern in push + tests
  • Loading branch information
stepan662 authored Dec 3, 2024
1 parent afaa272 commit 8414b49
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { BaseOptions } from '../options.js';
import { extname, join } from 'path';
import { readdir, readFile, stat } from 'fs/promises';
import { Command, Option } from 'commander';
import { glob } from 'glob';

import {
loading,
Expand All @@ -17,6 +16,7 @@ import { askString } from '../utils/ask.js';
import { mapImportFormat } from '../utils/mapImportFormat.js';
import { TolgeeClient, handleLoadableError } from '../client/TolgeeClient.js';
import { BodyOf } from '../client/internal/schema.utils.js';
import { windowsCompatibleGlob } from '../utils/windowsCompatibleGlob.js';

type ImportRequest = BodyOf<
'/v2/projects/{projectId}/single-step-import',
Expand Down Expand Up @@ -46,7 +46,7 @@ type PushOptions = BaseOptions & {

async function allInPattern(pattern: string) {
const files: File[] = [];
const items = await glob(pattern);
const items = await windowsCompatibleGlob(pattern);
for (const item of items) {
if ((await stat(item)).isDirectory()) {
files.push(...(await readDirectory(item)));
Expand Down
1 change: 1 addition & 0 deletions src/config/tolgeerc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function parseConfig(input: Schema, configDir: string): Schema {
}));
}

// convert relative paths in config to absolute
if (rc.pull?.path !== undefined) {
rc.pull.path = resolve(configDir, rc.pull.path);
}
Expand Down
5 changes: 2 additions & 3 deletions src/extractor/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import type {
ParserType,
VerboseOption,
} from './index.js';
import { glob } from 'glob';
import { extname } from 'path';

import { callWorker } from './worker.js';
import { exitWithError } from '../utils/logger.js';
import { windowsCompatibleGlob } from '../utils/windowsCompatibleGlob.js';

export const NullNamespace = Symbol('namespace.null');

Expand Down Expand Up @@ -94,9 +94,8 @@ export async function extractKeysOfFiles(opts: Opts) {
exitWithError("Missing '--patterns' or 'config.patterns' option");
}

const files = await glob(opts.patterns, {
const files = await windowsCompatibleGlob(opts.patterns, {
nodir: true,
windowsPathsNoEscape: process.platform === 'win32',
});

if (files.length === 0) {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/windowsCompatibleGlob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { glob, GlobOptionsWithFileTypesUnset } from 'glob';

export const windowsCompatibleGlob = (
pattern: string | string[],
options?: GlobOptionsWithFileTypesUnset | undefined
) => {
return glob(pattern, {
...options,
windowsPathsNoEscape: process.platform === 'win32',
});
};
3 changes: 3 additions & 0 deletions test/__fixtures__/validTolgeeRc/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": "Test"
}
3 changes: 3 additions & 0 deletions test/__fixtures__/validTolgeeRc/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function App() {
return null;
}
10 changes: 6 additions & 4 deletions test/unit/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { fileURLToPath } from 'url';
import { MockInstance, vi } from 'vitest';

import loadTolgeeRc from '#cli/config/tolgeerc.js';
import { windowsCompatibleGlob } from '#cli/utils/windowsCompatibleGlob.js';

const FIXTURES_PATH = new URL('../__fixtures__/', import.meta.url);

Expand Down Expand Up @@ -73,13 +74,14 @@ describe('.tolgeerc', () => {
const cfg = (await loadTolgeeRc(path))!;

// make sure all paths in the config file are expanded relatively to the config location
[
for (const path of [
cfg.extractor,
...cfg.patterns!,
...cfg.push!.files!.map((f) => f.path),
].forEach((path) => {
expect(path).toMatch(/[/\\]validTolgeeRc[/\\]/);
});
]) {
expect(path).toMatch(/[^/\\][/\\]validTolgeeRc[/\\][^/\\]/);
expect(await windowsCompatibleGlob(path!)).length.above(0);
}
});

it('converts projectId to number', async () => {
Expand Down

0 comments on commit 8414b49

Please sign in to comment.