Authored by 陈峰

mini cli

  1 +{
  2 + "wxpath": "/Applications/wechatwebdevtools.app/Contents/Resources/app.nw/bin"
  3 +}
@@ -28,6 +28,7 @@ App({ @@ -28,6 +28,7 @@ App({
28 awakeReported: false 28 awakeReported: false
29 }, 29 },
30 onLaunch(options) { 30 onLaunch(options) {
  31 + console.log(wx.getExtConfigSync());
31 this.globalData.udid = udid.get(); // 生成 UDID 32 this.globalData.udid = udid.get(); // 生成 UDID
32 33
33 verify.gen(); // 此处返回是是 Promise,需要调用接口的业务,最好在 then 里边执行 34 verify.gen(); // 此处返回是是 Promise,需要调用接口的业务,最好在 then 里边执行
  1 +const inquirer = require('inquirer');
  2 +const shelljs = require('shelljs');
  3 +const path = require('path');
  4 +const _ = require('lodash');
  5 +const fs = require('fs');
  6 +
  7 +module.exports = ({code_path, env}) => {
  8 + const dirs = shelljs.ls(path.join(__dirname, '../mode'));
  9 +
  10 + return inquirer.prompt([
  11 + {
  12 + type: 'list',
  13 + name: 'mode',
  14 + message: '选择小程序主体开发主体',
  15 + choices: dirs
  16 + }
  17 + ]).then(({mode}) => {
  18 + const configs = shelljs.ls(path.join(__dirname, '../mode', mode));
  19 +
  20 + _.each(configs, c => {
  21 + if (/\.js$/.test(c)) {
  22 + const fileName = c.replace(/\.js$/, '');
  23 + const baseConfig = require(path.join(__dirname, '../config', `${fileName}.base.js`));
  24 + let envConfig, modeConfig;
  25 +
  26 + if (env === 'production') {
  27 + envConfig = require(path.join(__dirname, '../config', `${fileName}.prod.js`));
  28 + } else {
  29 + envConfig = require(path.join(__dirname, '../config', `${fileName}.dev.js`));
  30 + }
  31 + modeConfig = require(path.join(__dirname, '../mode', mode, `${fileName}.js`));
  32 + const fullConfig = _.merge(baseConfig, envConfig, modeConfig);
  33 +
  34 + fs.writeFile(path.join(__dirname, '../', code_path, c),
  35 + `/* eslint-disable */
  36 +export default ${JSON.stringify(fullConfig, null, 2)}`,
  37 + err => {
  38 + if (err) {
  39 + throw err;
  40 + }
  41 + });
  42 + } else if (/\.json$/.test(c)) {
  43 + const baseConfig = require(path.join(__dirname, '../config', c));
  44 + const modeConfig = require(path.join(__dirname, '../mode', mode, c));
  45 + const fullConfig = _.merge(baseConfig, modeConfig);
  46 +
  47 + fs.writeFile(path.join(__dirname, '../', code_path, c), JSON.stringify(fullConfig, null, 2), err => {
  48 + if (err) {
  49 + throw err;
  50 + }
  51 + });
  52 + }
  53 + });
  54 + });
  55 +};
1 -const inquirer = require('inquirer');  
2 -const shelljs = require('shelljs');  
3 const path = require('path'); 1 const path = require('path');
4 -const _ = require('lodash');  
5 -const fs = require('fs'); 2 +const cp = require('child_process');
6 3
7 -module.exports = ({code_path, env}) => {  
8 - const dirs = shelljs.ls(path.join(__dirname, '../mode'));  
9 -  
10 - inquirer.prompt([  
11 - {  
12 - type: 'list',  
13 - name: 'mode',  
14 - message: '选择小程序主体开发主体',  
15 - choices: dirs  
16 - }  
17 - ]).then(({mode}) => {  
18 - try {  
19 - const configs = shelljs.ls(path.join(__dirname, '../mode', mode));  
20 -  
21 - _.each(configs, c => {  
22 - if (/\.js$/.test(c)) {  
23 - const fileName = c.replace(/\.js$/, '');  
24 - const baseConfig = require(path.join(__dirname, '../config', `${fileName}.base.js`));  
25 - let envConfig, modeConfig; 4 +const genConfig = require('./gen-config');
  5 +const wxrc = require('./wxrc');
26 6
27 - if (env === 'production') {  
28 - envConfig = require(path.join(__dirname, '../config', `${fileName}.prod.js`));  
29 - } else {  
30 - envConfig = require(path.join(__dirname, '../config', `${fileName}.dev.js`));  
31 - }  
32 - modeConfig = require(path.join(__dirname, '../mode', mode, `${fileName}.js`));  
33 - const fullConfig = _.merge(baseConfig, envConfig, modeConfig);  
34 -  
35 - fs.writeFile(path.join(__dirname, '../', code_path, c),  
36 - `export default ${JSON.stringify(fullConfig, null, 2)}`,  
37 - err => {  
38 - if (err) {  
39 - throw err;  
40 - }  
41 - });  
42 - } else if (/\.json$/.test(c)) {  
43 - const baseConfig = require(path.join(__dirname, '../config', c));  
44 - const modeConfig = require(path.join(__dirname, '../mode', mode, c));  
45 - const fullConfig = _.merge(baseConfig, modeConfig); 7 +module.exports = ({code_path, env}) => {
  8 + genConfig({code_path, env}).then(() => wxrc()).then(({wxpath}) => {
  9 + const ls = cp.spawn(`${wxpath}/cli`, ['-o', path.join(__dirname, '../', code_path)]);
46 10
47 - fs.writeFile(path.join(__dirname, '../', code_path, c), JSON.stringify(fullConfig, null, 2), err => {  
48 - if (err) {  
49 - throw err;  
50 - }  
51 - });  
52 - }  
53 - });  
54 - } catch (e) {  
55 - console.error(e);  
56 - } 11 + ls.stdout.on('data', (data) => {
  12 + console.log(data.toString());
  13 + });
  14 + ls.stderr.on('data', (data) => {
  15 + console.log(data.toString());
  16 + });
  17 + }).catch(e => {
  18 + console.error('error', e);
57 }); 19 });
58 }; 20 };
  1 +const path = require('path');
  2 +const cp = require('child_process');
  3 +
  4 +const genConfig = require('./gen-config');
  5 +const wxrc = require('./wxrc');
  6 +
1 module.exports = ({code_path, ver, desc}) => { 7 module.exports = ({code_path, ver, desc}) => {
2 - console.log(code_path, ver, desc); 8 + genConfig({code_path, env: 'production'}).then(() => wxrc()).then(({wxpath}) => {
  9 + const ls = cp.spawn(`${wxpath}/cli`, [
  10 + '-u', `${ver}@${path.join(__dirname, '../', code_path)}`, '--upload-desc', desc
  11 + ]);
  12 +
  13 + ls.stdout.on('data', (data) => {
  14 + console.log(data.toString());
  15 + });
  16 + ls.stderr.on('data', (data) => {
  17 + console.log(data.toString());
  18 + });
  19 + }).catch(e => {
  20 + console.error('error', e);
  21 + });
3 }; 22 };
  1 +const inquirer = require('inquirer');
  2 +const path = require('path');
  3 +const fs = require('fs');
  4 +
  5 +module.exports = () => {
  6 + const configPath = path.join(__dirname, '../.wxrc');
  7 + const existDev = fs.existsSync(configPath);
  8 +
  9 + return new Promise((resolve, reject) => {
  10 + let wxrc;
  11 +
  12 + if (existDev) {
  13 + wxrc = JSON.parse(fs.readFileSync(configPath));
  14 + } else {
  15 + inquirer.prompt([
  16 + {
  17 + type: 'input',
  18 + name: 'wxpath',
  19 + message: '小程序开发者工具安装目录',
  20 + }
  21 + ]).then(({wxpath}) => {
  22 + fs.writeFile(configPath, wxpath);
  23 + wxrc = {
  24 + wxpath
  25 + };
  26 + });
  27 + }
  28 + if (!fs.existsSync(path.join(wxrc.wxpath, 'cli'))) {
  29 + return reject('请先安装微信开发者工具');
  30 + }
  31 + resolve(wxrc);
  32 + });
  33 +};
1 module.exports = { 1 module.exports = {
2 - appid: 'wxe8bfc9b404772199' 2 + appid: 'wx3f08685409f22510'
3 }; 3 };
1 { 1 {
2 - "appid": "wxe8bfc9b404772199",  
3 - "projectname": "%E7%BA%A2%E4%BA%BA%E5%B0%8F%E5%BA%97" 2 + "appid": "wx3f08685409f22510",
  3 + "projectname": "有货店铺-VANS"
4 } 4 }
1 { 1 {
2 - "appid": "wxe8bfc9b40477219922222",  
3 - "projectname": "%E7%BA%A2%E4%BA%BA%E5%B0%8F%E5%BA%97" 2 + "appid": "wxe8bfc9b404772199",
  3 + "projectname": "有货官方-VANS"
4 } 4 }