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 | 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 14x 2x 16x 14x 1x 12x 14x 12x 1x 12x 14x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 16x 16x 16x 16x 12x 14x 14x 14x 14x 14x 14x 14x 16x 16x 16x 16x 16x 16x 16x 14x 12x 12x 12x 12x 12x 12x 12x | import { Tree, SchematicsException, Rule } from '@angular-devkit/schematics';
import { join, normalize } from '@angular-devkit/core';
import { Project, IndentationText, QuoteKind, SourceFile } from 'ts-morph';
import { BundleConfig, ModuleDefinition, Writer } from './types';
import { removeModuleImports, updateImportConfig, removeUnusedImports } from './utils/imports';
import { getWorkspace } from '../utils/backbase/package-utils';
const BBCoreModuleName = 'BackbaseCoreModule';
const bundleDefinitionExport = 'bundlesDefinition';
const createSourceFile = (filePath: string, sourceFileText: string): SourceFile =>
new Project({
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
quoteKind: QuoteKind.Single,
},
useInMemoryFileSystem: true,
}).createSourceFile(filePath, sourceFileText);
const bundleContent = (path: string, moduleName: string, imports: Map<string, Set<string>>, ngModules: Set<string>) => {
const importStatements = [...imports.entries()]
.sort(([a], [b]) => (a < b ? -1 : 1))
.map(([importPath, modules]) => `import { ${[...modules.values()].sort().join(', ')}} from ${importPath};`);
return `// ${path}
import { NgModule } from '@angular/core';
${importStatements.join('\n')}
@NgModule({
imports: [${[...ngModules].join(', ')}],
})
export class ${moduleName} {}
`;
};
const configContent = (bundles: Map<string, { file: string; components: Set<string> }>) => {
const definitions = [...bundles.entries()].map(
([bundleModule, { file, components }]) => `{
components: ['${[...components].join(`', '`)}'],
loadChildren: () => import('../bundles/${file}').then(m => m.${bundleModule}),
},`,
);
return `import { LazyConfig } from '@backbase/foundation-ang/core';
export const ${bundleDefinitionExport}: LazyConfig = [
${definitions.join('\n ')}
];
`;
};
export const writer: Writer = (
bundlesConfig: Array<BundleConfig>,
moduleDefinition: ModuleDefinition,
options: Partial<{ project: string }> = {},
): Rule => {
let bundleNumber = 1;
const generateBundleHash = () => bundleNumber++;
return async (tree: Tree): Promise<void> => {
const workspace = await getWorkspace(tree);
const projectName = options.project || workspace.extensions.defaultProject;
Iif (!projectName) {
throw new SchematicsException(`Couldn't find a default project`);
}
const project = workspace.projects.get(projectName);
Iif (!project?.sourceRoot) {
throw new SchematicsException('Project missing required "sourceRoot" property in angular.json');
}
const appModuleFilePath = join(normalize(project.sourceRoot), 'app', 'app.module.ts');
const existingAppModule = tree.read(appModuleFilePath);
Iif (!existingAppModule) {
throw new SchematicsException(`App module not found at ${appModuleFilePath}`);
}
const appModuleFile = createSourceFile(appModuleFilePath, existingAppModule.toString('utf-8'));
const bundles = new Map<string, { file: string; components: Set<string> }>();
const bundledModules = new Set<string>();
const addToBundle = (module: string, file: string, component: string): void => {
const bundle = bundles.get(module) || { file, components: new Set() };
Iif (bundle.file !== file) {
throw new SchematicsException(`Multiple sources of ${module} - ${bundle.file} / ${file}`);
}
bundle.components.add(component);
bundles.set(module, bundle);
};
bundlesConfig.forEach((bundleConfig) => {
const bundleHash = generateBundleHash();
const bundleFile = `bundle-${bundleHash}`;
const bundlePath = join(normalize(<string>project.sourceRoot), 'bundles', `${bundleFile}.ts`);
const bundleModuleName = `Bundle${bundleHash}Module`;
const ngModules = new Set<string>();
const imports = new Map<string, Set<string>>();
bundleConfig.forEach((classId) => {
const module = moduleDefinition.get(classId);
Iif (!module) {
throw new SchematicsException(`No module found for component with classId ${classId}`);
}
ngModules.add(module.ngModule.name);
bundledModules.add(module.ngModule.name);
const ngModulesFromImport = imports.get(module.importPath) || new Set<string>();
imports.set(module.importPath, ngModulesFromImport.add(module.ngModule.name));
addToBundle(bundleModuleName, bundleFile, classId);
});
tree.create(bundlePath, bundleContent(bundlePath, bundleModuleName, imports, ngModules));
});
const bundleDefinitionPath = join(normalize(project.sourceRoot), 'app', 'bundles-definition.ts');
tree.create(bundleDefinitionPath, configContent(bundles));
removeUnusedImports(appModuleFile, bundledModules);
appModuleFile.addImportDeclaration({
namedImports: [{ name: 'bundlesDefinition' }],
moduleSpecifier: './bundles-definition',
});
removeModuleImports(appModuleFile, 'AppModule', bundledModules);
updateImportConfig(appModuleFile, 'AppModule', BBCoreModuleName, [
{
key: 'lazyModules',
value: bundleDefinitionExport,
},
]);
tree.overwrite(appModuleFilePath, appModuleFile.getFullText());
};
};
|