Skip to content

Commit

Permalink
automatically update routes folder if updates are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-8 committed Oct 17, 2023
1 parent f0d7562 commit be2c354
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
40 changes: 34 additions & 6 deletions packages/kitbook/src/lib/plugins/vite/initKitbook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs'
import { join } from 'node:path'
import type { KitbookSettings } from 'kitbook'

const TYPINGS_EXT = '.d.ts'
Expand All @@ -11,32 +12,59 @@ const red = '\x1B[31m'
const bold = '\x1B[1m'
const reset = '\x1B[0m'

const LATEST_VERSION_WITH_ROUTES_UPDATE = '[email protected]^'
const FILE_WITH_NOTICE = '[...file]/+page.svelte'

export function initKitbook({ routesDirectory, kitbookRoute }: KitbookSettings) {
const kitbookDirectory = routesDirectory + kitbookRoute

try {
const kitbookDirectoryExists = fs.existsSync(kitbookDirectory)
const hasFileWithNotice = fs.existsSync(`${kitbookDirectory}/${FILE_WITH_NOTICE}`)

if (kitbookDirectoryExists) {
const files = fs.readdirSync(kitbookDirectory)
const hasFiles = files.length > 0
if (hasFiles)
if (hasFileWithNotice) {
const mainPageFile = fs.readFileSync(`${kitbookDirectory}/${FILE_WITH_NOTICE}`, 'utf-8')
if (mainPageFile?.includes(LATEST_VERSION_WITH_ROUTES_UPDATE))
return
}

const kitbookDirectoryExists = fs.existsSync(kitbookDirectory)
if (!kitbookDirectoryExists)
fs.mkdirSync(kitbookDirectory)

cleanDirectory(kitbookDirectory)

const src = 'node_modules/kitbook/dist/routes'
const destination = kitbookDirectory
fs.cpSync(src, destination, { recursive: true, filter: excludeDocFiles })
console.log(`${bold}${green}[Kitbook] Added Kitbook route files to ${kitbookDirectory}. You don't need to touch these.\n${reset}`)
console.log(`${bold}${green}[Kitbook] Added Kitbook route files to ${kitbookDirectory}. Don't edit these. They will be automatically updated by Kitbook in future versions when needed.\n${reset}`)
}
catch (e) {
console.error(`${bold}${red}[Kitbook] Error copying in needed routes: ${e}\n${reset}`)
}
}

function cleanDirectory(directoryPath: string) {
try {
const files = fs.readdirSync(directoryPath)

for (const file of files) {
const filePath = join(directoryPath, file)
const stats = fs.statSync(filePath)
if (stats.isDirectory()) {
cleanDirectory(filePath)
fs.rmdirSync(filePath)
}
else if (stats.isFile()) {
fs.unlinkSync(filePath)
console.info(`[Kitbook] Updating routes: old file deleted: ${filePath}`)
}
}
}
catch (err) {
console.error(`Error while cleaning directory: ${err.message}`)
}
}

export function excludeDocFiles(src: string) {
const partsOfFilesUsedJustForDevelopingKitbook = ['mock', TYPINGS_EXT, PAGE_MARKDOWN, LAYOUT_MARKDOWN, VARIANTS]
const skip = partsOfFilesUsedJustForDevelopingKitbook.some(file => src.includes(file))
Expand Down
2 changes: 2 additions & 0 deletions packages/kitbook/src/lib/routes/[...file]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
</script>

<MainPage {data} />

<!-- kitbook route files auto-generated by [email protected]^ - do not edit as they will be overwritten next time kitbook updates routing -->
2 changes: 1 addition & 1 deletion packages/kitbook/src/lib/routes/[...file]/+page.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { mainPageLoad as load } from 'kitbook';
export { mainPageLoad as load } from 'kitbook'
6 changes: 1 addition & 5 deletions packages/template/src/lib/Button.md
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<script lang="ts">
import Button from './Button.svelte';
</script>

# Button with Variants
A little documentation about this component goes **here**.
2 changes: 2 additions & 0 deletions packages/template/src/routes/kitbook/[...file]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export let data;
</script>

<MainPage {data} />

<!-- kitbook route files auto-generated by [email protected]^ - do not edit as they will be overwritten next time kitbook updates routing -->

0 comments on commit be2c354

Please sign in to comment.