convconv
Version:
Naming Conventions Converter
163 lines (162 loc) • 5.97 kB
JavaScript
;
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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert = __importStar(require("node:assert"));
const readline = __importStar(require("node:readline"));
const version_1 = require("./version");
const constants_1 = require("./constants");
const is_operations_1 = require("./is.operations");
const from_operations_1 = require("./from.operations");
function version() {
console.log("version: ", (0, version_1.getVersion)());
}
function help() {
console.log([
"\tconvconv - a tool to work with naming convensions",
"Examples:",
"\tcat file | convconv filter -c kebab",
"\t\tThis command filters lines which have a word which has kebab case convention.",
"\tcat file | convconv convert --from kebab --to camel",
"\t\tThis command converts first word which is kebab case to camel case.",
"",
"Lookup convconv man pages for complete manual",
].join("\n"));
}
function parseArgs() {
var _a, _b, _c, _d, _e, _f, _g, _h;
const argv = process.argv.slice(2);
if (argv.length === 0)
return help();
const showHelp = !!argv.find((p) => ["-h", "--help"].includes(p));
if (showHelp)
return help();
const showVersion = !!argv.find((p) => ["-v", "--version"].includes(p));
if (showVersion)
return version();
const [command, ...options] = argv[0].startsWith("-")
? ["convert", ...argv]
: argv;
const o = options.map((option) => option.trim());
const obj = {};
for (let i = 0; i < o.length; i++) {
const key = o[i];
if (i === o.length - 1) {
obj[key] = true;
continue;
}
const value = o[i + 1];
if (value.startsWith("-")) {
obj[key] = true;
continue;
}
obj[key] = value;
i++;
}
try {
switch (command) {
case "convert":
const from = (_b = (_a = obj["-f"]) !== null && _a !== void 0 ? _a : obj["--from"]) !== null && _b !== void 0 ? _b : "auto";
if (from !== "auto")
assert.ok(constants_1.CONVENTIONS.includes(from));
assert.ok(typeof from === "string");
const to = (_d = (_c = obj["-t"]) !== null && _c !== void 0 ? _c : obj["--to"]) !== null && _d !== void 0 ? _d : "";
assert.ok(constants_1.CONVENTIONS.includes(to));
assert.ok(typeof to === "string");
const all = (_f = (_e = obj["-a"]) !== null && _e !== void 0 ? _e : obj["--all"]) !== null && _f !== void 0 ? _f : false;
assert.ok(typeof all === "boolean");
return {
command: "convert",
args: {
from: from,
to: to,
all,
},
};
case "filter":
const c = (_h = (_g = obj["-c"]) !== null && _g !== void 0 ? _g : obj["--convention"]) !== null && _h !== void 0 ? _h : "";
assert.ok(typeof c === "string");
const conventions = c.split(",");
conventions.forEach((conv) => assert.ok(constants_1.CONVENTIONS.includes(conv)));
return {
command: "filter",
args: {
conventions: conventions,
},
};
default:
return help();
}
}
catch (_) {
return help();
}
}
function findWords(line) {
var _a;
return ((_a = line.match(/\b[a-zA-Z0-9\-\_]*\b/g)) !== null && _a !== void 0 ? _a : []).map((w) => w.toString());
}
function filterLine(line, conventions) {
let match = false;
findWords(line).forEach((word) => conventions.forEach((type) => {
if (is_operations_1.IS_OPERATIONS.isConvention(type, word))
match = true;
}));
if (match)
console.log(line);
}
function filter(types) {
const rl = readline.createInterface(process.stdin);
rl.on("line", (line) => filterLine(line, types));
}
function convertLine(line, args) {
const words = findWords(line);
for (const word of words) {
try {
line = line.replace(word, from_operations_1.FROM_OPERATIONS.autoFrom(word).toConvention(args.to));
if (!args.all) {
break;
}
}
catch (_) { }
}
console.log(line);
}
function convert(args) {
const rl = readline.createInterface(process.stdin);
rl.on("line", (line) => convertLine(line, args));
}
function main() {
const parsed = parseArgs();
if (!parsed)
return process.exit(1);
switch (parsed.command) {
case "filter":
return filter(parsed.args.conventions);
case "convert":
return convert(parsed.args);
}
}
main();