Skip to content

Commit

Permalink
✨ Add examples to README.MD automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
47PADO47 committed Aug 13, 2022
1 parent eb120a2 commit 4f6199b
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/UpdateDocs.ts
Expand Up @@ -62,6 +62,7 @@ const data = {
await console.log('[🌐] Updated all classes');

await readme(dir, readmeString);
await examples(dir);

return console.log('[πŸ“ƒ] Updated all files');
})();
Expand Down Expand Up @@ -90,6 +91,38 @@ async function readme(docsDir: string, newContent: string) {
console.log(`${tab}- Updated "README.md" βœ…`);
}

async function examples(docsDir: string) {
console.log('[πŸ“¦] Updating examples...');
const path = join(docsDir, 'Examples');
await existsOrCreate({
path,
type: 'dir'
});

let string = "";
const examples = fs.readdirSync(path);
examples.forEach(async (example: string) => {
string+=`\nβ€’ [${removeExtension(example)}](docs/Examples/${example})\n`
console.log(`${tab}- Added example "${example}" πŸ†—`);
});

if (!string.length) {
string = '\nThere are no examples yet.\n';
console.log(`${tab}- "Examples" folder is empty ⚠️`);
}

const readmePath = join(process.cwd(), 'README.md');
const readmeContent = await fs.readFileSync(readmePath, 'utf-8');
const split: string[] = readmeContent.split(`## Examples`);

if (split.length === 0) return error('Couldn\'t find "Examples" section in README.md');
const toReplace = split.pop()?.split('##')[0] ?? '';
fs.writeFileSync(readmePath, readmeContent.replace(toReplace, `\n${string}\n`));

console.log('[βœ…] Updated examples');
return;
}

async function existsOrCreate({ path, type = 'dir' }: FScheckOptions): Promise<boolean> {
if (await fs.existsSync(path)) return true;

Expand All @@ -100,7 +133,6 @@ async function existsOrCreate({ path, type = 'dir' }: FScheckOptions): Promise<b
return true;
} catch (e: unknown) {
error(`Couldn't create "${path}" path\n`);
return false;
}
}

Expand Down

0 comments on commit 4f6199b

Please sign in to comment.