All files / src/utils/backbase scope-utils.ts

95.55% Statements 43/45
90.9% Branches 20/22
100% Functions 6/6
95% Lines 38/40

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 842x       2x 2x             42x 42x     2x 28x 28x 3x   25x 25x     2x 3x 3x       3x 3x         3x     2x 11x 11x 2x   9x   9x     9x                   9x     2x 10x 10x 1x 9x 4x   5x     2x 8x     8x 8x 1x   8x    
import { Tree, SchematicsException } from '@angular-devkit/schematics';
 
import { CompilerOptions, MapLike } from 'typescript';
 
const PACKAGE_JSON = '/package.json';
const TSCONFIG = '/tsconfig.json';
 
interface TsConfig {
  compilerOptions?: CompilerOptions;
}
 
function readFile(host: Tree, path: string): string | undefined {
  const file = host.read(path);
  return file ? file.toString('utf-8') : undefined;
}
 
export const getDefaultNpmScope = (tree: Tree): string | undefined => {
  const packageJsonFile = readFile(tree, PACKAGE_JSON);
  if (!packageJsonFile) {
    return undefined;
  }
  const packageJson = JSON.parse(packageJsonFile);
  return packageJson.backbase && packageJson.backbase.defaultScope ? packageJson.backbase.defaultScope : undefined;
};
 
export const addScopeToPackageJson = (npmScope: string, tree: Tree): void => {
  const packageJsonFile = readFile(tree, PACKAGE_JSON);
  Iif (!packageJsonFile) {
    return;
  }
 
  const packageContent = JSON.parse(packageJsonFile);
  const newContent = {
    ...packageContent,
    backbase: { defaultScope: npmScope },
  };
 
  tree.overwrite(PACKAGE_JSON, JSON.stringify(newContent, null, 2));
};
 
export const addScopeToConfig = (npmScope: string, tree: Tree): void => {
  const tsConfigFile = readFile(tree, TSCONFIG);
  if (!tsConfigFile) {
    return;
  }
  const currentTsConfig: TsConfig = JSON.parse(tsConfigFile);
  const currentPaths: MapLike<string[]> =
    currentTsConfig.compilerOptions && currentTsConfig.compilerOptions.paths
      ? currentTsConfig.compilerOptions.paths
      : {};
  const newTsConfig: TsConfig = {
    ...currentTsConfig,
    compilerOptions: {
      ...currentTsConfig.compilerOptions,
      paths: {
        ...currentPaths,
        [`@${npmScope}/*`]: ['libs/*/public_api.ts'],
      },
    },
  };
  tree.overwrite(TSCONFIG, JSON.stringify(newTsConfig, null, 2));
};
 
export const getNpmScope = (npmScopeOption: string | undefined, tree: Tree): string => {
  const defaultNpmScope = getDefaultNpmScope(tree);
  if (!npmScopeOption && !defaultNpmScope) {
    throw new SchematicsException('No default NPM scope found. --npmScope is a required option');
  } else if (!npmScopeOption && defaultNpmScope) {
    return defaultNpmScope;
  }
  return npmScopeOption!;
};
 
export const setNpmScope = (scope: string, tree: Tree) => {
  Iif (!scope) {
    return;
  }
  const defaultNpmScope = getDefaultNpmScope(tree);
  if (!defaultNpmScope) {
    addScopeToPackageJson(scope, tree);
  }
  addScopeToConfig(scope, tree);
};