Repository navigation
Expand file tree
/
Copy pathwechatdevtools-cli.js
More file actions
172 lines (151 loc) · 6.38 KB
/
Copy pathwechatdevtools-cli.js
File metadata and controls
172 lines (151 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const path = require('path');
const fs = require('fs');
const Table = require('cli-table3');
const {
consoleSeparator,
findProcessPathFromCurrentProcessList,
executeCmd
} = require('./util.js');
// 微信开发者工具默认安装目录
const DEFAULT_WECHATDEVTOOLS_INSTALL_PATH_WINDOWS = 'D:/Program Files (x86)/Tencent/微信web开发者工具';
const DEFAULT_WECHATDEVTOOLS_INSTALL_PATH_MacOs = '/Applications/wechatwebdevtools.app';
class WechatdevtoolsCli {
/**
* 微信开发者工具命令行
*
* @param {string} cliPath 微信小程序开发者工具 CLI 的路径, 默认从当前进程中查找
*/
constructor(cliPath) {
this.cliPath = cliPath;
this.processName = 'wechatdevtools';
this.cliBin = (process.platform === 'win32') ? 'cli.bat' : 'cli';
}
/**
* 尝试从当前进程中查找"微信Web开发者工具" cli 可执行文件的路径
*
* @return {Promise<string>} cli 可执行文件的路径
*/
tryFindCliPathFromCurrentProcessList() {
return findProcessPathFromCurrentProcessList(this.processName).then((processPath) => {
return path.resolve(path.dirname(processPath), this.cliBin);
});
}
/**
* 执行"微信Web开发者工具"的命令行工具
*
* 也可以通过 HTTP 请求指示工具进行操作, 实现思路: http 服务在工具启动后自动开启,HTTP 服务端口号在用户目录下记录,可通过检查用户目录、检查用户目录下是否有端口文件及尝试连接来判断工具是否安装/启动。端口号文件位置:
* - macOS : ~/Library/Application Support/微信web开发者工具/Default/.ide
* - Windows : ~/AppData/Local/微信web开发者工具/User Data/Default/.ide
* - require('os').homedir()
* - https://github.com/pandolajs/pandora-boilerplate-wechat/blob/master/.pandora/scripts/utils/index.js
* - https://github.com/pandolajs/pandora-boilerplate-wechat/blob/master/.pandora/scripts/release.js
*
* @param {string} args 命令参数
* @return {Promise}
* @see https://developers.weixin.qq.com/miniprogram/dev/devtools/cli.html
* @see https://developers.weixin.qq.com/miniprogram/dev/devtools/http.html
*/
execute(args) {
if (this.cliPath) {
return this._executeCli(args);
} else {
return this.tryFindCliPathFromCurrentProcessList().then((cliPath) => {
if (cliPath) {
this.cliPath = cliPath;
} else {
console.log('Not find cliPath from process list, try use default cliPath');
this.cliPath = this._getDefaultCliPath();
}
return this._executeCli(args);
}, (error) => {
console.log('Find cliPath from process list fail', error);
console.log('try use default cliPath');
this.cliPath = this._getDefaultCliPath();
return this._executeCli(args);
});
}
}
_executeCli(args) {
if (this.cliPath) {
console.log(consoleSeparator);
console.log(`Use wechatdevtools cli: ${this.cliPath}`);
console.log(consoleSeparator);
return executeCmd(`"./${path.basename(this.cliPath)}" ${args}`, {
cwd: path.dirname(this.cliPath)
}).then(function(stout) {
return stout;
}, function(stderr) {
return stderr;
});
} else {
return Promise.reject('none cliPath');
}
}
_getDefaultCliPath() {
var cliPath = '';
if (process.platform === 'win32') {
cliPath = path.resolve(DEFAULT_WECHATDEVTOOLS_INSTALL_PATH_WINDOWS, this.cliBin);
} else {
cliPath = path.resolve(DEFAULT_WECHATDEVTOOLS_INSTALL_PATH_MacOs, 'Contents/MacOS', this.cliBin);
}
return cliPath;
}
/**
* 提交预览
*
* @param {string} projectRoot 项目根路径
* @return {Promise} 预览的额外信息
*/
preview(projectRoot) {
var previewInfoOutput = path.resolve(process.cwd(), 'preview-info.json');
var args = `-p ${path.resolve(projectRoot)} --preview-info-output "${previewInfoOutput}"`;
return this.execute(args).then(function() {
var previewInfo = {};
try {
if (fs.existsSync(previewInfoOutput)) {
previewInfo = JSON.parse(fs.readFileSync(previewInfoOutput));
console.log(consoleSeparator);
console.log(`本次预览的额外信息: ${JSON.stringify(previewInfo)}`);
fs.unlink(previewInfoOutput, function() {});
}
} catch (error) {
console.error('Read preview info fail :(', error);
}
return previewInfo;
});
}
/**
* 上传代码
*
* @param {string} projectRoot 项目根路径
* @param {string} version 版本号
* @param {string} desc 备注
* @return {Promise} 上传的额外信息
*/
upload(projectRoot, version, desc) {
var uploadInfoOutput = path.resolve(process.cwd(), 'upload-info.json');
var _projectRoot = path.resolve(projectRoot);
var args = `-u ${version}@${_projectRoot} --upload-desc "${desc}" --upload-info-output "${uploadInfoOutput}"`;
return this.execute(args).then(function() {
var uploadInfo = {};
try {
if (fs.existsSync(uploadInfoOutput)) {
console.log(consoleSeparator);
var table = new Table({
head: ['时间', '项目', '版本号', '项目备注']
});
table.push([new Date().toLocaleString(), _projectRoot, version, desc]);
console.log(table.toString());
uploadInfo = JSON.parse(fs.readFileSync(uploadInfoOutput));
console.log(consoleSeparator);
console.log(`本次上传的额外信息: ${JSON.stringify(uploadInfo)}`);
fs.unlink(uploadInfoOutput, function() {});
}
} catch (error) {
console.error('Read upload info fail :(', error);
}
return uploadInfo;
});
}
}
module.exports = WechatdevtoolsCli;