11import fs from 'fs'
22import 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" ;
44import { execSync , type ExecSyncOptionsWithBufferEncoding } from "child_process" ;
55import { 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 */
4457export 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