Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
100 additions
and
32 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,41 +1,109 @@ | ||
import Classeviva from './Classeviva'; | ||
import { readdirSync, readFileSync, writeFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import fs from 'fs'; | ||
import { Class, FScheckOptions } from './typings/Docs'; | ||
|
||
const tab = ' '; | ||
const data = { | ||
Methods: (cls: any) => Object.getOwnPropertyNames(Object.getPrototypeOf(new cls)).filter(method => method !== "constructor").map((m: string) => `> ${m}\n`), | ||
Properties: (cls: any) => Object.getOwnPropertyNames(new cls).map((prop: string) => `> ${prop}\n`), | ||
} as { [key: string]: (cls: Class) => string[]}; | ||
|
||
(async () => { | ||
console.log('[π] Updating docs...'); | ||
|
||
const dir: string = `${process.cwd()}/docs/`; | ||
const filesObj: { [key: string]: string } = {}; | ||
const files: string[] = readdirSync(dir); | ||
files.forEach((file: string)=> filesObj[file] = `${dir}/${file}`); | ||
const dir: string = join(process.cwd(), `docs`); | ||
const classesPath: string = join(__dirname, 'classes'); | ||
|
||
if (!await existsOrCreate({ path: dir }) || !await existsOrCreate({ path: classesPath })) return console.log('[β] Missing paths'); | ||
|
||
const classes: string[] = fs.readdirSync(classesPath).filter(file => file.endsWith('.js')); | ||
const files: string[] = Object.keys(data); | ||
|
||
const data = { | ||
Methods: Object.getOwnPropertyNames(Classeviva.prototype).filter((method: string) => method !== 'constructor').map((m: string) => `> ${m}\n`), | ||
Properties: Object.getOwnPropertyNames(new Classeviva).map((prop: string) => `> ${prop}\n`), | ||
Readme: files.filter((file: string) => file !== "Readme.md").map((file: string) => `- [${file}](${file})\n`), | ||
} as { [key: string]: string[] }; | ||
let readmeString = ''; | ||
for (const cls of classes) { | ||
console.log(`[π§±] Updating class "${cls}"...`); | ||
|
||
for (const file in filesObj) { | ||
if (!Object.prototype.hasOwnProperty.call(filesObj, file)) return; | ||
console.log(`[π] Loading ${file}...`); | ||
const clsPath: string = join(classesPath, cls); | ||
const { default: clsFile } = await import(clsPath) as { default: Class }; | ||
const clsName = removeExtension(cls); | ||
|
||
const docClass = join(dir, clsName); | ||
await existsOrCreate({ | ||
path: docClass, | ||
type: 'dir' | ||
}) | ||
|
||
const location: string = filesObj[file]; | ||
const content: string = readFileSync(location, 'utf8'); | ||
|
||
const split: string[] = file == 'Readme.md' ? content.split('## Documents') : content.split(`# ${file.replace(".md", "")}`);; | ||
const toReplace: string = split.length > 0 ? split[split.length-1] : ''; | ||
|
||
const newContent: string = data[file.replace('.md', '')].join('\n'); | ||
|
||
if (!toReplace.length) { | ||
console.log(` - "${file}" is empty β οΈ`); | ||
writeFileSync(location, `# ${file == 'Readme.md' ? 'Docs\nHere you can find infos about the `Classeviva` class. Choose a document from the list below:\n\n## Documents\n\n' : `${file}\n\n`}${newContent}`); | ||
} else { | ||
console.log(` - Updating "${file}"`); | ||
writeFileSync(location, content.replace(toReplace, `\n\n${newContent}`)); | ||
}; | ||
console.log(` - Updated "${file}" β `); | ||
readmeString+=`\nβ’ ${clsName}\n\n` | ||
for (const file of files) { | ||
console.log(`${tab}- Updating file "${file}"...`); | ||
|
||
const filePath: string = join(docClass, `${file}.md`); | ||
await existsOrCreate({ | ||
path: filePath, | ||
type: 'file' | ||
}); | ||
const content: string = fs.readFileSync(filePath, 'utf8'); | ||
|
||
const split: string[] = content.split(`# ${file}`); | ||
const toReplace: string = split.length > 0 ? (split.pop() ?? '' ): ''; | ||
|
||
const newContent: string = data[file](clsFile).join('\n'); | ||
|
||
if (!toReplace.length) { | ||
console.log(`${tab}- "${file}" is empty β οΈ`); | ||
fs.writeFileSync(filePath, `# ${file}\n\n${newContent}`); | ||
} else fs.writeFileSync(filePath, content.replace(toReplace, `\n\n${newContent}`)); | ||
|
||
readmeString += `${tab}- [${file}](${clsName}/${file}.md)\n`; | ||
console.log(`${tab}- Updated "${file}" π`); | ||
} | ||
console.log(`[β ] Updated class "${cls}"`); | ||
}; | ||
await console.log('[π] Updated all classes'); | ||
|
||
await readme(dir, readmeString); | ||
|
||
return console.log('[π] Updated all files'); | ||
})(); | ||
|
||
async function readme(docsDir: string, newContent: string) { | ||
const path = join(docsDir, 'README.md'); | ||
await existsOrCreate({ | ||
path, | ||
type: 'file' | ||
}) | ||
|
||
let content = await fs.readFileSync(path, 'utf-8'); | ||
if (!content) content = '# Docs\nHere you can find infos about the classes contained in this package. Choose a document from the list below:\n\n## Documents'; | ||
|
||
const split: string[] = content.split(`## Documents`); | ||
const toReplace: string = split.length > 0 ? (split.pop() ?? '' ): ''; | ||
|
||
if (!toReplace.length) { | ||
console.log(`${tab}- "README.md" is empty β οΈ`); | ||
fs.writeFileSync(path, `${content}\n${newContent}`); | ||
} else { | ||
console.log(`${tab}- Updating "README.md"`); | ||
fs.writeFileSync(path, content.replace(toReplace, `\n${newContent}`)); | ||
}; | ||
|
||
console.log(`${tab}- Updated "README.md" β `); | ||
}; | ||
|
||
async function existsOrCreate({ path, type = 'dir' }: FScheckOptions): Promise<boolean> { | ||
if (await fs.existsSync(path)) return true; | ||
|
||
try { | ||
type === 'dir' ? await fs.mkdirSync(path, { | ||
recursive: true | ||
}) : await fs.writeFileSync(path, ''); | ||
return true; | ||
} catch (e: unknown) { | ||
console.log(`[β] Couldn't create "${path}" path\n`); | ||
return false; | ||
}; | ||
}; | ||
|
||
return console.log('[π] Updated all files β '); | ||
})(); | ||
function removeExtension(str: string) { | ||
return str.split('.')[0]; | ||
}; |