UNPKG

pcf-scripts

Version:

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

56 lines (54 loc) 2.61 kB
"use strict"; // Copyright (C) Microsoft Corporation. All rights reserved. Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidateCode = ValidateCode; const fs = require("fs-extra"); const path = require("path"); const ts = require("typescript"); const diagnosticMessages_generated_1 = require("./diagnosticMessages.generated"); function ValidateCode(diag, codePath, constructorNameFromManifest) { if (!fs.existsSync(codePath)) { diag.pushA(diagnosticMessages_generated_1.strings.validation_code_path_does_not_exist, [codePath]); return false; } else if (fs.statSync(codePath).isDirectory()) { diag.push(diagnosticMessages_generated_1.strings.validation_code_path_must_be_file); return false; } // Make sure control doesn't use internal module and only exports one component let isValid = true; const program = ts.createProgram([codePath], {}); let numExported = 0; const srcFiles = program.getSourceFiles().filter((srcFile) => !srcFile.isDeclarationFile && path.resolve(srcFile.fileName) === codePath); const checker = program.getTypeChecker(); srcFiles.forEach((srcFile) => { srcFile.forEachChild((node) => { if (node.kind === ts.SyntaxKind.ModuleDeclaration) { isValid = false; diag.push(diagnosticMessages_generated_1.strings.validation_internal_module); return; } node.modifiers?.forEach((modifier) => { if (modifier.kind === ts.SyntaxKind.ExportKeyword) { numExported++; if (numExported > 1) { isValid = false; diag.push(diagnosticMessages_generated_1.strings.validation_multiple_exports); return; } // If node is an exported class, check to ensure the class constructor name matches the constructor name in ControlManifest.Input.xml if (ts.isClassDeclaration(node) && node.name) { const symbol = checker.getSymbolAtLocation(node.name); if (symbol && symbol.getName() !== constructorNameFromManifest) { isValid = false; diag.push(diagnosticMessages_generated_1.strings.validation_constructor_names_unmatched); return; } } } }); }); }); return isValid; } //# sourceMappingURL=sourceCodeValidator.js.map