# lofter-admin-cli

> js sandbox on browser

Latest version **1.0.6** (published 2024-10-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install lofter-admin-cli
pnpm add lofter-admin-cli
yarn add lofter-admin-cli
bun add lofter-admin-cli
```

Provides the command `lofter-admin-cli`.

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2024-10-18 |
| First published | 2021-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 50.9 KB |
| Known vulnerabilities | 0 (+29 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | luofeiyue, whiskeyi, yousy, guoyeeg, azgaga, liushichuan, yaochen, shenyuan, aflyermin, ignous, jeekdong |
| Keywords | micro frontend, micro-frontend, microfrontend |

## Links

- npm: https://www.npmjs.com/package/lofter-admin-cli
- npm.io page: https://npm.io/package/lofter-admin-cli

## Dependencies (11)

- [open](https://npm.io/package/open.md) ^8.4.0
- [axios](https://npm.io/package/axios.md) ^0.21.1
- [chalk](https://npm.io/package/chalk.md) ^4.1.1
- [inquirer](https://npm.io/package/inquirer.md) ^8.1.0
- [progress](https://npm.io/package/progress.md) ^2.0.3
- [commander](https://npm.io/package/commander.md) ^7.2.0
- [form-data](https://npm.io/package/form-data.md) ^4.0.0
- [minimatch](https://npm.io/package/minimatch.md) ^3.0.4
- [simple-git](https://npm.io/package/simple-git.md) ^2.39.0
- [sugar-server](https://npm.io/package/sugar-server.md) 1.0.0
- [@winman-f2e/nos-upload](https://npm.io/package/@winman-f2e/nos-upload.md) ^2.2.0

## Recent versions

- 1.0.6 (latest) — 2024-10-18
- 1.0.4-alpha.1 (alpha) — 2022-10-11
- 1.0.5 — 2023-02-21
- 1.0.4 — 2022-10-11
- 1.0.4-alpha.0 — 2021-11-23
- 1.0.3 — 2021-06-10
- 1.0.2 — 2021-06-10

## README

# lofter-admin-cli
这个是为微前端发布cli, 可以帮助你直接发布到应用平台

## 最新版本
```
release/v1.0.2
```

## 安装
1. package.json里添加依赖
  ```json
  "lofter-admin-cli": "1.0.2"
  ```
2. 运行 `npm install` 完成安装

## 更新版本
1. 修改 package.json 依赖为指定版本分支
  ```json
  "lofter-admin-cli": "1.0.2"
  ```
2. 运行 `npm install` 完成更新


## 命令说明

### lofter-admin-cli config
用于配置全局的用户信息
```shell
lofter-admin-cli config --set secret=xxx
```

### lofter-admin-cli upload
用于上传新版本
```shell
lofter-admin-cli upload --config lofter-app.js
```


配置文件说明
```typescript
module.exports = {
  /**
   * @param {number} appId - 必填
   * 子应用注册后分配的应用Id
   */
  appId: 123,

  /**
   * @param {string} secret - 可选
   * 登录应用平台后在个人页查看到的上传密钥
   * 也可以通过 lofter-admin-cli config --set secret=xxx设置到本地
   */
  secret: 'xxxxxxx',

  /**
   * @param {string} remote - 可选，一般无需添加
   * 指定上传的应用平台地址
   */
  remote: ''

  /**
   * @param {string} rootDir - 必填
   * 需上传文件根目录
   */
  rootDir: 'dist',

  /**
   * @param {string} html - 可选
   * 重要：html 和 entry 至少使用一个，否则无法正确启动该应用
   * 指定加载的html文件地址，路径相对于rootDir
   * 使用 html 模式支持webpack分包，此时 publicPath 设置为 '/' 即可
   */
  html: 'index.html',

  /**
   * @param {string[]} entry - 可选
   * 重要：html 和 entry 至少使用一个，否则无法正确启动该应用
   * 额外的入口文件，相对于rootDir, 支持 * 通配符
   * 会自动挂载到html中
   * 配置html后，一般无需再次配置 entry
   */
  entry: ['umi.css', 'umi.js'],

  /**
   * @param {string[]} excludes - 可选
   * 不需要上传的文件，相对于rootDir，可以用于减少上传的文件数量，加快上传
   */
  excludes: ['xxx.yyy'],

  /**
   * @param {string[]} includes - 可选
   * 指定需要上传的文件，相对于rootDir
   */
  includes: [],

  /**
   * @param {Object} requestRewrite - 可选
  *  客户端请求重写配置，key value形式
  *  请求url如果匹配由key生成的正则，会用value进行replace
  *  此处统一配置，可以免于修改每处请求的url
  */
  requestRewrite: {}
}
```

## 转发规则运行原理
### requestRewrite工作代码
```javascript
const rewriteReg = new RegExp(rewriteKey);
if (rewriteReg.test(url)) {
  newUrl = url.replace(rewriteReg, requestRewrite[rewriteKey]);
  return true;
}
```

### 整体工作原理
1. 正常情况下，如果不配置requestRewrite，**应用A**配置的路由为`/yyy`, 发出的请求`/xxx`，沙箱提供的request方法都会修改url为`/api/yyy/xxx`，目的是把所有请求都发送到主应用的node层来处理，并且附带**应用A**的应用路由
2. 此时如果在[应用平台](https://lofter.hz.netease.com/app/platform#/?key=conf)配置了转发规则，`/yyy`转发到`1.1.1.1`，则最终node层会把该应用A的所有请求都转发到`1.1.1.1`
3. 如果**应用B**配置了requestRewrite，沙箱提供的request方法会根据配置的rewrite规则修改，而不再根据规则1修改，比如`'^/aaa': '/api/bbb/aaa'`规则会把所有`/aaa`开头的请求重写为`/api/bbb/aaa`
4. 如果在这个基础上，在[应用平台](https://lofter.hz.netease.com/app/platform#/?key=conf)配置了转发规则，`/bbb`转发到`2.2.2.2`，那么最终**应用B**下所有`/aaa`开头的请求，都会转发到`2.2.2.2`这个地址

### 要点总结
1. 只有最终发送出去以`/api`开头的请求，才能被node层代理到，执行应用平台的转发规则
2. 如果不配置`requestRewrite`或者请求url没有命中`requestRewrite`中的规则，请求会被**微前端沙箱**自动加上`/api/{应用路由}`
3. 如果请求url被配置的`requestRewrite`规则命中，则会以该规则为准，**微前端沙箱**不再进行二次处理
4. 规则的value最好以`/api`开头，以获得node层转发和转发规则处理的能力，并且会在请求的header中附带用户的openId登录信息

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