Skip to content

Commit

Permalink
🐛 Fix numeric keys being converted to arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
leolabs committed Aug 31, 2023
1 parent 37de55d commit 272092e
Show file tree
Hide file tree
Showing 4 changed files with 4,052 additions and 4,103 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"commander": "^6.1.0",
"deep-object-diff": "^1.1.0",
"dotenv": "^8.2.0",
"flattenjs": "^2.0.0",
"flat": "^5.0.2",
"html-entities": "^2.3.2",
"inquirer": "^7.3.3",
"lodash": "^4.17.20",
Expand All @@ -47,6 +47,7 @@
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/flat": "^5.0.2",
"@types/inquirer": "^7.3.1",
"@types/jest": "^26.0.14",
"@types/lodash": "^4.14.161",
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import chalk from 'chalk';
import commander from 'commander';
import * as flatten from 'flattenjs';
import { flatten, unflatten } from 'flat';
import * as fs from 'fs';
import { omit } from 'lodash';
import * as path from 'path';
Expand Down Expand Up @@ -421,9 +421,9 @@ function createTranslator(
);
let cacheDiff: string[] = [];
if (fs.existsSync(cachePath) && !fs.statSync(cachePath).isDirectory()) {
const cachedFile = flatten.convert(
const cachedFile = flatten(
JSON.parse(fs.readFileSync(cachePath).toString().trim()),
);
) as any;
const cDiff = diff(cachedFile, sourceFile.content);
cacheDiff = Object.keys(cDiff).filter((k) => cDiff[k]);
const changedItems = Object.keys(cacheDiff).length.toString();
Expand Down Expand Up @@ -471,7 +471,7 @@ function createTranslator(
const newContent =
JSON.stringify(
sourceFile.type === 'key-based'
? flatten.undo(translatedFile)
? unflatten(translatedFile, { object: true })
: translatedFile,
null,
2,
Expand Down
72 changes: 39 additions & 33 deletions src/util/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import { flatten } from 'flat';
import * as fs from 'fs';
import * as path from 'path';
import * as flatten from 'flattenjs';

export type FileType = 'key-based' | 'natural' | 'auto';

export type DirectoryStructure = 'default' | 'ngx-translate';

export interface TranslatableFile {
name: string,
originalContent: string,
type: FileType,
content: object,
name: string;
originalContent: string;
type: FileType;
content: object;
}

export const getAvailableLanguages = (directory: string, directoryStructure: DirectoryStructure) => {
export const getAvailableLanguages = (
directory: string,
directoryStructure: DirectoryStructure,
) => {
const directoryContent = fs.readdirSync(directory);

switch (directoryStructure) {
case 'default':
return directoryContent
.map(d => path.resolve(directory, d))
.filter(d => fs.statSync(d).isDirectory())
.map(d => path.basename(d));
.map((d) => path.resolve(directory, d))
.filter((d) => fs.statSync(d).isDirectory())
.map((d) => path.basename(d));

case 'ngx-translate':
return directoryContent
.filter(f => f.endsWith('.json'))
.map(f => f.slice(0, -5));
.filter((f) => f.endsWith('.json'))
.map((f) => f.slice(0, -5));
}
}
};

export const detectFileType = (json: any): FileType => {
const invalidKeys = Object.keys(json).filter(
k => typeof json[k] === 'string' && (k.includes('.') || k.includes(' ')),
(k) => typeof json[k] === 'string' && (k.includes('.') || k.includes(' ')),
);

return invalidKeys.length > 0 ? 'natural' : 'key-based';
Expand All @@ -43,28 +46,28 @@ export const loadTranslations = (
fileType: FileType = 'auto',
) =>
fs
.readdirSync(directory)
.filter(f => f.endsWith('.json'))
.map(f => {
const json = require(path.resolve(directory, f));
const type = fileType === 'auto' ? detectFileType(json) : fileType;

return {
name: f,
originalContent: json,
type,
content:
type === 'key-based'
? flatten.convert(require(path.resolve(directory, f)))
: require(path.resolve(directory, f)),
} as TranslatableFile;
});
.readdirSync(directory)
.filter((f) => f.endsWith('.json'))
.map((f) => {
const json = require(path.resolve(directory, f));
const type = fileType === 'auto' ? detectFileType(json) : fileType;

return {
name: f,
originalContent: json,
type,
content:
type === 'key-based'
? flatten(require(path.resolve(directory, f)), { safe: true })
: require(path.resolve(directory, f)),
} as TranslatableFile;
});

export const fixSourceInconsistencies = (
directory: string,
cacheDir: string,
) => {
const files = loadTranslations(directory).filter(f => f.type === 'natural');
const files = loadTranslations(directory).filter((f) => f.type === 'natural');

for (const file of files) {
const fixedContent = Object.keys(file.content).reduce(
Expand All @@ -84,13 +87,16 @@ export const fixSourceInconsistencies = (
}
};

export const evaluateFilePath =
(directory: string, dirStructure: DirectoryStructure, lang: string) => {
export const evaluateFilePath = (
directory: string,
dirStructure: DirectoryStructure,
lang: string,
) => {
switch (dirStructure) {
case 'default':
return path.resolve(directory, lang);

case 'ngx-translate':
return path.resolve(directory);
}
};
};
Loading

0 comments on commit 272092e

Please sign in to comment.