wx-miniprogram-skill
微信小程序 Linux/macOS/Windows CI 技能,基于 miniprogram-ci。
版本
v2.0.1
快速开始
1. 初始化环境
node wxmini-ci.js init --project-path /path/to/your/project
2. 配置
创建 wxmini-ci.config.js 或使用 config 命令配置。
3. 使用
node wxmini-ci.js check # 检查配置
node wxmini-ci.js preview ... # 预览
node wxmini-ci.js upload ... # 上传
命令行使用
node wxmini-ci.js <command> [options]
可用命令
| 命令 | 说明 |
|---|---|
| init | 初始化环境(安装 miniprogram-ci) |
| config | 查看/修改配置 |
| check | 检查配置是否完整 |
| preview | 预览(生成二维码) |
| upload | 上传代码 |
| build-npm | 构建 npm |
| upload-function | 上传云函数 |
| upload-storage | 上传云存储 |
| get-sourcemap | 获取 sourceMap |
全局选项
| 选项 | 说明 |
|---|---|
| --appid | 小程序 appid |
| --private-key | 私钥文件路径 |
| --project-path | 项目路径 |
| --type | 项目类型(默认 miniProgram) |
| --output-dir | 输出目录 |
输出目录默认值
| 平台 | 默认输出目录 |
|---|---|
| Linux/Mac/Windows | 系统临时目录下的 wxmini-ci 子目录 |
命令详解
init - 初始化环境
node wxmini-ci.js init --project-path /path/to/project
初始化项目环境:
- 检查项目目录是否存在
- 安装
miniprogram-ci(如果未安装) - 创建输出目录
config - 查看/修改配置
# 查看当前配置
node wxmini-ci.js config
# 获取指定配置项
node wxmini-ci.js config --get appid
# 设置配置项
node wxmini-ci.js config --set appid YOUR_APPID
check - 配置检查
node wxmini-ci.js check --appid <appid> --private-key <keypath> --project-path <path>
检查:AppID、私钥文件、项目目录、project.config.json 是否存在。
preview - 预览
node wxmini-ci.js preview \
--appid YOUR_APPID \
--private-key ~/.credentials/private.YOUR_APPID.key \
--project-path /path/to/your/project \
--version 1.0.0 \
--desc "预览描述" \
--qrcode-format terminal \
--page-path pages/index/index \
--search-query a=1 \
--robot 1 \
--output-dir /tmp/wxmini-ci
preview 特有选项:
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --version | string | 必填 | 版本号 |
| --desc | string | 当前时间 | 描述 |
| --qrcode-format | string | terminal | 二维码格式:terminal/base64/image |
| --page-path | string | 主页面 | 预览页面路径 |
| --search-query | string | 空 | 启动参数 |
| --scene | number | 1011 | 场景值 |
| --robot | number | 1 | CI 机器人 1-30 |
| --output-dir | string | 系统临时目录/wxmini-ci | 二维码输出目录 |
upload - 上传
node wxmini-ci.js upload \
--appid YOUR_APPID \
--private-key ~/.credentials/private.YOUR_APPID.key \
--project-path /path/to/your/project \
--version 1.0.0 \
--desc "修复bug" \
--setting.es6 true \
--setting.minify true \
--robot 1
upload 特有选项:
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --version | string | 必填 | 版本号 |
| --desc | string | 当前时间 | 上传描述 |
| --setting | object | 见下方 | 编译设置 |
| --robot | number | 1 | CI 机器人 1-30 |
| --threads | number | 1 | 编译线程数 |
setting 编译设置:
| 设置 | 类型 | 说明 |
|---|---|---|
| --setting.es6 | boolean | ES6 转 ES5 |
| --setting.es7 | boolean | 增强编译 |
| --setting.minify | boolean | 压缩代码 |
| --setting.minifyJS | boolean | 压缩 JS |
| --setting.minifyWXML | boolean | 压缩 WXML |
| --setting.minifyWXSS | boolean | 压缩 WXSS |
| --setting.codeProtect | boolean | 代码保护 |
| --setting.autoPrefixWXSS | boolean | 样式自动补全 |
build-npm - 构建 npm
node wxmini-ci.js build-npm \
--appid YOUR_APPID \
--private-key ~/.credentials/private.YOUR_APPID.key \
--project-path /path/to/your/project
upload-function - 上传云函数
node wxmini-ci.js upload-function \
--appid YOUR_APPID \
--private-key ~/.credentials/private.YOUR_APPID.key \
--project-path /path/to/your/project \
--env cloud-xxxx \
--name my-function \
--path ./functions/my-function \
--remote-npm-install false
upload-function 特有选项:
| 选项 | 类型 | 必填 | 说明 |
|---|---|---|---|
| --env | string | 是 | 云环境 ID |
| --name | string | 是 | 云函数名称 |
| --path | string | 是 | 云函数目录 |
| --remote-npm-install | boolean | 否 | 云端安装依赖,默认 false |
⚠️ 注意:云函数上传可能需要 miniprogram-ci@alpha 版本
upload-storage - 上传云存储
node wxmini-ci.js upload-storage \
--appid YOUR_APPID \
--private-key ~/.credentials/private.YOUR_APPID.key \
--project-path /path/to/your/project \
--env cloud-xxxx \
--path ./dist \
--remote-path /my-folder
upload-storage 特有选项:
| 选项 | 类型 | 必填 | 说明 |
|---|---|---|---|
| --env | string | 是 | 云环境 ID |
| --path | string | 是 | 本地文件目录 |
| --remote-path | string | 否 | 远端目录 |
⚠️ 注意:云存储上传需要 miniprogram-ci@alpha 版本
get-sourcemap - 获取 SourceMap
node wxmini-ci.js get-sourcemap \
--appid YOUR_APPID \
--private-key ~/.credentials/private.YOUR_APPID.key \
--project-path /path/to/your/project \
--robot 1 \
--output ./sourcemap
get-sourcemap 特有选项:
| 选项 | 类型 | 必填 | 说明 |
|---|---|---|---|
| --robot | number | 是 | CI 机器人 |
| --output | string | 是 | 保存路径 |
环境变量
支持通过环境变量配置,优先级最高(高于配置文件和命令行参数):
| 环境变量 | 对应配置 | 说明 |
|---|---|---|
| WXMINI_APPID | appid | 小程序 appid |
| WXMINI_PRIVATE_KEY_PATH | privateKeyPath | 私钥文件路径 |
| WXMINI_PROJECT_PATH | projectPath | 项目路径 |
| WXMINI_TYPE | type | 项目类型 |
| WXMINI_OUTPUT_DIR | outputDir | 输出目录 |
示例:
export WXMINI_APPID=YOUR_APPID
export WXMINI_PRIVATE_KEY_PATH=~/.credentials/private.YOUR_APPID.key
export WXMINI_PROJECT_PATH=/path/to/your/project
node wxmini-ci.js upload --version 1.0.0 --desc "发布"
配置文件
支持通过 wxmini-ci.config.js 配置文件减少命令行参数。
配置优先级
配置优先级(从高到低):
- 命令行参数 - 直接指定,如
--appid xxx - 环境变量 -
WXMINI_*系列变量 - 配置文件 projects -
wxmini-ci.config.js中的projects映射 - 顶层配置 - 单项目时的简化写法
- 默认配置 - 内置默认值
多项目配置
支持同时管理多个小程序项目,通过 projects 映射配置:
const os = require('os');
const path = require('path');
module.exports = {
// 默认项目(可选,如果不指定项目则使用 default)
default: 'project-a',
// 项目映射
projects: {
'project-a': {
appid: 'YOUR_APPID',
privateKeyPath: '~/.credentials/private.YOUR_APPID.key',
projectPath: '/path/to/your/project-a',
type: 'miniProgram'
},
'project-b': {
appid: 'YOUR_APPID_B',
privateKeyPath: '~/.credentials/private.YOUR_APPID_B.key',
projectPath: '/path/to/your/project-b',
type: 'miniProgram'
}
},
// 编译设置(可选)
setting: {
es6: true,
minify: true
}
}
使用指定项目:
# 使用默认项目
node wxmini-ci.js upload --version 1.0.0
# 使用 project-b
node wxmini-ci.js upload --project project-b --version 1.0.0
单项目配置(简化写法)
如果只需要配置一个项目,可以直接使用顶层配置:
const os = require('os');
const path = require('path');
module.exports = {
appid: 'YOUR_APPID',
privateKeyPath: '~/.credentials/private.YOUR_APPID.key',
projectPath: '/path/to/your/project',
type: 'miniProgram',
outputDir: path.join(os.tmpdir(), 'wxmini-ci'),
setting: {
es6: true,
minify: true
}
}
常见问题
IP 不在白名单
Error: ip xxx.xxx.xxx.xxx not in whitelist
解决:登录 mp.weixin.qq.com → 开发管理 → 开发设置 → IP白名单,加入服务器 IP。
私钥文件不存在
Error: private key not found
解决:确认私钥文件路径正确,内容为微信公众平台下载的 .key 文件。
robot 无权限
Error: robot xx has no permission
解决:确保调用接口的账号有使用对应 CI 机器人的权限。
工作流程示例
# 1. 初始化环境(首次使用)
node wxmini-ci.js init --project-path /path/to/project
# 2. 查看当前配置
node wxmini-ci.js config
# 3. 配置项目
node wxmini-ci.js config --set appid YOUR_APPID
node wxmini-ci.js config --set privateKeyPath ~/.credentials/private.YOUR_APPID.key
# 4. 检查配置
node wxmini-ci.js check
# 5. 预览测试
node wxmini-ci.js preview --version 1.0.0 --desc "功能测试"
# 6. 上传发布
node wxmini-ci.js upload --version 1.0.1 --desc "修复登录bug"
