background

Small program has provided ci package, can achieve automatic script upload function, but the version number has to be manually modified by the developer every time, it is more troublesome, and from the community small program official response is, will not provide this function

Implement a simple script yourself

Project root path updateVersion. Js

const semver = require('semver')
let { version } = require('./package.json')
const fs = require('fs')

// Update the small version first
let patchVersion = semver.inc(version, 'patch')

Parse after the update
let patchVersionObject = semver.parse(patchVersion)

// When the smaller version reaches 10
if (patchVersionObject && patchVersionObject.patch > 10) {
  // Update version
  patchVersion = semver.inc(patchVersionObject, 'minor')}// If the version is more than 20, carry
if (patchVersionObject && patchVersionObject.minor > 20) {
  patchVersion = semver.inc(patchVersionObject, 'major')}function updateVersion () {
  let strem = fs.readFileSync('./package.json').toString()
  // Convert to JSON
  strem = JSON.parse(strem)

  strem.version = patchVersion

  // Write to the file
  fs.writeFileSync('./package.json'.JSON.stringify(strem, null.2))

  console.log('Version updated successfully')}/** * Execute updateVersion() */ depending on the release environment, if the release is packaged in production

module.exports = updateVersion
Copy the code

Applet automatic upload script

const ci = require('miniprogram-ci');
const updateVersion = require('./updateVersion.js')
const env = process.env.releaseEnv

const appid = ' ';
const privatekey = ' ';


// Different companies may have n sets of environments, but we only care about the production environment to update the version number
if (env === 'prod') {
  updateVersion()
}

// Read the version after
const pkg = require('./package.json');

async function upload() {
  const project = new ci.Project({
    appid,
    type: 'miniProgram'.// Modify your own directory
    projectPath: './dist'.privateKeyPath: privatekey,
    ignores: ['node_modules/**/*']});try {
    await ci.upload({
      project,
      version: pkg.version,
      desc,
      setting: {
        es6: false.es7: false.autoPrefixWXSS: true.minify: true,}}); }catch (error) {
    throw error;
  }
}

upload()

Copy the code

The trigger time of the above script can be triggered according to the actual situation of the company. If the company does not have a complete publishing system, individual soldier can directly execute the NPM script, or configure it in THE CI of GitLab. When the test branch merges with the Master branch, runner will be triggered, or the content change of the Master branch will automatically trigger the script

Let you learn knowledge clearly, there are codes, there are explanations, copy of the walk, learn will! Give it a thumbs up 😁😁😁

The nuggets article theme and code highlighting use the following configuration:

highlight: monokai theme: fancy