-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added function to add entities to sidebar automatically
- Loading branch information
1 parent
f2a2ad9
commit aca09f8
Showing
3 changed files
with
71 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { existsSync, readFileSync } from "fs"; | ||
import { formatFilePath } from "../../../filePaths/index.js"; | ||
import { formatTableName } from "../../utils.js"; | ||
import { replaceFile } from "../../../../utils.js"; | ||
|
||
export const addLinkToSidebar = (tableName: string) => { | ||
const { tableNameKebabCase, tableNameNormalEnglishCapitalised } = | ||
formatTableName(tableName); | ||
const sidebarConfigPath = formatFilePath("config/nav.ts", { | ||
prefix: "rootPath", | ||
removeExtension: false, | ||
}); | ||
const configExists = existsSync(sidebarConfigPath); | ||
if (!configExists) return; | ||
|
||
const configContents = readFileSync(sidebarConfigPath, "utf-8"); | ||
const initContents = `export const additionalLinks: AdditionalLinks[] = [];`; | ||
const replacedInitContents = `export const additionalLinks: AdditionalLinks[] = [ | ||
{ | ||
title: "Entities", | ||
links: [ | ||
{ | ||
href: "/${tableNameKebabCase}", | ||
title: "${tableNameNormalEnglishCapitalised}", | ||
icon: Globe, | ||
}, | ||
], | ||
}, | ||
]; | ||
`; | ||
let newContent: string; | ||
if (configContents.search(initContents) > 0) { | ||
newContent = configContents.replace(initContents, replacedInitContents); | ||
} else { | ||
if (configContents.search(tableNameKebabCase) > 0) return; | ||
const searchQuery = ` title: "Entities", | ||
links: [ | ||
`; | ||
const replacement = ` title: "Entities", | ||
links: [ | ||
{ | ||
href: "/${tableNameKebabCase}", | ||
title: "${tableNameNormalEnglishCapitalised}", | ||
icon: Globe, | ||
}, | ||
`; | ||
newContent = configContents.replace(searchQuery, replacement); | ||
console.log(newContent); | ||
} | ||
replaceFile(sidebarConfigPath, newContent); | ||
}; |
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