@@ -19,7 +19,7 @@ export const hasConflict = (content: string): boolean => {
1919
2020export type CollectFilesOptions = Pick <
2121 NormalizedConfig ,
22- "include" | "exclude" | "matcher" | "includeNonConflicted" | "debug"
22+ "include" | "exclude" | "matcher" | "includeNonConflicted" | "debug" | "backupDir"
2323> & {
2424 /** Root directory to start traversal (defaults to `process.cwd()`). */
2525 root ?: string ;
@@ -34,9 +34,15 @@ export type CollectFilesOptions = Pick<
3434 * @param options - Collection options, including `fileFilter` and traversal root.
3535 * @returns A promise that resolves with an array of `{ filePath, content }`.
3636 */
37- export const listMatchingFiles = async ( options : CollectFilesOptions ) : Promise < FileEntry [ ] > => {
38- const { root = process . cwd ( ) , include, exclude, matcher, includeNonConflicted, debug } = options ;
39-
37+ export const listMatchingFiles = async ( {
38+ root = process . cwd ( ) ,
39+ include,
40+ exclude,
41+ matcher,
42+ includeNonConflicted,
43+ debug,
44+ backupDir,
45+ } : CollectFilesOptions ) : Promise < FileEntry [ ] > => {
4046 for ( const p of [ ...include , ...exclude ] ) {
4147 if ( p . startsWith ( "!" ) ) throw new Error ( `Negation not allowed in include/exclude: ${ p } ` ) ;
4248 if ( p . includes ( "\\" ) ) console . warn ( `Use '/' as path separator: ${ p } ` ) ;
@@ -47,7 +53,11 @@ export const listMatchingFiles = async (options: CollectFilesOptions): Promise<F
4753 return matcher . isMatch ( posixPath , include ) && ! matcher . isMatch ( posixPath , exclude ) ;
4854 } ;
4955
50- const skipDirMatcher = createSkipDirectoryMatcher ( include , exclude , matcher ) ;
56+ const skipDirMatcher = createSkipDirectoryMatcher (
57+ include ,
58+ backupDir ? [ ...exclude , backupDir ] : exclude ,
59+ matcher ,
60+ ) ;
5161
5262 const fileEntries : FileEntry [ ] = [ ] ;
5363
@@ -147,3 +157,24 @@ export const backupFile = async (filePath: string, backupDir = ".merge-backups")
147157
148158 return backupPath ;
149159} ;
160+
161+ export const restoreBackups = async ( backupDir = ".merge-backups" ) => {
162+ const walk = async ( dir : string , relativeDir = "" ) => {
163+ const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
164+
165+ for ( const entry of entries ) {
166+ const srcPath = path . join ( dir , entry . name ) ;
167+ const relativePath = path . join ( relativeDir , entry . name ) ;
168+ const destPath = path . join ( process . cwd ( ) , relativePath ) ;
169+
170+ if ( entry . isDirectory ( ) ) {
171+ await walk ( srcPath , relativePath ) ;
172+ } else {
173+ await fs . mkdir ( path . dirname ( destPath ) , { recursive : true } ) ;
174+ await fs . copyFile ( srcPath , destPath ) ;
175+ }
176+ }
177+ } ;
178+
179+ await walk ( backupDir ) ;
180+ } ;
0 commit comments