-
Notifications
You must be signed in to change notification settings - Fork 10
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
1 parent
5d45a14
commit db25bba
Showing
7 changed files
with
302 additions
and
1 deletion.
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,129 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
import jsdom from "jsdom"; | ||
import MarkdownIt from "markdown-it"; | ||
|
||
import { docsPath, hooksPath, languagesFormat } from "../const"; | ||
|
||
import { readDirRecursively, tableToJson } from "./utils"; | ||
|
||
const md = new MarkdownIt(); | ||
|
||
async function writeComment(markdownPath) { | ||
const markdown = fs.readFileSync(markdownPath, "utf-8"); | ||
const result = md.render(markdown, "utf-8"); | ||
const dom: HTMLElement = new jsdom.JSDOM(result).window.document; | ||
|
||
const target = dom.querySelector("h3")?.textContent; | ||
const targetDescription = dom.querySelectorAll("p")[0]?.textContent; | ||
const targetRequireParameterList = tableToJson(dom.querySelectorAll("table")[0]); | ||
const targetRequireParameterInsertList: string[] = []; | ||
for (const row of targetRequireParameterList) { | ||
let targetRequireParameterContent = " * @param"; | ||
for (let i = 0; i < row.length; i++) { | ||
if ((i == 0 || i == 3) && row[i]?.textContent) { | ||
const replacedStr = row[i].innerHTML | ||
.replace(/<a href="(.*)">(.*)<\/a>/, "[$2]($1)") | ||
.replace(/<code>(.*?)<\/code>/g, "`$1`"); | ||
targetRequireParameterContent = targetRequireParameterContent + " " + replacedStr; | ||
} | ||
if (i == 1 && row[i]?.textContent) { | ||
targetRequireParameterContent = | ||
targetRequireParameterContent + " " + "{" + row[i].textContent + "}"; | ||
} | ||
} | ||
targetRequireParameterInsertList.push(targetRequireParameterContent); | ||
} | ||
const targetReturnParameterList = tableToJson(dom.querySelectorAll("table")[1]); | ||
const targetReturnParameterInsertList: string[] = []; | ||
for (const row of targetReturnParameterList) { | ||
let targetReturnParameterContent = " * @return"; | ||
for (let i = 0; i < row.length; i++) { | ||
if ((i == 0 || i == 1) && row[i]?.textContent) { | ||
targetReturnParameterContent = targetReturnParameterContent + " " + row[i].textContent; | ||
} | ||
} | ||
targetReturnParameterInsertList.push(targetReturnParameterContent); | ||
} | ||
|
||
const files = fs.readdirSync(hooksPath); | ||
files.forEach(file => { | ||
const filePath = path.join(hooksPath, file); | ||
|
||
let content = fs.readFileSync(filePath, "utf-8"); | ||
|
||
if (content.includes(`export function ${target}`)) { | ||
let comment = ` | ||
/** | ||
* ${targetDescription} | ||
* | ||
`; | ||
if (targetRequireParameterInsertList.length > 0) { | ||
comment = comment.concat(targetRequireParameterInsertList.join("\n")); | ||
} | ||
if (targetReturnParameterInsertList.length > 0) { | ||
comment = comment.concat(`\n`); | ||
comment = comment.concat(targetReturnParameterInsertList.join("\n")); | ||
} | ||
comment = comment.concat(`\n */`); | ||
const position = content.indexOf(`export function ${target}`); | ||
content = content.slice(0, position - 1) + comment + content.slice(position - 1); | ||
fs.writeFileSync(filePath, content); | ||
} | ||
}); | ||
} | ||
|
||
async function cleanComment(filePath) { | ||
let content = fs.readFileSync(filePath, "utf-8"); | ||
const regex = /\/\*(.*?)\*\//gs; | ||
|
||
content = content.replace(regex, (match, group) => { | ||
if (group.includes("@ignore")) { | ||
return match; | ||
} else if (group.includes("@__PURE__")) { | ||
return match; | ||
} else { | ||
return ""; | ||
} | ||
}); | ||
fs.writeFileSync(filePath, content); | ||
} | ||
|
||
//hooks clean | ||
await readDirRecursively(`${hooksPath}`, async (filePath: string) => { | ||
if ( | ||
filePath.includes("client.ts") || | ||
filePath.includes("context.ts") || | ||
filePath.includes("tracks.ts") || | ||
filePath.includes("users.ts") | ||
) { | ||
await cleanComment(filePath); | ||
} | ||
}); | ||
|
||
//hooks inject | ||
await readDirRecursively(`${docsPath}/hooks`, async (filePath: string) => { | ||
if (filePath.includes(languagesFormat[1])) { | ||
await writeComment(filePath); | ||
} | ||
}); | ||
|
||
//interfaces clean | ||
await readDirRecursively(`${hooksPath}`, async (filePath: string) => { | ||
if ( | ||
filePath.includes("client.ts") || | ||
filePath.includes("context.ts") || | ||
filePath.includes("tracks.ts") || | ||
filePath.includes("users.ts") | ||
) { | ||
await cleanComment(filePath); | ||
} | ||
}); | ||
|
||
//interfaces inject | ||
await readDirRecursively(`${docsPath}/hooks`, async (filePath: string) => { | ||
if (filePath.includes(languagesFormat[1])) { | ||
await writeComment(filePath); | ||
} | ||
}); |
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,99 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
import jsdom from "jsdom"; | ||
import MarkdownIt from "markdown-it"; | ||
|
||
import { componentsPath, docsPath, languagesFormat } from "../const"; | ||
|
||
import { readDirRecursively, tableToJson } from "./utils"; | ||
|
||
const md = new MarkdownIt(); | ||
|
||
async function writeComment(markdownPath) { | ||
const markdown = fs.readFileSync(markdownPath, "utf-8"); | ||
const result = md.render(markdown, "utf-8"); | ||
const dom: HTMLElement = new jsdom.JSDOM(result).window.document; | ||
|
||
const target = dom.querySelector("h3")?.textContent; | ||
const targetDescription = dom.querySelectorAll("p")[0]?.textContent; | ||
const targetRequireParameterList = tableToJson(dom.querySelectorAll("table")[0]); | ||
|
||
const files = fs.readdirSync(componentsPath); | ||
files.forEach(file => { | ||
const filePath = path.join(componentsPath, file); | ||
|
||
let content = fs.readFileSync(filePath, "utf-8"); | ||
|
||
if (content.includes(`export function ${target}`)) { | ||
let comment = ` | ||
/** | ||
* ${targetDescription} | ||
`; | ||
comment = comment.concat(`\n */`); | ||
const position = content.indexOf(`export function ${target}`); | ||
content = content.slice(0, position - 1) + comment + content.slice(position - 1); | ||
|
||
for (const row of targetRequireParameterList) { | ||
let interfaceName = ""; | ||
let replacedStr = ""; | ||
if (row[0]?.textContent) { | ||
interfaceName = row[0].textContent; | ||
} | ||
if (row[3]?.innerHTML) { | ||
replacedStr = row[3]?.innerHTML | ||
.replace(/<a href="(.*)">(.*)<\/a>/, "[$2]($1)") | ||
.replace(/<li>(.*?)<\/li>/g, "$1") | ||
.replace(/<code>(.*?)<\/code>/g, "`$1`"); | ||
} | ||
const interfaceComment = ` | ||
/** | ||
* ${replacedStr} | ||
*/\n`; | ||
if (content.includes(`readonly ${interfaceName}`)) { | ||
const interfacePosition = content.indexOf(`readonly ${interfaceName}`); | ||
content = | ||
content.slice(0, interfacePosition - 1) + | ||
interfaceComment + | ||
content.slice(interfacePosition - 1); | ||
} | ||
} | ||
fs.writeFileSync(filePath, content); | ||
} | ||
}); | ||
} | ||
|
||
async function cleanComment(filePath) { | ||
let content = fs.readFileSync(filePath, "utf-8"); | ||
const regex = /\/\*(.*?)\*\//gs; | ||
|
||
content = content.replace(regex, (match, group) => { | ||
if (group.includes("@ignore")) { | ||
return match; | ||
} else if (group.includes("@__PURE__")) { | ||
return match; | ||
} else { | ||
return ""; | ||
} | ||
}); | ||
fs.writeFileSync(filePath, content); | ||
} | ||
|
||
//components clean | ||
await readDirRecursively(`${componentsPath}`, async (filePath: string) => { | ||
if ( | ||
!filePath.includes(".stories.tsx") && | ||
!filePath.includes("styles.ts") && | ||
!filePath.includes("TrackBoundary.tsx") && | ||
!filePath.includes("UserCover.tsx") | ||
) { | ||
await cleanComment(filePath); | ||
} | ||
}); | ||
|
||
// components inject | ||
await readDirRecursively(`${docsPath}/components`, async (filePath: string) => { | ||
if (filePath.includes(languagesFormat[1])) { | ||
await writeComment(filePath); | ||
} | ||
}); |
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,32 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
export function tableToJson(table) { | ||
if (!table) { | ||
return []; | ||
} | ||
const data: HTMLTableCellElement[][] = []; | ||
for (let i = 1; i < table.rows.length; i++) { | ||
const tableRow: HTMLTableRowElement = table.rows[i]; | ||
const rowData: HTMLTableCellElement[] = []; | ||
for (let j = 0; j < tableRow.cells.length; j++) { | ||
rowData.push(tableRow.cells[j]); | ||
} | ||
data.push(rowData); | ||
} | ||
return data; | ||
} | ||
|
||
export async function readDirRecursively(dir, handler) { | ||
const files = fs.readdirSync(dir); | ||
files.forEach(async file => { | ||
const filePath = path.join(dir, file); | ||
const stat = fs.statSync(filePath); | ||
|
||
if (stat.isDirectory()) { | ||
readDirRecursively(filePath, handler); | ||
} else { | ||
handler && handler(filePath); | ||
} | ||
}); | ||
} |