Skip to content

Commit

Permalink
fix: handling glob paths incorrectly on Windows
Browse files Browse the repository at this point in the history
Fixes an issue with glob paths being handled incorrectly on Windows caused by a breaking change in the glob package which no longer allows backslashes as path separators.
  • Loading branch information
kleinfreund committed May 18, 2022
1 parent 9680c12 commit 18473cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/create-report/language-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import isValidGlob from 'is-valid-glob';
import { SimpleFile, I18NLanguage, I18NItem } from '../types';

export function readLanguageFiles (src: string): SimpleFile[] {
if (!isValidGlob(src)) {
// Replace backslash path segments to make the path work with the glob package.
// https://github.com/Spittal/vue-i18n-extract/issues/159
const normalizedSrc = src.replace(/\\/g, '/');
if (!isValidGlob(normalizedSrc)) {
throw new Error(`languageFiles isn't a valid glob pattern.`);
}

const targetFiles = glob.sync(src);
const targetFiles = glob.sync(normalizedSrc);

if (targetFiles.length === 0) {
throw new Error('languageFiles glob has no files.');
Expand Down
7 changes: 5 additions & 2 deletions src/create-report/vue-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import glob from 'glob';
import fs from 'fs';

export function readVueFiles (src: string): SimpleFile[] {
if (!isValidGlob(src)) {
// Replace backslash path segments to make the path work with the glob package.
// https://github.com/Spittal/vue-i18n-extract/issues/159
const normalizedSrc = src.replace(/\\/g, '/');
if (!isValidGlob(normalizedSrc)) {
throw new Error(`vueFiles isn't a valid glob pattern.`);
}

const targetFiles = glob.sync(src);
const targetFiles = glob.sync(normalizedSrc);

if (targetFiles.length === 0) {
throw new Error('vueFiles glob has no files.');
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/create-report/language-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import { readLanguageFiles, writeMissingToLanguageFiles, removeUnusedFromLanguag
import { expectedFromParsedLanguageFiles, expectedI18NReport } from '../../fixtures/expected-values';
import { languageFiles } from '../../fixtures/resolved-sources';

const languageFilesWithBackslashes = languageFiles.replace(/\//g, '\\');

describe('file: create-report/language-files', () => {
describe('function: parselanguageFiles', () => {
it('Parse the file glob into an I18NLanguage object', () => {
it.each([
languageFiles,
languageFilesWithBackslashes
])('Parse the file glob into an I18NLanguage object', (languageFiles) => {
const I18NLanguage = parselanguageFiles(languageFiles);
expect(I18NLanguage).toEqual(expectedFromParsedLanguageFiles);
});
Expand Down Expand Up @@ -48,4 +53,3 @@ describe('file: create-report/language-files', () => {
});
});
})

9 changes: 7 additions & 2 deletions tests/unit/create-report/vue-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { expectedFromParsedVueFiles } from '../../fixtures/expected-values';
import { vueFiles } from '../../fixtures/resolved-sources';
import path from 'path';

const vueFilesWithBackslashes = vueFiles.replace(/\//g, '\\');

describe('file: create-report/vue-files', () => {
describe('function: parseVueFiles', () => {
it('Parse the file glob into I18n items', () => {
const I18NItems = parseVueFiles(vueFiles);
it.each([
vueFiles,
vueFilesWithBackslashes
])('Parse the file glob into I18n items', (files) => {
const I18NItems = parseVueFiles(files);
expect(I18NItems).toEqual(expectedFromParsedVueFiles);
});

Expand Down

0 comments on commit 18473cb

Please sign in to comment.