sortier
Version:
An opinionated code sorter
71 lines (70 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = run;
const index_js_1 = require("../config/index.js");
const ignored_file_error_js_1 = require("../error/ignored-file-error.js");
const unsupported_extension_error_js_1 = require("../error/unsupported-extension-error.js");
const index_js_2 = require("../lib/format-file/index.js");
const log_utils_js_1 = require("../utilities/log-utils.js");
const args_parser_js_1 = require("./args-parser.js");
const get_files_js_1 = require("./get-files.js");
function run(args) {
const context = (0, args_parser_js_1.parseArgs)(args);
(0, index_js_1.resolveOptions)(process.cwd());
if (context.filepatterns.length === 0) {
log_utils_js_1.LogUtils.log(log_utils_js_1.LoggerVerboseOption.Normal, "Must provide a file pattern to run sortier over (e.g. `sortier --ignore-unknown './**/*.ts'`)");
return 1;
}
const files = (0, get_files_js_1.getFiles)(context);
if (files.length === 0) {
if (context.filepatterns[0].indexOf("\\") !== -1) {
log_utils_js_1.LogUtils.log(log_utils_js_1.LoggerVerboseOption.Normal, "Sortier no longer supports file paths that contain '\\' (see fast-glob@3.0.0 release notes). Is your glob pattern correct?");
}
else {
log_utils_js_1.LogUtils.log(log_utils_js_1.LoggerVerboseOption.Normal, `No filepaths found for file pattern(s) ${context.filepatterns.map((value) => `"${value}"`).join(" ")}`);
}
return 1;
}
let error = null;
files.map((filePath) => {
const start = Date.now();
try {
(0, index_js_2.formatFile)(filePath);
const end = Date.now();
const measured = end - start;
log_utils_js_1.LogUtils.log(log_utils_js_1.LoggerVerboseOption.Normal, `${filePath} - ${measured}ms`);
}
catch (e) {
const end = Date.now();
const measured = end - start;
let level = log_utils_js_1.LoggerVerboseOption.Normal;
let shouldPrint = true;
if (e instanceof ignored_file_error_js_1.IgnoredFileError) {
level = log_utils_js_1.LoggerVerboseOption.Diagnostic;
}
else if (e instanceof unsupported_extension_error_js_1.UnsupportedExtensionError && context.ignoreUnknown) {
shouldPrint = false;
}
else {
error = e;
}
if (shouldPrint) {
log_utils_js_1.LogUtils.log(level, `${filePath} - ${getStringFromError(e)} - ${measured}ms`);
}
}
});
if (error != null) {
return 1;
}
return 0;
}
function getStringFromError(e) {
if (e instanceof Error) {
const { message } = e;
return message;
}
else if (typeof e === "string") {
return e;
}
return e;
}