@dreamor/atlas-cli 0.7.3 → 0.7.5

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.
@@ -1,6 +1,9 @@
1
+ import { execSync } from 'child_process';
2
+ import { createRequire } from 'module';
1
3
  import { writeCookies, readCookies, fetchCookiesFromDaemon } from './session.js';
2
4
  import { isJsonMode, jsonOk, log } from '../util/output.js';
3
5
  import { AtlasError } from '../util/errors.js';
6
+ const _require = createRequire(import.meta.url);
4
7
  const BANMA_HOST = 'banma-yuntu.alibaba-inc.com';
5
8
  // 白名单:只保存 getBanmaIdentity 真正需要的 cookies,避免整域 cookies 落盘
6
9
  const WANT_COOKIES = new Set(['access_token', 'buc_username', 'buc_userinfo']);
@@ -16,23 +19,67 @@ async function loadFirefox() {
16
19
  return pw.firefox;
17
20
  }
18
21
  catch {
19
- throw new Error("未安装 playwright。运行时需要:设 ATLAS_AUTO_BOOTSTRAP=1 自动安装,或执行 `npm install playwright && npx playwright install`");
22
+ throw new Error("未安装 playwright。请执行: npm i -g playwright && npx playwright install");
23
+ }
24
+ }
25
+ /**
26
+ * 检测 launch 错误是否为"浏览器二进制未安装",自动用内置 playwright 下载。
27
+ */
28
+ async function ensureBrowserInstalled() {
29
+ const pwPath = _require.resolve('playwright/package.json');
30
+ const cliJs = pwPath.replace(/package\.json$/, 'cli.js');
31
+ log('首次运行,正在自动安装 Firefox 浏览器...');
32
+ try {
33
+ execSync(`node "${cliJs}" install firefox`, {
34
+ stdio: 'inherit',
35
+ timeout: 300_000,
36
+ });
37
+ }
38
+ catch {
39
+ throw new Error('Firefox 浏览器自动安装失败。请手动执行: npx playwright install firefox');
20
40
  }
21
41
  }
22
42
  export async function login(_port) {
23
43
  log('正在打开浏览器进行 Banma SSO 登录...');
24
44
  const firefox = await loadFirefox();
25
- const browser = await firefox.launch({
26
- headless: false,
27
- });
45
+ let browser;
46
+ try {
47
+ browser = await firefox.launch({
48
+ headless: false,
49
+ });
50
+ }
51
+ catch (e) {
52
+ if (e instanceof Error && e.message.includes('Executable doesn\'t exist')) {
53
+ await ensureBrowserInstalled();
54
+ browser = await firefox.launch({ headless: false });
55
+ }
56
+ else {
57
+ throw e;
58
+ }
59
+ }
28
60
  const page = await browser.newPage();
29
61
  log('请在浏览器中完成 SSO 登录...');
30
62
  log('登录成功后页面会自动跳转,CLI 会自动捕获 cookies');
31
63
  await page.goto(`https://${BANMA_HOST}/`, { waitUntil: 'domcontentloaded', timeout: 60000 });
32
- // 等待 access_token cookie 出现(最多 120s),比脆弱的 url 判断 + 固定 timeout 更可靠
33
- await page.waitForFunction(() => document.cookie.includes('access_token'), { timeout: 120_000 });
64
+ // 等待 access_token cookie 出现(最多 120s)。
65
+ // 注意 access_token 是 HttpOnly cookie,不能用 document.cookie 检测,
66
+ // 必须走 page.context().cookies() 才能读到。
67
+ const MAX_WAIT = 120_000;
68
+ const POLL_INTERVAL = 500;
69
+ let foundToken = false;
70
+ for (let elapsed = 0; elapsed < MAX_WAIT; elapsed += POLL_INTERVAL) {
71
+ const allCookies = await page.context().cookies();
72
+ if (allCookies.some((c) => c.name === 'access_token' && c.value)) {
73
+ foundToken = true;
74
+ break;
75
+ }
76
+ await page.waitForTimeout(POLL_INTERVAL);
77
+ }
78
+ if (!foundToken) {
79
+ log('SSO 登录检测超时。如果您已完成登录,请按 Ctrl+C 重试或确认账号权限。');
80
+ }
34
81
  // 额外等待确保残留 cookie 写入完毕
35
- await page.waitForTimeout(1000);
82
+ await page.waitForTimeout(500);
36
83
  // Extract cookies — 精确白名单,避免整域 cookies 落盘
37
84
  const cookies = await page.context().cookies();
38
85
  const atlasCookies = cookies
@@ -74,8 +74,6 @@ export async function updateCmd(opts) {
74
74
  log(`当前版本 ${ATLAS_VERSION}`);
75
75
  log('通过 npm 安装的 atlas 由 npm registry 管理升级,请运行:');
76
76
  log(` ${npmCmd}`);
77
- log('');
78
- log('指定的 NPM_TOKEN 拥有 Publish 权限时,npm 会从 registry 自动拉取最新版。');
79
77
  if (opts.json || isJsonMode()) {
80
78
  jsonOk({
81
79
  mode: 'npm',
@@ -1 +1 @@
1
- export const ATLAS_VERSION = '0.7.3';
1
+ export const ATLAS_VERSION = '0.7.5';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamor/atlas-cli",
3
- "version": "0.7.3",
3
+ "version": "0.7.5",
4
4
  "description": "Atlas CLI - 斑马云图人力基线管理工具",
5
5
  "type": "module",
6
6
  "bin": {