1+ import _ from 'lodash' ;
12import $ from '@/core/app' ;
23import { ENV } from '@/vendor/open-api' ;
34import { failed , success } from '@/restful/response' ;
@@ -104,7 +105,7 @@ async function refresh(_, res) {
104105 success ( res ) ;
105106}
106107
107- async function gistBackupAction ( action ) {
108+ async function gistBackupAction ( action , keep ) {
108109 // read token
109110 const { gistToken, syncPlatform } = $ . read ( SETTINGS_KEY ) ;
110111 if ( ! gistToken ) throw new Error ( 'GitHub Token is required for backup!' ) ;
@@ -114,6 +115,9 @@ async function gistBackupAction(action) {
114115 key : GIST_BACKUP_KEY ,
115116 syncPlatform,
116117 } ) ;
118+ let currentContent = $ . read ( '#sub-store' ) ;
119+ currentContent = currentContent ? JSON . parse ( currentContent ) : { } ;
120+ if ( $ . env . isNode ) currentContent = JSON . parse ( JSON . stringify ( $ . cache ) ) ;
117121 let content ;
118122 const settings = $ . read ( SETTINGS_KEY ) ;
119123 const updated = settings . syncTime ;
@@ -162,7 +166,8 @@ async function gistBackupAction(action) {
162166 $ . info ( `还原备份中...` ) ;
163167 content = await gist . download ( GIST_BACKUP_FILE_NAME ) ;
164168 try {
165- if ( Object . keys ( JSON . parse ( content ) . settings ) . length === 0 ) {
169+ content = JSON . parse ( content ) ;
170+ if ( Object . keys ( content . settings ) . length === 0 ) {
166171 throw new Error ( '备份文件应该至少包含 settings 字段' ) ;
167172 }
168173 } catch ( err ) {
@@ -173,10 +178,15 @@ async function gistBackupAction(action) {
173178 ) ;
174179 throw new Error ( 'Gist 备份文件校验失败, 无法还原' ) ;
175180 }
181+ if ( keep ) {
182+ $ . info ( `保留原有设置 ${ keep } ` ) ;
183+ keep . split ( ',' ) . forEach ( ( path ) => {
184+ _ . set ( content , path , _ . get ( currentContent , path ) ) ;
185+ } ) ;
186+ }
176187 // restore settings
177- $ . write ( content , '#sub-store' ) ;
188+ $ . write ( JSON . stringify ( content , null , ` ` ) , '#sub-store' ) ;
178189 if ( $ . env . isNode ) {
179- content = JSON . parse ( content ) ;
180190 $ . cache = content ;
181191 $ . persistCache ( ) ;
182192 }
@@ -188,7 +198,7 @@ async function gistBackupAction(action) {
188198 }
189199}
190200async function gistBackup ( req , res ) {
191- const { action } = req . query ;
201+ const { action, keep } = req . query ;
192202 // read token
193203 const { gistToken } = $ . read ( SETTINGS_KEY ) ;
194204 if ( ! gistToken ) {
@@ -201,7 +211,7 @@ async function gistBackup(req, res) {
201211 ) ;
202212 } else {
203213 try {
204- await gistBackupAction ( action ) ;
214+ await gistBackupAction ( action , keep ) ;
205215 success ( res ) ;
206216 } catch ( err ) {
207217 $ . error (
0 commit comments