Skip to content

Commit fd37919

Browse files
committed
add cli options
1 parent 6dd621e commit fd37919

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

package.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
{
22
"name": "logcat-in-devtools",
3-
"version": "0.1.2",
4-
"description": "view android adb logcat logs in chrome devtools console",
3+
"version": "0.1.3",
4+
"description": "View android adb logcat logs in chrome devtools console",
5+
"homepage": "https://github.com/nieheyong/logcat-in-devtools.git",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/nieheyong/logcat-in-devtools.git"
9+
},
510
"bin": "src/index.js",
6-
"keywords": [],
11+
"keywords": [
12+
"logcat",
13+
"logcat viewer",
14+
"adb",
15+
"devtools"
16+
],
717
"author": "niehey@163.com",
818
"license": "MIT",
919
"scripts": {
1020
"start": "npx ."
1121
},
1222
"dependencies": {
13-
"chalk": "^4.1.2"
23+
"chalk": "^4.1.2",
24+
"commander": "^11.1.0"
1425
},
1526
"devDependencies": {
1627
"@types/node": "^22.7.4"

src/adb.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { spawn, execSync } = require("child_process");
22
const { appLog, appLogError, exitProcess } = require('./stdio');
33
const chalk = require("chalk");
44

5-
function checkAdbDevice() {
5+
function checkAdbDevice(serial) {
66
const outputText = execSync("adb devices", { encoding: "utf8" });
77
const outputLines = outputText.trim().split("\n");
88
appLog(`\n$ adb devices\n${outputText.trim()}\n `);
@@ -19,7 +19,15 @@ function checkAdbDevice() {
1919
exitProcess(1);
2020
}
2121

22-
if (process.env.ANDROID_SERIAL) {
22+
if (serial) {
23+
const exist = validDevices.find((device) => serial === device.split('\t')[0]);
24+
if (!exist) {
25+
appLog(chalk.red
26+
(`The device with serial ${serial} not connected.\n`)
27+
);
28+
exitProcess(1);
29+
}
30+
} else if (process.env.ANDROID_SERIAL) {
2331
appLog(`Environment Variable ${chalk.yellow('ANDROID_SERIAL')}: ${process.env.ANDROID_SERIAL}`);
2432
const exist = validDevices.find((device) => process.env.ANDROID_SERIAL === device.split('\t')[0]);
2533
if (!exist) {
@@ -32,19 +40,22 @@ Please check the device serial or unset the ${chalk.yellow("ANDROID_SERIAL")} en
3240
} else if (devices.length > 1) {
3341
appLog(
3442
`Multiple adb devices detected, will use the first device.\n` +
35-
`You can also set the ${chalk.yellow("ANDROID_SERIAL")} env to specify device`
43+
`You can also use --serial <SERIAL> to specify device`
3644
);
3745
const [firstDevice] = validDevices[0].split("\t");
3846
appLog(chalk.green(`Using adb device: ${firstDevice}`));
3947
process.env.ANDROID_SERIAL = firstDevice;
4048
}
4149
}
4250

43-
function listenAdbLogCat(onLogCallback) {
51+
function listenAdbLogCat(params) {
52+
const { onLog, serial } = params;
53+
4454
// clear logcat buffer
45-
execSync("adb logcat -c");
55+
execSync(`adb${serial ? ` -s ${serial}` : ''} logcat -c`);
4656

47-
const adbProcess = spawn("adb", ["logcat"]);
57+
const extraArgs = serial ? ['-s', serial] : []
58+
const adbProcess = spawn("adb", [...extraArgs, 'logcat']);
4859

4960
let leftover = "";
5061
adbProcess.stdout.on("data", (data) => {
@@ -53,7 +64,7 @@ function listenAdbLogCat(onLogCallback) {
5364
const lines = fullChunk.split("\n");
5465
leftover = lines.pop();
5566

56-
lines.forEach(onLogCallback);
67+
lines.forEach(onLog);
5768
});
5869

5970
adbProcess.stderr.on("data", (data) => {

src/index.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node --inspect
22
const inspector = require("inspector");
33
const chalk = require("chalk");
4+
const { program } = require('commander');
5+
46
const packageJson = require("../package.json");
57
const { checkAdbDevice, listenAdbLogCat, styleLogcatLine } = require('./adb');
68
const { muteStdio, vmLog, appLog, appLogError, listenForKeypress } = require("./stdio");
@@ -41,14 +43,14 @@ function showInspectTips() {
4143
]);
4244
}
4345

44-
function run() {
46+
function run(cliOptions) {
4547
muteStdio()
4648
appLog(`\n${chalk.yellow(packageJson.name)}@${packageJson.version}\n `);
47-
checkAdbDevice();
49+
checkAdbDevice(cliOptions.serial);
4850

49-
if (process.env.LOG_PATTERN) {
50-
logPattern = new RegExp(process.env.LOG_PATTERN);
51-
appLog(`Detect LOG_PATTERN: ${chalk.yellow(process.env.LOG_PATTERN)}\nJavascript RegExp:`, logPattern);
51+
if (cliOptions.match) {
52+
logPattern = new RegExp(cliOptions.match);
53+
appLog(`Detect match: ${chalk.yellow(cliOptions.match)}\nJavascript RegExp:`, logPattern);
5254
}
5355

5456
if (!process.features.inspector) {
@@ -58,6 +60,22 @@ function run() {
5860

5961
showInspectTips();
6062

61-
listenAdbLogCat(processAdbLogLine);
63+
listenAdbLogCat({
64+
serial: cliOptions.serial,
65+
onLog: processAdbLogLine
66+
});
6267
}
63-
run();
68+
69+
70+
console.log('')
71+
program
72+
.name(packageJson.name)
73+
.description(packageJson.description)
74+
.version(packageJson.version);
75+
76+
program
77+
.option('-m, --match <RegExp>', 'only print messages that match RegExp')
78+
.option('-s, --serial <SERIAL>', 'use device with given serial (overrides $ANDROID_SERIAL)')
79+
.action((options) => {
80+
run(options);
81+
}).parse();

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ color-name@~1.1.4:
3636
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
3737
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
3838

39+
commander@^11.1.0:
40+
version "11.1.0"
41+
resolved "https://registry.yarnpkg.com/commander/-/commander-11.1.0.tgz#62fdce76006a68e5c1ab3314dc92e800eb83d906"
42+
integrity sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==
43+
3944
has-flag@^4.0.0:
4045
version "4.0.0"
4146
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"

0 commit comments

Comments
 (0)