UNPKG

pcf-scripts

Version:

This package contains a module for building PowerApps Component Framework (PCF) controls. See project homepage how to install.

127 lines (125 loc) 9.11 kB
"use strict"; // Copyright (C) Microsoft Corporation. All rights reserved. Object.defineProperty(exports, "__esModule", { value: true }); exports.DesignTypesGenerator = void 0; // reference for typescript compiler api: https://github.com/microsoft/TypeScript-wiki/blob/master/Using-the-Compiler-API.md const semver_1 = require("semver"); const ts = require("typescript"); function createInterfaceDeclaration(modifiers, name, typeParameters, heritageClauses, members) { if (!ts.version || (0, semver_1.lt)(ts.version, "4.8.0")) { // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call return ts.factory.createInterfaceDeclaration(undefined, modifiers, name, typeParameters, heritageClauses, members); } else { return ts.factory.createInterfaceDeclaration(modifiers, name, typeParameters, heritageClauses, members); } } function getTypeParameterDeclaration() { const typeParamDeclaration = [ createTypeParameterDeclaration(ts.factory.createIdentifier("TInputs")), createTypeParameterDeclaration(ts.factory.createIdentifier("TDesign")), ]; return typeParamDeclaration; } function createTypeParameterDeclaration(name, constraint, defaultType) { if (!ts.version || (0, semver_1.lt)(ts.version, "4.7.0")) { // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-member-access return ts.factory.createTypeParameterDeclaration(name, constraint, defaultType); } else { return ts.factory.createTypeParameterDeclaration(undefined, name, constraint, defaultType); } } function createParameterDeclaration(modifiers, dotDotDotToken, name, questionToken, type, initializer) { if (!ts.version || (0, semver_1.lt)(ts.version, "4.8.0")) { // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-member-access return ts.factory.createParameterDeclaration(undefined, modifiers, dotDotDotToken, name, questionToken, type, initializer); } else { return ts.factory.createParameterDeclaration(modifiers, dotDotDotToken, name, questionToken, type, initializer); } } class DesignTypesGenerator { constructor() { this._parsedDesignMap = {}; } generateDesignTypes(parsedDesignMaps) { if (ts.factory === undefined) { throw new Error("Please update typescript version to 4.2.4 or above to use PCF Theming feature."); } this._parsedDesignMap = parsedDesignMaps; const resultFile = ts.createSourceFile( /*fileName*/ "output.ts", /*sourceText*/ "", /*languageVersion*/ ts.ScriptTarget.Latest, /*setParentNodes*/ false, /*scriptKind*/ ts.ScriptKind.TS); const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed, }); const result = printer.printList(ts.ListFormat.MultiLine, this.makeAST(), resultFile); return result; } makeAST() { const iDesignInterface = this.generateDesignInterface(); const designContextInterface = this.generateDesignContextInterface(); const designComponentInterface = this.generateDesignComponentInterface(); return ts.factory.createNodeArray([iDesignInterface, designContextInterface, designComponentInterface], false); } generateDesignInterface() { return createInterfaceDeclaration([ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], ts.factory.createIdentifier("IDesign"), undefined, undefined, this.getDefaultInterfaceMembers().concat(this.getInterfaceMembersFromDesignMappingXml())); } generateDesignComponentInterface() { return createInterfaceDeclaration([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], ts.factory.createIdentifier("DesignComponent"), [...getTypeParameterDeclaration(), createTypeParameterDeclaration(ts.factory.createIdentifier("TOutputs"))], [ ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [ ts.factory.createExpressionWithTypeArguments(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ComponentFramework"), ts.factory.createIdentifier("StandardControl")), [ ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("TInputs"), undefined), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("TOutputs"), undefined), ]), ]), ], [ ts.factory.createMethodSignature(undefined, ts.factory.createIdentifier("init"), undefined, undefined, [ createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier("context"), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("DesignContext"), [ ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("TInputs"), undefined), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("TDesign"), undefined), ]), undefined), createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier("notifyOutputChanged"), ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createFunctionTypeNode(undefined, [], ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)), undefined), createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier("state"), ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createTypeReferenceNode(ts.factory.createQualifiedName(ts.factory.createIdentifier("ComponentFramework"), ts.factory.createIdentifier("Dictionary")), undefined), undefined), createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier("container"), ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("HTMLDivElement"), undefined), undefined), ], ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)), ts.factory.createMethodSignature(undefined, ts.factory.createIdentifier("updateView"), undefined, undefined, [ createParameterDeclaration(undefined, undefined, ts.factory.createIdentifier("context"), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("DesignContext"), [ ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("TInputs"), undefined), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("TDesign"), undefined), ]), undefined), ], ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)), ts.factory.createMethodSignature(undefined, ts.factory.createIdentifier("destroy"), undefined, undefined, [], ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)), ts.factory.createMethodSignature(undefined, ts.factory.createIdentifier("getOutputs"), ts.factory.createToken(ts.SyntaxKind.QuestionToken), undefined, [], ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("TOutputs"), undefined)), ]); } generateDesignContextInterface() { return createInterfaceDeclaration([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], ts.factory.createIdentifier("DesignContext"), getTypeParameterDeclaration(), [ ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [ ts.factory.createExpressionWithTypeArguments(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ComponentFramework"), ts.factory.createIdentifier("Context")), [ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("TInputs"), undefined)]), ]), ], [ ts.factory.createPropertySignature(undefined, ts.factory.createIdentifier("design"), undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("IDesign"), undefined)), ]); } getDefaultInterfaceMembers() { return [ ts.factory.createPropertySignature(undefined, ts.factory.createIdentifier("DesignLanguageId"), undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)), ts.factory.createPropertySignature(undefined, ts.factory.createIdentifier("ThemeId"), undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)), ]; } getInterfaceMembersFromDesignMappingXml() { const designProperties = Object.keys(this._parsedDesignMap) .filter((key) => key !== "$") .map((key) => { return ts.factory.createPropertySignature(undefined, ts.factory.createIdentifier(key), undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)); }); return designProperties; } } exports.DesignTypesGenerator = DesignTypesGenerator; //# sourceMappingURL=designTypesGenerator.js.map