Skip to content

Commit

Permalink
feat: ✨ 增加lib模块
Browse files Browse the repository at this point in the history
  • Loading branch information
MuyianKing committed Dec 26, 2024
1 parent be4234c commit 8db505c
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 31 deletions.
29 changes: 27 additions & 2 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
快速创建项目脚手架
</p>

![NPM Version](https://img.shields.io/npm/v/%40muyianking%2Fcli) ![NPM License](https://img.shields.io/npm/l/%40muyianking%2Fcli)
<p align="center">
<a href="https://www.npmjs.com/package/@muyianking/cli" style="text-decoration: none;"><img src="https://img.shields.io/npm/v/%40muyianking%2Fcli"/></a>
<a href="https://www.npmjs.com/package/@muyianking/cli" style="text-decoration: none;"><img src="https://img.shields.io/npm/l/%40muyianking%2Fcli"/></a>
<a href="https://www.npmjs.com/package/@muyianking/cli" style="text-decoration: none;"><img src="https://img.shields.io/npm/dm/%40muyianking%2Fcli"/></a>
</p>

## Install

Expand All @@ -19,12 +23,33 @@ yarn global add @muyianking/cli
pnpm i @muyianking/cli -g
```

### Usage
## Usage

```bash
# 创建web项目
mu web

# 创建h5项目
mu h5

# 创建简易html项目
mu html
```

## Contributors

<!-- readme: collaborators,contributors -start -->
<table>
<tbody>
<tr>
<td align="center">
<a href="https://github.com/MuyianKing">
<img src="https://avatars.githubusercontent.com/u/44827414?v=4" width="100;" alt="MuyianKing"/>
<br />
<sub><b>MuyianKing</b></sub>
</a>
</td>
</tr>
<tbody>
</table>
<!-- readme: collaborators,contributors -end -->
14 changes: 9 additions & 5 deletions core/bin/command/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import process from 'node:process'
import ora from 'ora'

export default function () {
const cmd = ['web', 'h5', 'html']
const cmd = ['web', 'h5', 'html', 'lib']

const spinner = ora().start()

Expand All @@ -11,13 +11,17 @@ export default function () {
} else if (process.argv.length === 3 && !cmd.includes(process.argv[2])) {
spinner.fail(`找不到命令:mu <command>`)
} else {
spinner.stop()
return
}

spinner.succeed(`Usage:
mu web 创建一个web项目
mu h5 创建一个h5项目
mu html 创建普通项目(没有打包工具)
`)
mu web 创建一个web项目
mu h5 创建一个h5项目
mu lib 创建一个Lib项目
mu html 创建普通项目(没有打包工具)
`)

spinner.stop()
}
63 changes: 63 additions & 0 deletions core/bin/command/inquirer.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import path from 'node:path'
import process from 'node:process'
import inquirer from 'inquirer'
import ora from 'ora'
import { checkMkdirExists } from '../utils/copy.js'
import download from '../utils/download.js'

function inquirerPrompt(argv) {
const { name } = argv
return new Promise((resolve, reject) => {
inquirer.prompt([
{
type: 'input',
name: 'name',
message: '项目名称',
default: name,
validate(val) {
if (!val) {
return '请输入项目名称:'
}
return true
},
},
{
type: 'list',
name: 'build_type',
message: '组件类型',
choices: ['Vue组件'],
filter(value) {
return {
Vue组件: 'vue-comp',
}[value]
},
},
]).then((answers) => {
resolve(answers)
}).catch((error) => {
reject(error)
})
})
}

export default function (argv) {
inquirerPrompt(argv).then(async (answers) => {
const { name, build_type } = answers
const isMkdirExists = checkMkdirExists(
path.resolve(process.cwd(), `./${name}`),
)

const spinner = ora().start()

if (isMkdirExists) {
spinner.warning(`${name}文件夹已经存在`)
} else {
const _path = ['lib', build_type]

// 下载文件
await download(name, _path)

spinner.succeed(`created successfully`)
}
})
}
4 changes: 4 additions & 0 deletions core/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { hideBin } from 'yargs/helpers'
import inquirerH5Prompt from './command/inquirer.h5.js'
import inquirerHtmlPrompt from './command/inquirer.html.js'
import inquirerPrompt from './command/inquirer.js'
import inquirerLibPrompt from './command/inquirer.lib.js'
import inquirerWebPrompt from './command/inquirer.web.js'

// 无操作提示
Expand All @@ -18,6 +19,9 @@ yargs(hideBin(process.argv))
.command('h5', '新建一个H5项目', (argv) => {
inquirerH5Prompt(argv)
})
.command(['lib'], '新建一个Lib项目', (argv) => {
inquirerLibPrompt(argv)
})
.command(['html'], '新建一个HTML项目', (argv) => {
inquirerHtmlPrompt(argv)
})
Expand Down
46 changes: 23 additions & 23 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"name": "@muyianking/cli",
"type": "module",
"version": "0.2.1",
"description": "慕易安的脚手架",
"author": "muyianking",
"license": "ISC",
"keywords": [
"cli",
"vue",
"rsbuild"
],
"bin": {
"mu": "./bin/index.js"
},
"dependencies": {
"copy-dir": "^1.3.0",
"fs-extra": "^11.2.0",
"inquirer": "8.2.5",
"mustache": "^4.2.0",
"ora": "^8.1.1",
"yargs": "^17.7.2"
}
}
"name": "@muyianking/cli",
"type": "module",
"version": "0.2.1",
"description": "慕易安的脚手架",
"author": "muyianking",
"license": "ISC",
"keywords": [
"cli",
"vue",
"rsbuild"
],
"bin": {
"mu": "./bin/index.js"
},
"dependencies": {
"copy-dir": "^1.3.0",
"fs-extra": "^11.2.0",
"inquirer": "8.2.5",
"mustache": "^4.2.0",
"ora": "^8.1.1",
"yargs": "^17.7.2"
}
}
2 changes: 1 addition & 1 deletion script/utils/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { copyFile, readFileSync } from 'node:fs'
* @param {string} path 文件路径
* @returns
*/
export default function getObjectFromJson(path) {
export function getObjectFromJson(path) {
const data = readFileSync(path)
return JSON.parse(data)
}
Expand Down

0 comments on commit 8db505c

Please sign in to comment.