Skip to content

Commit

Permalink
Merge pull request #1279 from IgniteUI/bpenkov/reveal-dashboard-schem…
Browse files Browse the repository at this point in the history
…a-13.4

Adding schema to template dependency 13.4.x
  • Loading branch information
wnvko authored Jul 11, 2024
2 parents 1b2ac23 + e484cd7 commit 06d8d5d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
3 changes: 3 additions & 0 deletions packages/core/types/TemplateDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export interface TemplateDependency {
/** Add an identifier into `ngModule` provides metadata */
provide?: string | string[];

/** Add an identifier into `ngModule` schema metadata */
schema?: string | string[];

/** The package/path(TBD) to import the dependency from */
from?: string;

Expand Down
74 changes: 46 additions & 28 deletions packages/core/typescript/TypeScriptFileUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class TypeScriptFileUpdate {
declarations: string[],
imports: Array<{ name: string, root: boolean, standalone?: boolean }>,
providers: string[],
schemas: Array<{ name: string, standalone?: boolean }>,
exports: string[]
};

Expand Down Expand Up @@ -52,8 +53,8 @@ export class TypeScriptFileUpdate {
}

// should we support both standalone and module-based components in the same app?
if (this.ngMetaEdits.imports.some(x => x.standalone)) {
transforms.push(this.componentMetaTransformer);
if (this.ngMetaEdits.imports.some(x => x.standalone) || (this.ngMetaEdits.schemas.some(x => x.standalone))) {
transforms.push(this.componentMetaTransformer);
} else if (Object.keys(this.ngMetaEdits).filter(x => this.ngMetaEdits[x].length).length) {
transforms.push(this.ngModuleTransformer);
}
Expand Down Expand Up @@ -141,13 +142,14 @@ export class TypeScriptFileUpdate {
declare: this.asArray(dep.declare, variables),
import: this.asArray(dep.import, variables),
provide: this.asArray(dep.provide, variables),
schema: this.asArray(dep.schema, variables),
// tslint:disable-next-line:object-literal-sort-keys
export: this.asArray(dep.export, variables)
};

if (dep.from) {
// request import
const identifiers = [...copy.import, ...copy.declare, ...copy.provide];
const identifiers = [...copy.import, ...copy.declare, ...copy.provide, ...copy.schema];
this.requestImport(identifiers, Util.applyConfigTransformation(dep.from, variables));
}
const imports = copy.import
Expand All @@ -163,6 +165,11 @@ export class TypeScriptFileUpdate {
.filter(x => !this.ngMetaEdits.providers.find(p => p === x));
this.ngMetaEdits.providers.push(...providers);

const schemas = copy.schema
.map(x => ({ name: x, root: dep.root }))
.filter(x => !this.ngMetaEdits.schemas.find(s => s.name === x.name));
this.ngMetaEdits.schemas.push(...schemas);

const exportsArr = copy.export
.filter(x => !this.ngMetaEdits.exports.find(p => p === x));
this.ngMetaEdits.exports.push(...exportsArr);
Expand All @@ -174,18 +181,24 @@ export class TypeScriptFileUpdate {
public addStandaloneImport(dep: TemplateDependency, variables?: { [key: string]: string }) {
const copy = {
import: this.asArray(dep.import, variables),
provide: this.asArray(dep.provide, variables)
provide: this.asArray(dep.provide, variables),
schema: this.asArray(dep.schema, variables)
};
if (dep.from) {
// request import
const identifiers = [...copy.import, ...copy.provide];
const identifiers = [...copy.import, ...copy.provide, ...copy.schema];
this.requestImport(identifiers, Util.applyConfigTransformation(dep.from, variables));
}

const imports = copy.import
.map(x => ({ name: x, root: dep.root, standalone: true }))
.filter(x => !this.ngMetaEdits.imports.find(i => i.name === x.name));
this.ngMetaEdits.imports.push(...imports);

const schemas = copy.schema
.map(x => ({ name: x, root: dep.root, standalone: true}))
.filter(x => !this.ngMetaEdits.schemas.find(i => i.name === x.name));
this.ngMetaEdits.schemas.push(...schemas);
}

/**
Expand Down Expand Up @@ -249,6 +262,7 @@ export class TypeScriptFileUpdate {
declarations: [],
imports: [],
providers: [],
schemas: [],
exports: []
};
this.createdStringLiterals = [];
Expand Down Expand Up @@ -547,23 +561,19 @@ export class TypeScriptFileUpdate {
const objProperties = ts.visitNodes(obj.properties, visitor);
const newProps = [];
for (const prop of missingProperties) {
let arrayExpr;
switch (prop) {
case "imports":
const importDeps = this.ngMetaEdits.imports;
arrayExpr = ts.factory.createArrayLiteralExpression(
importDeps.map(x => TsUtils.createIdentifier(x.name, x.root ? "forRoot" : ""))
);
break;
case "declarations":
case "providers":
case "exports":
arrayExpr = ts.factory.createArrayLiteralExpression(
this.ngMetaEdits[prop].map(x => ts.factory.createIdentifier(x))
);
break;
if (this.ngMetaEdits[prop].length) {
const expr = ts.factory.createArrayLiteralExpression(
this.ngMetaEdits[prop].map(x => {
if (typeof x === "string") {
return TsUtils.createIdentifier(x);
}
if (typeof x === "object" && "name" in x) {
return TsUtils.createIdentifier(x.name, x.root ? "forRoot" : "")
}
})
);
newProps.push(ts.factory.createPropertyAssignment(prop, expr));
}
newProps.push(ts.factory.createPropertyAssignment(prop, arrayExpr));
}

return ts.factory.updateObjectLiteralExpression(obj, [
Expand All @@ -585,6 +595,11 @@ export class TypeScriptFileUpdate {
.filter(x => alreadyImported.indexOf(x.name) === -1)
.map(x => TsUtils.createIdentifier(x.name, x.root ? "forRoot" : ""));
break;
case "schemas":
identifiers = this.ngMetaEdits.schemas
.filter(x => alreadyImported.indexOf(x.name) === -1)
.map(x => TsUtils.createIdentifier(x.name));
break;
case "declarations":
case "providers":
case "exports":
Expand Down Expand Up @@ -618,19 +633,22 @@ export class TypeScriptFileUpdate {
protected componentMetaTransformer: ts.TransformerFactory<ts.Node> =
<T extends ts.Node>(context: ts.TransformationContext) => (rootNode: T) => {
const visitComponent: ts.Visitor = (node: ts.Node): ts.Node => {
let importsExpr = null;
const prop = "imports";
const properties = Object.keys(this.ngMetaEdits);
if (node.kind === ts.SyntaxKind.ObjectLiteralExpression &&
node.parent &&
node.parent.kind === ts.SyntaxKind.CallExpression) {
const obj = (node as ts.ObjectLiteralExpression);
const objProperties = ts.visitNodes(obj.properties, visitor);
const newProps = [];
const importDeps = this.ngMetaEdits.imports;
importsExpr = ts.factory.createArrayLiteralExpression(
importDeps.map(x => TsUtils.createIdentifier(x.name))
);
newProps.push(ts.factory.createPropertyAssignment(prop, importsExpr));
const missingProperties = properties.filter(x => !obj.properties.find(o => o.name.getText() === x));
for (const prop of missingProperties) {
if (this.ngMetaEdits[prop].length) {
const expr = ts.factory.createArrayLiteralExpression(
this.ngMetaEdits[prop].map(x => TsUtils.createIdentifier(x.name))
);
newProps.push(ts.factory.createPropertyAssignment(prop, expr));
}
}
return context.factory.updateObjectLiteralExpression(obj, [
...objProperties,
...newProps
Expand Down

0 comments on commit 06d8d5d

Please sign in to comment.