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 138 | 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 162x 15x 6x 6x 3x 9x 150x 150x 165x 150x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | import { chain, externalSchematic, schematic, Rule, Tree } from '@angular-devkit/schematics';
import { Schema as WidgetSchema } from './schema';
import {
addBackbaseTemplates,
getLibPath,
readFileOrThrow,
getWorkspace,
getAppPath,
readFile,
projectPrefix,
nameWithSuffix,
} from '../utils/backbase/package-utils';
import * as ts from 'typescript';
import * as strings from '../utils/angular/strings';
import Project, { SyntaxKind, Node } from 'ts-simple-ast';
import { insertImport } from '../utils/angular/ast-utils';
import { addImportToModule, insert, addDeclarationToModule } from '../utils/nrwl/ast-utils';
interface WidgetTemplate {
name: string;
itemName: string;
npmScope?: string;
className?: string;
}
export default function (options: WidgetSchema): Rule {
return async (host: Tree) => {
const workspace = await getWorkspace(host);
const project = options.project || workspace.extensions.defaultProject;
const prefix = await projectPrefix(host, project);
const componentOptions = {
name: options.name,
inlineTemplate: options.inlineTemplate,
project: options.project,
prefix,
path: `libs/${options.name}/src`,
skipImport: true,
inlineStyle: true,
flat: true,
};
const widgetOptions = {
...options,
...componentOptions,
};
const templateOptions = {
name: options.name,
itemName: options.itemName || `${prefix}-${nameWithSuffix(options.name)}`,
};
return chain([
schematic('library', widgetOptions),
externalSchematic('@schematics/angular', 'component', componentOptions),
addBackbaseTemplates<WidgetTemplate>(templateOptions),
addToModule(options),
addToMocks(options),
]);
};
}
function addToModule(options: any): Rule {
const className = strings.capitalize(strings.camelize(options.name));
return (host: Tree) => {
const modulePath = `${getLibPath(options.name)}/src/${options.name}.module.ts`;
const moduleSource = readFileOrThrow(host, modulePath);
const sourceFile = ts.createSourceFile(modulePath, moduleSource, ts.ScriptTarget.Latest, true);
insert(host, modulePath, [
insertImport(sourceFile, modulePath, 'BackbaseCoreModule', '@backbase/foundation-ang/core'),
insertImport(sourceFile, modulePath, `${className}Component`, `./${options.name}.component`),
...addImportToModule(
sourceFile,
modulePath,
`BackbaseCoreModule.withConfig({
classMap: { ${className}Component }
})`,
),
...addDeclarationToModule(sourceFile, modulePath, `${className}Component`),
]);
return host;
};
}
function findNode(node: Node, kind: SyntaxKind, text?: string): Node | null {
if (node.getKind() === kind) {
if (text) {
const childNode = findNode(node, SyntaxKind.Identifier);
if (childNode !== null && childNode.getText() === text) {
return node;
}
} else {
return node;
}
}
let foundNode: Node | null = null;
node.forEachChild((childNode) => {
foundNode = foundNode || findNode(childNode, kind, text);
});
return foundNode;
}
function addToMocks(options: WidgetSchema): Rule {
const className = strings.capitalize(strings.camelize(options.name));
return async (host: Tree): Promise<Rule> => {
const project = options.project || (await getWorkspace(host)).extensions.defaultProject;
return () => {
Iif (!project) {
throw new Error(`Couldn't find a default project`);
}
const mocksPath = `${getAppPath(project)}/environments/environment.ts`;
const mocksSource = readFile(host, mocksPath);
Iif (mocksSource === undefined) {
return host;
}
const astProject = new Project({ useVirtualFileSystem: true });
const mocks = astProject.createSourceFile('foo', mocksSource);
const itemModel = `{
name: '${options.name}',
properties: {
classId: '${className}Component'
}
}`;
const pageModel = findNode(mocks, SyntaxKind.VariableDeclaration, 'pageModel');
const children = pageModel && findNode(pageModel, SyntaxKind.ArrayLiteralExpression);
Iif (children === null) {
return host;
}
const notEmpty = !!findNode(children, SyntaxKind.ObjectLiteralExpression);
mocks.insertText(children.getPos() + 2, `${itemModel}${notEmpty ? ', ' : ''}`);
host.overwrite(mocksPath, mocks.getText());
return host;
};
};
}
|