qcobjects-cli
Version:
qcobjects cli command line tool
207 lines • 9.91 kB
JavaScript
/**
* QCObjects Framework CLI
* ________________
*
* Author: Jean Machuca <correojean@gmail.com>
*
* Cross Browser Javascript Framework for MVC Patterns
* QCObjects is licensed under the MIT License
* [LICENSE] (https://github.com/QCObjects/qcobjects-cli/blob/main/LICENSE.txt)
*
* Copyright (C) 2015 Jean Machuca,<correojean@gmail.com>
*
* Everyone is permitted to copy and distribute verbatim copies of this
* license document, but changing it is not allowed.
*/
/*eslint no-unused-vars: "off"*/
/*eslint no-redeclare: "off"*/
/*eslint no-empty: "off"*/
/*eslint strict: "off"*/
/*eslint no-mixed-operators: "off"*/
/*eslint no-undef: "off"*/
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandHandler = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const node_url_1 = require("node:url");
const glob_1 = __importDefault(require("glob"));
const eslint_1 = require("eslint");
const globals_1 = __importDefault(require("globals"));
const tsParserModule = __importStar(require("@typescript-eslint/parser"));
const tsPluginModule = __importStar(require("@typescript-eslint/eslint-plugin"));
const qcobjects_1 = 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: {
...globals_1.default.browser,
...globals_1.default.node,
...globals_1.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 qcobjects_1.InheritClass {
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 => glob_1.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 => node_path_1.default.resolve(cwd, name))
.find(appConfigPath => node_fs_1.default.existsSync(appConfigPath));
if (options.config) {
const explicitConfigPath = node_path_1.default.resolve(cwd, options.config);
if (!node_fs_1.default.existsSync(explicitConfigPath)) {
console.warn(`Config file not found: ${explicitConfigPath}`);
process.exit(1);
}
flatConfig = (await import((0, node_url_1.pathToFileURL)(explicitConfigPath).href)).default;
console.log(`Using config file: ${explicitConfigPath}`);
}
else if (appConfigPath) {
flatConfig = (await import((0, node_url_1.pathToFileURL)(appConfigPath).href)).default;
console.log(`Using app config file: ${appConfigPath}`);
}
else if (node_fs_1.default.existsSync(node_path_1.default.resolve(cwd, ".eslintrc")) || node_fs_1.default.existsSync(node_path_1.default.resolve(cwd, ".eslintrc.js")) || node_fs_1.default.existsSync(node_path_1.default.resolve(cwd, ".eslintrc.cjs")) || node_fs_1.default.existsSync(node_path_1.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 eslint_1.Linter({ cwd, configType: "flat" });
let errorCount = 0;
let warningCount = 0;
let fixedCount = 0;
for (const file of files) {
const absolutePath = node_path_1.default.resolve(cwd, file);
const source = node_fs_1.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++;
node_fs_1.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);
});
}
}
exports.CommandHandler = CommandHandler;
(0, qcobjects_1.Package)("com.qcobjects.cli.commands.lint", [
CommandHandler
]);
//# sourceMappingURL=cli-commands-lint.js.map