UNPKG

maestro-cli-roku

Version:

command line tools for maestro-roku projects

292 lines (291 loc) 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var fs = require("fs-extra"); var path = require("path"); var ChangeExtension_1 = require("../utils/ChangeExtension"); var Feedback_1 = require("../utils/Feedback"); var Utils_1 = require("../utils/Utils"); var FileType_1 = require("./FileType"); var xmldoc = require('../utils/xmldoc'); /** * describes a file in our project. */ var File = /** @class */ (function () { function File(fsPath, projectPath, filename, extension) { this.tagIds = new Set(); this.fieldIds = new Set(); //dumping ground for extra data, while the dust settles on some impl details this.extendedData = new Map(); this.filename = filename; this._fsPath = fsPath; this._fullPath = path.join(fsPath, filename); this._pkgPath = path.join(projectPath, filename); this._pkgUri = "pkg:/" + path.join(projectPath, filename); this.projectPath = projectPath; this.extension = extension; this._importedPaths = new Set(); this.nonCheckedImportedPaths = new Set(); this.importedFiles = []; this.requiredFiles = []; this.componentIds = new Set(); this._bindings = []; this.associatedFile = null; this.parentFile = null; this._fileContents = null; this.hasProcessedImports = false; this.hasProcessedBindings = false; this._instantiators = []; } Object.defineProperty(File.prototype, "instantiators", { get: function () { return this._instantiators; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "fileType", { get: function () { switch (this.extension.toLowerCase()) { case '.brs': return this.associatedFile ? FileType_1.FileType.CodeBehind : FileType_1.FileType.Brs; case '.xml': return this.associatedFile ? FileType_1.FileType.ViewXml : FileType_1.FileType.Xml; case '.bs': return FileType_1.FileType.UncompiledBrighterscript; default: return FileType_1.FileType.Other; } }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "isDirty", { get: function () { return this._isDirty; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "isCompiledBrs", { get: function () { return this._isCompiledBrs; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "bindings", { get: function () { return this._bindings; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "isMixin", { get: function () { return this.filename.endsWith('Mixin'); }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "fsPath", { get: function () { return this._fsPath; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "importedPaths", { get: function () { return this._importedPaths; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "fullPath", { get: function () { return this._fullPath; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "pkgPath", { get: function () { return this._pkgPath; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "pkgUri", { get: function () { return this._pkgUri; }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "normalizedFileName", { get: function () { return this.filename.replace('.brs', '').replace('-', '_').replace('.', '_'); }, enumerable: true, configurable: true }); Object.defineProperty(File.prototype, "normalizedFullFileName", { get: function () { return this.fullPath.replace('/', '_') + this.normalizedFileName; }, enumerable: true, configurable: true }); File.prototype.getFileContents = function () { if (this._fileContents === null) { this._fileContents = fs.readFileSync(this.fullPath, 'utf8'); } return this._fileContents; }; File.prototype.setFileContents = function (fileContents) { if (!this.isProcessingDisabled) { this._fileContents = fileContents; this._isDirty = true; } else { Feedback_1.feedbackError(this, 'tried to set file contents on a file which is included in processingExcludedPaths', true); } }; File.prototype.saveFileContents = function () { if (!this.isProcessingDisabled) { try { fs.writeFileSync(this.fullPath, this._fileContents, 'utf8'); } catch (e) { Feedback_1.feedbackError(this, "could not save file at path " + this.fullPath + " - does the path exist?", true); } this._isDirty = false; } else { Feedback_1.feedbackError(this, 'tried to save file contents on a file which is included in processingExcludedPaths', true); } }; File.prototype.unloadContents = function () { this._fileContents = null; }; File.prototype.getAllParentImportPaths = function (paths) { if (paths === void 0) { paths = null; } if (!paths) { paths = []; } else { paths = paths.concat(this.importedFiles.map(function (file) { return file.pkgPath.toLowerCase(); })); } if (this.parentFile) { return this.parentFile.getAllParentImportPaths(paths); } else { return paths; } }; File.prototype.getAllParentImportPathsAsSet = function (paths) { if (paths === void 0) { paths = null; } if (!paths) { paths = new Set(); } else { Utils_1.addSetItems(paths, new Set(this.importedFiles.map(function (file) { return file.pkgPath.toLowerCase(); }))); } if (this.parentFile) { return this.parentFile.getAllParentImportPathsAsSet(paths); } else { return paths; } }; File.prototype.getAllParentBindings = function (bindings) { if (bindings === void 0) { bindings = null; } if (!bindings) { bindings = []; } else { bindings = bindings.concat(this.bindings); } if (this.parentFile) { return this.parentFile.getAllParentBindings(bindings); } else { return bindings; } }; File.prototype.getAllParentTagIds = function (ids) { if (ids === void 0) { ids = null; } if (!ids) { ids = new Set(); } else { Utils_1.addSetItems(ids, this.tagIds); } if (this.parentFile) { return this.parentFile.getAllParentTagIds(ids); } else { return ids; } }; File.prototype.getAllParentFieldIds = function (ids) { if (ids === void 0) { ids = null; } if (!ids) { ids = new Set(); } else { Utils_1.addSetItems(ids, this.fieldIds); } if (this.parentFile) { return this.parentFile.getAllParentFieldIds(ids); } else { return ids; } }; File.prototype.toString = function () { return "DESCRIPTOR: " + this.filename + " TYPE " + this.fileType + " PATH " + this.fullPath; }; File.prototype.convertToBrightScript = function (classProcessor, namespaceProcessor, importProcessor) { namespaceProcessor.processNamespacesForFile(this); classProcessor.parseFile(this); namespaceProcessor.removeNamepsaceDefinitions(this); importProcessor.parseFileImports(this); this.removeImportStatements(); var originalFilePath = this._fullPath; this.extension = '.brs'; this.filename = ChangeExtension_1.changeExtension(this.filename, '.brs'); this._fullPath = path.join(this._fsPath, this.filename); this._pkgPath = path.join(this.projectPath, this.filename); this._pkgUri = "pkg:/" + path.join(this.projectPath, this.filename); fs.moveSync(originalFilePath, this._fullPath); this._isCompiledBrs = true; }; File.prototype.loadXmlContents = function (fileMap) { if (this.fileType === FileType_1.FileType.Xml || this.fileType === FileType_1.FileType.ViewXml) { try { this.xmlDoc = new xmldoc.XmlDocument(this.getFileContents()); if (this.xmlDoc.name && this.xmlDoc.name && this.xmlDoc.name.toLowerCase() === 'component') { if (this.xmlDoc.attr && this.xmlDoc.attr.name) { fileMap.addXMLComponent(this.xmlDoc.attr.name, this); } } } catch (e) { Feedback_1.feedbackError(this, 'Could not parse xml in file: ' + e.message); } } }; File.prototype.removeImportStatements = function () { var regex = new RegExp("^(?: *|\\t*)(import\\s*\\\"(.*)\\\".*)", "gim"); var updatedContents = this.getFileContents().replace(regex, "'$1"); this.setFileContents(updatedContents); }; File.prototype.convertBrighterScriptXMLImports = function () { var regex = new RegExp("(uri=\\\".*)(\\.bs)\\\"", "gim"); var updatedContents = this.getFileContents().replace(regex, "$1.brs\""); this.setFileContents(updatedContents); }; return File; }()); exports.File = File;