From 5053af7e411b1311b428276fc62eda2a35627e0f Mon Sep 17 00:00:00 2001 From: DARSAN Date: Thu, 21 Nov 2024 15:04:54 +0530 Subject: [PATCH] CLI updated and new command `init` added to initialise configuration. --- bin/initConfig.ts | 8 +++ bin/rjs.ts | 134 ++++++++++++++++++------------------- bin/userconfig-template.js | 17 +++++ package.json | 11 +-- yarn.lock | 12 +++- 5 files changed, 108 insertions(+), 74 deletions(-) create mode 100644 bin/initConfig.ts create mode 100644 bin/userconfig-template.js diff --git a/bin/initConfig.ts b/bin/initConfig.ts new file mode 100644 index 0000000..2823cac --- /dev/null +++ b/bin/initConfig.ts @@ -0,0 +1,8 @@ +import { copyFileSync } from "node:fs"; +import { join } from "node:path"; + +export default function initConfig() { + const sourceConfig = join(__dirname, "userconfig-template.js"); + const dest = join(process.cwd(), "richie.config.js"); + copyFileSync(sourceConfig, dest); +} diff --git a/bin/rjs.ts b/bin/rjs.ts index 5b14115..2d491fb 100644 --- a/bin/rjs.ts +++ b/bin/rjs.ts @@ -1,82 +1,80 @@ -#! node -import { hideBin } from "yargs/helpers"; -import yargs from "yargs/yargs"; +#! /usr/bin/env node + +import { Command } from "commander"; import { richieOptions } from "../lib/types"; import iconAssociator from "./iconAssociator"; +import initConfig from "./initConfig"; import { makeRichie } from "./richieMaker"; -type availableCommandsOP = "init" | "make"; - -function main(): Promise { - const availableCommands: availableCommandsOP[] = ["init", "make"]; - const givenCommand: availableCommandsOP = process - .argv[2] as availableCommandsOP; - - const unsupportedAlert = (): void => { - console.log( - `Unsupported command\nAvailable commands are\n${availableCommands}`, - ); - }; - - if (!availableCommands.includes(givenCommand)) { - unsupportedAlert(); - process.exit(1); - } +const program = new Command(); - //handle flag options - const argv: richieOptions = yargs(hideBin(process.argv)) - .option("destDir", { - alias: "d", - type: "string", - description: "Destination directory", - default: "dist", - }) - .option("omitPatterns", { - alias: "no", - type: "array", - description: "Omit directory / glob pattern", - default: [], - }) - .option("norm", { - alias: "p", - type: "boolean", - description: "Preserve current destination dir as it is", - default: false, - }).argv as richieOptions; +async function main() { + program + .name("richie") + .description( + "Richie.js an open source SEO tool, rich result generator.", + ) + .version("2.0.0"); - return new Promise((resolve, reject) => { - switch (givenCommand) { - case "make": - makeRichie({ - destDir: argv.destDir, - omitPatterns: argv.omitPatterns, - norm: argv.norm, - }) - .then(() => { - resolve(); - }) - .catch((err) => { - reject(err); - }); - break; + // 'make' command + program + .command("make") + .description("Generate rich result snippet for all HTML and inject it") + .option("-d, --destDir ", "Destination directory", "dist") + .option( + "-o, --omitPatterns ", + "Omit directory / glob patterns", + [], + ) + .option( + "-p, --norm", + "Preserve current destination directory as it is", + false, + ) + .action(async (options: richieOptions) => { + try { + await makeRichie({ + destDir: options.destDir, + omitPatterns: options.omitPatterns, + norm: options.norm, + }); + console.log( + "✅ Rich result snippets are generated for all HTML & saved.", + ); + } catch (err) { + console.error("⚠️ Error generating rich result snippets:", err); + process.exit(1); + } + }); - case "init": - iconAssociator() - .then(() => { - resolve(); - }) - .catch((err) => { - reject(err); - }); - break; - default: - unsupportedAlert(); + // 'init' command + program + .command("init") + .description("Initialize Richie.js configurations") + .action(async () => { + try { + await iconAssociator(); + initConfig(); + console.log("🚀 Richie.js configuration initialised."); + } catch (err) { + console.error("⚠️ Error initializing icon associations:", err); process.exit(1); - } + } + }); + + // Handle unsupported commands + program.on("command:*", (commands) => { + console.error( + `⚠️ Unsupported command: ${commands[0]}\nAvailable commands are: make, init`, + ); + process.exit(1); }); + + // Parse the arguments + await program.parseAsync(process.argv); } main().catch((err) => { - console.log(err); + console.error("⚠️ Unexpected error:", err); process.exit(1); }); diff --git a/bin/userconfig-template.js b/bin/userconfig-template.js new file mode 100644 index 0000000..d47eb3d --- /dev/null +++ b/bin/userconfig-template.js @@ -0,0 +1,17 @@ +/** @type {import("@cresteem/richie-js").rjsOptions} */ +const config = { + domainAddress: "example.com", + preference: { + isCarousals: { + movie: false, + course: false, + recipe: false, + restaurant: false, + }, + isProductVar: false, + breadcrumb: false, + siteSearchBoxFieldName: "query", + }, +}; + +exports.default = config; diff --git a/package.json b/package.json index fcd5c65..f08b36b 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,8 @@ }, "scripts": { "bundle": "node ./dev/bundler.js", - "dev": "rimraf dist && tsc -p tscdev.json && yarn bundle -dev", - "build": "cls && rimraf dist && tsc -p tsconfig.json && yarn bundle -prod", + "dev": "rimraf dist && tsc -p tscdev.json && yarn bundle -dev && yarn ncp ./bin/userconfig-template.js ./dist/bin/userconfig-template.js", + "build": "cls && rimraf dist && tsc -p tsconfig.json && yarn bundle -prod && yarn ncp ./bin/userconfig-template.js ./dist/bin/userconfig-template.js", "test": "jest", "clean": "cls && rimraf dist", "deploy": "yarn build && yarn publish --access public && git push" @@ -68,14 +68,15 @@ "dependencies": { "@prettier/sync": "0.5.2", "cheerio": "1.0.0", + "commander": "^12.1.0", "country-list": "2.3.0", "glob": "11.0.0", "js-sha3": "^0.9.3", "luxon": "3.5.0", - "prettier": "^3.3.3", - "yargs": "17.7.2" + "prettier": "^3.3.3" }, "devDependencies": { + "ncp": "^2.0.0", "@babel/core": "7.26.0", "@babel/preset-env": "7.26.0", "@babel/preset-typescript": "7.26.0", @@ -95,4 +96,4 @@ "ts-node": "10.9.2", "typescript": "^5.6.3" } -} +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 03564b3..98d9757 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2112,6 +2112,11 @@ color-name@~1.1.4: resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== +commander@^12.1.0: + version "12.1.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-12.1.0.tgz#01423b36f501259fdaac4d0e4d60c96c991585d3" + integrity sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA== + concat-map@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" @@ -3553,6 +3558,11 @@ natural-compare@^1.4.0: resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw== +ncp@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ncp/-/ncp-2.0.0.tgz#195a21d6c46e361d2fb1281ba38b91e9df7bdbb3" + integrity sha512-zIdGUrPRFTUELUvr3Gmc7KZ2Sw/h1PiVM0Af/oHB6zgnV1ikqSfRk+TOufi79aHYCW3NiOXmr1BP5nWbzojLaA== + netmask@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/netmask/-/netmask-2.0.2.tgz#8b01a07644065d536383835823bc52004ebac5e7" @@ -4457,7 +4467,7 @@ yargs-parser@^21.1.1: resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35" integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw== -yargs@17.7.2, yargs@^17.3.1, yargs@^17.7.2: +yargs@^17.3.1, yargs@^17.7.2: version "17.7.2" resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.7.2.tgz#991df39aca675a192b816e1e0363f9d75d2aa269" integrity sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==