| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- /**
- * 写入 DMS token(本机开发用),配套 `vite.config.ts` 的「每请求现读 `.dms-token`」。
- *
- * 为什么要有它:DMS token 是外部 OAuth 签发的 JWT,**约 24 小时就过期**;
- * 以前只能手改 `.env.development.local` 再**重启 dev server**,麻烦且容易忘。
- * 现在 token 放在单行文件 `.dms-token`(gitignore),代理每个请求现读 —— 换 token 不用重启。
- *
- * 用法:
- * npm run dms:token # 从**剪贴板**读(浏览器里复制完直接跑,最省事)
- * npm run dms:token -- <token> # 显式传(注意 -- 不能省)
- * npm run dms:token -- --check # 只看当前 token 的状态,不写
- *
- * ⚠️ token 落在**被 gitignore 的本机文件**里,与 `.env.*.local` 同一性质:不随仓库分发。
- * DMS 的文档要求「token 不写进任何文件」,本仓库的落地口径是
- * **不写进任何会入库的文件**(见 harness/docs/reference/DMS_API.md 的鉴权节)。
- */
- import { readFileSync, writeFileSync, existsSync } from 'node:fs';
- import { execFileSync } from 'node:child_process';
- import { DMS_TOKEN_FILE, readDmsToken, parseJwtPayload, describeDmsToken } from './dms-token.mjs';
- const args = process.argv.slice(2);
- const explicit = args.find((a) => !a.startsWith('--'));
- const checkOnly = args.includes('--check');
- if (checkOnly) {
- const token = readDmsToken();
- if (!token) {
- console.error(`当前没有可用的 token:文件 ${DMS_TOKEN_FILE} 不存在或为空。`);
- process.exit(1);
- }
- console.log(`${DMS_TOKEN_FILE}\n ${describeDmsToken(token)}`);
- process.exit(0);
- }
- /** 从剪贴板取(Windows / macOS / Linux 各一条命令;取不到返回空串) */
- const readClipboard = () => {
- const cmds =
- process.platform === 'win32'
- ? [['powershell', ['-NoProfile', '-Command', 'Get-Clipboard']]]
- : process.platform === 'darwin'
- ? [['pbpaste', []]]
- : [
- ['xclip', ['-selection', 'clipboard', '-o']],
- ['xsel', ['--clipboard', '--output']],
- ];
- for (const [cmd, cmdArgs] of cmds) {
- try {
- return execFileSync(cmd, cmdArgs, { encoding: 'utf8' }).trim();
- } catch {
- /* 换下一条命令 */
- }
- }
- return '';
- };
- const token = String(explicit || readClipboard() || '').trim();
- if (!token) {
- console.error('没拿到 token:既没有参数,剪贴板也是空的。');
- console.error('用法:npm run dms:token -- <token> (或先把 token 复制到剪贴板,再跑 npm run dms:token)');
- process.exit(1);
- }
- const summary = describeDmsToken(token);
- if (!parseJwtPayload(token)) {
- console.error(summary);
- console.error('已中止,没有写入 —— 写进去也只会让 DMS 静默失败。');
- process.exit(1);
- }
- writeFileSync(DMS_TOKEN_FILE, `${token}\n`, 'utf8');
- console.log(`已写入 ${DMS_TOKEN_FILE}`);
- console.log(` ${summary}`);
- console.log('dev server **不用重启**(代理每个请求现读这个文件)。');
|