@splunk/rum-cli
Version:
Tools for handling symbol and mapping files for symbolication
126 lines (125 loc) • 5.59 kB
JavaScript
;
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.injectFile = injectFile;
const filesystem_1 = require("../utils/filesystem");
const utils_1 = require("./utils");
/**
* Injects the code snippet into the JS file to permanently associate the JS file with its sourceMapId.
*
* The code snippet will be injected at the end of the file, or just before the
* "//# sourceMappingURL=" comment if it exists.
*
* This operation is idempotent.
*
* If dryRun is true, this function will not write to the file system.
*/
function injectFile(jsFilePath, sourceMapId, options, logger) {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
if (options.dryRun) {
logger.info(`sourceMapId ${sourceMapId} would be injected to ${jsFilePath}`);
return;
}
const lines = [];
let sourceMappingUrlIndex = -1;
let existingSnippetIndex = -1;
let existingSnippet = '';
/*
* Read the file into memory, and record any significant line indexes
*/
let readlinesIndex = 0;
try {
const fileStream = (0, filesystem_1.makeReadStream)(jsFilePath);
try {
for (var _d = true, _e = __asyncValues((0, filesystem_1.readlines)(fileStream)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const line = _c;
if (line.startsWith(utils_1.SOURCE_MAPPING_URL_COMMENT_PREFIX)) {
sourceMappingUrlIndex = readlinesIndex;
}
if (line.startsWith(utils_1.SNIPPET_PREFIX)) {
existingSnippetIndex = readlinesIndex;
existingSnippet = line;
}
lines.push(line);
readlinesIndex++;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
}
catch (e) {
(0, utils_1.throwJsFileReadError)(e, jsFilePath, options);
}
const snippet = getCodeSnippet(sourceMapId);
/*
* No work required if the snippet already exists in the file (i.e. from a previous manual run)
*/
if (existingSnippet === snippet) {
logger.debug(`sourceMapId ${sourceMapId} already injected into ${jsFilePath}`);
return;
}
/*
* Insert the code snippet at the correct location
*/
if (existingSnippetIndex >= 0) {
lines.splice(existingSnippetIndex, 1, snippet); // overwrite the existing snippet
}
else if (sourceMappingUrlIndex >= 0) {
lines.splice(sourceMappingUrlIndex, 0, snippet);
}
else {
lines.push(snippet);
}
/*
* Write new JavaScript file contents to the file system
*/
logger.debug(`injecting sourceMapId ${sourceMapId} into ${jsFilePath}`);
try {
yield (0, filesystem_1.overwriteFileContents)(jsFilePath, lines);
}
catch (e) {
(0, utils_1.throwJsFileOverwriteError)(e, jsFilePath, options);
}
});
}
function getCodeSnippet(sourceMapId) {
return utils_1.SNIPPET_TEMPLATE.replace('__SOURCE_MAP_ID_PLACEHOLDER__', sourceMapId);
}