# custom_cmd

> ## 1.创建 npm 包，在其中创建命令

Latest version **1.2.2** (published 2024-03-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install custom_cmd
pnpm add custom_cmd
yarn add custom_cmd
bun add custom_cmd
```

Provides the commands `cmd1`, `init_npm_pkg`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2024-03-21 |
| First published | 2024-03-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 4 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | egu0 |
| Keywords | CLI, CMD |

## Links

- npm: https://www.npmjs.com/package/custom_cmd
- npm.io page: https://npm.io/package/custom_cmd

## Dependencies (4)

- [math](https://npm.io/package/math.md) ^0.0.3
- [shelljs](https://npm.io/package/shelljs.md) ^0.8.5
- [inquirer](https://npm.io/package/inquirer.md) ^8.2.6
- [commander](https://npm.io/package/commander.md) ^12.0.0

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 1.2.2 (latest) — 2024-03-21
- 1.2.1 — 2024-03-21
- 1.2.0 — 2024-03-21
- 1.1.0 — 2024-03-21

## README

# 通过 nodejs 项目创建 CLI

## 1.创建 npm 包，在其中创建命令

1、创建 npm 初始包，名为 `custom_cmd`


```sh
mkdir custom_cmd && cd custom_cmd

#创建 package.json
npm init
```

2、在 `package.json` 中配置 `bin` 属性，将 `cmd1` 命令映射到 `cmd1.js` 文件

```json
{
  "bin": {
    "cmd1": "cmd1.js"
  }
}
```

3、创建文件 `cmd1.js`

```js
#!/usr/bin/env node
//告诉系统这个脚本文件应该用哪个解释器来执行。在 Unix 系统下通过 ./cmd1.js 命令运行该 js 时，
//系统会查找 /usr/bin/env 路径下的 node 解释器来执行文件中的 JavaScript 代码

console.log('hi!');
```

3、使用 `npm link` 创建软链接，**链接当前包到全局 node_modules 目录**

```
➜  ~ ls -lha ~/.nvm/versions/node/v16.20.2/lib/node_modules
total 28K
lrwxrwxrwx 1 eugene eugene   42 Mar 21 17:22 custom_cmd -> ../../../../../../node_projects/custom_cmd
drwxr-xr-x 7 eugene eugene 4.0K Mar 21 17:16 npm
```

4、此时 `cmd1` 命令在本地全局可用了

## 2.使用这个包及其中的命令

5、在另一个包中使用 **npm install custom_cmd** 命令使用 `custom_cmd` 包

```
  "dependencies": {
    "custom_cmd": "file:../custom_cmd"
  },
```



6、使用 `npm publish` 发布包（需要先[创建 npm-registry 账号](https://www.npmjs.com/signup)，然后通过 `npm login` 登录）

7、在其他机器通过 `npm i custom_cmd -g` 全局安装，安装后可以在全局使用

## 3.创建复杂的命令

[init_npm_pkg.js](https://www.npmjs.com/package/custom_cmd?activeTab=code)

所用的第三方包

- `commander`：解析命令行，包括参数处理
- `inquirer`：提供了多种命令行交互式接口，比如输出提示信息、接收输入、接收选择
- `shelljs`：允许开发者在 js 代码中调用 shell 命令

## 4.参考

<https://juejin.cn/post/7347910198831054898>

## 5.补充 npm link/unlink 示例

### npm link

通过旧版 vue cli 脚手架创建项目

```
npm install -g @vue/cli
vue create my-project
```

发现 `my-project/package.json` 中定义了 npm 命令可以使用的几个脚本：

```json
{
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  }
}
```

这里的 `vue-cli-service` 想必也是一条命令，来自哪里呢？

来自：`node_modules/@vue/cli-service/bin/vue-cli-service.js`

这个命令可以**全局使用**吗？答：不可以

如何才能全局使用呢？ 

在 `@vue/cli-service` 包的目录中运行 `npm link`，**它会在全局 node_modules 目录中创建一个指向该包的符号链接**。

```
(base) ➜  cli-service git:(master) ✗ pwd
/home/eugene/node_projects/hello/node_modules/@vue/cli-service

(base) ➜  cli-service git:(master) ✗ npm link
added 1 package, and audited 3 packages in 2s
found 0 vulnerabilities
```

```
(base) ➜  node_modules git:(v0.39.7) pwd
/home/eugene/.nvm/versions/node/v18.19.1/lib/node_modules

(base) ➜  node_modules git:(v0.39.7) ll @vue
total 4.0K
lrwxrwxrwx 1 eugene eugene 70 Mar 21 20:46 cli-service -> ../../../../../../../node_projects/hello/node_modules/@vue/cli-service
```

### npm link xxx

在其他项目的目录中运行 `npm link 包名`，它会在该项目的 `node_modules` 目录中创建一个指向全局链接的包的符号链接。

```
(base) ➜  custom_cmd git:(master) ✗ npm link @vue/cli-service

added 1 package, and audited 76 packages in 3s

20 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities


(base) ➜  custom_cmd git:(master) ✗ ll node_modules/@vue 
total 0
lrwxrwxrwx 1 eugene eugene 44 Mar 21 20:48 cli-service -> ../../../hello/node_modules/@vue/cli-service
```

注意：执行 `npm link xxx` 不会改动 `package.json` 文件

### npm unlink [xxx]

这里的 “xxx” 是你之前用 `npm link` 创建符号链接时使用的包的名称。

如果你想要**去掉全局符号链接**，可以**在包的目录中**运行 `npm unlink`，这将会从全局 `node_modules` 目录中去掉该包的符号链接。

请注意，如果你在多个项目中使用了同一个包的符号链接，你需要**先**在每个项目中都运行 `npm unlink` 命令来去掉它们，然后再去掉全局符号链接。

## 6.注意

如果您使用了 nvm 等工具，请确保每次执行命令时都是用了相同的 node 环境。

---
_Source: https://npm.io/package/custom_cmd · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
