All files / builder/library output-template-metadata.service.ts

71.42% Statements 30/42
64.28% Branches 9/14
87.5% Functions 7/8
70.73% Lines 29/41

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 1081x 1x     1x 1x 1x             1x 2x       1x   1x 1x       1x 1x         1x     1x 1x     1x 1x 1x     1x               1x   1x         1x 1x                                                             5x   1x   1x     1x         1x   5x      
import { join, normalize, resolve } from '@angular-devkit/core';
import { Inject, Injectable } from 'static-injector';
import ts from 'typescript';
import { MetaCollection, ResolvedDataGroup } from '../mini-program-compiler';
import { GLOBAL_TEMPLATE_SUFFIX, LIBRARY_OUTPUT_ROOTDIR } from './const';
import { getUseComponents } from './merge-using-component-path';
import {
  ENTRY_FILE_TOKEN,
  ENTRY_POINT_TOKEN,
  RESOLVED_DATA_GROUP_TOKEN,
} from './token';
import { ExtraTemplateData } from './type';
 
@Injectable()
export class OutputTemplateMetadataService {
  private selfUseComponents!: Record<string, string>;
  private selfMetaCollection!: MetaCollection;
  constructor(
    @Inject(ENTRY_FILE_TOKEN) private entryFile: string,
    @Inject(RESOLVED_DATA_GROUP_TOKEN)
    private dataGroup: ResolvedDataGroup,
    @Inject(ENTRY_POINT_TOKEN) private entryPoint: string
  ) {}
 
  run(fileName: string, data: string, sourceFile: ts.SourceFile) {
    const list = data.split(/\n|\r\n/g);
    list.splice(
      list.length - 1,
      0,
      `${this.getSelfTemplate()};${this.getLibraryTemplate()}`
    );
    return list.join('\n');
  }
  private getSelfTemplate() {
    const selfMetaCollection = this.dataGroup.otherMetaCollectionGroup['$self'];
    Iif (!selfMetaCollection) {
      return '';
    }
    this.selfMetaCollection = selfMetaCollection;
    const templateStr = selfMetaCollection.templateList
      .map((item) => item.content)
      .join('');
 
    const extraTemplateData: ExtraTemplateData = {
      template: templateStr,
      outputPath: resolve(
        normalize('/'),
        join(normalize(LIBRARY_OUTPUT_ROOTDIR), this.entryPoint, 'self')
      ),
    };
 
    delete this.dataGroup.otherMetaCollectionGroup['$self'];
 
    return `let $self_${GLOBAL_TEMPLATE_SUFFIX}=${JSON.stringify(
      extraTemplateData
    )}`;
  }
  private getLibraryTemplate() {
    Eif (!Object.keys(this.dataGroup.otherMetaCollectionGroup).length) {
      return '';
    }
    const obj: Record<string, ExtraTemplateData> = {};
    for (const key in this.dataGroup.otherMetaCollectionGroup) {
      if (
        Object.prototype.hasOwnProperty.call(
          this.dataGroup.otherMetaCollectionGroup,
          key
        )
      ) {
        const element = this.dataGroup.otherMetaCollectionGroup[key];
        const templateStr = element.templateList
          .map((item) => item.content)
          .join('');
 
        const useComponents = getUseComponents(
          Array.from(element.libraryPath),
          Array.from(element.localPath),
          this.entryPoint
        );
        const extraTemplateData: ExtraTemplateData = {
          template: templateStr,
          useComponents: useComponents,
        };
        obj[key] = extraTemplateData;
      }
    }
    return `let library_${GLOBAL_TEMPLATE_SUFFIX}=${JSON.stringify(obj)}`;
  }
 
  getSelfUseComponents() {
    if (!this.selfUseComponents) {
      const selfMetaCollection =
        this.selfMetaCollection ||
        this.dataGroup.otherMetaCollectionGroup['$self'];
      Iif (!selfMetaCollection) {
        return {};
      }
      const useComponents = getUseComponents(
        Array.from(selfMetaCollection.libraryPath),
        Array.from(selfMetaCollection.localPath),
        this.entryPoint
      );
      this.selfUseComponents = useComponents;
    }
    return this.selfUseComponents;
  }
}