-
Notifications
You must be signed in to change notification settings - Fork 6
/
autojs-deploy.js
460 lines (439 loc) · 14 KB
/
autojs-deploy.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
const { Adb } = require("@devicefarmer/adbkit");
const { readFile } = require("fs/promises");
const EventEmitter = require("events");
const path = require("path");
const {
SourceMapConsumer,
} = require("source-map");
class AutojsDeployPlugin {
constructor (options = {}) {
this.options = {
...AutojsDeployPlugin.defaultOptions,
...options,
};
if (options.remoteDir) {
options.remoteDir = path.posix.resolve(options.remoteDir);
}
this.adb = Adb.createClient(this.options.adb);
}
// 缺省配置
static defaultOptions = {
packageName: {},
project: {},
build: {
ui: true,
},
deploy: {
run: true,
skipSourceMap: true,
},
logcat: {
stdout: process.stdout,
sourceMap: true,
},
};
// 读取AutoJS项目的`project.json`文件
async getProjectConfig () {
let projectConfig = await readFile(
this.options.configFile,
"utf-8",
);
projectConfig = JSON.parse(projectConfig);
this.options.project = projectConfig;
if (!projectConfig.projectDirectory) {
projectConfig.projectDirectory = projectConfig.packageName || projectConfig.name || require(path.join(process.cwd(), "package.json")).name;
}
projectConfig.projectDirectory = path.posix.resolve("/storage/emulated/0/脚本/", projectConfig.projectDirectory);
return projectConfig;
}
// 获取手机中安装的AutoJS Pro 或者AutoX的包名
async getPackageName (device) {
let packageName = this.options.packageName[device.serial];
if (!packageName) {
let packages = await this.shell(device, "pm list package org.autojs.");
packages = packages.trim().split(/\r?\n/g);
if (packages.length) {
packageName = packages[0].replace(/^package\s*:\s*/, "").trim();
this.options.packageName[device.serial] = packageName;
}
}
device.packageName = packageName;
return device;
}
// 日志显示功能,AutoJS Pro可直接使用,AutoX需要在项目代码中配置日志文件路径:`console.setGlobalLogConfig({file: files.join(context.getExternalFilesDir("logs"),"log.txt")});`
async logcat (compilation) {
if (!this.options.logcat) {
return;
}
const project = await this.getProjectConfig();
const logcat = async (device, packageName) => {
if (!packageName) {
return;
}
let cmd = compilation.compiler.options.watch ? "tail -f" : "cat";
cmd += ` /storage/emulated/0/Android/data/${packageName}/files/logs/log.txt`;
const log = await device.shell(cmd);
const transform = new LogTransform();
const stdout = this.options.logcat.stdout || process.stdout;
transform.colors = stdout.hasColors && stdout.hasColors() && (this.options.logcat.colors || transform.colors);
transform.sourceMap = this.options.logcat.sourceMap;
const startOptput = () => {
console.log("> adb shell", cmd);
log.pipe(
transform,
).pipe(
stdout,
);
};
if (compilation.compiler.options.watch) {
log.once("data", startOptput);
} else {
startOptput();
}
this.options.logcat.manager.once("close", () => log.end());
return log;
};
const start = async (device) => {
return await Promise.all(
[
project.packageName,
device.packageName,
].map(packageName => logcat(device, packageName)),
);
};
if (this.options.logcat.manager) {
this.options.logcat.manager.emit("close");
} else {
this.options.logcat.manager = new EventEmitter();
if (compilation.compiler.options.watch) {
process.nextTick(async () => {
const tracker = await this.adb.trackDevices();
tracker.on("add", async (device) => {
device = this.adb.getDevice(device.id);
await device.waitForDevice();
await this.getPackageName(device);
await start(device);
});
});
}
}
await this.eachDevice(start);
}
// 通过ADB在手机的shell中运行命令
shell (device, ...args) {
return device.shell(...args)
.then(Adb.util.readAll)
.then((output) => {
return output.toString();
});
}
// 遍历所有通过ADB连接到PC的手机
async eachDevice (...args) {
let devices = await this.adb.listDevices();
devices = devices.map(device => this.adb.getDevice(device.id));
devices = (await Promise.all(
devices.map(async (device) => {
await this.getPackageName(device);
return device.packageName && device;
}),
)).filter(Boolean);
return await Promise.all(devices.map(...args));
}
// 通过ADB将webpack输出的文件部署文件到手机上,会跳过sourceMap文件,手机目录在`options.remoteDir`中配置,未声明会自动选择`/storage/emulated/0/脚本/{project.json中的packageName、name或者package.json中的name}`
deploy (compilation) {
if (!this.options.deploy) {
return;
}
const remoteDir = this.options.project.projectDirectory;
let assets = compilation.getAssets();
if (this.options.deploy.skipSourceMap) {
assets = assets.filter(asset =>
!("development" in asset.info) && !("extractedComments" in asset.info),
);
}
const localDir = compilation.compiler.outputPath;
return this.eachDevice(async (device) => {
console.log(`> adb push ${localDir} ${remoteDir}`);
await Promise.all(
assets.map(asset => (
device.push(
path.join(localDir, asset.name),
path.posix.join(remoteDir, asset.name),
)
)),
);
if (this.options.deploy.run) {
const jsFileList = assets.filter(asset =>
"javascriptModule" in asset.info,
).map(asset => asset.name);
let jsFile = jsFileList[0];
if (jsFileList.length > 1) {
const { main } = await this.getProjectConfig();
if (main && jsFileList.includes(main)) {
jsFile = main;
}
}
if (!jsFile) {
return;
}
console.log(`> adb shell am start -a android.intent.action.MAIN -n ${device.packageName}/org.autojs.autojs.external.shortcut.ShortcutActivity -e path ${path.posix.join(remoteDir, jsFile)}`);
await device.startActivity({
debug: true,
action: "android.intent.action.MAIN",
component: device.packageName + "/org.autojs.autojs.external.shortcut.ShortcutActivity",
extras: {
path: path.posix.join(remoteDir, jsFile),
},
});
}
});
}
// 在webpack变异文件的队列中加入“project.json”、js文件中添加`"ui";指令头`、收集webpack生成的sourceMap文件,供logcat相关功能调用
async updateAsset (compilation) {
const project = await this.getProjectConfig();
if (compilation.compiler.options.watch && this.options.logcat?.sourceMap) {
this.options.logcat.sourceMap = {};
const remoteDir = project.projectDirectory;
const posixPath = (sPath) => (path.isAbsolute(sPath) ? path.relative(process.cwd(), sPath) : sPath).replaceAll(path.win32.sep, path.posix.sep);
const contextPath = posixPath(compilation.options.context);
const outputPath = posixPath(compilation.compiler.outputPath);
compilation.getAssets().forEach((asset) => {
if ("development" in asset.info) {
const sourceMap = JSON.parse(asset.source.source());
sourceMap.sources = sourceMap.sources.map(file => {
const uri = file.match(/^webpack:\/\/([^/]+\/)?\.\/(.*)$/);
if (uri) {
return path.posix.join(contextPath, uri[2]);
}
return file;
});
const file = sourceMap.file;
sourceMap.file = path.posix.join(outputPath, file);
this.options.logcat.sourceMap[path.posix.join(remoteDir, file)] = new SourceMapConsumer(sourceMap);
}
});
}
if (!this.options.build) {
return;
}
const RawSource = compilation.compiler.webpack.sources.RawSource;
compilation.emitAsset(
"project.json",
new RawSource(JSON.stringify(project, 0, compilation.options.mode === "development" ? 4 : 0)),
);
let ui = this.options.build.ui;
if (ui) {
if (!Array.isArray(ui)) {
ui = [ui];
}
ui.forEach(fileName => {
if (typeof fileName !== "string") {
fileName = project.main;
}
compilation.updateAsset(
fileName,
(source) => {
return new RawSource("\"ui\";" + source.source());
},
);
});
}
}
apply (compiler) {
compiler.hooks.thisCompilation.tap(AutojsDeployPlugin.name, (compilation) => {
compilation.hooks.processAssets.tapPromise(
{
name: AutojsDeployPlugin.name,
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
},
() => {
return this.updateAsset(compilation);
},
);
});
compiler.hooks.done.tapPromise(AutojsDeployPlugin.name, async (stats) => {
const compilation = stats.compilation;
try {
await this.deploy(compilation);
} catch (ex) {
if (ex.cause?.code === "ENOENT") {
console.error("Could not find 'adb' in PATH. Please set options.adb of " + AutojsDeployPlugin.name);
} else {
throw ex;
}
}
if (compilation.compiler.options.watch) {
await this.logcat(compilation);
}
});
}
}
module.exports = AutojsDeployPlugin;
const { Transform } = require("stream");
const styles = require("ansi-styles");
const json5 = require("json5");
class LogTransform extends Transform {
constructor () {
super();
this.colors = {
V: styles.gray,
I: styles.green,
W: styles.yellow,
E: styles.red,
};
// "https://cdn.jsdelivr.net/gh/kkevsekk1/AutoX/autojs/src/main/assets",
// "https://github.dev/kkevsekk1/AutoX/tree/dev-test/autojs/src/main/assets"
this.assets = "https://github.dev/kkevsekk1/AutoX/tree/dev-test/autojs/src/main/assets";
}
// 将文件路径转换为source map映射的文件路径
toSourcePath (options) {
const consumer = this.sourceMap && this.sourceMap[options.file];
if (consumer) {
const originalPos = consumer.originalPositionFor && consumer.originalPositionFor(options);
if (originalPos?.source) {
options.file = originalPos.source;
if (originalPos.name && (!options.name || !(/\b\w*E(rror|xception):/.test(options.name) || options.name.includes(originalPos.name)))) {
options.name = originalPos.name;
}
options.line = originalPos.line;
options.column = originalPos.column;
} else {
options.file = consumer.file || consumer;
}
}
return options;
}
// 将日志中的错误信息中的trace统一格式并转换文件路径
traceFormat (options) {
if (options.pos) {
const arrPos = options.pos.match(/\d+/g);
options.line = +arrPos[0] || 1;
options.column = +arrPos[1] || 0;
}
let {
file,
prefix,
name,
line,
column,
} = this.toSourcePath(options);
if (file.startsWith("file:///android_asset/modules/")) {
file = this.assets + file.slice(21) + "#L" + line;
} else {
file = [file, line || "1", column || "0"].join(":");
}
prefix = prefix || "";
if (name) {
return `${prefix}${name} (${file})`;
} else {
return `${prefix}${file}`;
}
}
_transform (string, encoding, callback) {
string = string.toString();
if (this.sourceMap) {
string = string.replaceAll(
/\bfile:\/\/\/android_asset(\/.*?)#(\d+)/g,
// "https://cdn.jsdelivr.net/gh/kkevsekk1/AutoX/autojs/src/main/assets/modules/__json2__.js#L493",
// "https://github.dev/kkevsekk1/AutoX/tree/dev-test/autojs/src/main/assets" + file + "#L" + line,
(s, file, line) => this.assets + file + "#L" + line,
).replaceAll(
// 替换以下两种错误日志格式中的文件路径和行号(文件路径和行号带括号):
// XxxError: error_messarg (/some/path/to/file:69:54)
// at function_name (/some/path/to/file:69:54)
/^((?:[\d:.]+\/[A-Z]:|\s*at)\s+)?(.*?)\s+\((.*?)((?:[:#]\d+)+)\)$/gm,
(s, prefix, name, file, pos) => this.traceFormat({
prefix,
name,
file,
pos,
}),
).replaceAll(
// 替换以下两种错误日志格式中的文件路径和行号(文件路径和行号不带括号,函数名如果存在、带括号):
// at /some/path/to/file:69:54 (function_name)
// at /some/path/to/file:69:54
/^(\s*at\s+)(.*?)((?:[:#]\d+)+)(?:\s+\((.*)\))?$/gm,
(s, prefix, file, pos, name) => this.traceFormat({
prefix,
file,
pos,
name,
}),
).replaceAll(
// 替换类似JSON格式的报错
// { [JavaException: message ]
// fileName: 'file:///android_asset/modules/filename.js',
// lineNumber: 8848 }`;
/\{\s+\[(.+)\]([\s\S]*?)\}/gm,
(s, message, jsonBody) => {
let errInfo;
try {
/* eslint no-new-func: "off" */
errInfo = json5.parse(`{${jsonBody}}`);
} catch (ex) {
// return s;
}
if (!errInfo || !errInfo.fileName || !/\b\w*E(rror|xception)/.test(message)) {
return s;
}
return `${message}\n${this.traceFormat({
prefix: "\tat ",
file: errInfo.fileName,
line: +errInfo.lineNumber,
column: +errInfo.columnNumber,
})}`;
},
);
}
if (this.colors) {
let currColor = null;
string = string.replaceAll(/^([\d:.]+)\/([A-Z]):\s/gm, (s, timestamp, level) => {
s = "";
const newColor = this.colors[level] || null;
if (newColor !== currColor) {
if (currColor) {
s += currColor.close;
}
if (newColor) {
s += newColor.open;
}
currColor = newColor;
}
// if (this.timestamp) {
// s += `${timestamp}/${level}: `;
// }
return s;
});
if (currColor) {
string += currColor.close;
currColor = null;
}
}
callback(null, string);
}
_flush (callback) {
callback();
}
}
// require("fs").createReadStream("lot.txt").pipe(process.output);
// const fs = require("fs");
// const logCat = new LogCat();
// const sourceMap = JSON.parse(
// fs.readFileSync("dist/miui_cleaner_app/main.js.map", "utf-8"),
// );
// let context = process.cwd();
// let output = path.resolve("dist/miui_cleaner_app");
// context = path.relative(process.cwd(), context);
// output = path.relative(process.cwd(), output).replaceAll(path.win32.sep, path.posix.sep);
// sourceMap.sources = sourceMap.sources.map(file => {
// const uri = file.match(/^webpack:\/\/([^/]+\/)?(\.\/.*)$/);
// if (uri) {
// return path.posix.join(context, uri[2]);
// }
// return file;
// });
// sourceMap.file = path.posix.join(output, "main.js");
// logCat.sourceMap = {
// "/storage/emulated/0/脚本/com.github.gucong3000.miui.cleaner/main.js": "dist/miui_cleaner_app/main.js" || new SourceMapConsumer(sourceMap),
// };
// fs.createReadStream("log.txt").pipe(logCat).pipe(process.stdout);