All files / i18n index.ts

92.86% Statements 39/42
62.5% Branches 5/8
100% Functions 10/10
92.86% Lines 39/42

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  1x 1x 1x 1x 1x   1x 4x 4x                       4x 4x               4x 4x 4x 1x     3x 3x       3x     3x           3x   3x 3x         4x 3x 3x       3x   3x 3x 3x 3x   3x 3x         4x 3x 3x       3x   3x 3x   3x 3x      
import {Schema as I18NOptions} from './schema';
import {apply, chain, MergeStrategy, mergeWith, move, Rule, SchematicsException, Tree, url} from '@angular-devkit/schematics';
import * as CJSON from 'comment-json';
import {extraDevDependencies} from './dependencies';
import {CodeUtils} from '../utils/code-utils';
import {installDeps, sortByKey, YangUtils} from '../utils/yang-utils';
 
export default function (options: I18NOptions): Rule {
  return () => {
    return chain([
      addFiles(),
      updatePackageJson(),
      updateCore(),
      updateShared(),
      installDeps(options.skipInstall)
    ]);
  };
}
 
 
function addFiles(): (tree: Tree) => Rule {
  return (tree: Tree) => {
    return mergeWith(apply(url('./files'), [
      move(''),
    ]), MergeStrategy.Overwrite);
  }
}
 
 
function updatePackageJson(): (tree: Tree) => Tree {
  return (tree: Tree) => {
    const filePath = 'package.json';
    if (!tree.exists(filePath)) {
      throw new SchematicsException(`File ${filePath} does not exist.`);
    }
 
    const source = tree.read(filePath);
    Iif (!source) {
      throw new SchematicsException(`File ${filePath} is empty.`);
    }
 
    const json = CJSON.parse(source.toString('utf-8'));
 
    // Add dependencies
    json.dependencies = {
      ...json.dependencies,
      ...extraDevDependencies
    };
 
    // Sort dependencies by name
    json.dependencies = sortByKey(json.dependencies);
 
    tree.overwrite(filePath, CJSON.stringify(json, null, 2));
    return tree;
  };
}
 
function updateCore(): (tree: Tree) => Tree {
  return (tree: Tree) => {
    const filePath = YangUtils.CORE_MODULE_FILE;
    Iif (!tree.exists(filePath)) {
      throw new SchematicsException(`File ${filePath} does not exist.`);
    }
 
    const sourceFile = CodeUtils.readSourceFile(tree, filePath);
 
    CodeUtils.addImport(sourceFile, 'I18NModule', './i18n.module');
    CodeUtils.addImport(sourceFile, 'init as i18nInitializer', './i18n.initializer');
    CodeUtils.insertInVariableArray(sourceFile, 'INITIALIZERS', 'i18nInitializer');
    CodeUtils.insertInVariableArray(sourceFile, 'MODULES', 'I18NModule');
 
    CodeUtils.writeSourceFile(tree, filePath, sourceFile);
    return tree;
  };
}
 
function updateShared(): (tree: Tree) => Tree {
  return (tree: Tree) => {
    const filePath = YangUtils.SHARED_MODULE_FILE;
    Iif (!tree.exists(filePath)) {
      throw new SchematicsException(`File ${filePath} does not exist.`);
    }
 
    const sourceFile = CodeUtils.readSourceFile(tree, filePath);
 
    CodeUtils.addImport(sourceFile, 'TranslateModule', '@ngx-translate/core');
    CodeUtils.insertInVariableArray(sourceFile, 'MODULES', 'TranslateModule');
 
    CodeUtils.writeSourceFile(tree, filePath, sourceFile);
    return tree;
  };
}