comp-hub 0.24.11 → 0.24.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -0
- package/bin/cli.js +85 -85
- package/dist/index.js +6 -6
- package/dist/website/container/assets/{index-7uaISyj6.css → index-BYk1oUdx.css} +1 -1
- package/dist/website/container/assets/{index-D-kSplb-.js → index-Bh-7G32o.js} +1 -1
- package/dist/website/container/index.html +30 -30
- package/dist/website/container/js/vue2-sfc-loader.js +260 -260
- package/dist/website/container/js/vue3-sfc-loader.js +289 -289
- package/dist/website/item/assets/{_plugin-vue_export-helper-QbqQO4ox.js → _plugin-vue_export-helper-DxxfcOdc.js} +3 -3
- package/dist/website/item/assets/{index-DET66N-d.js → index-72hY5o0R.js} +1 -1
- package/dist/website/item/assets/{index-CM1x-MX7.js → index-9qF8L1Ai.js} +1 -1
- package/dist/website/item/assets/{index-CVcrfYTM.js → index-BOtBx3QH.js} +1 -1
- package/dist/website/item/assets/{index-bWyqvr0X.js → index-BP-Hobhf.js} +2 -2
- package/dist/website/item/assets/{index-DuVzU_zG.js → index-BSG2Yw4G.js} +1 -1
- package/dist/website/item/assets/{index-JQHS5Bgt.js → index-CKK0FQdj.js} +1 -1
- package/dist/website/item/assets/{index-Batvey83.css → index-CpAyVxuN.css} +1 -1
- package/dist/website/item/assets/{index-BkY05Tv7.js → index-DQYfYuF4.js} +1 -1
- package/dist/website/item/assets/{index-C-3KyzTN.js → index-DVcVMesT.js} +1 -1
- package/dist/website/item/assets/{index-DkhALoDA.js → index-N8Vg_cIr.js} +1 -1
- package/dist/website/item/assets/index-Xz6f1Mt2.js +1 -0
- package/dist/website/item/assets/{index-DFXSaMS_.js → index-hIjMK5Yi.js} +1 -1
- package/dist/website/item/assets/{index-DkNMxT8b.js → index-i-3hC4x8.js} +1 -1
- package/dist/website/item/assets/{index-CTV-ZEPA.js → index-l2j6LNIW.js} +1 -1
- package/dist/website/item/assets/{index-BKR3SGFw.js → index-oA6pOE-t.js} +1 -1
- package/dist/website/item/assets/{index.vue_vue_type_script_setup_true_lang-D-MRPHB3.js → index.vue_vue_type_script_setup_true_lang-BE37s0in.js} +1 -1
- package/dist/website/item/assets/{index.vue_vue_type_script_setup_true_lang-BM36txPt.js → index.vue_vue_type_script_setup_true_lang-BESMbsv-.js} +1 -1
- package/dist/website/item/assets/{index.vue_vue_type_script_setup_true_lang-DgcUyHym.js → index.vue_vue_type_script_setup_true_lang-BkGQwmGu.js} +1 -1
- package/dist/website/item/assets/{index.vue_vue_type_script_setup_true_lang-DltSw7Od.js → index.vue_vue_type_script_setup_true_lang-CTHEzXxd.js} +1 -1
- package/dist/website/item/assets/{index.vue_vue_type_script_setup_true_lang-BuqMuhvQ.js → index.vue_vue_type_script_setup_true_lang-CgQ0VsKY.js} +1 -1
- package/dist/website/item/assets/{index.vue_vue_type_script_setup_true_lang-CxFzGE-8.js → index.vue_vue_type_script_setup_true_lang-Dc3mH9oB.js} +1 -1
- package/dist/website/item/assets/{useCompAttrs-DQk6B19O.js → useCompAttrs-Bi9YFJz7.js} +1 -1
- package/dist/website/item/assets/{useCompAttrsLocal-BcuGFHgh.js → useCompAttrsLocal-CMm5R48t.js} +1 -1
- package/dist/website/item/index.html +26 -26
- package/dist/website/item/js/vue2-sfc-loader.js +260 -260
- package/dist/website/item/js/vue3-sfc-loader.js +289 -289
- package/dist/website/main/assets/{cloud-upload-DYVQscjr.js → cloud-upload-BQyuKBFx.js} +1 -1
- package/dist/website/main/assets/{directive-Dbdo6JU1.js → directive-BNaJyPyt.js} +1 -1
- package/dist/website/main/assets/{el-backtop-CUk59UkR.js → el-backtop-CAVkRT3t.js} +1 -1
- package/dist/website/main/assets/{el-dropdown-item-CCHgLQ3R.js → el-dropdown-item-LoEPo17j.js} +1 -1
- package/dist/website/main/assets/{el-link-Dn3unxMj.js → el-link-DR51yJbS.js} +1 -1
- package/dist/website/main/assets/{home-rounded-DtkoFB8J.js → home-rounded-CDFXYXKw.js} +1 -1
- package/dist/website/main/assets/{index-BW75ivdG.js → index-6rmXA65e.js} +1 -1
- package/dist/website/main/assets/{index-Dy7z51MI.js → index-B3SSixYp.js} +1 -1
- package/dist/website/main/assets/{index-88YgWgD8.js → index-BI_UW_hm.js} +1 -1
- package/dist/website/main/assets/{index-C8nVi6pj.js → index-BKIOkUBk.js} +1 -1
- package/dist/website/main/assets/{index-4DC8vfu4.js → index-BRyTIrd8.js} +1 -1
- package/dist/website/main/assets/{index-DgHskqgi.js → index-BZD3cBTL.js} +1 -1
- package/dist/website/main/assets/{index-CNuqVB0_.js → index-BbEocPzm.js} +1 -1
- package/dist/website/main/assets/{index-D4WSt5_w.js → index-BduACWcN.js} +2 -2
- package/dist/website/main/assets/{index-vuA-MHzf.js → index-BhQ5YTKd.js} +1 -1
- package/dist/website/main/assets/{index-D4TGSZYa.js → index-Buvk-XuY.js} +1 -1
- package/dist/website/main/assets/{index-QbGn6iWg.js → index-C2drfmhL.js} +2 -2
- package/dist/website/main/assets/index-CB6Jprze.js +1 -0
- package/dist/website/main/assets/{index-QD_gyeeM.js → index-ClH_wqn_.js} +1 -1
- package/dist/website/main/assets/{index-Bnk4Fpek.js → index-CuTXHaln.js} +1 -1
- package/dist/website/main/assets/{index-C1YiQKb5.js → index-Cwhqp83e.js} +1 -1
- package/dist/website/main/assets/{index-CWr3FTpG.css → index-D5zAUVvV.css} +1 -1
- package/dist/website/main/assets/{index-CzemkZ2l.js → index-DCHUbdeS.js} +1 -1
- package/dist/website/main/assets/{index-ZUI1KF6K.js → index-DH5FCwLY.js} +1 -1
- package/dist/website/main/assets/{index-BGTUs1A0.js → index-DJMB48kb.js} +1 -1
- package/dist/website/main/assets/{index-CxQQXmBl.js → index-DRO1T8Xn.js} +1 -1
- package/dist/website/main/assets/{index-CFBH9J5D.js → index-DT79I49x.js} +1 -1
- package/dist/website/main/assets/{index-Cc7oYtDJ.js → index-DYfT7u1x.js} +1 -1
- package/dist/website/main/assets/{index-Duzq8Ych.js → index-DeSSNMfi.js} +1 -1
- package/dist/website/main/assets/{index-WHVQ_Jg4.js → index-DfCaWzjg.js} +1 -1
- package/dist/website/main/assets/{index-CNEWvnvP.js → index-DkLqgnVI.js} +1 -1
- package/dist/website/main/assets/{index-CAtjjaSp.js → index-DzVRov2s.js} +1 -1
- package/dist/website/main/assets/{index-Di49hgeB.js → index-GzN8kgSA.js} +6 -6
- package/dist/website/main/assets/{index-CEPj58Z2.js → index-L831PR8Y.js} +1 -1
- package/dist/website/main/assets/{index-DAQABx2r.js → index-fBVqJuwZ.js} +1 -1
- package/dist/website/main/assets/{index-CiTJ9oPO.js → index-iVx57HNo.js} +1 -1
- package/dist/website/main/assets/{index-B04-VmUb.js → index-llKMLnmR.js} +1 -1
- package/dist/website/main/assets/{index-CMxmSx0J.js → index-pniPjuPr.js} +1 -1
- package/dist/website/main/assets/{index-DCkcLb46.js → index-ty_d2Sd3.js} +1 -1
- package/dist/website/main/assets/{index.es-DVR1ra0Y.js → index.es-DhYLGDtD.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_script_setup_true_lang-CQfW1NKd.js → index.vue_vue_type_script_setup_true_lang-AZe5PfHV.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_script_setup_true_lang-DUsOZq7f.js → index.vue_vue_type_script_setup_true_lang-BGaSkJ1N.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_script_setup_true_lang-BQYic4XW.js → index.vue_vue_type_script_setup_true_lang-BKp21x_z.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_script_setup_true_lang-D-7lodcs.js → index.vue_vue_type_script_setup_true_lang-CyssBIWa.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_script_setup_true_lang-3P8vyDVQ.js → index.vue_vue_type_script_setup_true_lang-D6yhbvCC.js} +2 -2
- package/dist/website/main/assets/{index.vue_vue_type_script_setup_true_lang-xreKgU7o.js → index.vue_vue_type_script_setup_true_lang-DKLKJChd.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_script_setup_true_lang-DjXf_Mfa.js → index.vue_vue_type_script_setup_true_lang-DnjqFELA.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_style_index_0_lang-D2SGytlu.js → index.vue_vue_type_style_index_0_lang-3HmrxWr4.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_style_index_0_lang-DtY_7uDH.js → index.vue_vue_type_style_index_0_lang-9vFg3PD-.js} +1 -1
- package/dist/website/main/assets/{index.vue_vue_type_style_index_0_lang-B5WPE5H_.js → index.vue_vue_type_style_index_0_lang-C8ZEv5wR.js} +1 -1
- package/dist/website/main/assets/{kid-star-D08PvJss.js → kid-star-0IgRQhy3.js} +1 -1
- package/dist/website/main/assets/{md5-BcIieTd6.js → md5-Dv_Vs6T5.js} +1 -1
- package/dist/website/main/assets/{settings-ULeCW0hc.js → settings-DJNoFINA.js} +1 -1
- package/dist/website/main/assets/{supervised-user-circle-DifM9Exn.js → supervised-user-circle-5FKcgZke.js} +1 -1
- package/dist/website/main/assets/{sync-saved-locally-rounded-BkhbBr-F.js → sync-saved-locally-rounded-EroOsQq7.js} +1 -1
- package/dist/website/main/assets/{terminal-Cluo2cLi.js → terminal-pnp0qB8c.js} +1 -1
- package/dist/website/main/assets/{useCacheOssStore-Di0Jfdt8.js → useCacheOssStore-Dehw5yF4.js} +1 -1
- package/dist/website/main/assets/{useCompEditStore-BKvAV_ax.js → useCompEditStore-DCk_n2hu.js} +1 -1
- package/dist/website/main/assets/{useCompMetaLocalStore-BTjFocQj.js → useCompMetaLocalStore-DV-kJ6CO.js} +1 -1
- package/dist/website/main/assets/{useCompMetaStore-Cu0eRMzK.js → useCompMetaStore-CmXxMQ45.js} +1 -1
- package/dist/website/main/assets/{useIframeBroadcast-yBewQn96.js → useIframeBroadcast-Cpoicxog.js} +1 -1
- package/dist/website/main/assets/{useTeamHeaders-BoyUY36Z.js → useTeamHeaders-CkqGH2ip.js} +1 -1
- package/dist/website/main/assets/{useVscodeOpen-DU3GVN6T.js → useVscodeOpen-C_4PfFa5.js} +1 -1
- package/dist/website/main/index.html +2 -2
- package/package.json +37 -37
- package/dist/website/item/assets/index-BXINO-fn.js +0 -1
- package/dist/website/main/assets/index-BqWZ1raH.js +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# comp-hub
|
|
2
|
+
|
|
3
|
+
A Vue-based component preview and sharing platform that makes component reuse simple and efficient.
|
|
4
|
+
|
|
5
|
+
## 📖 Introduction
|
|
6
|
+
|
|
7
|
+
comp-hub is a component management platform designed for frontend developers, dedicated to solving pain points such as component reuse, missing documentation, and low standardization. By providing real-time preview, dependency management, and version control, it makes component sharing and usage simple and efficient.
|
|
8
|
+
|
|
9
|
+
## 🚀 Core Features
|
|
10
|
+
|
|
11
|
+
- **Real-time Preview & Dependency Management**: Online real-time component preview with automatic dependency version matching, ensuring what you see is what you get after download
|
|
12
|
+
- **Lightweight Component Architecture**: Components contain only minimal runtime units, seamlessly integrating into existing project environments
|
|
13
|
+
- **Simple Upload Process**: Complete component publishing in three steps, supporting preview configuration, documentation, and version management
|
|
14
|
+
- **Documentation Support**: Standard Markdown documentation support to help users quickly understand and use components
|
|
15
|
+
- **Version Management**: Semantic versioning with multi-version coexistence and switching
|
|
16
|
+
- **Cross-project Reuse**: Standardized component structure ensures high compatibility and portability across different projects
|
|
17
|
+
|
|
18
|
+
## 📦 Supported Frameworks
|
|
19
|
+
|
|
20
|
+
| Framework | Version | Status |
|
|
21
|
+
|-----------|---------|--------|
|
|
22
|
+
| Vue | 2.x | ✅ Supported |
|
|
23
|
+
| Vue | 3.x | ✅ Supported |
|
|
24
|
+
| React | - | ⏳ Planned |
|
|
25
|
+
|
|
26
|
+
## 💻 Installation & Usage
|
|
27
|
+
|
|
28
|
+
### Requirements
|
|
29
|
+
|
|
30
|
+
- Node.js >= 16.0.0
|
|
31
|
+
- npm >= 8.0.0
|
|
32
|
+
|
|
33
|
+
### Installation
|
|
34
|
+
|
|
35
|
+
#### Method 1: Global Installation (Recommended)
|
|
36
|
+
|
|
37
|
+
Install comp-hub CLI globally:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install comp-hub@latest -g
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### Method 2: Local Dev Dependency Installation
|
|
44
|
+
|
|
45
|
+
Install only in the current project:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm install comp-hub@latest -D
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Start Service
|
|
52
|
+
|
|
53
|
+
#### Global Installation
|
|
54
|
+
|
|
55
|
+
Execute in the project directory:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
comphub
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### Local Installation
|
|
62
|
+
|
|
63
|
+
If installed locally with `-D`, use `npx`:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npx comphub
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
After successful startup, access the platform at:
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
http://localhost:5000
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### CLI Options
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
comphub [options]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
| Option | Short | Type | Default | Description |
|
|
82
|
+
|--------|-------|------|---------|-------------|
|
|
83
|
+
| `--port` | `-p` | number | 5000 | Service port |
|
|
84
|
+
| `--dir` | `-d` | path | "./" | Resource directory relative path |
|
|
85
|
+
| `--log` | `-l` | level | "info" | Log level |
|
|
86
|
+
| `--version` | `-v` | - | - | Show version number |
|
|
87
|
+
| `--help` | `-h` | - | - | Show help information |
|
|
88
|
+
|
|
89
|
+
### Configuration File
|
|
90
|
+
|
|
91
|
+
Create `.comphub.json` or `.comphub.js` in the project root:
|
|
92
|
+
|
|
93
|
+
**JSON Format:**
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
{
|
|
97
|
+
"port": 5000,
|
|
98
|
+
"dir": "./src",
|
|
99
|
+
"log": "info"
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**JS Format:**
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
module.exports = {
|
|
107
|
+
port: 5000,
|
|
108
|
+
dir: './src',
|
|
109
|
+
log: 'info'
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
| Config | Type | Default | Description |
|
|
114
|
+
|--------|------|---------|-------------|
|
|
115
|
+
| `port` | number | 5000 | Service startup port |
|
|
116
|
+
| `dir` | string | "./" | Component code root directory path |
|
|
117
|
+
| `log` | string | "info" | Log level: "debug", "info", "warn", "error" |
|
|
118
|
+
|
|
119
|
+
## 📚 Documentation
|
|
120
|
+
|
|
121
|
+
Complete usage guide and API documentation:
|
|
122
|
+
|
|
123
|
+
**[comp-hub Official Documentation](https://docs.comphub.cn/en/)**
|
|
124
|
+
|
|
125
|
+
## 🔧 Component Specifications
|
|
126
|
+
|
|
127
|
+
- Components must be uploaded as **folders**, with the folder name as the component name
|
|
128
|
+
- Component folder must contain `index.vue` as the entry file
|
|
129
|
+
- Recommended to provide `README.md` describing component functionality and usage
|
|
130
|
+
- Support `comp.json` file for component metadata configuration
|
|
131
|
+
|
|
132
|
+
## 🎯 Version Management
|
|
133
|
+
|
|
134
|
+
comp-hub supports semantic versioning:
|
|
135
|
+
|
|
136
|
+
- `X.Y.Z` format (e.g., 1.2.3)
|
|
137
|
+
- `X`: Major version, incompatible API changes
|
|
138
|
+
- `Y`: Minor version, backward-compatible functionality additions
|
|
139
|
+
- `Z`: Patch version, backward-compatible bug fixes
|
|
140
|
+
|
|
141
|
+
## 🤝 Contributing
|
|
142
|
+
|
|
143
|
+
Contributions and suggestions are welcome!
|
|
144
|
+
|
|
145
|
+
## 📝 License
|
|
146
|
+
|
|
147
|
+
[MIT License](LICENSE)
|
package/bin/cli.js
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const { program, Option } = require('commander');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const { spawn } = require('child_process');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
|
|
7
|
-
const projectRoot = path.resolve(__dirname, '..');
|
|
8
|
-
|
|
9
|
-
const readJson = (p) => JSON.parse(fs.readFileSync(p, 'utf8'));
|
|
10
|
-
|
|
11
|
-
//读取包信息
|
|
12
|
-
function getLocalPkgInfo() {
|
|
13
|
-
const pkgPath = path.join(projectRoot, 'package.json');
|
|
14
|
-
const pkg = readJson(pkgPath);
|
|
15
|
-
const entry = path.join(projectRoot, pkg.main || 'dist/index.js');
|
|
16
|
-
return { pkg, entry };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const { pkg, entry } = getLocalPkgInfo();
|
|
21
|
-
|
|
22
|
-
//命令行
|
|
23
|
-
program
|
|
24
|
-
.option('-p, --port <number>', '启动端口', (v) => parseInt(v, 10), 5000)
|
|
25
|
-
.option('-d, --dir <path>', '资源目录相对路径', './')
|
|
26
|
-
.option('-l, --log <level>', '日志等级', (v) => {
|
|
27
|
-
const ok = ['debug', 'info', 'warn', 'error'];
|
|
28
|
-
if (!ok.includes(v)) throw new Error(`日志等级必须是 ${ok.join(', ')}`);
|
|
29
|
-
return v;
|
|
30
|
-
}, 'info')
|
|
31
|
-
.version(pkg.version, '-v, --version', '显示版本号')
|
|
32
|
-
.addOption(new Option('--allow-debug', '是否允许调试模式').hideHelp())
|
|
33
|
-
.addOption(new Option('--dir-absolute <path>', '资源目录绝对路径').hideHelp())
|
|
34
|
-
.addOption(new Option('--api <url>', 'API地址').hideHelp())
|
|
35
|
-
.addOption(new Option('--cwd-relative <path>', '当前工作目录相对路径').hideHelp())
|
|
36
|
-
.helpOption('-h, --help', '显示帮助信息');
|
|
37
|
-
|
|
38
|
-
program.parse();
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
//主流程
|
|
42
|
-
function main() {
|
|
43
|
-
//检查当前工作目录是否是node项目
|
|
44
|
-
if (!fs.existsSync(path.join(process.cwd(), 'package.json'))) {
|
|
45
|
-
console.error('❌ 当前工作目录不是node工程,无法启动');
|
|
46
|
-
process.exit(1);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const opts = program.opts();
|
|
50
|
-
|
|
51
|
-
console.log(`✅ ${pkg.name} v${pkg.version}`);
|
|
52
|
-
|
|
53
|
-
//忽略传递的参数配置
|
|
54
|
-
const ignoreArgs = ['-v', '--version', '-h', '--help'];
|
|
55
|
-
const userArgs = process.argv.slice(2).filter((a) => !ignoreArgs.includes(a));
|
|
56
|
-
|
|
57
|
-
const env = {
|
|
58
|
-
...process.env,
|
|
59
|
-
COMP_HUB_VERSION: pkg.version,
|
|
60
|
-
COMP_HUB_CONFIG: JSON.stringify(opts),
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
if (!fs.existsSync(entry)) {
|
|
64
|
-
console.log('\nℹ️ 入口文件不存在,启动失败');
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const child = spawn(process.execPath, [entry, ...userArgs], {
|
|
69
|
-
stdio: 'inherit',
|
|
70
|
-
cwd: process.cwd(),
|
|
71
|
-
env,
|
|
72
|
-
shell: process.platform === 'win32',
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
child.on('exit', (code) => process.exit(code ?? 0));
|
|
76
|
-
child.on('error', (e) => (console.error(`❌ 启动失败:${e.message}`), process.exit(1)));
|
|
77
|
-
|
|
78
|
-
['SIGINT', 'SIGTERM'].forEach((sig) =>
|
|
79
|
-
process.on(sig, () => {
|
|
80
|
-
console.log('\n⚠️ 终止进程...');
|
|
81
|
-
child.kill(sig);
|
|
82
|
-
})
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
main();
|
|
2
|
+
const { program, Option } = require('commander');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { spawn } = require('child_process');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
|
|
7
|
+
const projectRoot = path.resolve(__dirname, '..');
|
|
8
|
+
|
|
9
|
+
const readJson = (p) => JSON.parse(fs.readFileSync(p, 'utf8'));
|
|
10
|
+
|
|
11
|
+
//读取包信息
|
|
12
|
+
function getLocalPkgInfo() {
|
|
13
|
+
const pkgPath = path.join(projectRoot, 'package.json');
|
|
14
|
+
const pkg = readJson(pkgPath);
|
|
15
|
+
const entry = path.join(projectRoot, pkg.main || 'dist/index.js');
|
|
16
|
+
return { pkg, entry };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const { pkg, entry } = getLocalPkgInfo();
|
|
21
|
+
|
|
22
|
+
//命令行
|
|
23
|
+
program
|
|
24
|
+
.option('-p, --port <number>', '启动端口', (v) => parseInt(v, 10), 5000)
|
|
25
|
+
.option('-d, --dir <path>', '资源目录相对路径', './')
|
|
26
|
+
.option('-l, --log <level>', '日志等级', (v) => {
|
|
27
|
+
const ok = ['debug', 'info', 'warn', 'error'];
|
|
28
|
+
if (!ok.includes(v)) throw new Error(`日志等级必须是 ${ok.join(', ')}`);
|
|
29
|
+
return v;
|
|
30
|
+
}, 'info')
|
|
31
|
+
.version(pkg.version, '-v, --version', '显示版本号')
|
|
32
|
+
.addOption(new Option('--allow-debug', '是否允许调试模式').hideHelp())
|
|
33
|
+
.addOption(new Option('--dir-absolute <path>', '资源目录绝对路径').hideHelp())
|
|
34
|
+
.addOption(new Option('--api <url>', 'API地址').hideHelp())
|
|
35
|
+
.addOption(new Option('--cwd-relative <path>', '当前工作目录相对路径').hideHelp())
|
|
36
|
+
.helpOption('-h, --help', '显示帮助信息');
|
|
37
|
+
|
|
38
|
+
program.parse();
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
//主流程
|
|
42
|
+
function main() {
|
|
43
|
+
//检查当前工作目录是否是node项目
|
|
44
|
+
if (!fs.existsSync(path.join(process.cwd(), 'package.json'))) {
|
|
45
|
+
console.error('❌ 当前工作目录不是node工程,无法启动');
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const opts = program.opts();
|
|
50
|
+
|
|
51
|
+
console.log(`✅ ${pkg.name} v${pkg.version}`);
|
|
52
|
+
|
|
53
|
+
//忽略传递的参数配置
|
|
54
|
+
const ignoreArgs = ['-v', '--version', '-h', '--help'];
|
|
55
|
+
const userArgs = process.argv.slice(2).filter((a) => !ignoreArgs.includes(a));
|
|
56
|
+
|
|
57
|
+
const env = {
|
|
58
|
+
...process.env,
|
|
59
|
+
COMP_HUB_VERSION: pkg.version,
|
|
60
|
+
COMP_HUB_CONFIG: JSON.stringify(opts),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (!fs.existsSync(entry)) {
|
|
64
|
+
console.log('\nℹ️ 入口文件不存在,启动失败');
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const child = spawn(process.execPath, [entry, ...userArgs], {
|
|
69
|
+
stdio: 'inherit',
|
|
70
|
+
cwd: process.cwd(),
|
|
71
|
+
env,
|
|
72
|
+
shell: process.platform === 'win32',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
child.on('exit', (code) => process.exit(code ?? 0));
|
|
76
|
+
child.on('error', (e) => (console.error(`❌ 启动失败:${e.message}`), process.exit(1)));
|
|
77
|
+
|
|
78
|
+
['SIGINT', 'SIGTERM'].forEach((sig) =>
|
|
79
|
+
process.on(sig, () => {
|
|
80
|
+
console.log('\n⚠️ 终止进程...');
|
|
81
|
+
child.kill(sig);
|
|
82
|
+
})
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
main();
|