Skip to content

Commit

Permalink
CLI updated and new command init added to initialise configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Nov 21, 2024
1 parent 40c2198 commit 5053af7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 74 deletions.
8 changes: 8 additions & 0 deletions bin/initConfig.ts
Original file line number Diff line number Diff line change
@@ -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);
}
134 changes: 66 additions & 68 deletions bin/rjs.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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 <string>", "Destination directory", "dist")
.option(
"-o, --omitPatterns <patterns...>",
"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);
});
17 changes: 17 additions & 0 deletions bin/userconfig-template.js
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -95,4 +96,4 @@
"ts-node": "10.9.2",
"typescript": "^5.6.3"
}
}
}
12 changes: 11 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 5053af7

Please sign in to comment.