@cloudbase/toolbox 0.7.16-beta.1 → 0.7.16-beta.2

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.
Files changed (2) hide show
  1. package/lib/web/web.js +50 -38
  2. 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,60 @@ function isTruthyFlag(value) {
60
60
  return ['1', 'true', 'yes', 'on'].includes(String(value).trim().toLowerCase());
61
61
  }
62
62
  function isVSCodeEnvironment() {
63
- return !!(process.env.VSCODE_PID || process.env.VSCODE_CWD || process.env.TERM_PROGRAM === 'vscode');
63
+ return Boolean(process.env.VSCODE_IPC_HOOK_CLI
64
+ || process.env.VSCODE_PID
65
+ || process.env.TERM_PROGRAM === 'vscode');
64
66
  }
65
- function openUrlByVSCode(url) {
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
- return new Promise((resolve, reject) => {
68
- (0, child_process_1.execFile)('code', ['--open-url', url], (error) => {
69
- if (error) {
70
- reject(error);
71
- return;
72
- }
73
- resolve();
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 hasSystemBrowserOpener() {
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
- if (process.platform !== 'linux') {
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
+ const code = (error === null || error === void 0 ? void 0 : error.code) || 'UNKNOWN';
103
+ console.warn(`自动打开浏览器失败(${code})。`);
104
+ if (shouldUseBrowserEnvFallback()) {
105
+ yield openUrlByBrowserEnv(url);
106
+ }
107
+ }));
108
+ }
81
109
  return true;
82
110
  }
83
- return new Promise((resolve) => {
84
- (0, child_process_1.execFile)('which', ['xdg-open'], (error) => {
85
- resolve(!error);
86
- });
87
- });
111
+ catch (e) {
112
+ if (shouldUseBrowserEnvFallback()) {
113
+ return openUrlByBrowserEnv(url);
114
+ }
115
+ return false;
116
+ }
88
117
  });
89
118
  }
90
119
  // 从 Web 页面中获取数据
@@ -101,27 +130,10 @@ function getDataFromWeb(getUrl, type, options = {}) {
101
130
  console.log('\n\n若链接未自动打开,请手动复制此链接至浏览器,或尝试使用其他登录方式:');
102
131
  console.log(`\n${url}`);
103
132
  if (!noBrowser) {
104
- const hasOpener = yield hasSystemBrowserOpener();
105
- if (hasOpener) {
106
- // 对 url 转码, 避免 wsl 无法正常打开地址
107
- // https://www.npmjs.com/package/open#url
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
- }
133
+ // url 转码, 避免 wsl 无法正常打开地址
134
+ // https://www.npmjs.com/package/open#url
135
+ // https://github.com/sindresorhus/open/blob/master/index.js#L48
136
+ yield openUrl(url);
125
137
  }
126
138
  return new Promise((resolve, reject) => {
127
139
  let finished = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/toolbox",
3
- "version": "0.7.16-beta.1",
3
+ "version": "0.7.16-beta.2",
4
4
  "description": "The toolbox for cloudbase",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {