tsro
Version:
TypeScript Remove Orphaned (tsro) is a CLI utility and library for TypeScript projects that detects and removes files containing invalid imports — imports that refer to nonexistent entities or nonexistent modules.
54 lines (53 loc) • 3.8 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _CliOutput_instances, _CliOutput_deletedFileCount, _CliOutput_projectRoot, _CliOutput_logger, _CliOutput_mode, _CliOutput_previousFile, _CliOutput_relativePath;
import pc from "picocolors";
import { relative } from "node:path";
import { formatCount } from "./formatCount.js";
export class CliOutput {
constructor({ logger, projectRoot, mode, }) {
_CliOutput_instances.add(this);
_CliOutput_deletedFileCount.set(this, 0);
_CliOutput_projectRoot.set(this, void 0);
_CliOutput_logger.set(this, void 0);
_CliOutput_mode.set(this, void 0);
_CliOutput_previousFile.set(this, void 0);
__classPrivateFieldSet(this, _CliOutput_logger, logger, "f");
__classPrivateFieldSet(this, _CliOutput_mode, mode, "f");
__classPrivateFieldSet(this, _CliOutput_projectRoot, projectRoot, "f");
}
deleteFile(diagnostic) {
var _a;
const { file, code, line, char } = diagnostic;
__classPrivateFieldGet(this, _CliOutput_logger, "f").write(`${pc.yellow("import")} ${__classPrivateFieldGet(this, _CliOutput_instances, "m", _CliOutput_relativePath).call(this, file)}:${pc.gray(`${line}:${char}`.padEnd(7))} ${pc.gray(`'${code}'`)}\n`);
if (__classPrivateFieldGet(this, _CliOutput_previousFile, "f") !== file) {
__classPrivateFieldSet(this, _CliOutput_deletedFileCount, (_a = __classPrivateFieldGet(this, _CliOutput_deletedFileCount, "f"), _a++, _a), "f");
}
__classPrivateFieldSet(this, _CliOutput_previousFile, file, "f");
}
done() {
const result = __classPrivateFieldGet(this, _CliOutput_deletedFileCount, "f") > 0
? `${__classPrivateFieldGet(this, _CliOutput_mode, "f") === "check" ? "delete" : "deleted"} ${formatCount(__classPrivateFieldGet(this, _CliOutput_deletedFileCount, "f"), "file")}`
: "";
if (__classPrivateFieldGet(this, _CliOutput_mode, "f") === "check" && result.length > 0) {
__classPrivateFieldGet(this, _CliOutput_logger, "f").write(pc.red(pc.bold(`✖ ${result}\n`)));
return {
code: 1,
};
}
__classPrivateFieldGet(this, _CliOutput_logger, "f").write(pc.green(pc.bold(`✔ ${result.length > 0 ? result : "all good!"}\n`)));
return { code: 0 };
}
}
_CliOutput_deletedFileCount = new WeakMap(), _CliOutput_projectRoot = new WeakMap(), _CliOutput_logger = new WeakMap(), _CliOutput_mode = new WeakMap(), _CliOutput_previousFile = new WeakMap(), _CliOutput_instances = new WeakSet(), _CliOutput_relativePath = function _CliOutput_relativePath(file) {
return relative(__classPrivateFieldGet(this, _CliOutput_projectRoot, "f"), file).replaceAll("\\", "/");
};