|
1 | 1 | import fs from "fs/promises";
|
2 | 2 | import path from "path";
|
| 3 | +import readline from "readline"; |
3 | 4 |
|
4 | 5 | import { updateVideosMetadata } from "./src/updateVideos.js";
|
5 | 6 | import { updateImages } from "./src/updateImages.js";
|
@@ -37,4 +38,68 @@ const processFiles = async (folderPath) => {
|
37 | 38 | }
|
38 | 39 | };
|
39 | 40 |
|
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(); |
0 commit comments