1.9.15 • Published 1 month ago

miniprogram-ci v1.9.15

Weekly downloads
720
License
MIT
Repository
-
Last release
1 month ago

介绍

miniprogram-ci 是从微信开发者工具中抽离的关于小程序/小游戏项目代码的编译模块。

使用前需要使用小程序管理员身份访问"微信公众平台-开发-开发设置"后下载代码上传密钥,并配置 IP 白名单,才能进行上传、预览操作。

注意事项

  1. 代码上传密钥拥有预览、上传代码的权限
  2. 代码上传密钥不会明文存储在微信公众平台上,一旦遗失必须重置,请妥善保管
  3. 未配置IP白名单的,将无法使用 miniprogram-ci 进行预览和上传
  4. 可选择不对IP进行限制,但务必明白风险

功能

miniprogram-ci 目前提供以下能力: 1. 上传代码,对应小程序开发者工具的上传 1. 预览代码,对应小程序开发者工具的预览 1. 构建 npm,对应小程序开发者工具的: 菜单-工具-构建npm 1. 上传云开发云函数代码,对应小程序开发者工具的上传云函数能力 1. 代理,配置 miniprogram-ci 的网络请求代理方式 1. 支持获取最近上传版本的 sourceMap 1. 支持 node 脚本调用方式和 命令行 调用方式

脚本调用

npm install miniprogram-ci --save

项目对象

项目对象是本模块主要的入参,可以依据下边的定义自行实现

项目对象的定义:

interface IProject {
  appid: string
  type: string
  projectPath: string
  privateKey: string
  attr(): Promise<IProjectAttr>
  stat(prefix: string, filePath: string): IStat | undefined
  getFile(prefix: string, filePath: string): Promise<Buffer>
  getFileList(prefix: string, extName: string): string[]
  updateFiles: () => void
}
类型说明
appid属性小程序/小游戏项目的 appid
type属性项目的类型,有效值 miniProgram/miniProgramPlugin/miniGame/miniGamePlugin
projectPath属性项目的路径,即 project.config.json 所在的目录
privateKey属性私钥,在获取项目属性和上传时用于鉴权使用,在 微信公众平台 上使用小程序管理员登录后下载
attr异步方法项目的属性,如指定了 privateKey 则会使用真实的项目属性
stat同步方法特定目录下前缀下(prefix)文件路径 (filePath) 的 stat, 如果不存在则返回 undefined
getFile异步方法特定目录下前缀下(prefix)文件路径 (filePath) 的 Buffer
getFileList同步方法特定目录下前缀下(prefix)文件路径 (filePath) 下的文件列表
updateFile同步方法更新项目文件

也可以通过指定项目路径来创建该对象

const ci = require('miniprogram-ci')
const project = new ci.Project({
  appid: 'wxsomeappid',
  type: 'miniProgram',
  projectPath: 'the/project/path',
  privateKeyPath: 'the/privatekey/path',
  ignores: ['node_modules/**/*'],
})
类型必填说明
appidstring合法的小程序/小游戏 appid
projectPathstring项目路径,即 project.config.json 所在的目录
privateKeyPathstring私钥的路径
typestring显示指明当前的项目类型, 默认为 miniProgram,有效值 miniProgram/miniProgramPlugin/miniGame/miniGamePlugin
ignoresstring[]指定需要排除的规则

上传

const ci = require('miniprogram-ci')
;(async () => {
  const project = new ci.Project({
    appid: 'wxsomeappid',
    type: 'miniProgram',
    projectPath: 'the/project/path',
    privateKeyPath: 'the/path/to/privatekey',
    ignores: ['node_modules/**/*'],
  })
  const uploadResult = await ci.upload({
    project,
    version: '1.1.1',
    desc: 'hello',
    setting: {
      es6: true,
    },
    onProgressUpdate: console.log,
  })
  console.log(uploadResult)
})()

参数

类型必填说明
projectIProject#项目对象
versionstring自定义版本号
descstring自定义备注
settingobject#编译设置
onProgressUpdatefunction进度更新监听函数
robotnumber指定使用哪一个 ci 机器人,可选值:1 ~ 30

返回

类型必填说明
subPackageInfoArray<{name:string, size:number}>小程序包信息, name__FULL__ 时表示整个小程序包, name__APP__ 时表示小程序主包,其他情况都表示分包
pluginInfoArray<{pluginProviderAppid: string, version: string, size:number}>小程序插件信息
devPluginIdstring插件开发模式下,上传版本的插件 id

预览

const ci = require('miniprogram-ci')
;(async () => {
  const project = new ci.Project({
    appid: 'wxsomeappid',
    type: 'miniProgram',
    projectPath: 'the/project/path',
    privateKeyPath: 'the/path/to/privatekey',
    ignores: ['node_modules/**/*'],
  })
  const previewResult = await ci.preview({
    project,
    desc: 'hello',
    setting: {
      es6: true,
    },
    qrcodeFormat: 'image',
    qrcodeOutputDest: '/path/to/qrcode/file/destination.jpg',
    onProgressUpdate: console.log,
    // pagePath: 'pages/index/index', // 预览页面
    // searchQuery: 'a=1&b=2',  // 预览参数 [注意!]这里的`&`字符在命令行中应写成转义字符`\&`
    // scene: 1011, // 场景值
  })
  console.log(previewResult)
})()

参数

类型必填说明
projectIProject#项目对象
descstring自定义备注
settingobject#编译设置
onProgressUpdatefunction进度更新监听函数
robotnumber指定使用哪一个 ci 机器人,可选值:1 ~ 30
qrcodeFormatstring返回二维码文件的格式 "image""base64", 默认值 "terminal" 供调试用
qrcodeOutputDeststring二维码文件保存路径
pagePath:string预览页面路径
searchQuery:string预览页面路径启动参数
scenenumber默认值 1011,具体含义见场景值列表

返回

类型必填说明
subPackageInfoArray<{name:string, size:number}>小程序包信息, name__FULL__ 时表示整个小程序包, name__APP__ 时表示小程序主包,其他情况都表示分包
pluginInfoArray<{pluginProviderAppid: string, version: string, size:number}>小程序插件信息

拉取最近上传版本的sourceMap

const ci = require('miniprogram-ci')
;(async () => {
  const project = new ci.Project({
    appid: 'wxsomeappid',
    type: 'miniProgram',
    projectPath: 'the/project/path',
    privateKeyPath: 'the/path/to/privatekey',
    ignores: ['node_modules/**/*'],
  })
  await ci.getDevSourceMap({
    project,
    robot: 1,
    sourceMapSavePath: './sm.zip'
  })
})()

入参

类型必填说明
projectIProject#项目对象
robotnumber指定使用哪一个 ci 机器人,可选值:1 ~ 30
sourceMapSavePathstring保存的路径

构建npm

对应开发者工具构建npm功能。

const ci = require('miniprogram-ci')
;(async () => {
  const project = new ci.Project({
    appid: 'wxsomeappid',
    type: 'miniProgram',
    projectPath: 'the/project/path',
    privateKeyPath: 'the/path/to/privatekey',
    ignores: ['node_modules/**/*'],
  })
  // 在有需要的时候构建npm
  const warning = await ci.packNpm(project, {
    ignores: ['pack_npm_ignore_list'],
    reporter: (infos) => { console.log(infos) }
  })
  console.warn(warning)
  // 可对warning进行格式化
  /*
    warning.map((it, index) => {
            return `${index + 1}. ${it.msg}
    \t> code: ${it.code}
    \t@ ${it.jsPath}:${it.startLine}-${it.endLine}`
          }).join('---------------\n')
  */
  // 完成构建npm之后,可用ci.preview或者ci.upload
})()
参数类型必填说明
projectIProject项目对象
options.ignoresstring[]指定构建npm需要排除的规则
options.reporterfunction构建回调信息

自定义 node_modules 位置的构建 npm

有的时候,需要被构建模块对应的 node_modules 可能并不在小程序项目内,所以提供了一个新的接口来支持这个需求。 例如有如下项目结构

├── lib # lib目录存放要被构建的 node_modules
│   ├── node_modules
│   │   └── is-object
│   └── package.json
└── miniprogram-project # 这里是小程序项目路径
    ├── miniprogram # 我们希望最终把 miniprogram_npm 构建在 miniprogram/ 目录之下
    │   ├── app.js
    │   ├── app.json
    │   ├── app.wxss
    │   ├── pages
    │   │   ├── index
    │   │   └── logs
    │   └── sitemap.json
    └── project.config.json

于是可以这样调用

let packResult = await ci.packNpmManually({
  packageJsonPath: './lib/package.json',
  miniprogramNpmDistDir: './miniprogram-project/miniprogram/',
})

console.log('pack done, packResult:', packResult)
// 输出 pack done, packResult: { miniProgramPackNum: 0, otherNpmPackNum: 1, warnList: [] }

得到的最终项目

.
├── lib
│   ├── node_modules
│   │   └── is-object
│   └── package.json
└── miniprogram-project
    ├── miniprogram
    │   ├── app.js
    │   ├── app.json
    │   ├── app.wxss
    │   ├── miniprogram_npm # <--- 这就是构建出来的由 lib/node_modules 里 miniprogram_npm 了
    │   ├── pages
    │   └── sitemap.json
    └── project.config.json
参数类型必填说明
options.packageJsonPathstring希望被构建的node_modules 对应的 package.json 的路径
options.miniprogramNpmDistDirstring被构建 miniprogram_npm 的目标位置目标位置
options.ignoresstring[]指定需要排除的规则

上传云开发云函数

对应开发者工具云开发云函数上传能力。

const ci = require('miniprogram-ci')
;(async () => {
  const project = new ci.Project({
    appid: 'wxsomeappid',
    type: 'miniProgram',
    projectPath: 'the/project/path',
    privateKeyPath: 'the/path/to/privatekey',
    ignores: ['node_modules/**/*'],
  })

  const result = await ci.cloud.uploadFunction({
    project,
    env: '云环境 ID',
    name: '云函数名称',
    path: '云函数代码目录',
    remoteNpmInstall: true, // 是否云端安装依赖
  })
  console.warn(result)
})()
参数类型必填说明
projectIProject项目对象
envstring云环境 ID
namestring云函数名称
pathstring云函数代码目录
remoteNpmInstallboolean是否云端安装依赖,默认 false

remoteNpmInstall 额外说明:true 时云端安装依赖,不会上传本地 node_modules,false 时全量上传,包括上传 node_modules

代理

miniprogram-ci 使用了 get-proxy 模块来自动获取代理地址。 如果不适用ci.proxy()方法或者--proxy参数来指定代理,那么 miniprogram-ci 将会按照以下顺序去获取 https 代理地址

  1. 获取环境变量中的 HTTPS_PROXY
  2. 获取环境变量中的 https_proxy
  3. 获取环境变量中的 HTTP_PROXY
  4. 获取环境变量中的 http_proxy
  5. 获取npm配置的 https-proxy
  6. 获取npm配置的 http-proxy
  7. 获取npm配置的 proxy
  8. sercerce_wechat.com 加入到环境变量中的 no_proxy,miniprogram-ci 将不通过代理发送网络请求
const ci = require('miniprogram-ci')
ci.proxy('YOUR_PROXY_URL')

命令行调用

npm install -g miniprogram-ci
#help
miniprogram-ci --help

#preview
miniprogram-ci \
  preview \
  --pp ./demo-proj/ \
  --pkp ./private.YOUR_APPID.key \
  --appid YOUR_APPID \
  --uv PACKAGE_VERSION \
  -r 1 \
  --enable-es6 true \
  --proxy YOUR_PROXY \
  --qrcode-format image \
  --qrcode-output-dest '/tmp/x.jpg' \

#upload

miniprogram-ci \
  upload \
  --pp ./demo-proj/ \
  --pkp ./private.YOUR_APPID.key \
  --appid YOUR_APPID \
  --uv PACKAGE_VERSION \
  -r 1 \
  --enable-es6 true \

#pack-npm
miniprogram-ci \
  pack-npm \
  --pp ./YOUR_PROJECT/ \
  --pkp ./private.YOUR_APPID.key \
  --appid YOUR_APPID \

#pack-npm-manually
miniprogram-ci \
pack-npm-manually \
--pack-npm-manually-package-json-path PACKAGE_JSON_PATH \
--pack-npm-manually-miniprogram-npm-dist-dir DISTPATH

#cloudbase upload cloudfunction
miniprogram-ci cloud functions upload \
  --pp ./YOUR_PROJECT/ \
  --appid YOUR_APPID \
  --pkp ./private.YOUR_APPID.key \
  --env YOUR_CLOUD_ENV_ID \   
  --name YOUR_CLOUD_FUNCTION_NAME \
  --path ./YOUR_CLOUD_FUNCTION_FOLDER_PATH/ \
  --remote-npm-install true

#Proxy
export HTTPS_PROXY = YOUR_PROXY_URL # 可以在shell脚本里声明临时proxy

miniprogram-ci \
  upload \
  --pp ./demo-proj/ \
  --pkp ./private.YOUR_APPID.key \
  --appid YOUR_APPID \
  --uv PACKAGE_VERSION \
  -r 1 \
  --enable-es6 true \
  --proxy YOUR_PROXY_URL # 也可以使用这个参数声明proxy

  #get dev source map
  miniprogram-ci \
  get-dev-source-map \
  --pp ./demo-proj/ \
  --pkp ./private.YOUR_APPID.key \
  --appid YOUR_APPID \
  -r 1 \ # 获取具体哪个robot最近上传的版本的sourceMap
  --source-map-save-path ./sourcemap.zip #保存路径,推荐zip结尾,最后得到的是一个zip包

编译设置

类型说明
es6boolean对应于微信开发者工具的 "es6 转 es5"
es7boolean对应于微信开发者工具的 "增强编译"
minifyJSboolean上传时压缩 JS 代码
minifyWXMLboolean上传时压缩 WXML 代码
minifyWXSSboolean上传时压缩 WXSS 代码
minifyboolean上传时压缩所有代码,对应于微信开发者工具的 "上传时压缩代码"
codeProtectboolean对应于微信开发者工具的 "上传时进行代码保护"
autoPrefixWXSSboolean对应于微信开发者工具的 "上传时样式自动补全"

Tips

miniprogram-ci1.0.28 开始支持第三方平台开发的上传和预览,调用方式与普通开发模式无异。 使用第三方平台开发模式时,应注意:

  • 请确保项目中存在正确的 ext.json
  • 密钥文件是第三方平台绑定的开发小程序 appid 的密钥文件
  • ip白名单是第三方平台绑定的开发小程序 appid 的 ip 白名单
  • 调用传入的 appid 是第三方平台绑定的开发小程序 appid 关于第三方平台开发模式,请参考这里
1.9.27

1 month ago

1.9.26

1 month ago

1.9.16

1 month ago

1.9.15

3 months ago

1.9.14

3 months ago

1.9.13

3 months ago

1.9.11

3 months ago

1.9.10

7 months ago

1.9.9

7 months ago

1.9.8

11 months ago

1.9.7

12 months ago

1.9.6

1 year ago

1.8.60

1 year ago

1.9.5

1 year ago

1.9.3

1 year ago

1.9.1

1 year ago

1.8.53

2 years ago

1.8.25

2 years ago

1.8.35

2 years ago

1.8.18

2 years ago

1.8.12

2 years ago

1.8.0

2 years ago

1.6.10

2 years ago

1.6.1

3 years ago

1.5.12

3 years ago

1.5.1

3 years ago

1.4.22

3 years ago

1.4.13

3 years ago

1.3.13

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.2.3

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.0.98

3 years ago

1.0.94

3 years ago

1.0.93

3 years ago

1.0.83

4 years ago

1.0.79

4 years ago

1.0.76

4 years ago

1.0.72

4 years ago

1.0.60

4 years ago

1.0.58

4 years ago

1.0.57

4 years ago

1.0.51

4 years ago

1.0.48

4 years ago

1.0.45

4 years ago

1.0.44

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.33

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.23

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.5

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago