Skip to content

Commit 406e531

Browse files
authored
Merge pull request #5 from girishsuri15/dryrun
added the dry option
2 parents 50339f0 + ddd014e commit 406e531

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

src/fileOperation.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as fs from 'fs';
2+
3+
import { promisify } from "util";
4+
const mkdir = promisify(fs.mkdir);
5+
const rename = promisify(fs.rename);
6+
7+
class Operation {
8+
private dryRun: boolean;
9+
private summary: Record<string, string>;
10+
/**
11+
*
12+
* @param dryRun - If true, operations will be simulated without making changes.
13+
*/
14+
constructor(dryRun: boolean) {
15+
this.dryRun = dryRun;
16+
this.summary = {};
17+
18+
}
19+
20+
async rename(oldPath: string, newPath: string): Promise<void> {
21+
if (this.dryRun) {
22+
this.summary[this.extractFileName(oldPath)] = this.extractFileWithParentDir(newPath);
23+
} else {
24+
await rename(oldPath, newPath);
25+
console.log(`Renamed: ${oldPath} -> ${newPath}`);
26+
}
27+
}
28+
29+
async mkdir(dirPath: string): Promise<void> {
30+
if (this.dryRun) {
31+
console.log(`[Dry Run] directory will be create ${dirPath}`);
32+
} else {
33+
await mkdir(dirPath);
34+
}
35+
}
36+
getSummary() {
37+
return this.summary;
38+
}
39+
private extractFileName(filePath: string): string {
40+
const parts = filePath.split("/");
41+
return parts[parts.length - 1];
42+
}
43+
private extractFileWithParentDir(filePath: string): string {
44+
const parts = filePath.split("/");
45+
return parts[parts.length - 2] + "/" + parts[parts.length - 1];
46+
}
47+
}
48+
49+
export default Operation;

src/index.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import fs from "fs";
55
import path from "path";
66
import { promisify } from "util";
77
import { version } from "../package.json";
8+
import Operation from "./fileOperation";
89

910
const readdir = promisify(fs.readdir);
10-
const mkdir = promisify(fs.mkdir);
11-
const rename = promisify(fs.rename);
1211
const stat = promisify(fs.stat);
1312

1413
type Options = {
1514
ext: boolean;
1615
name: boolean;
1716
ignoreDotfiles: boolean;
17+
dryrun: boolean;
1818
};
1919

2020
// Define mappings for file types to folder names
@@ -28,17 +28,28 @@ const FILE_TYPE_MAP: Record<string, string> = {
2828
};
2929

3030
const program = new Command();
31-
31+
let fileOperation: Operation;
3232
program
3333
.version(version)
3434
.argument("[directory]", "Directory to tidy up", ".") // Default to current directory
3535
.description("Organize files in a directory based on their extensions")
3636
.option("--ext", "Use the file extensions as folder names")
3737
.option("--name", "Group files by starting name")
38+
.option("--dryrun", "Show what would be done without making changes", false)
3839
.option("--ignore-dotfiles", "Ignore dotfiles", true)
3940
.action(async (inputDir: string, options: Options) => {
4041
const dirPath = path.resolve(inputDir);
42+
console.log(`Organizing files in directory: ${dirPath}`);
4143
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+
4253
if (options.ext && options.name) {
4354
console.error("Only one of --ext or --name can be used at a time");
4455
process.exit(1);
@@ -141,7 +152,6 @@ async function getFileNameGroups(
141152
*/
142153
async function organizeFiles(dirPath: string, options: Options): Promise<void> {
143154
let fileTypes: Record<string, string[]>;
144-
145155
if (options.name) {
146156
fileTypes = await getFileNameGroups(dirPath, options);
147157
} else {
@@ -161,10 +171,9 @@ async function organizeFiles(dirPath: string, options: Options): Promise<void> {
161171
let folderCreated = false;
162172

163173
if (!fs.existsSync(folderPath)) {
164-
await mkdir(folderPath);
174+
await fileOperation.mkdir(folderPath);
165175
folderCreated = true;
166176
}
167-
168177
let filesAdded = 0;
169178
for (const filePath of filePaths) {
170179
const fileName = path.basename(filePath);
@@ -180,7 +189,7 @@ async function organizeFiles(dirPath: string, options: Options): Promise<void> {
180189
counter++;
181190
}
182191
}
183-
await rename(filePath, newFilePath);
192+
await fileOperation.rename(filePath, newFilePath);
184193
filesAdded++;
185194
}
186195
summary.push({ folder: folderName, created: folderCreated, filesAdded });
@@ -190,9 +199,23 @@ async function organizeFiles(dirPath: string, options: Options): Promise<void> {
190199
const lastDir = lastPath[lastPath.length - 1];
191200

192201
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+
}
197212
}
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+
198221
}

0 commit comments

Comments
 (0)