@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
184 lines • 11.4 kB
JavaScript
;
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkForSkipCallsCheck = exports.checkForSkipCalls = void 0;
var tslib_1 = require("tslib");
var arrays_1 = require("./arrays");
var common_1 = require("./common");
var fileSystem2_1 = require("./fileSystem2");
var logger_1 = require("./logger");
var path_1 = require("./path");
function checkForSkipCalls(options) {
if (options === void 0) { options = {}; }
return {
name: "No skip() calls",
check: function () { return checkForSkipCallsCheck(options); },
};
}
exports.checkForSkipCalls = checkForSkipCalls;
/**
* Check the source files found under the provided startPaths for only() function calls. Returns the
* number of source files found that reference the only() function.
* @param startPaths The paths to start looking for source files in.
* @param logger The logger to use. If no logger is specified, then a console logger will be used.
* @returns The number of source files found that contain only() function calls.
*/
function checkForSkipCallsCheck(options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var startPathArray, logger, filesWithSkipCalls, exitCode, logSkip, startPathArray_1, startPathArray_1_1, startPath, sourceFilePaths, _loop_1, sourceFilePaths_1, sourceFilePaths_1_1, sourceFilePath, e_1_1, e_2_1;
var e_2, _a, e_1, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
startPathArray = !options.startPaths ? [process.cwd()] : typeof options.startPaths === "string" ? [options.startPaths] : options.startPaths;
logger = options.logger || logger_1.getDefaultLogger();
filesWithSkipCalls = 0;
exitCode = 0;
logSkip = options.skipIsWarning ? logger.logWarning : logger.logError;
_c.label = 1;
case 1:
_c.trys.push([1, 14, 15, 16]);
startPathArray_1 = tslib_1.__values(startPathArray), startPathArray_1_1 = startPathArray_1.next();
_c.label = 2;
case 2:
if (!!startPathArray_1_1.done) return [3 /*break*/, 13];
startPath = startPathArray_1_1.value;
logger.logSection("Looking for *.skip(...) function calls in files starting at \"" + startPath + "\"...");
return [4 /*yield*/, fileSystem2_1.getChildFilePaths(startPath, {
recursive: function (folderPath) { return path_1.getName(folderPath) !== "node_modules"; },
fileCondition: function (filePath) { return filePath.endsWith(".ts") || filePath.endsWith(".js"); }
})];
case 3:
sourceFilePaths = _c.sent();
if (!!sourceFilePaths) return [3 /*break*/, 4];
logger.logError(" No source files (*.ts, *.js) found.");
return [3 /*break*/, 12];
case 4:
_loop_1 = function (sourceFilePath) {
var relativeSourceFilePath, allowedSkipLines, sourceFileContents, sourceFileLines, skipLines, i, sourceFileLine, reportAllowedSkips_1, skipsToReport, numberWidth, skipLines_1, skipLines_1_1, skipLine, skipLines_2, skipLines_2_1, skipLine, allowed;
var e_3, _a, e_4, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
relativeSourceFilePath = path_1.pathWithoutFileExtension(path_1.pathRelativeTo(sourceFilePath, startPath));
allowedSkipLines = options.allowedSkips && options.allowedSkips[relativeSourceFilePath] || [];
return [4 /*yield*/, fileSystem2_1.readFileContents(sourceFilePath)];
case 1:
sourceFileContents = (_c.sent());
sourceFileLines = common_1.getLines(sourceFileContents);
skipLines = [];
for (i = 0; i < sourceFileLines.length; ++i) {
sourceFileLine = sourceFileLines[i];
if (sourceFileLine.indexOf(".skip(") !== -1) {
skipLines.push({
lineNumber: i,
text: sourceFileLine,
allowed: allowedSkipLines === "all" || arrays_1.contains(allowedSkipLines, i)
});
}
}
if (skipLines.length > 0) {
reportAllowedSkips_1 = options.reportAllowedSkips || false;
skipsToReport = arrays_1.where(skipLines, function (skipLine) { return reportAllowedSkips_1 || !skipLine.allowed; });
if (skipsToReport.length > 0) {
logSkip(" Found " + skipLines.length + " *.skip(...) call" + (skipLines.length === 1 ? "" : "s") + " in \"" + sourceFilePath + "\".");
++filesWithSkipCalls;
numberWidth = 1;
try {
for (skipLines_1 = (e_3 = void 0, tslib_1.__values(skipLines)), skipLines_1_1 = skipLines_1.next(); !skipLines_1_1.done; skipLines_1_1 = skipLines_1.next()) {
skipLine = skipLines_1_1.value;
numberWidth = Math.max(numberWidth, skipLine.lineNumber.toString().length);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (skipLines_1_1 && !skipLines_1_1.done && (_a = skipLines_1.return)) _a.call(skipLines_1);
}
finally { if (e_3) throw e_3.error; }
}
try {
for (skipLines_2 = (e_4 = void 0, tslib_1.__values(skipLines)), skipLines_2_1 = skipLines_2.next(); !skipLines_2_1.done; skipLines_2_1 = skipLines_2.next()) {
skipLine = skipLines_2_1.value;
allowed = skipLine.allowed;
if (!allowed || reportAllowedSkips_1) {
logSkip(" Line " + common_1.padLeft(skipLine.lineNumber, numberWidth) + ". " + skipLine.text + (allowed ? " (ALLOWED)" : ""));
if (!allowed && !options.skipIsWarning) {
++exitCode;
}
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (skipLines_2_1 && !skipLines_2_1.done && (_b = skipLines_2.return)) _b.call(skipLines_2);
}
finally { if (e_4) throw e_4.error; }
}
}
}
return [2 /*return*/];
}
});
};
_c.label = 5;
case 5:
_c.trys.push([5, 10, 11, 12]);
sourceFilePaths_1 = (e_1 = void 0, tslib_1.__values(sourceFilePaths)), sourceFilePaths_1_1 = sourceFilePaths_1.next();
_c.label = 6;
case 6:
if (!!sourceFilePaths_1_1.done) return [3 /*break*/, 9];
sourceFilePath = sourceFilePaths_1_1.value;
return [5 /*yield**/, _loop_1(sourceFilePath)];
case 7:
_c.sent();
_c.label = 8;
case 8:
sourceFilePaths_1_1 = sourceFilePaths_1.next();
return [3 /*break*/, 6];
case 9: return [3 /*break*/, 12];
case 10:
e_1_1 = _c.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 12];
case 11:
try {
if (sourceFilePaths_1_1 && !sourceFilePaths_1_1.done && (_b = sourceFilePaths_1.return)) _b.call(sourceFilePaths_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 12:
startPathArray_1_1 = startPathArray_1.next();
return [3 /*break*/, 2];
case 13: return [3 /*break*/, 16];
case 14:
e_2_1 = _c.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 16];
case 15:
try {
if (startPathArray_1_1 && !startPathArray_1_1.done && (_a = startPathArray_1.return)) _a.call(startPathArray_1);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 16:
if (filesWithSkipCalls === 0) {
logger.logInfo("Found 0 source files that contain *.skip(...) calls.");
}
else {
logSkip("Found " + filesWithSkipCalls + " source file" + (filesWithSkipCalls === 1 ? "" : "s") + " that contain" + (filesWithSkipCalls === 1 ? "s" : "") + " *.skip(...) calls.");
}
process.exitCode = exitCode;
return [2 /*return*/, exitCode];
}
});
});
}
exports.checkForSkipCallsCheck = checkForSkipCallsCheck;
//# sourceMappingURL=checkForSkipCalls.js.map