-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: iamspdarsan <[email protected]>
- Loading branch information
Showing
12 changed files
with
439 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
.vscode | ||
dummy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.