Skip to content

Commit f52d056

Browse files
committed
feat(cli): 新增配置获取/更新、同步的 cli 入口
1 parent 37d2b38 commit f52d056

File tree

5 files changed

+89
-43
lines changed

5 files changed

+89
-43
lines changed

packages/cli/src/index.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import EventEmitter from 'events'
12
import * as path from 'path'
23
import pc from "picocolors"
34
import { program } from 'commander'
45
import { gt } from 'semver'
56
import { add, current, doctor, list, remove, scan, upgrade, use } from './libs'
67
import notification from './notification.json'
8+
import { sync } from './libs/sync'
9+
import { getConfig, setConfig } from './libs/config'
710
const figlet = require('figlet')
811
const pkgJsonPath = path.resolve(__dirname, '..', 'package.json')
912
const pkgJson = require(pkgJsonPath)
@@ -13,7 +16,6 @@ if (gt('1.4.0', pkgJson.version)) {
1316
}
1417
console.log('');
1518

16-
1719
const artText = figlet.textSync('G C M', {
1820
font: 'Standard',
1921
horizontalLayout: 'default',
@@ -25,6 +27,9 @@ const artText = figlet.textSync('G C M', {
2527
console.log(`> gcm ${process.argv[2]}
2628
${pc.green(artText)}`);
2729

30+
// 解决事件监听过多,运行命令后弹出警告信息的问题(默认最大监听器数量为 10 个)
31+
EventEmitter.setMaxListeners(20)
32+
2833
program
2934
.version(pkgJson.version)
3035
.command('use <alias>')
@@ -119,6 +124,45 @@ program
119124
}
120125
})
121126

127+
program
128+
.version(pkgJson.version)
129+
.command('get-config <type>')
130+
.description('get configuration')
131+
.action((type: 'sync') => {
132+
try {
133+
getConfig(type)
134+
} catch (error) {
135+
console.error(error)
136+
process.exit(1)
137+
}
138+
})
139+
140+
program
141+
.version(pkgJson.version)
142+
.command('set-config <type>')
143+
.description('update configuration')
144+
.action((type: 'sync') => {
145+
try {
146+
setConfig(type)
147+
} catch (error) {
148+
console.error(error)
149+
process.exit(1)
150+
}
151+
})
152+
153+
program
154+
.version(pkgJson.version)
155+
.command('sync')
156+
.description('sync config to remote')
157+
.action(() => {
158+
try {
159+
sync()
160+
} catch (error) {
161+
console.error(error)
162+
process.exit(1)
163+
}
164+
})
165+
122166
program
123167
.version(pkgJson.version)
124168
.command('current')

packages/cli/src/libs/sync/const.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,8 @@ export const TEMP_SYNC_DIR = path.join(GCM_CONFIG_DIR, TEMP_FOLDER_NAME);
1717
/**
1818
* 配置仓库名称
1919
*/
20-
const GIT_REPO_NAME = "config";
21-
/**
22-
* 配置仓库中用于存放 GCM 配置的目录名称
23-
*/
24-
const SUB_FOLDER = "gcm";
25-
/**
26-
* GCM 配置文件名称
27-
*/
28-
const GCM_CONFIG_FILE_NAME = "config.json";
20+
export const CONFIG_REPO_NAME = "config";
2921
/**
3022
* 仓库临时存放目录路径
3123
*/
32-
export const REPO_FOLDER_PATH = path.join(TEMP_SYNC_DIR, GIT_REPO_NAME);
33-
/**
34-
* 仓库临时存放目录的 GCM 配置存放目录路径
35-
*/
36-
export const CONFIG_FOLDER_PATH = path.join(REPO_FOLDER_PATH, SUB_FOLDER);
37-
/**
38-
* 仓库临时存放目录的 GCM 配置文件存放路径
39-
*/
40-
export const GCM_CONFIG_FILE_PATH = path.join(CONFIG_FOLDER_PATH, GCM_CONFIG_FILE_NAME);
24+
export const REPO_FOLDER_PATH = path.join(TEMP_SYNC_DIR, CONFIG_REPO_NAME);
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import fs from "fs";
2-
import { getAllUserConfigs } from "@lexmin0412/gcm-api";
3-
import { GCM_CONFIG_FILE_PATH } from "./const";
2+
import { getAllUserConfigs, readConfigs } from "@lexmin0412/gcm-api";
3+
import { REPO_FOLDER_PATH } from "./const";
44
import { cloneConfigRepo, pushConfig, writeConfigIntoLocalRepo } from "./steps";
55
import { isEqual } from "./utils";
66
import { createServerAndOpenPage } from "./server";
7+
import path from "path";
78

89
// 获取本地用户配置
910
const localUserConfigs = getAllUserConfigs();
1011

1112
/**
1213
* 同步配置主函数
1314
*/
14-
const sync = async () => {
15+
export const sync = async () => {
1516
// clone 用户配置仓库
1617
await cloneConfigRepo();
1718
// 判断本地配置是否与远程配置一致
19+
const { sync } = readConfigs()
20+
const GCM_CONFIG_FILE_PATH = path.join(REPO_FOLDER_PATH, sync.dir, sync.filename);
1821
const remoteUserConfigs = JSON.parse(fs.readFileSync(GCM_CONFIG_FILE_PATH, "utf8"));
1922
if (!remoteUserConfigs?.length) {
2023
// 远程配置不存在,直接写入本地配置
2124
writeConfigIntoLocalRepo(localUserConfigs);
22-
pushConfig();
25+
await pushConfig();
2326
} else if (isEqual(localUserConfigs, remoteUserConfigs)) {
2427
console.log("本地配置与远程配置一致,无需同步");
2528
} else {
@@ -32,11 +35,9 @@ const sync = async () => {
3235
// 写入本地配置
3336
writeConfigIntoLocalRepo(mergedConfig);
3437
// 推送配置
35-
pushConfig();
36-
process.exit(0);
38+
await pushConfig();
3739
}
40+
process.exit(0);
3841
}
3942

40-
sync()
41-
4243

packages/cli/src/libs/sync/server.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const createServerAndOpenPage = async (options: {
1515
const purePath = req.url?.slice(0, req.url.indexOf("?"));
1616
if (!purePath || purePath === "/") {
1717
const html = fs.readFileSync(
18-
path.join(__dirname, "../../server/index.html")
18+
path.join(__dirname, "../../../server/index.html")
1919
);
2020
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
2121
return res.end(html);
@@ -44,9 +44,7 @@ export const createServerAndOpenPage = async (options: {
4444
headless: false,
4545
});
4646
const page = await browser.newPage();
47-
await page.goto(url, {
48-
49-
});
47+
await page.goto(url);
5048
// 给页面注入全局变量
5149
await page.addScriptTag({
5250
content: `
@@ -59,7 +57,7 @@ export const createServerAndOpenPage = async (options: {
5957
`,
6058
});
6159
// 监听页面中的点击事件
62-
await page.exposeFunction("onClickEvent", (event) => {
60+
await page.exposeFunction("onClickEvent", (event: any) => {
6361
console.log("点击事件发生:", event);
6462
if (event.target === 'submit_btn') {
6563

@@ -86,8 +84,11 @@ export const createServerAndOpenPage = async (options: {
8684
console.log('合并输入框', document.querySelector('#mergedConfig'))
8785
console.log('合并输入框的值', document.querySelector('#mergedConfig')?.innerHTML)
8886
// 调用 Node.js 中暴露的函数
87+
// @ts-ignore
8988
window.onClickEvent({
90-
target: event.target.id,
89+
// @ts-ignore
90+
target: event.target?.id,
91+
// @ts-ignore
9192
data: document.querySelector('#mergedConfig')?.value
9293
});
9394
});

packages/cli/src/libs/sync/steps.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import fs from 'fs'
22
import inquirer from "inquirer";
3-
import { CONFIG_FOLDER_PATH, GCM_CONFIG_FILE_PATH, REPO_FOLDER_PATH, TEMP_SYNC_DIR } from "./const";
3+
import { CONFIG_REPO_NAME, REPO_FOLDER_PATH, TEMP_SYNC_DIR } from "./const";
44
import { execSync, type ExecSyncOptionsWithBufferEncoding } from "child_process";
55
import { UserConfig } from '../../types';
6+
import { readConfigs } from '@lexmin0412/gcm-api';
7+
import { setConfig as setSyncConfig } from '../config'
8+
import path from 'path';
69

710
/**
811
* 执行命令行脚本的统一选项
@@ -24,15 +27,25 @@ export const cloneConfigRepo = async () => {
2427
// 创建临时工作目录
2528
fs.mkdirSync(TEMP_SYNC_DIR);
2629

27-
// 获取用户配置仓库
28-
const { configRepoUrl } = await inquirer.prompt([
29-
{
30-
type: "input",
31-
name: "configRepoUrl",
32-
message: "请输入配置仓库地址(建议使用 SSH 地址)",
30+
// 获取用户配置仓库地址
31+
const { sync } = readConfigs()
32+
if (!sync?.repoUrl) {
33+
const { isContinue } = await inquirer.prompt([
34+
{
35+
type: "confirm",
36+
name: "isContinue",
37+
message: "检测到同步配置不存在, 是否立即配置?",
38+
default: true,
39+
}
40+
])
41+
if (!isContinue) {
42+
console.log("已取消同步配置")
43+
process.exit(0)
3344
}
34-
])
35-
execSync(`git clone ${configRepoUrl}`, {
45+
await setSyncConfig('sync')
46+
}
47+
const { sync: newSyncConfig } = readConfigs()
48+
execSync(`git clone ${newSyncConfig.repoUrl} ${CONFIG_REPO_NAME}`, {
3649
cwd: TEMP_SYNC_DIR,
3750
});
3851
console.log("下载远程配置成功");
@@ -43,10 +56,12 @@ export const cloneConfigRepo = async () => {
4356
*/
4457
export const writeConfigIntoLocalRepo = async (config: UserConfig[]) => {
4558
console.log("开始写入配置到本地仓库");
46-
// 判断 configFolder 是否存在,否则新建目录
59+
const { sync } = readConfigs()
60+
const CONFIG_FOLDER_PATH = path.join(REPO_FOLDER_PATH, sync.dir);
4761
if (!fs.existsSync(CONFIG_FOLDER_PATH)) {
4862
fs.mkdirSync(CONFIG_FOLDER_PATH);
4963
}
64+
const GCM_CONFIG_FILE_PATH = path.join(CONFIG_FOLDER_PATH, sync.filename);
5065
// 判断 configPath 是否存在,否则新建文件
5166
if (!fs.existsSync(GCM_CONFIG_FILE_PATH)) {
5267
fs.writeFileSync(GCM_CONFIG_FILE_PATH, "{}");
@@ -73,6 +88,7 @@ export const pushConfig = async () => {
7388
message: "请输入你的 git 邮箱",
7489
},
7590
]);
91+
console.log(`用户: ${name}, ${email}`)
7692
execSync(`git config user.name ${name}`, EXEC_OPTIONS);
7793
execSync(`git config user.email ${email}`, EXEC_OPTIONS);
7894
execSync("git add .", EXEC_OPTIONS);

0 commit comments

Comments
 (0)