All files / lib/parsers i18n.json.parser.ts

97.06% Statements 66/68
90.91% Branches 20/22
100% Functions 15/15
96.97% Lines 64/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 1555x 5x 5x 5x 5x 5x 5x 5x   5x           5x 5x 5x 5x               5x         10x     10x       10x   10x 10x   10x 1x     2x           4x 1x         13x 1x   2x     12x       13x 1x   2x     12x       15x   15x   15x       15x           15x   15x       15x 32x     47x 47x     15x 49x   49x       49x 15x     49x   49x   49x   49x 205x 222x 222x           15x       30x 30x 64x         10x   10x 10x 1x     10x   5x  
import { I18nParser } from './i18n.parser';
import { I18N_PARSER_OPTIONS } from '../i18n.constants';
import { Inject, OnModuleDestroy } from '@nestjs/common';
import * as path from 'path';
import * as fs from 'fs';
import { getDirectories, getFiles } from '../utils/file';
import * as flat from 'flat';
import { promisify } from 'util';
import { I18nTranslation } from '../interfaces/i18n-translation.interface';
import {
  Observable,
  Subject,
  merge as ObservableMerge,
  from as ObservableFrom,
} from 'rxjs';
import * as chokidar from 'chokidar';
import { switchMap } from 'rxjs/operators';
const readFile = promisify(fs.readFile);
const exists = promisify(fs.exists);
 
export interface I18nJsonParserOptions {
  path: string;
  filePattern?: string;
  watch?: boolean;
}
 
const defaultOptions: Partial<I18nJsonParserOptions> = {
  filePattern: '*.json',
  watch: false,
};
 
export class I18nJsonParser extends I18nParser implements OnModuleDestroy {
  private watcher?: chokidar.FSWatcher;
 
  private events: Subject<string> = new Subject();
 
  constructor(
    @Inject(I18N_PARSER_OPTIONS)
    private options: I18nJsonParserOptions,
  ) {
    super();
    this.options = this.sanitizeOptions(options);
 
    if (this.options.watch) {
      this.watcher = chokidar
        .watch(this.options.path, { ignoreInitial: true })
        .on('all', event => {
          this.events.next(event);
        });
    }
  }
 
  async onModuleDestroy() {
    if (this.watcher) {
      await this.watcher.close();
    }
  }
 
  async languages(): Promise<string[] | Observable<string[]>> {
    if (this.options.watch) {
      return ObservableMerge(
        ObservableFrom(this.parseLanguages()),
        this.events.pipe(switchMap(() => this.parseLanguages())),
      );
    }
    return this.parseLanguages();
  }
 
  async parse(): Promise<I18nTranslation | Observable<I18nTranslation>> {
    if (this.options.watch) {
      return ObservableMerge(
        ObservableFrom(this.parseTranslations()),
        this.events.pipe(switchMap(() => this.parseTranslations())),
      );
    }
    return this.parseTranslations();
  }
 
  private async parseTranslations(): Promise<I18nTranslation> {
    const i18nPath = path.normalize(this.options.path + path.sep);
 
    const translations: I18nTranslation = {};
 
    Iif (!(await exists(i18nPath))) {
      throw new Error(`i18n path (${i18nPath}) cannot be found`);
    }
 
    Iif (!this.options.filePattern.match(/\*\.[A-z]+/)) {
      throw new Error(
        `filePattern should be formatted like: *.json, *.txt, *.custom etc`,
      );
    }
 
    const languages = await this.parseLanguages();
 
    const pattern = new RegExp(
      '.' + this.options.filePattern.replace('.', '.'),
    );
 
    const files = await [
      ...languages.map(l => path.join(i18nPath, l)),
      i18nPath,
    ].reduce(async (files, path) => {
      (await files).push(...(await getFiles(path, pattern)));
      return files;
    }, Promise.resolve([]));
 
    for (const file of files) {
      let global = false;
 
      const key = path
        .dirname(path.relative(i18nPath, file))
        .split(path.sep)[0];
 
      if (key === '.') {
        global = true;
      }
 
      const data = JSON.parse(await readFile(file, 'utf8'));
 
      const prefix = path.basename(file).split('.')[0];
 
      const flatData = flat.flatten(data);
 
      for (const property of Object.keys(flatData)) {
        [...(global ? languages : [key])].forEach(lang => {
          translations[lang] = !!translations[lang] ? translations[lang] : {};
          translations[lang][`${global ? '' : `${prefix}.`}${property}`] =
            flatData[property];
        });
      }
    }
 
    return translations;
  }
 
  private async parseLanguages(): Promise<string[]> {
    const i18nPath = path.normalize(this.options.path + path.sep);
    return (await getDirectories(i18nPath)).map(dir =>
      path.relative(i18nPath, dir),
    );
  }
 
  private sanitizeOptions(options: I18nJsonParserOptions) {
    options = { ...defaultOptions, ...options };
 
    options.path = path.normalize(options.path + path.sep);
    if (!options.filePattern.startsWith('*.')) {
      options.filePattern = '*.' + options.filePattern;
    }
 
    return options;
  }
}