@cloudbase/toolbox 0.7.16-beta.0 → 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 +53 -26
  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,21 +60,62 @@ 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');
66
+ }
67
+ function parseBrowserCommand(browser) {
68
+ const parts = browser.match(/(?:[^\s"]+|"[^"]*")+/g) || [];
69
+ return parts.map((item) => item.replace(/^"(.*)"$/, '$1'));
64
70
  }
65
- function openUrlByVSCode(url) {
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
  }
93
+ function shouldUseBrowserEnvFallback() {
94
+ return process.platform === 'linux' && isVSCodeEnvironment();
95
+ }
96
+ function openUrl(url) {
97
+ return __awaiter(this, void 0, void 0, function* () {
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
+ }
109
+ return true;
110
+ }
111
+ catch (e) {
112
+ if (shouldUseBrowserEnvFallback()) {
113
+ return openUrlByBrowserEnv(url);
114
+ }
115
+ return false;
116
+ }
117
+ });
118
+ }
78
119
  // 从 Web 页面中获取数据
79
120
  function getDataFromWeb(getUrl, type, options = {}) {
80
121
  var _a, _b;
@@ -86,27 +127,13 @@ function getDataFromWeb(getUrl, type, options = {}) {
86
127
  throw new error_1.CloudBaseError('callbackTimeout must be a positive number');
87
128
  }
88
129
  const url = getUrl(port);
89
- console.log('若链接未自动打开,请手动复制此链接至浏览器,或尝试使用其他登录方式:');
130
+ console.log('\n\n若链接未自动打开,请手动复制此链接至浏览器,或尝试使用其他登录方式:');
90
131
  console.log(`\n${url}`);
91
132
  if (!noBrowser) {
92
133
  // 对 url 转码, 避免 wsl 无法正常打开地址
93
134
  // https://www.npmjs.com/package/open#url
94
135
  // https://github.com/sindresorhus/open/blob/master/index.js#L48
95
- try {
96
- yield (0, open_1.default)(url, { url: true });
97
- }
98
- catch (e) {
99
- const code = (e === null || e === void 0 ? void 0 : e.code) || 'UNKNOWN';
100
- console.warn(`自动打开浏览器失败(${code})。`);
101
- if (isVSCodeEnvironment()) {
102
- try {
103
- yield openUrlByVSCode(url);
104
- }
105
- catch (vscodeError) {
106
- // ignore error
107
- }
108
- }
109
- }
136
+ yield openUrl(url);
110
137
  }
111
138
  return new Promise((resolve, reject) => {
112
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.0",
3
+ "version": "0.7.16-beta.2",
4
4
  "description": "The toolbox for cloudbase",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {