Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projects): add script: gen-route
- Loading branch information
1 parent
4134955
commit 697c1b6
Showing
4 changed files
with
89 additions
and
2 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
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,79 @@ | ||
import process from 'node:process'; | ||
import path from 'node:path'; | ||
import { writeFile } from 'node:fs/promises'; | ||
import { existsSync, mkdirSync } from 'node:fs'; | ||
import prompts from 'prompts'; | ||
import { green, red } from 'kolorist'; | ||
|
||
/** generate route */ | ||
export async function generateRoute() { | ||
const result = await prompts([ | ||
{ | ||
type: 'text', | ||
name: 'routeName', | ||
message: 'please enter route name', | ||
initial: 'demo-route_child' | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'addRouteParams', | ||
message: 'add route params?', | ||
initial: false | ||
}, | ||
{ | ||
type: pre => (pre ? 'text' : null), | ||
name: 'routeParams', | ||
message: 'please enter route params', | ||
initial: 'id' | ||
} | ||
]); | ||
|
||
const PAGE_DIR_NAME_PATTERN = /^[\w-]+[0-9a-zA-Z]+$/; | ||
|
||
if (!PAGE_DIR_NAME_PATTERN.test(result.routeName)) { | ||
throw new Error(`${red('route name is invalid, it only allow letters, numbers, "-" or "_"')}. | ||
For example: | ||
(1) one level route: ${green('demo-route')} | ||
(2) two level route: ${green('demo-route_child')} | ||
(3) multi level route: ${green('demo-route_child_child')} | ||
(4) group route: ${green('_ignore_demo-route')}' | ||
`); | ||
} | ||
|
||
const PARAM_REG = /^\w+$/g; | ||
|
||
if (!PARAM_REG.test(result.routeParams)) { | ||
throw new Error(red('route params is invalid, it only allow letters, numbers or "_".')); | ||
} | ||
|
||
const cwd = process.cwd(); | ||
|
||
const [dir, ...rest] = result.routeName.split('_') as string[]; | ||
|
||
let routeDir = path.join(cwd, 'src', 'views', dir); | ||
|
||
if (rest.length) { | ||
routeDir = path.join(routeDir, rest.join('_')); | ||
} | ||
|
||
if (!existsSync(routeDir)) { | ||
mkdirSync(routeDir, { recursive: true }); | ||
} else { | ||
throw new Error(red('route already exists')); | ||
} | ||
|
||
const fileName = result.routeParams ? `[${result.routeParams}].vue` : 'index.vue'; | ||
|
||
const vueTemplate = `<script setup lang="ts"></script> | ||
<template> | ||
<div>${result.routeName}</div> | ||
</template> | ||
<style scoped></style> | ||
`; | ||
|
||
const filePath = path.join(routeDir, fileName); | ||
|
||
await writeFile(filePath, vueTemplate); | ||
} |
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
697c1b6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
soybean-admin – ./
soybean-admin-git-main-soybeanjs.vercel.app
soybean-admin-soybeanjs.vercel.app
soybean-admin-eta.vercel.app