-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automatically update routes folder if updates are needed
- Loading branch information
Showing
5 changed files
with
40 additions
and
12 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,4 +1,5 @@ | ||
import fs from 'node:fs' | ||
import { join } from 'node:path' | ||
import type { KitbookSettings } from 'kitbook' | ||
|
||
const TYPINGS_EXT = '.d.ts' | ||
|
@@ -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)) | ||
|
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 |
---|---|---|
|
@@ -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 --> |
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 +1 @@ | ||
export { mainPageLoad as load } from 'kitbook'; | ||
export { mainPageLoad as load } from 'kitbook' |
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,5 +1 @@ | ||
<script lang="ts"> | ||
import Button from './Button.svelte'; | ||
</script> | ||
|
||
# Button with Variants | ||
A little documentation about this component goes **here**. |
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 |
---|---|---|
|
@@ -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 --> |