@cloudbase/toolbox 0.7.16-beta.1 → 0.7.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/web/web.js +50 -40
- package/package.json +1 -1
package/lib/web/web.js
CHANGED
|
@@ -16,10 +16,10 @@ exports.getDataFromWeb = void 0;
|
|
|
16
16
|
const open_1 = __importDefault(require("open"));
|
|
17
17
|
const query_string_1 = __importDefault(require("query-string"));
|
|
18
18
|
const http_1 = __importDefault(require("http"));
|
|
19
|
+
const child_process_1 = require("child_process");
|
|
19
20
|
const system_1 = require("../system");
|
|
20
21
|
const error_1 = require("../error");
|
|
21
22
|
const html_1 = require("../html");
|
|
22
|
-
const child_process_1 = require("child_process");
|
|
23
23
|
// 创建本地 Web 服务器,接受 Web 控制台传入的信息
|
|
24
24
|
function createLocalServer() {
|
|
25
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -60,31 +60,58 @@ function isTruthyFlag(value) {
|
|
|
60
60
|
return ['1', 'true', 'yes', 'on'].includes(String(value).trim().toLowerCase());
|
|
61
61
|
}
|
|
62
62
|
function isVSCodeEnvironment() {
|
|
63
|
-
return
|
|
63
|
+
return Boolean(process.env.VSCODE_IPC_HOOK_CLI
|
|
64
|
+
|| process.env.VSCODE_PID
|
|
65
|
+
|| process.env.TERM_PROGRAM === 'vscode');
|
|
64
66
|
}
|
|
65
|
-
function
|
|
67
|
+
function parseBrowserCommand(browser) {
|
|
68
|
+
const parts = browser.match(/(?:[^\s"]+|"[^"]*")+/g) || [];
|
|
69
|
+
return parts.map((item) => item.replace(/^"(.*)"$/, '$1'));
|
|
70
|
+
}
|
|
71
|
+
function openUrlByBrowserEnv(url) {
|
|
72
|
+
var _a;
|
|
66
73
|
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
const browser = (_a = process.env.BROWSER) === null || _a === void 0 ? void 0 : _a.trim();
|
|
75
|
+
if (!browser) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
const [command, ...args] = parseBrowserCommand(browser);
|
|
79
|
+
if (!command) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const finalArgs = args.map((arg) => (arg === '%s' ? url : arg));
|
|
83
|
+
if (!args.includes('%s')) {
|
|
84
|
+
finalArgs.push(url);
|
|
85
|
+
}
|
|
86
|
+
return new Promise((resolve) => {
|
|
87
|
+
(0, child_process_1.execFile)(command, finalArgs, (error) => {
|
|
88
|
+
resolve(!error);
|
|
74
89
|
});
|
|
75
90
|
});
|
|
76
91
|
});
|
|
77
92
|
}
|
|
78
|
-
function
|
|
93
|
+
function shouldUseBrowserEnvFallback() {
|
|
94
|
+
return process.platform === 'linux' && isVSCodeEnvironment();
|
|
95
|
+
}
|
|
96
|
+
function openUrl(url) {
|
|
79
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
-
|
|
98
|
+
try {
|
|
99
|
+
const child = yield (0, open_1.default)(url, { url: true });
|
|
100
|
+
if (child === null || child === void 0 ? void 0 : child.once) {
|
|
101
|
+
child.once('error', (error) => __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
if (shouldUseBrowserEnvFallback()) {
|
|
103
|
+
yield openUrlByBrowserEnv(url);
|
|
104
|
+
}
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
81
107
|
return true;
|
|
82
108
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
109
|
+
catch (e) {
|
|
110
|
+
if (shouldUseBrowserEnvFallback()) {
|
|
111
|
+
return openUrlByBrowserEnv(url);
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
88
115
|
});
|
|
89
116
|
}
|
|
90
117
|
// 从 Web 页面中获取数据
|
|
@@ -98,30 +125,13 @@ function getDataFromWeb(getUrl, type, options = {}) {
|
|
|
98
125
|
throw new error_1.CloudBaseError('callbackTimeout must be a positive number');
|
|
99
126
|
}
|
|
100
127
|
const url = getUrl(port);
|
|
101
|
-
console.log('\n\n
|
|
102
|
-
console.log(`\n${url}`);
|
|
128
|
+
console.log('\n\n若链接未自动打开,请手动复制至浏览器,或尝试其他登录方式:');
|
|
129
|
+
console.log(`\n${url}\n`);
|
|
103
130
|
if (!noBrowser) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// https://github.com/sindresorhus/open/blob/master/index.js#L48
|
|
109
|
-
try {
|
|
110
|
-
yield (0, open_1.default)(url, { url: true });
|
|
111
|
-
}
|
|
112
|
-
catch (e) {
|
|
113
|
-
const code = (e === null || e === void 0 ? void 0 : e.code) || 'UNKNOWN';
|
|
114
|
-
console.warn(`自动打开浏览器失败(${code})。`);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (isVSCodeEnvironment()) {
|
|
118
|
-
try {
|
|
119
|
-
yield openUrlByVSCode(url);
|
|
120
|
-
}
|
|
121
|
-
catch (vscodeError) {
|
|
122
|
-
// ignore error
|
|
123
|
-
}
|
|
124
|
-
}
|
|
131
|
+
// 对 url 转码, 避免 wsl 无法正常打开地址
|
|
132
|
+
// https://www.npmjs.com/package/open#url
|
|
133
|
+
// https://github.com/sindresorhus/open/blob/master/index.js#L48
|
|
134
|
+
yield openUrl(url);
|
|
125
135
|
}
|
|
126
136
|
return new Promise((resolve, reject) => {
|
|
127
137
|
let finished = false;
|