Skip to content

Commit

Permalink
feat(projects): add script: gen-route
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jan 21, 2024
1 parent 4134955 commit 697c1b6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -33,6 +33,7 @@
"cleanup": "sa cleanup",
"commit": "sa git-commit",
"dev": "vite",
"gen-route": "sa gen-route",
"lint": "eslint . --fix",
"prepare": "simple-git-hooks",
"preview": "vite preview",
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/src/commands/index.ts
Expand Up @@ -3,3 +3,4 @@ export * from './cleanup';
export * from './update-pkg';
export * from './changelog';
export * from './release';
export * from './router';
79 changes: 79 additions & 0 deletions packages/scripts/src/commands/router.ts
@@ -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);
}
10 changes: 8 additions & 2 deletions packages/scripts/src/index.ts
@@ -1,10 +1,10 @@
import cac from 'cac';
import { blue, lightGreen } from 'kolorist';
import { version } from '../package.json';
import { cleanup, genChangelog, gitCommit, gitCommitVerify, release, updatePkg } from './commands';
import { cleanup, genChangelog, generateRoute, gitCommit, gitCommitVerify, release, updatePkg } from './commands';
import { loadCliOptions } from './config';

type Command = 'cleanup' | 'update-pkg' | 'git-commit' | 'git-commit-verify' | 'changelog' | 'release';
type Command = 'cleanup' | 'update-pkg' | 'git-commit' | 'git-commit-verify' | 'changelog' | 'release' | 'gen-route';

type CommandAction<A extends object> = (args?: A) => Promise<void> | void;

Expand Down Expand Up @@ -82,6 +82,12 @@ export async function setupCli() {
action: async args => {
await release(args?.execute, args?.push);
}
},
'gen-route': {
desc: 'generate route',
action: async () => {
await generateRoute();
}
}
};

Expand Down

1 comment on commit 697c1b6

@vercel
Copy link

@vercel vercel bot commented on 697c1b6 Jan 21, 2024

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

Please sign in to comment.