Skip to content

Commit

Permalink
#13's 4/6 and #12 are fixed
Browse files Browse the repository at this point in the history
Signed-off-by: iamspdarsan <[email protected]>
  • Loading branch information
darsan-in committed May 23, 2024
1 parent 4c5d61d commit daf3de4
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
.vscode
dummy
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## License

RICHIE JS © 2024 by [CRESTEEM](https://www.cresteem.com/) is licensed under [APACHE LICENSE VERSION 2.0](https://www.apache.org/licenses/LICENSE-2.0.txt)
RICHIE JS © 2024 by 🚀[CRESTEEM](https://www.cresteem.com/) is licensed under [APACHE LICENSE VERSION 2.0](https://www.apache.org/licenses/LICENSE-2.0.txt)

## Contribution

Expand All @@ -17,3 +17,40 @@ In addition to the terms of the Apache License, Version 2.0, you are required to
"This software includes RICHIE JS © 2024 by [CRESTEEM](https://www.cresteem.com/). Licensed under the Apache License, Version 2.0."

This attribution must be displayed in a prominent location in any documentation, notices, or marketing materials accompanying the distribution or use of this software.

## 👨🏻‍💻 CLI
```bash
//make rich result
npx rjs make

//store output in different directories
npx rjs make -d destFolder

//omit files and folders
npx rjs make --no relativePath/subpath
```

### ⚠️ By default Richie JS removes previous outputs or emptying destination directory.
#### If you want to disable this? use --norm or -p flag
```bash
npx rjs make -p
/* or */
npx rjs make --norm
```

## ⚙️🛠️ Configuration
You can override default setting by making <b><q>rjs.config.json</q></b> inside project's root directory.


## 🤖 Configuring Richie JS with intellisense support
#### ⚠️ Both are only for VS Code

#### Option-1 : System User wide support
```bash
npx rjs isense user
```

#### Option-2 : Project wide support
```bash
npx rjs isense ws
```
190 changes: 190 additions & 0 deletions bin/richieMaker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import { rmSync } from "fs";
import { readFile } from "fs/promises";
import { globSync } from "glob";
import { dirname, join, relative, basename } from "path";
import configuration from "../configLoader";
import { richies } from "../lib/options";

import { mkdirpSync } from "mkdirp";
import { richie } from "../richie";
const { reservedNames, preference } = configuration;
import { richieOptions } from "../lib/options";

const richieCarousals: Partial<richies>[] = [
"movie",
"course",
"recipe",
"restaurant",
];

const isCarousals = preference.isCarousals;

const richieDefaultOptions: richieOptions = {
searchExtensions: ["html"],
};

export async function makeRichie(
options: richieOptions = richieDefaultOptions,
): Promise<void> {
//making sure not miss-out unpassed parameter to default vaule
options = { ...richieDefaultOptions, ...options };

//remove previous op dir and files
if (!options.norm) {
rmSync(options.destDir as string, { recursive: true });
}

const concurrentOPS: Promise<void>[] = [];

//search for available files
const filePatterns: string[] = options.searchExtensions?.map(
(elem: string) => {
return join(process.cwd(), `**/*.${elem}`).replace(/\\/g, "/");
},
) as string[];

//update omitpatterns
options.omitPatterns = [
options.destDir as string,
"node_modules",
...(options.omitPatterns ?? []),
]?.map(
(elem: string) =>
join(process.cwd(), `${elem}`).replace(/\\/g, "/") + "/**",
);

const availableFiles: string[] = globSync(filePatterns, {
ignore: options.omitPatterns,
});

console.log("Number of Files: ", availableFiles.length);

//Read the file and check for input artifacts
for (const file of availableFiles) {
concurrentOPS.push(
new Promise((resolve, reject) => {
readFile(file, { encoding: "utf8" })
.then((htmlText) => {
const neededRichies: richies[] =
richieTypeAcquisition(htmlText);

neededRichies.forEach((richieName) => {
const dest = join(
process.cwd(),
options.destDir ?? "",
dirname(relative(process.cwd(), file)),
basename(file),
);

try {
//make dir
mkdirpSync(dirname(dest));
} catch (err: any) {
/* console.log(err.code); */
}

//carousal handler
if (
richieCarousals.includes(richieName) ||
richieName === "product"
) {
switch (richieName) {
case "recipe":
if (isCarousals.recipe) {
richieName = "crecipe";
}
break;
case "movie":
if (isCarousals.movie) {
richieName = "cmovie";
}
break;
case "restaurant":
if (isCarousals.restaurant) {
richieName = "crestaurant";
}
break;
case "course":
if (isCarousals.course) {
richieName = "ccourse";
}
break;
case "product":
if (preference.isProductVar) {
richieName = "productwv";
}
break;
}
}

//

richie(richieName, file, dest)
.then(() => {
resolve();
})
.catch((err) => {
reject(err);
});
});
})
.catch((err) => {
reject(err);
});
}),
);
}

await Promise.all(concurrentOPS);
}

// File type acquisition based on artifacts
function richieTypeAcquisition(htmlText: string): richies[] {
const availableTypes: richies[] = [];

//a=len(2)
const noIDTypes: richies[] = ["breadcrumb", "searchbox"];

//b=len(5) a+b = 7
const IDTypesVars: richies[] = [
"crecipe",
"cmovie",
"course",
"crestaurant",
"productwv",
];

//c=len(13) a+b+c = 20
const IDTypesRecord: Partial<Record<richies, string>> = {
article: reservedNames.article.baseID,
recipe: reservedNames.recipe.baseID,
movie: reservedNames.movie.baseID,

restaurant: reservedNames.restaurant.baseID,
course: reservedNames.course.baseID,
event: reservedNames.events.baseID,

faq: reservedNames.faqPage.baseID,
video: reservedNames.video.baseID,
localbusiness: reservedNames.localBusiness.baseID,

organization: reservedNames.organisation.baseID,
product: reservedNames.product.baseID,
profile: reservedNames.profilePage.baseID,

software: reservedNames.softwareApp.baseID,
};

Object.keys(IDTypesRecord).forEach((richieName) => {
const searchPattern: string | boolean =
IDTypesRecord[richieName as richies] ?? false;

if (searchPattern) {
if (htmlText.includes(searchPattern)) {
availableTypes.push(richieName as richies);
}
}
});

return availableTypes;
}
81 changes: 81 additions & 0 deletions bin/rjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#! node
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { isense } from "./schemaWriter";
import { makeRichie } from "./richieMaker";
import { richieOptions } from "lib/options";

type availableCommandsOP = "isense" | "make";

function main(): Promise<void> {
const availableCommands: availableCommandsOP[] = ["isense", "make"];
const givenCommand: availableCommandsOP = process
.argv[2] as availableCommandsOP;

const unsupportedAlert = () => {
console.log(
`Unsupported command\nAvailable commands are\n${availableCommands}`,
);
};

if (!availableCommands.includes(givenCommand)) {
unsupportedAlert();
process.exit(1);
}

//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;

return new Promise((resolve, reject) => {
switch (givenCommand) {
case "isense":
isense()
.then(() => {
resolve();
})
.catch((err) => {
reject(err);
});
break;
case "make":
makeRichie({
destDir: argv.destDir,
omitPatterns: argv.omitPatterns,
norm: argv.norm,
})
.then(() => {
resolve();
})
.catch((err) => {
reject(err);
});
break;
default:
unsupportedAlert();
process.exit(1);
}
});
}

main().catch((err) => {
console.log(err);
process.exit(1);
});
Loading

0 comments on commit daf3de4

Please sign in to comment.