@@ -5,16 +5,16 @@ import fs from "fs";
5
5
import path from "path" ;
6
6
import { promisify } from "util" ;
7
7
import { version } from "../package.json" ;
8
+ import Operation from "./fileOperation" ;
8
9
9
10
const readdir = promisify ( fs . readdir ) ;
10
- const mkdir = promisify ( fs . mkdir ) ;
11
- const rename = promisify ( fs . rename ) ;
12
11
const stat = promisify ( fs . stat ) ;
13
12
14
13
type Options = {
15
14
ext : boolean ;
16
15
name: boolean ;
17
16
ignoreDotfiles: boolean ;
17
+ dryrun: boolean ;
18
18
} ;
19
19
20
20
// Define mappings for file types to folder names
@@ -28,17 +28,28 @@ const FILE_TYPE_MAP: Record<string, string> = {
28
28
} ;
29
29
30
30
const program = new Command ( ) ;
31
-
31
+ let fileOperation : Operation ;
32
32
program
33
33
. version ( version )
34
34
. argument ( "[directory]" , "Directory to tidy up" , "." ) // Default to current directory
35
35
. description ( "Organize files in a directory based on their extensions" )
36
36
. option ( "--ext" , "Use the file extensions as folder names" )
37
37
. option ( "--name" , "Group files by starting name" )
38
+ . option ( "--dryrun" , "Show what would be done without making changes" , false )
38
39
. option ( "--ignore-dotfiles" , "Ignore dotfiles" , true )
39
40
. action ( async ( inputDir : string , options : Options ) => {
40
41
const dirPath = path . resolve ( inputDir ) ;
42
+ console . log ( `Organizing files in directory: ${ dirPath } ` ) ;
41
43
try {
44
+ if ( options . dryrun ) {
45
+ console . log ( "Running in dry run mode. No changes will be made." ) ;
46
+ fileOperation = new Operation ( true ) ;
47
+ }
48
+ else {
49
+ console . log ( "Running in normal mode. Changes will be made." ) ;
50
+ fileOperation = new Operation ( false ) ;
51
+ }
52
+
42
53
if ( options . ext && options . name ) {
43
54
console . error ( "Only one of --ext or --name can be used at a time" ) ;
44
55
process . exit ( 1 ) ;
@@ -141,7 +152,6 @@ async function getFileNameGroups(
141
152
*/
142
153
async function organizeFiles ( dirPath : string , options : Options ) : Promise < void > {
143
154
let fileTypes : Record < string , string [ ] > ;
144
-
145
155
if ( options . name ) {
146
156
fileTypes = await getFileNameGroups ( dirPath , options ) ;
147
157
} else {
@@ -161,10 +171,9 @@ async function organizeFiles(dirPath: string, options: Options): Promise<void> {
161
171
let folderCreated = false ;
162
172
163
173
if ( ! fs . existsSync ( folderPath ) ) {
164
- await mkdir ( folderPath ) ;
174
+ await fileOperation . mkdir ( folderPath ) ;
165
175
folderCreated = true ;
166
176
}
167
-
168
177
let filesAdded = 0 ;
169
178
for ( const filePath of filePaths ) {
170
179
const fileName = path . basename ( filePath ) ;
@@ -180,7 +189,7 @@ async function organizeFiles(dirPath: string, options: Options): Promise<void> {
180
189
counter ++ ;
181
190
}
182
191
}
183
- await rename ( filePath , newFilePath ) ;
192
+ await fileOperation . rename ( filePath , newFilePath ) ;
184
193
filesAdded ++ ;
185
194
}
186
195
summary . push ( { folder : folderName , created : folderCreated , filesAdded } ) ;
@@ -190,9 +199,23 @@ async function organizeFiles(dirPath: string, options: Options): Promise<void> {
190
199
const lastDir = lastPath [ lastPath . length - 1 ] ;
191
200
192
201
console . log ( `Organization Summary for '${ lastDir } ':` ) ;
193
- for ( const { folder, created, filesAdded } of summary ) {
194
- console . log ( `- Folder: ${ folder } ` ) ;
195
- console . log ( ` - ${ created ? "Created" : "Already existed" } ` ) ;
196
- console . log ( ` - Files added: ${ filesAdded } ` ) ;
202
+
203
+ if ( options . dryrun ) {
204
+ console . log ( "Dry Run Output:" ) ;
205
+ const dryRunFilePath = path . join ( "dryrun.json" ) ;
206
+ const data = fileOperation . getSummary ( ) ;
207
+ fs . writeFileSync ( dryRunFilePath , JSON . stringify ( data , null , 2 ) , "utf-8" ) ;
208
+ for ( const file in data ) {
209
+ console . log ( `- ${ file } -> ${ data [ file ] } ` ) ;
210
+
211
+ }
197
212
}
213
+ else {
214
+ for ( const { folder, created, filesAdded } of summary ) {
215
+ console . log ( `- Folder: ${ folder } ` ) ;
216
+ console . log ( ` - ${ created ? "Created" : "Already existed" } ` ) ;
217
+ console . log ( ` - Files added: ${ filesAdded } ` ) ;
218
+ }
219
+ }
220
+
198
221
}
0 commit comments