@sasjs/lint
Version:
Linting and formatting for SAS code
68 lines • 3.73 kB
JavaScript
;
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatFolder = void 0;
const file_1 = require("@sasjs/utils/file");
const path_1 = __importDefault(require("path"));
const lint_1 = require("../lint");
const asyncForEach_1 = require("../utils/asyncForEach");
const getLintConfig_1 = require("../utils/getLintConfig");
const listSasFiles_1 = require("../utils/listSasFiles");
const formatFile_1 = require("./formatFile");
const excludeFolders = [
'.git',
'.github',
'.vscode',
'node_modules',
'sasjsbuild',
'sasjsresults'
];
/**
* Automatically formats all SAS files in the folder at the given path.
* @param {string} folderPath - the path to the folder to be formatted.
* @param {LintConfig} configuration - an optional configuration. When not passed in, this is read from the .sasjslint file.
* @returns {Promise<FormatResult>} Resolves successfully when all SAS files in the given folder have been formatted.
*/
const formatFolder = (folderPath, configuration) => __awaiter(void 0, void 0, void 0, function* () {
const config = configuration || (yield (0, getLintConfig_1.getLintConfig)());
const diagnosticsBeforeFormat = yield (0, lint_1.lintFolder)(folderPath);
const diagnosticsCountBeforeFormat = Array.from(diagnosticsBeforeFormat.values()).reduce((a, b) => a + b.length, 0);
const fileNames = yield (0, listSasFiles_1.listSasFiles)(folderPath);
yield (0, asyncForEach_1.asyncForEach)(fileNames, (fileName) => __awaiter(void 0, void 0, void 0, function* () {
const filePath = path_1.default.join(folderPath, fileName);
yield (0, formatFile_1.formatFile)(filePath);
}));
const subFolders = (yield (0, file_1.listSubFoldersInFolder)(folderPath)).filter((f) => !excludeFolders.includes(f));
yield (0, asyncForEach_1.asyncForEach)(subFolders, (subFolder) => __awaiter(void 0, void 0, void 0, function* () {
yield (0, exports.formatFolder)(path_1.default.join(folderPath, subFolder), config);
}));
const diagnosticsAfterFormat = yield (0, lint_1.lintFolder)(folderPath);
const diagnosticsCountAfterFormat = Array.from(diagnosticsAfterFormat.values()).reduce((a, b) => a + b.length, 0);
const fixedDiagnosticsCount = diagnosticsCountBeforeFormat - diagnosticsCountAfterFormat;
const updatedFilePaths = [];
Array.from(diagnosticsBeforeFormat.keys()).forEach((filePath) => {
const diagnosticsBefore = diagnosticsBeforeFormat.get(filePath) || [];
const diagnosticsAfter = diagnosticsAfterFormat.get(filePath) || [];
if (diagnosticsBefore.length !== diagnosticsAfter.length) {
updatedFilePaths.push(filePath);
}
});
return {
updatedFilePaths,
fixedDiagnosticsCount,
unfixedDiagnostics: diagnosticsAfterFormat
};
});
exports.formatFolder = formatFolder;
//# sourceMappingURL=formatFolder.js.map