Skip to content

Commit

Permalink
run generator on prep
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser committed Dec 19, 2024
1 parent 673130f commit 620cc2e
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 121 deletions.
1 change: 1 addition & 0 deletions www/apps/book/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-react-hooks": "^5.0.0",
"postcss": "^8",
"tags": "*",
"tailwind": "*",
"tailwindcss": "^3.3.0",
"tsconfig": "*",
Expand Down
3 changes: 3 additions & 0 deletions www/apps/book/scripts/prepare.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { generateEditedDates } from "build-scripts"
import path from "path"
import { generateTags } from "tags"

async function main() {
await generateEditedDates()
await generateTags(path.resolve("..", "..", "packages", "tags"))
}

void main()
1 change: 1 addition & 0 deletions www/apps/resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"eslint-plugin-react-hooks": "^5.0.0",
"postcss": "^8",
"remark-rehype-plugins": "*",
"tags": "*",
"tailwind": "*",
"tailwindcss": "^3.3.0",
"ts-node": "^10.9.2",
Expand Down
3 changes: 3 additions & 0 deletions www/apps/resources/scripts/prepare.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { generateEditedDates, generateSidebar } from "build-scripts"
import { generateTags } from "tags"
import { main as generateSlugChanges } from "./generate-slug-changes.mjs"
import { main as generateFilesMap } from "./generate-files-map.mjs"
import { sidebar } from "../sidebar.mjs"
import path from "path"

async function main() {
await generateSidebar(sidebar)
await generateSlugChanges()
await generateFilesMap()
await generateEditedDates()
await generateTags(path.resolve("..", "..", "packages", "tags"))
}

void main()
1 change: 1 addition & 0 deletions www/packages/tags/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./utils/index.js"
121 changes: 2 additions & 119 deletions www/packages/tags/src/scripts/generate-tags.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,3 @@
import { statSync } from "fs"
import { mkdir, readdir, rm, writeFile } from "fs/promises"
import path from "path"
import type { Tags } from "types"
import { findPageTitle, getFrontMatterSync } from "utils"
import { generateTags } from "../utils/generate-tags.js"

type ConfigItem = {
path: string
contentPaths: string[]
}

const config: ConfigItem[] = [
{
path: path.resolve("..", "..", "apps", "book"),
contentPaths: ["app"],
},
{
path: path.resolve("..", "..", "apps", "resources"),
contentPaths: ["app", "references"],
},
{
path: path.resolve("..", "..", "apps", "ui"),
contentPaths: [path.join("src", "content", "docs")],
},
{
path: path.resolve("..", "..", "apps", "user-guide"),
contentPaths: ["app"],
},
]

function normalizePageTitle(title: string): string {
// remove variables from title
return title.replaceAll(/\$\{.+\}/g, "").trim()
}

function tagNameToFileName(tagName: string): string {
return `${tagName.toLowerCase().replaceAll(" ", "-")}.ts`
}

function tagNameToVarName(tagName: string): string {
return tagName
.toLowerCase()
.replaceAll(/\s([a-zA-Z\d])/g, (captured) => captured.toUpperCase().trim())
}

async function main() {
const tags: Tags = {}
async function getTags(item: ConfigItem) {
async function scanDirectory(dirPath: string) {
const files = await readdir(dirPath)

for (const file of files) {
const fullPath = path.join(dirPath, file)
if (!file.endsWith(".mdx") || file.startsWith("_")) {
if (statSync(fullPath).isDirectory()) {
await scanDirectory(fullPath)
}
continue
}

const frontmatter = getFrontMatterSync(fullPath)
const fileBasename = path.basename(file)

frontmatter.tags?.forEach((tag) => {
if (!Object.hasOwn(tags, tag)) {
tags[tag] = []
}

tags[tag].push({
title: normalizePageTitle(
frontmatter.sidebar_label || findPageTitle(fullPath) || ""
),
path:
frontmatter.slug ||
fullPath.replace(item.path, "").replace(`/${fileBasename}`, ""),
})
})
}
}

for (const contentPath of item.contentPaths) {
const basePath = path.join(item.path, contentPath)

await scanDirectory(basePath)
}
}

await Promise.all(
config.map(async (item) => {
await getTags(item)
})
)

const tagsDir = path.join("src", "tags")
// clear existing tags
await rm(tagsDir, {
recursive: true,
force: true,
})
await mkdir(tagsDir)
// write tags
const files: string[] = []
await Promise.all(
Object.keys(tags).map(async (tagName) => {
const fileName = tagNameToFileName(tagName)
const varName = tagNameToVarName(tagName)

const content = `export const ${varName} = ${JSON.stringify(tags[tagName], null, 2)}`

await writeFile(path.join(tagsDir, fileName), content)
files.push(fileName.replace(/\.ts$/, ".js"))
})
)

// write index.ts
const indexContent = files.map((file) => `export * from "./${file}"\n`)
await writeFile(path.join(tagsDir, "index.ts"), indexContent)
}

void main()
void generateTags()
119 changes: 119 additions & 0 deletions www/packages/tags/src/utils/generate-tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { statSync } from "fs"
import { mkdir, readdir, rm, writeFile } from "fs/promises"
import path from "path"
import type { Tags } from "types"
import { findPageTitle, getFrontMatterSync } from "utils"

type ConfigItem = {
path: string
contentPaths: string[]
}

const config: ConfigItem[] = [
{
path: path.resolve("..", "..", "apps", "book"),
contentPaths: ["app"],
},
{
path: path.resolve("..", "..", "apps", "resources"),
contentPaths: ["app", "references"],
},
{
path: path.resolve("..", "..", "apps", "ui"),
contentPaths: [path.join("src", "content", "docs")],
},
{
path: path.resolve("..", "..", "apps", "user-guide"),
contentPaths: ["app"],
},
]

function normalizePageTitle(title: string): string {
// remove variables from title
return title.replaceAll(/\$\{.+\}/g, "").trim()
}

function tagNameToFileName(tagName: string): string {
return `${tagName.toLowerCase().replaceAll(" ", "-")}.ts`
}

function tagNameToVarName(tagName: string): string {
return tagName
.toLowerCase()
.replaceAll(/\s([a-zA-Z\d])/g, (captured) => captured.toUpperCase().trim())
}

export async function generateTags(basePath?: string) {
basePath = basePath || path.resolve()
const tags: Tags = {}
async function getTags(item: ConfigItem) {
async function scanDirectory(dirPath: string) {
const files = await readdir(dirPath)

for (const file of files) {
const fullPath = path.join(dirPath, file)
if (!file.endsWith(".mdx") || file.startsWith("_")) {
if (statSync(fullPath).isDirectory()) {
await scanDirectory(fullPath)
}
continue
}

const frontmatter = getFrontMatterSync(fullPath)
const fileBasename = path.basename(file)

frontmatter.tags?.forEach((tag) => {
if (!Object.hasOwn(tags, tag)) {
tags[tag] = []
}

tags[tag].push({
title: normalizePageTitle(
frontmatter.sidebar_label || findPageTitle(fullPath) || ""
),
path:
frontmatter.slug ||
fullPath.replace(item.path, "").replace(`/${fileBasename}`, ""),
})
})
}
}

for (const contentPath of item.contentPaths) {
const basePath = path.join(item.path, contentPath)

await scanDirectory(basePath)
}
}

await Promise.all(
config.map(async (item) => {
await getTags(item)
})
)

const tagsDir = path.join(basePath, "src", "tags")
// clear existing tags
await rm(tagsDir, {
recursive: true,
force: true,
})
await mkdir(tagsDir)
// write tags
const files: string[] = []
await Promise.all(
Object.keys(tags).map(async (tagName) => {
const fileName = tagNameToFileName(tagName)
const varName = tagNameToVarName(tagName)

const content = `export const ${varName} = ${JSON.stringify(tags[tagName], null, 2)}`

await writeFile(path.join(tagsDir, fileName), content)
files.push(fileName.replace(/\.ts$/, ".js"))
})
)

// write index.ts
const indexContent = files.map((file) => `export * from "./${file}"\n`)
await writeFile(path.join(tagsDir, "index.ts"), indexContent)
}
2 changes: 2 additions & 0 deletions www/packages/tags/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./generate-tags.js"
export * from "./tags.js"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Tag, Tags } from "types"
import * as tags from "./tags/index.js"
import * as tags from "../tags/index.js"

export const getTagItems = (tagName: string): Tag | undefined => {
if (!Object.hasOwn(tags, tagName)) {
Expand Down
4 changes: 3 additions & 1 deletion www/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7142,6 +7142,7 @@ __metadata:
rehype-mdx-code-props: ^2.0.0
rehype-slug: ^6.0.0
remark-rehype-plugins: "*"
tags: "*"
tailwind: "*"
tailwindcss: ^3.3.0
tsconfig: "*"
Expand Down Expand Up @@ -14819,6 +14820,7 @@ __metadata:
remark-directive: ^3.0.0
remark-frontmatter: ^5.0.0
remark-rehype-plugins: "*"
tags: "*"
tailwind: "*"
tailwindcss: ^3.3.0
ts-node: ^10.9.2
Expand Down Expand Up @@ -15652,7 +15654,7 @@ __metadata:
languageName: node
linkType: hard

"tags@workspace:packages/tags":
"tags@*, tags@workspace:packages/tags":
version: 0.0.0-use.local
resolution: "tags@workspace:packages/tags"
dependencies:
Expand Down

0 comments on commit 620cc2e

Please sign in to comment.