As above, in addition to uploading code into the developer tools,
We can also use the miniProgram-CI library provided by wechat developer tools to realize one-click uploading.
This provides great convenience for batch uploading of multiple engineering codes.
Let’s try it out.
Create a new upload.js file to store our upload script
touch upload.js
Copy the code
Introducing miniprogram – ci library
npm i miniprogram-ci --save
Copy the code
Introduce it in code
const ci = require('miniprogram-ci');
Copy the code
With uploadAPI provided by the miniProgram-CI library, let’s write:
; (async () => { const project = new ci.Project({ appid: 'xxx', type: 'miniProgram', projectPath: 'the/path/to/project', privateKeyPath: 'the/path/to/privatekey', ignores: ['node_modules/**/*'],}) const uploadResult = await ci.upload({project, version: 'version ', desc:' project note ', setting: {es6: true, }, onProgressUpdate: console.log, }) console.log('uploadResult:', uploadResult); }) ()Copy the code
There are a few parameters to note:
- Appid is the appID of the applets to be uploaded
- ProjectPath Specifies the project directory
- PrivateKeyPath Enter the directory for storing the key
- Version and DESC correspond to the version number and project remarks in the screenshot in Figure 1
At this point, we have the script to upload the code, but we still need some time.
We need to get hold of the uploaded secret key, which is privateKeyPath.
Use the small program administrator to access “wechat public Platform – development – Development Settings”, download the code to upload the key, and configure the IP whitelist.
I will not turn on IP whitelist for testing purposes, but you must understand the risks.
OK, let’s execute the uplaod.js script.
node upload.js
Copy the code
If the following information is displayed on the console, the upload is successful.
Log in the small program background, we can also see the submission record.
At this point, we have a preliminary run through small program code upload script.
The process of executing the upload script actually does a precompilation check on the project code.
For example, in our project code, we deliberately define two variables with the same name.
The following error occurs when executing the script:
Looking back at the previous upload script, there is one detail that could be optimized.
Note that the Upload API provides a listener for upload progress updates, onProgressUpdate.
The above code simply plugs into console.log and prints the progress to the console.
We can optimize:
Define a callback function cb that does two things:
- Filter the string in the callback and record the “file name” of the upload.
- If any errors are reported, mark them
Look at the code (which uses the Kuler library to do string coloring) :
const uploadSucFiles = []; let haveError = false; const cb = output => { if (haveError) return; if (!! output.code) { haveError = true; console.log(output); console.log(kuler('something wrong is happened.pleace check.', '#ff4d4f')); return; } const allowSuffixs = ['js', 'json', 'wxml', 'wxss', 'ts', 'less', 'sass']; const curSuffix = output._msg.slice(output._msg.lastIndexOf('.') + 1); _msg if (allowsuffixs.indexof (curSuffix) >= 0 &&output. _status === 'done') { uploadSucFiles.push(`${output._msg} ${kuler('is uploaded.', '#2da44e')}`); // clear terminal output console.clear(); console.log(uploadSucFiles.join('\n')); }}; // call const uploadResult = await ci.upload({project, version: 'version ', desc:' project note ', setting: {es6: true, }, onProgressUpdate: cb, })Copy the code
The modified console output, as shown in the screenshot below, smells like a package upload 😛.
In case of error, print call stack directly, red prompt
The last
Let’s take a look at the complete upload.js file.
const ci = require('miniprogram-ci'); const kuler = require('kuler'); const uploadSucFiles = []; let haveError = false; const cb = output => { if (haveError) return; if (!! output.code) { haveError = true; console.log(output); console.log(kuler('something wrong is happened.pleace check.', '#ff4d4f')); return; } const allowSuffixs = ['js', 'json', 'wxml', 'wxss', 'ts', 'less', 'sass']; const curSuffix = output._msg.slice(output._msg.lastIndexOf('.') + 1); _msg if (allowsuffixs.indexof (curSuffix) >= 0 &&output. _status === 'done') { uploadSucFiles.push(`${output._msg} ${kuler('is uploaded.', '#2da44e')}`); // clear terminal output console.clear(); console.log(uploadSucFiles.join('\n')); }}; ; (async () => { try { const project = new ci.Project({ appid: 'wx202120212021', type: 'miniProgram', projectPath: './wx202120212021', privateKeyPath: './private.wx202120212021.key', ignores: ['node_modules/**/*'],}) const uploadResult = await ci.upload({project, version: 'version ', desc:' project note ', setting: {es6: true, }, onProgressUpdate: cb, }) // console.log('uploadResult:', uploadResult); console.log('\n'); The console. The log (` ${kuler (' upload success ~ ', '# ff5722')} 🎉 🥳 `); } catch (error) { cb(error); }}) ()Copy the code
Entire directory structure
The project has also been uploaded to Github, see here miniprogram-Ci-onestep.
After clone is removed, you need to replace the corresponding file name and upload the secret key.
In addition
The miniProgram-CI library supports node script invocation as well as command line invocation.
The installation
npm install -g miniprogram-ci
Copy the code
The sample
miniprogram-ci \ upload \ --pp ./demo-proj/ \ --pkp ./private.YOUR_APPID.key \ --appid YOUR_APPID \ --uv PACKAGE_VERSION \ -r 1 \ --enable-es6 true \Copy the code