36005e2c06a39c44647bf17e4d9edf02b8e5c763..0600bf71ad8a7704434530ada93144337f851dc8
15 hours ago Tevin
refactor: 仅 Windows PATH,移除 npm bin 与 node/npm 文档说明
0600bf diff | tree
15 hours ago Tevin
docs: 添加 README 使用说明
199a57 diff | tree
15 hours ago Tevin
feat: 添加 hello 示例命令与 bin 垫片
0c4c00 diff | tree
1 files modified
4 files added
80 ■■■■■ changed files
README.md 55 ●●●●● patch | view | raw | blame | history
bin/hello.cmd 4 ●●●● patch | view | raw | blame | history
bin/hello.mjs 3 ●●●●● patch | view | raw | blame | history
commands/hello/cli.mjs 14 ●●●●● patch | view | raw | blame | history
package.json 4 ●●●● patch | view | raw | blame | history
README.md
New file
@@ -0,0 +1,55 @@
# cc-cmds
多个 **相互独立** 的 Node.js CLI 命令放在同一仓库里统一维护;**没有**统一总入口,每个命令有自己的名称与实现。
本仓库 **仅约定在 Windows 下通过 PATH 调用**:把 `bin` 目录加入用户或系统的 **PATH** 后,在终端中直接使用各命令的 **`.cmd` 名称**(例如 `hello`)。
## 环境要求
- **Windows**
- 已安装 **Node.js** ≥ 18,且 `node` 已在 PATH 中(由 `.cmd` 垫片调用,无需手动输入 `node`)
## 目录约定
| 路径 | 说明 |
|------|------|
| `commands/<命令名>/` | 该命令的全部业务代码;入口一般为 **`cli.mjs`**。 |
| `bin/<命令名>.mjs` | 由对应 **`.cmd`** 调用:转跳到 `commands/<命令名>/cli.mjs`。 |
| `bin/<命令名>.cmd` | **对外命令名**:需与期望在终端里输入的名称一致(如 `hello.cmd` → 输入 `hello`)。 |
## 配置 PATH
1. 复制本仓库的 **`bin` 文件夹的绝对路径**(例如 `D:\AiSim2\.cc-cmds\bin`)。
2. **Windows 设置** → **系统** → **关于** → **高级系统设置** → **环境变量**。
3. 在「用户变量」或「系统变量」里编辑 **Path**,新建一项填入上述路径,确定保存。
4. **重新打开** 终端,执行 `where hello` 应能解析到 `...\bin\hello.cmd`。
## 使用规则(新增一个命令)
1. **实现**
   新建 `commands/<命令名>/`,编写 **`cli.mjs`**:
   - 使用 ES Module(`import` / `export`)
   - 自行解析 **`process.argv.slice(2)`**(第一个用户参数对应 `argv[0]`)
2. **Node 垫片**
   新建 `bin/<命令名>.mjs`:
   ```js
   #!/usr/bin/env node
   import "../commands/<命令名>/cli.mjs";
   ```
3. **Windows 入口**
   复制 `bin/hello.cmd` 为 `bin/<命令名>.cmd`,将其中的 `hello.mjs` 改为 **`<命令名>.mjs`**(保持与上一步文件名一致)。
无需在 `package.json` 中注册 `bin`;是否加入 PATH 由本机环境决定。
## 示例
- 终端输入:`hello` 或 `hello 你的名字`(PATH 已包含 `bin` 时)
- 实现:`commands/hello/cli.mjs`
- 垫片:`bin/hello.mjs`、`bin/hello.cmd`
---
本项目为 **MIT** 许可(见 `package.json`)。
bin/hello.cmd
New file
@@ -0,0 +1,4 @@
@echo off
setlocal
set "SCRIPT_DIR=%~dp0"
node "%SCRIPT_DIR%hello.mjs" %*
bin/hello.mjs
New file
@@ -0,0 +1,3 @@
#!/usr/bin/env node
/** 垫片:将全局命令名映射到具体实现目录 */
import "../commands/hello/cli.mjs";
commands/hello/cli.mjs
New file
@@ -0,0 +1,14 @@
#!/usr/bin/env node
/**
 * 独立命令入口(示例):全局安装后命令名为 hello
 * 也可:node commands/hello/cli.mjs [参数...]
 */
async function main(argv) {
  const name = argv[0] || "world";
  console.log(`Hello, ${name}!`);
}
main(process.argv.slice(2)).catch((e) => {
  console.error(e);
  process.exit(1);
});
package.json
@@ -1,12 +1,12 @@
{
  "name": "cc-cmds",
  "version": "0.1.0",
  "description": "多个独立 CLI 命令合集(各自入口,仅同属一个 npm 包管理)",
  "description": "多个独立 Node.js CLI 命令合集(通过 Windows PATH 调用 bin 下命令)",
  "type": "module",
  "files": ["bin", "commands"],
  "engines": {
    "node": ">=18"
  },
  "keywords": ["cli", "global"],
  "keywords": ["cli"],
  "license": "MIT"
}