@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
181 lines • 9.58 kB
JavaScript
"use strict";
/**
* 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.checkForOnlyCallsCheck = exports.checkForOnlyCalls = void 0;
var tslib_1 = require("tslib");
var fileSystem2_1 = require("./fileSystem2");
var logger_1 = require("./logger");
var path_1 = require("./path");
var common_1 = require("./common");
/**
* Create an AdditionalCheck that verifies that the file at the provided path contains the provided
* text.
* @param filePath The path to the file to check.
* @param textToFind The text to find in the file.
*/
function checkForOnlyCalls(options) {
if (options === void 0) { options = {}; }
return {
name: "No only() calls",
check: function () { return checkForOnlyCallsCheck(options); },
};
}
exports.checkForOnlyCalls = checkForOnlyCalls;
/**
* 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 checkForOnlyCallsCheck(options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var startPathArray, logger, exitCode, startPathArray_1, startPathArray_1_1, startPath, sourceFilePaths, sourceFilePaths_1, sourceFilePaths_1_1, sourceFilePath, sourceFileContents, sourceFileLines, onlyLines, i, sourceFileLine, numberWidth, onlyLines_1, onlyLines_1_1, onlyLine, onlyLines_2, onlyLines_2_1, onlyLine, e_1_1, e_2_1, e_3_1;
var e_3, _a, e_2, _b, e_4, _c, e_1, _d;
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
startPathArray = !options.startPaths ? [process.cwd()] : typeof options.startPaths === "string" ? [options.startPaths] : options.startPaths;
logger = options.logger || logger_1.getDefaultLogger();
return [4 /*yield*/, logger.logVerbose("startPathArray: " + JSON.stringify(startPathArray))];
case 1:
_e.sent();
exitCode = 0;
_e.label = 2;
case 2:
_e.trys.push([2, 24, 25, 26]);
startPathArray_1 = tslib_1.__values(startPathArray), startPathArray_1_1 = startPathArray_1.next();
_e.label = 3;
case 3:
if (!!startPathArray_1_1.done) return [3 /*break*/, 23];
startPath = startPathArray_1_1.value;
return [4 /*yield*/, logger.logSection("Looking for *.only(...) function calls in files starting at \"" + startPath + "\"...")];
case 4:
_e.sent();
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 5:
sourceFilePaths = _e.sent();
if (!!sourceFilePaths) return [3 /*break*/, 7];
return [4 /*yield*/, logger.logError(" No source files (*.ts, *.js) found.")];
case 6:
_e.sent();
return [3 /*break*/, 22];
case 7:
_e.trys.push([7, 20, 21, 22]);
sourceFilePaths_1 = (e_2 = void 0, tslib_1.__values(sourceFilePaths)), sourceFilePaths_1_1 = sourceFilePaths_1.next();
_e.label = 8;
case 8:
if (!!sourceFilePaths_1_1.done) return [3 /*break*/, 19];
sourceFilePath = sourceFilePaths_1_1.value;
logger.logVerbose(" Checking " + sourceFilePath + "...");
return [4 /*yield*/, fileSystem2_1.readFileContents(sourceFilePath)];
case 9:
sourceFileContents = (_e.sent());
sourceFileLines = common_1.getLines(sourceFileContents);
onlyLines = [];
for (i = 0; i < sourceFileLines.length; ++i) {
sourceFileLine = sourceFileLines[i];
if (sourceFileLine.indexOf(".only(") !== -1) {
onlyLines.push({ lineNumber: i, text: sourceFileLine });
}
}
if (!(onlyLines.length > 0)) return [3 /*break*/, 18];
return [4 /*yield*/, logger.logError(" Found " + onlyLines.length + " *.only(...) call" + (onlyLines.length === 1 ? "" : "s") + " in \"" + sourceFilePath + "\".")];
case 10:
_e.sent();
exitCode += onlyLines.length;
numberWidth = 1;
try {
for (onlyLines_1 = (e_4 = void 0, tslib_1.__values(onlyLines)), onlyLines_1_1 = onlyLines_1.next(); !onlyLines_1_1.done; onlyLines_1_1 = onlyLines_1.next()) {
onlyLine = onlyLines_1_1.value;
numberWidth = Math.max(numberWidth, onlyLine.lineNumber.toString().length);
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (onlyLines_1_1 && !onlyLines_1_1.done && (_c = onlyLines_1.return)) _c.call(onlyLines_1);
}
finally { if (e_4) throw e_4.error; }
}
_e.label = 11;
case 11:
_e.trys.push([11, 16, 17, 18]);
onlyLines_2 = (e_1 = void 0, tslib_1.__values(onlyLines)), onlyLines_2_1 = onlyLines_2.next();
_e.label = 12;
case 12:
if (!!onlyLines_2_1.done) return [3 /*break*/, 15];
onlyLine = onlyLines_2_1.value;
return [4 /*yield*/, logger.logError(" Line " + common_1.padLeft(onlyLine.lineNumber, numberWidth) + ". " + onlyLine.text)];
case 13:
_e.sent();
_e.label = 14;
case 14:
onlyLines_2_1 = onlyLines_2.next();
return [3 /*break*/, 12];
case 15: return [3 /*break*/, 18];
case 16:
e_1_1 = _e.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 18];
case 17:
try {
if (onlyLines_2_1 && !onlyLines_2_1.done && (_d = onlyLines_2.return)) _d.call(onlyLines_2);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 18:
sourceFilePaths_1_1 = sourceFilePaths_1.next();
return [3 /*break*/, 8];
case 19: return [3 /*break*/, 22];
case 20:
e_2_1 = _e.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 22];
case 21:
try {
if (sourceFilePaths_1_1 && !sourceFilePaths_1_1.done && (_b = sourceFilePaths_1.return)) _b.call(sourceFilePaths_1);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 22:
startPathArray_1_1 = startPathArray_1.next();
return [3 /*break*/, 3];
case 23: return [3 /*break*/, 26];
case 24:
e_3_1 = _e.sent();
e_3 = { error: e_3_1 };
return [3 /*break*/, 26];
case 25:
try {
if (startPathArray_1_1 && !startPathArray_1_1.done && (_a = startPathArray_1.return)) _a.call(startPathArray_1);
}
finally { if (e_3) throw e_3.error; }
return [7 /*endfinally*/];
case 26:
if (!(exitCode === 0)) return [3 /*break*/, 28];
return [4 /*yield*/, logger.logInfo("Found 0 source files that contain *.only(...) calls.")];
case 27:
_e.sent();
return [3 /*break*/, 30];
case 28: return [4 /*yield*/, logger.logError("Found " + exitCode + " source file" + (exitCode === 1 ? "" : "s") + " that contain" + (exitCode === 1 ? "s" : "") + " *.only(...) calls.")];
case 29:
_e.sent();
_e.label = 30;
case 30:
process.exitCode = exitCode;
return [2 /*return*/, exitCode];
}
});
});
}
exports.checkForOnlyCallsCheck = checkForOnlyCallsCheck;
//# sourceMappingURL=checkForOnlyCalls.js.map