UNPKG

qcobjects-cli

Version:

qcobjects cli command line tool

171 lines (170 loc) • 8.47 kB
"use strict"; var global = (() => { var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var cli_commands_lint_exports = {}; __export(cli_commands_lint_exports, { CommandHandler: () => CommandHandler }); var import_node_fs = __toESM(require("node:fs")); var import_node_path = __toESM(require("node:path")); var import_node_url = require("node:url"); var import_glob = __toESM(require("glob")); var import_eslint = require("eslint"); var import_globals = __toESM(require("globals")); var tsParserModule = __toESM(require("@typescript-eslint/parser")); var tsPluginModule = __toESM(require("@typescript-eslint/eslint-plugin")); var import_qcobjects = require("qcobjects"); const tsParser = tsParserModule.default ?? tsParserModule; const tsPlugin = tsPluginModule.default ?? tsPluginModule; const DEFAULT_TARGETS = ["src/**/*.ts"]; const DEFAULT_IGNORES = [ "node_modules/**", "build/**", "public/**", "dist/**", "coverage/**", ".git/**" ]; const DEFAULT_FLAT_CONFIG = [ { files: ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.mjs", "**/*.cjs"], plugins: { "@typescript-eslint": tsPlugin }, languageOptions: { parser: tsParser, ecmaVersion: "latest", sourceType: "module", globals: { ...import_globals.default.browser, ...import_globals.default.node, ...import_globals.default.commonjs } }, rules: { "no-undef": "error", "no-unused-vars": ["warn", { argsIgnorePattern: "^_", varsIgnorePattern: "^_" }], "no-empty": "off", "no-constant-condition": "off", "no-redeclare": "off", "no-var": "off", "no-mixed-operators": "off" } } ]; class CommandHandler extends import_qcobjects.InheritClass { static { __name(this, "CommandHandler"); } choiceOption; constructor({ switchCommander }) { super({ switchCommander }); this.choiceOption = { async lint(targets, options) { try { const cwd = process.cwd(); const requestedTargets = targets.length > 0 ? targets : DEFAULT_TARGETS; const ignoreFlags = DEFAULT_IGNORES.map((ignored) => new RegExp(ignored.replace(/\*\*/g, ".*"))); const files = requestedTargets.flatMap((target) => import_glob.default.sync(target, { cwd, nodir: true })).filter((file) => !ignoreFlags.some((flag) => flag.test(file))); if (files.length === 0) { console.warn(`No files matched: ${requestedTargets.join(", ")}`); process.exit(0); } let flatConfig = DEFAULT_FLAT_CONFIG; const appConfigPath = ["eslint.config.js", "eslint.config.mjs", "eslint.config.cjs"].map((name) => import_node_path.default.resolve(cwd, name)).find((appConfigPath2) => import_node_fs.default.existsSync(appConfigPath2)); if (options.config) { const explicitConfigPath = import_node_path.default.resolve(cwd, options.config); if (!import_node_fs.default.existsSync(explicitConfigPath)) { console.warn(`Config file not found: ${explicitConfigPath}`); process.exit(1); } flatConfig = (await import((0, import_node_url.pathToFileURL)(explicitConfigPath).href)).default; console.log(`Using config file: ${explicitConfigPath}`); } else if (appConfigPath) { flatConfig = (await import((0, import_node_url.pathToFileURL)(appConfigPath).href)).default; console.log(`Using app config file: ${appConfigPath}`); } else if (import_node_fs.default.existsSync(import_node_path.default.resolve(cwd, ".eslintrc")) || import_node_fs.default.existsSync(import_node_path.default.resolve(cwd, ".eslintrc.js")) || import_node_fs.default.existsSync(import_node_path.default.resolve(cwd, ".eslintrc.cjs")) || import_node_fs.default.existsSync(import_node_path.default.resolve(cwd, ".eslintrc.json"))) { console.warn("Legacy .eslintrc config detected but not supported; using built-in QCObjects settings. Migrate to eslint.config.mjs to override defaults."); } else { console.log("No app eslint config found. Using built-in QCObjects settings (overridable with an eslint.config.mjs in your app)."); } const linter = new import_eslint.Linter({ cwd, configType: "flat" }); let errorCount = 0; let warningCount = 0; let fixedCount = 0; for (const file of files) { const absolutePath = import_node_path.default.resolve(cwd, file); const source = import_node_fs.default.readFileSync(absolutePath, "utf8"); const result = options.fix ? linter.verifyAndFix(source, flatConfig, absolutePath) : { fixed: false, output: source, messages: linter.verify(source, flatConfig, absolutePath) }; if (result.fixed) { fixedCount++; import_node_fs.default.writeFileSync(absolutePath, result.output); } for (const message of result.messages) { const severity = message.severity === 2 ? "error" : message.severity === 1 ? "warning" : "info"; const output = `${file}:${message.line}:${message.column} ${severity} ${message.message} (${message.ruleId ?? "parse"})`; if (message.severity === 2) { errorCount++; console.error(output); } else if (message.severity === 1) { warningCount++; console.warn(output); } else { console.log(output); } } } const summary = `${files.length} file(s) linted, ${errorCount} error(s), ${warningCount} warning(s), ${fixedCount} file(s) fixed.`; if (errorCount > 0) { console.error(summary); process.exit(1); } console.log(summary); process.exit(0); } catch (e) { console.warn(`Something went wrong trying to lint: ${e.message}`); process.exit(1); } } }; const commandHandler = this; const lintCommand = switchCommander.program.command("lint [targets...]").allowExcessArguments(false).option("-f, --fix", "Automatically fix problems").option("-c, --config <path>", "Use a specific eslint config file").description("Lints JS/TS files using built-in QCObjects settings. Default target: src/**/*.ts").action(function(targets, options) { commandHandler.choiceOption.lint.call(commandHandler, targets ?? [], options); }); } } (0, import_qcobjects.Package)("com.qcobjects.cli.commands.lint", [ CommandHandler ]); return __toCommonJS(cli_commands_lint_exports); })(); //# sourceMappingURL=cli-commands-lint.js.map