Skip to content

Commit

Permalink
feat(cli): add port option for preview command (#1834)
Browse files Browse the repository at this point in the history
  • Loading branch information
chouchouji authored Dec 25, 2024
1 parent 9cbf3e4 commit f611882
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/varlet-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ varlet-cli build
```shell
# playground-ignore
varlet-cli preview
# set port number
varlet-cli preview -p <port>
```

#### Compile component library
Expand Down
2 changes: 2 additions & 0 deletions packages/varlet-cli/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ varlet-cli build
```shell
# playground-ignore
varlet-cli preview
# set port number
varlet-cli preview -p <port>
```

#### 构建组件库
Expand Down
5 changes: 3 additions & 2 deletions packages/varlet-cli/lib/node/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ program
program
.command('preview')
.description('Preview varlet site for production')
.action(async () => {
.option('-p, --port <port>', 'port number')
.action(async (options) => {
const { preview } = await import('./commands/preview.js');
return preview();
return preview(options);
});
program
.command('compile')
Expand Down
6 changes: 3 additions & 3 deletions packages/varlet-cli/src/node/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ program
program
.command('preview')
.description('Preview varlet site for production')
.action(async () => {
.option('-p, --port <port>', 'port number')
.action(async (options) => {
const { preview } = await import('./commands/preview.js')

return preview()
return preview(options)
})

program
Expand Down
9 changes: 7 additions & 2 deletions packages/varlet-cli/src/node/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import { SITE_OUTPUT_PATH } from '../shared/constant.js'

const { pathExistsSync } = fse

export async function preview() {
export type PreviewCommandOptions = {
port?: string
}

export async function preview(options: PreviewCommandOptions) {
const { port = 5500 } = options
if (!pathExistsSync(SITE_OUTPUT_PATH)) {
logger.warning('Cannot find the site folder, you must first run the build command to build the document site')
return
}

try {
await x('live-server', ['--port=5500'], { nodeOptions: { cwd: SITE_OUTPUT_PATH, stdio: 'inherit' } })
await x('live-server', [`--port=${Number(port)}`], { nodeOptions: { cwd: SITE_OUTPUT_PATH, stdio: 'inherit' } })
} catch (e: any) {
logger.error(e.toString())
}
Expand Down

0 comments on commit f611882

Please sign in to comment.