Skip to content

Commit 9e5d247

Browse files
HosseinHossein
Hossein
authored and
Hossein
committed
get folder path from user
1 parent cfee938 commit 9e5d247

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# updateMetaData
1+
# updateMetaData
2+
3+
After cloning the repo, run the following command in your terminal window:
4+
5+
npm run start

index.js

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "fs/promises";
22
import path from "path";
3+
import readline from "readline";
34

45
import { updateVideosMetadata } from "./src/updateVideos.js";
56
import { updateImages } from "./src/updateImages.js";
@@ -37,4 +38,68 @@ const processFiles = async (folderPath) => {
3738
}
3839
};
3940

40-
processFiles("./files");
41+
const getUserInput = (question) => {
42+
const rl = readline.createInterface({
43+
input: process.stdin,
44+
output: process.stdout,
45+
});
46+
47+
return new Promise((resolve) => {
48+
rl.question(question, (answer) => {
49+
rl.close();
50+
resolve(answer);
51+
});
52+
});
53+
};
54+
55+
const validateFolderPath = async (folderPath) => {
56+
try {
57+
const folderStats = await fs.stat(folderPath);
58+
59+
if (!folderStats.isDirectory()) {
60+
throw new Error("Provided path is not a directory.");
61+
}
62+
63+
return folderPath;
64+
} catch (error) {
65+
throw new Error(
66+
"Invalid folder path. Please provide a valid directory path."
67+
);
68+
}
69+
};
70+
71+
const removeQuotes = (str) => {
72+
return str.replace(/^'|'$/g, "");
73+
};
74+
75+
const main = async () => {
76+
console.log("Hello! Please provide the folder path:");
77+
78+
let folderPath;
79+
let validPath = false;
80+
81+
while (!validPath) {
82+
try {
83+
folderPath = await getUserInput("> ");
84+
folderPath = folderPath.trim();
85+
86+
if (!folderPath) {
87+
throw new Error(
88+
"Folder path cannot be empty. Please provide a valid directory path."
89+
);
90+
}
91+
92+
folderPath = removeQuotes(folderPath); // Remove single quotes if present
93+
folderPath = path.resolve(folderPath);
94+
folderPath = await validateFolderPath(folderPath);
95+
96+
validPath = true;
97+
} catch (error) {
98+
console.error(error.message);
99+
}
100+
}
101+
102+
processFiles(folderPath);
103+
};
104+
105+
main();

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"name": "updateMetaData",
3+
"version": "1.0.0",
24
"type": "module",
35
"dependencies": {
46
"buffer": "^6.0.3",
@@ -14,14 +16,14 @@
1416
"@babel/preset-env": "^7.22.9",
1517
"eslint": "^8.46.0"
1618
},
17-
"name": "updateexif",
18-
"version": "1.0.0",
19-
"main": "processImages.js",
19+
"main": "index.js",
2020
"scripts": {
21+
"install-deps": "npm install",
22+
"start": "npm run install-deps && node index.js",
2123
"test": "echo \"Error: no test specified\" && exit 1"
2224
},
2325
"keywords": [],
24-
"author": "",
26+
"author": "Hossein Torabi",
2527
"license": "ISC",
2628
"description": ""
2729
}

0 commit comments

Comments
 (0)