Skip to content

Commit c3f0150

Browse files
committed
Adding option to --ignore-dotfiles by request (opt in)
1 parent 10bdc9d commit c3f0150

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ If no directory is specified, the current directory (`.`) is used by default.
4949
- `--ext`: Organize files into folders based on their **file extensions**.
5050
- `--name`: Group files by their **starting names**.
5151
- `--date`: Group files by their **creation dates**.
52+
- `--ignore-dotfiles`: Ignore dotfiles when organizing.
5253

53-
> **Note**: These options cannot be used together. For example, you cannot use `--ext` and `--name` simultaneously.
54+
> **Note**: Some of these options cannot be used together. For example, you cannot use `--ext` and `--name` simultaneously, but you can use `--ignore-dotfiles` with any one other option.
5455
5556
---
5657

@@ -110,6 +111,23 @@ Organization Summary for '/path/to/directory':
110111
- Files added: 3
111112
```
112113

114+
### Organize by Name and Ignore Dotfiles
115+
```bash
116+
tidyup /path/to/directory --name --ignore-dotfiles
117+
```
118+
119+
Example output:
120+
121+
```
122+
Organization Summary for '/path/to/directory':
123+
- Folder: project-a
124+
- Created
125+
- Files added: 4
126+
- Folder: report
127+
- Already existed
128+
- Files added: 2
129+
```
130+
113131
### Invalid Option Combination
114132

115133
```bash

src/index.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const mkdir = promisify(fs.mkdir);
1010
const rename = promisify(fs.rename);
1111
const stat = promisify(fs.stat);
1212

13+
type Options = {
14+
ext: boolean;
15+
name: boolean;
16+
ignoreDotfiles: boolean;
17+
};
18+
1319
// Define mappings for file types to folder names
1420
const FILE_TYPE_MAP: Record<string, string> = {
1521
".png": "images-png",
@@ -28,10 +34,11 @@ program
2834
.description("Organize files in a directory based on their extensions")
2935
.option("--ext", "Use the file extetions as folder names")
3036
.option("--name", "Group files by starting name")
31-
.action(async (inputDir: string, option: { ext: boolean; name: boolean }) => {
37+
.option("--ignore-dotfiles", "Ignore dotfiles")
38+
.action(async (inputDir: string, options: Options) => {
3239
const dirPath = path.resolve(inputDir);
3340
try {
34-
if (option.ext && option.name) {
41+
if (options.ext && options.name) {
3542
console.error("Only one of --ext or --name can be used at a time");
3643
process.exit(1);
3744
}
@@ -42,7 +49,7 @@ program
4249
process.exit(1);
4350
}
4451

45-
await organizeFiles(dirPath, option);
52+
await organizeFiles(dirPath, options);
4653
} catch (error: any) {
4754
console.error("An error occurred:", error.message);
4855
process.exit(1);
@@ -51,18 +58,32 @@ program
5158

5259
program.parse(process.argv);
5360

61+
/**
62+
* Check if a file is a dotfile (starts with a dot)
63+
* @param fileName - The name of the file to check
64+
* @returns True if the file is a dotfile, false otherwise
65+
*/
66+
function isDotFile(fileName: string): boolean {
67+
return fileName.startsWith(".");
68+
}
69+
5470
/**
5571
* Get all file types present in a directory.
5672
* @param dirPath - The path to the directory.
5773
* @returns A record where keys are file extensions and values are file paths.
5874
*/
5975
async function getFileTypes(
60-
dirPath: string
76+
dirPath: string,
77+
options: Options
6178
): Promise<Record<string, string[]>> {
6279
const files = await readdir(dirPath);
6380
const fileTypes: Record<string, string[]> = {};
6481

6582
for (const file of files) {
83+
if (options.ignoreDotfiles && isDotFile(file)) {
84+
continue;
85+
}
86+
6687
const filePath = path.join(dirPath, file);
6788
const fileStat = await stat(filePath);
6889

@@ -84,12 +105,17 @@ async function getFileTypes(
84105
* @returns A record where keys are base names and values are file paths.
85106
*/
86107
async function getFileNameGroups(
87-
dirPath: string
108+
dirPath: string,
109+
options: Options
88110
): Promise<Record<string, string[]>> {
89111
const files = await readdir(dirPath);
90112
const nameGroups: Record<string, string[]> = {};
91113

92114
for (const file of files) {
115+
if (options.ignoreDotfiles && isDotFile(file)) {
116+
continue;
117+
}
118+
93119
const filePath = path.join(dirPath, file);
94120
const fileStat = await stat(filePath);
95121

@@ -112,14 +138,14 @@ async function getFileNameGroups(
112138

113139
async function organizeFiles(
114140
dirPath: string,
115-
options: { ext: boolean; name: boolean }
141+
options: Options
116142
): Promise<void> {
117143
let fileTypes: Record<string, string[]>;
118144

119145
if (options.name) {
120-
fileTypes = await getFileNameGroups(dirPath);
146+
fileTypes = await getFileNameGroups(dirPath, options);
121147
} else {
122-
fileTypes = await getFileTypes(dirPath);
148+
fileTypes = await getFileTypes(dirPath, options);
123149
}
124150

125151
const summary: { folder: string; created: boolean; filesAdded: number }[] =

0 commit comments

Comments
 (0)