Skip to content

Commit

Permalink
Merge pull request #6 from nutgaard/fix/no-exit-on-fix
Browse files Browse the repository at this point in the history
fix/no exit on fix
  • Loading branch information
nutgaard authored Nov 15, 2023
2 parents 5c9ea55 + f692d96 commit fac3d5d
Show file tree
Hide file tree
Showing 10 changed files with 566 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/builder/compile-intl-text-bundles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface IntlTextBundle {
type Formatter = (content: IntlTextBundle) => string;
const formatMap: Record<Format, Formatter> = {
json: jsonFormatter,
jsonlut: jsonLutFormatter,
script: scriptFormatter,
formatjs: formatjsFormatter,
};
Expand All @@ -24,6 +25,28 @@ export function compileIntlTextBundles(files: IntlFile[], format: Format): [stri
function jsonFormatter(content: IntlTextBundle): string {
return JSON.stringify(content, null, 2);
}

function jsonLutFormatter(content: IntlTextBundle): string {
const out = {};
const entries = Object.entries(content);
for (const [key, value] of entries) {
const parts = key.split('/');
let current: Record<string, any> = out;

for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const isLast = i === parts.length - 1;
if (isLast) {
current[part] = value;
} else {
current[part] = current[part] ?? {};
current = current[part];
}
}
}

return JSON.stringify(out, null, 2);
}
function scriptFormatter(content: IntlTextBundle): string {
return `const texts = ${jsonFormatter(content)};\n\nexport default texts;`;
}
Expand Down
6 changes: 4 additions & 2 deletions src/commands/fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { getFilesystem } from '../utils/get-filesystem';
import { FixOptions } from '../config';

export default class Fix {
static async run(logger: Logger, config: FixOptions) {
static async run(logger: Logger, config: FixOptions, exitOnSuccess: boolean) {
const files = getIntlFiles(config.srcDir);
const result = validateStructure(files);
if (!result.error) {
console.log('No errors found');
process.exit(0);
if (exitOnSuccess) {
process.exit(0);
}
}
result.printLogs();
console.log('Attempting to fix errors...');
Expand Down
2 changes: 1 addition & 1 deletion src/commands/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Watch {
process.stdin.on('keypress', (ch: string, key: Key) => {
if (key.ctrl && key.name === 'c') process.exit(0);
if (key.name === 'f') {
Fix.run(logger, config);
Fix.run(logger, config, false);
}
});
process.stdin.setRawMode(true);
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const validFormats = ['script', 'json', 'formatjs'];
export type Format = 'script' | 'json' | 'formatjs';
export const validFormats = ['script', 'json', 'jsonlut', 'formatjs'];
export type Format = 'script' | 'json' | 'jsonlut' | 'formatjs';

export interface BuildOptions {
srcDir: string;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ program
.command('fix', 'Attempts to fix validation issues by creating missing files')
.argument('<srcDir>', 'source folder of your i18n files')
.action(({ logger, args, options }) => {
FixCmd.run(logger, { ...args, ...options } as unknown as FixOptions);
FixCmd.run(logger, { ...args, ...options } as unknown as FixOptions, true);
});

program.run();
39 changes: 39 additions & 0 deletions test/build-volume-from-fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'fs';
import path from 'path';
import { NestedDirectoryJSON } from 'memfs/lib/volume';

export function buildVolumeFromFs(directory: string): NestedDirectoryJSON {
const directoryJson: NestedDirectoryJSON = {};

walkDirectory(directory, (file) => {
const relativeFilePath = path.relative(directory, file);
const parts = relativeFilePath.split(path.sep);

let jsonObject: NestedDirectoryJSON = directoryJson;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const isLast = i === parts.length - 1;

if (isLast) {
jsonObject[part] = fs.readFileSync(file, 'utf-8');
} else {
jsonObject[part] = jsonObject[part] ?? {};
jsonObject = jsonObject[part] as NestedDirectoryJSON;
}
}
});

return directoryJson;
}

function walkDirectory(directory: string, callback: (file: string) => void) {
fs.readdirSync(directory).forEach((file) => {
const dirPath = path.join(directory, file);
const isDirectory = fs.statSync(dirPath).isDirectory();
if (isDirectory) {
walkDirectory(dirPath, callback);
} else {
callback(dirPath);
}
});
}
Loading

0 comments on commit fac3d5d

Please sign in to comment.