UNPKG

burp-brightscript

Version:

lightweight processor for roku brightscript projects

161 lines 7.34 kB
"use strict"; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var Debug = require("debug"); var path = require("path"); var FileDescriptor_1 = require("./FileDescriptor"); var MacroValue_1 = require("./MacroValue"); var os = require('os'); var debug = Debug('FileProcessor'); var NO_FUNCTION = 'NoFunction'; var FileProcessor = /** @class */ (function () { function FileProcessor(config) { this.functionNameRegex = new RegExp('^\\s*(function|sub)\\s*([0-9a-z_]*)s*\\(', 'i'); this.functionEndRegex = new RegExp('^\s*(end sub|end function)', 'i'); this._config = config; this._warnings = []; this._errors = []; } Object.defineProperty(FileProcessor.prototype, "errors", { get: function () { return this._errors; }, enumerable: true, configurable: true }); Object.defineProperty(FileProcessor.prototype, "warnings", { get: function () { return this._warnings; }, enumerable: true, configurable: true }); FileProcessor.prototype.processFileWithPath = function (absolutePath, isUsingGlobalReplace) { if (isUsingGlobalReplace === void 0) { isUsingGlobalReplace = false; } try { var fd = new FileDescriptor_1.default(path.dirname(path.resolve(absolutePath)), path.basename(absolutePath), path.extname(absolutePath).toLowerCase()); var success = this.processFile(fd, isUsingGlobalReplace, false); return success ? fd.fileContents : undefined; } catch (e) { console.error(e); } return undefined; }; FileProcessor.prototype.processFile = function (fileDescriptor, isUsingGlobalReplace, isSaving) { var e_1, _a, e_2, _b; if (isUsingGlobalReplace === void 0) { isUsingGlobalReplace = false; } if (isSaving === void 0) { isSaving = true; } var code = fileDescriptor ? fileDescriptor.fileContents : null; if (!code || !code.trim()) { debug("no code for current descriptor"); this.errors.push('No code for file' + (fileDescriptor ? fileDescriptor.fullPath : "unknown file")); return false; } if (isUsingGlobalReplace) { var filename = fileDescriptor.normalizedFileName; var currentLocation = ''; var filePath = fileDescriptor.fullPath; var packagePath = filePath = fileDescriptor.getPackagePath('', this.rootPath); var functionName = NO_FUNCTION; var isInFunction = false; var isDirty = false; var isBrs = fileDescriptor.extension.toLowerCase() === '.brs'; var lines = code.split(/\r?\n/); for (var lineNumber = 1; lineNumber <= lines.length; lineNumber++) { currentLocation = filePath + ':' + lineNumber.toString(); var line = lines[lineNumber - 1]; if (isInFunction) { if (this.functionEndRegex.test(line)) { isInFunction = false; functionName = NO_FUNCTION; } } else { var functionNameMatch = this.getFunctionFromLine(line); if (functionNameMatch) { isInFunction = true; functionName = functionNameMatch; } } try { //run each of the user's regex's and replace with the replaced text for (var _c = __values(this._config.replacements), _d = _c.next(); !_d.done; _d = _c.next()) { var replacement = _d.value; if (replacement.replacement === MacroValue_1.MacroValue.CommentLine) { if (line.match(replacement.regex)) { line = "'" + line; } isDirty = true; } else if (line.match(new RegExp(replacement.regex, 'ig'))) { var replacementValue = replacement.replacement; replacementValue = replacementValue.replace(MacroValue_1.MacroValue.FileName, filename); replacementValue = replacementValue.replace(MacroValue_1.MacroValue.FullPath, packagePath + "(" + lineNumber.toString() + ")"); replacementValue = replacementValue.replace(MacroValue_1.MacroValue.FunctionName, functionName); replacementValue = replacementValue.replace(MacroValue_1.MacroValue.LineNumber, lineNumber.toString().trim()); if (isBrs) { replacementValue = replacementValue.replace(MacroValue_1.MacroValue.SourceLocation, "\"" + packagePath + "(" + lineNumber.toString() + ")\""); } line = line.replace(new RegExp(replacement.regex, 'ig'), replacementValue); isDirty = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_d && !_d.done && (_a = _c.return)) _a.call(_c); } finally { if (e_1) throw e_1.error; } } lines[lineNumber - 1] = line; if (isDirty) { fileDescriptor.setFileContents(lines.join(os.EOL)); if (isSaving) { fileDescriptor.saveFileContents(); } } } } else { var oldCode = code; try { for (var _e = __values(this._config.replacements), _f = _e.next(); !_f.done; _f = _e.next()) { var replacement = _f.value; code = code.replace(new RegExp(replacement.regex, 'gim'), replacement.replacement); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_f && !_f.done && (_b = _e.return)) _b.call(_e); } finally { if (e_2) throw e_2.error; } } if (oldCode.length !== code.length) { //crude; but efficient for our purposes fileDescriptor.setFileContents(code); if (isSaving) { fileDescriptor.saveFileContents(); } } } return true; }; FileProcessor.prototype.getFunctionFromLine = function (line) { var matches = line.match(this.functionNameRegex); return matches ? matches[2] : null; }; return FileProcessor; }()); exports.FileProcessor = FileProcessor; //# sourceMappingURL=FileProcessor.js.map