All files / builder/library setup-component-data.service.ts

100% Statements 32/32
75% Branches 9/12
100% Functions 4/4
100% Lines 31/31

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 981x 1x 1x       1x       1x 1x     1x   1x 2x     1x 1x 1x 1x             16x 16x 11x     5x 5x 5x     5x       5x 5x 5x 5x   5x                         5x                                     5x 5x     5x 5x               5x      
import { join, normalize, resolve, strings } from '@angular-devkit/core';
import { Inject, Injectable } from 'static-injector';
import { changeComponent } from '../component-template-inject/change-component';
import type { ExportLibraryComponentMeta } from '../library';
import { ResolvedDataGroup } from '../mini-program-compiler';
import { BuildPlatform } from '../platform/platform';
import {
  LIBRARY_COMPONENT_METADATA_SUFFIX,
  LIBRARY_OUTPUT_ROOTDIR,
} from './const';
import { getComponentOutputPath } from './get-library-path';
import { getUseComponents } from './merge-using-component-path';
import { OutputTemplateMetadataService } from './output-template-metadata.service';
import { CustomStyleSheetProcessor } from './stylesheet-processor';
import { ENTRY_POINT_TOKEN, RESOLVED_DATA_GROUP_TOKEN } from './token';
 
@Injectable()
export class SetupComponentDataService {
  constructor(
    @Inject(RESOLVED_DATA_GROUP_TOKEN)
    private dataGroup: ResolvedDataGroup,
    @Inject(ENTRY_POINT_TOKEN) private entryPoint: string,
    private addGlobalTemplateService: OutputTemplateMetadataService,
    private buildPlatform: BuildPlatform
  ) {}
  run(
    data: string,
    originFileName: string,
    customStyleSheetProcessor: CustomStyleSheetProcessor
  ) {
    const changedData = changeComponent(data);
    if (!changedData) {
      return data;
    }
    const useComponentPath =
      this.dataGroup.useComponentPath.get(originFileName)!;
    const componentClassName = changedData.componentName;
    const componentDirName = strings.dasherize(
      strings.camelize(componentClassName)
    );
    const libraryPath = getComponentOutputPath(
      this.entryPoint,
      componentClassName
    );
    const styleUrlList = this.dataGroup.style.get(originFileName);
    const styleContentList: string[] = [];
    styleUrlList?.forEach((item) => {
      styleContentList.push(customStyleSheetProcessor.styleMap.get(item)!);
    });
    const selfTemplateImportStr = this.dataGroup.otherMetaCollectionGroup[
      '$self'
    ]
      ? `<import src="${resolve(
          normalize('/'),
          join(
            normalize(LIBRARY_OUTPUT_ROOTDIR),
            this.entryPoint,
            'self' + this.buildPlatform.fileExtname.contentTemplate
          )
        )}"/>`
      : '';
 
    const insertComponentData: ExportLibraryComponentMeta = {
      id:
        strings.classify(this.entryPoint) +
        strings.classify(strings.camelize(componentDirName)),
      className: componentClassName,
      content:
        selfTemplateImportStr +
        this.dataGroup.outputContent.get(originFileName)!,
      libraryPath: libraryPath,
      useComponents: {
        ...getUseComponents(
          useComponentPath.libraryPath,
          useComponentPath.localPath,
          this.entryPoint
        ),
        ...this.addGlobalTemplateService.getSelfUseComponents(),
      },
      moduleId: this.entryPoint,
    };
    Eif (styleContentList.length) {
      insertComponentData.style = styleContentList.join('\n');
    }
 
    const list = changedData.content.split(/\n|\r\n/g);
    list.splice(
      Math.max(list.length - 1, 0),
      0,
      `let ${componentClassName}_${LIBRARY_COMPONENT_METADATA_SUFFIX}=${JSON.stringify(
        insertComponentData
      )}`
    );
 
    return list.join('\n');
  }
}