Created
September 30, 2020 06:59
-
-
Save robvanderleek/ce96d14dbe70c9f279354f6ca02c30d1 to your computer and use it in GitHub Desktop.
Backpack issue 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/index.js b/index.js | |
index 3cbc816..03e0acb 100755 | |
--- a/index.js | |
+++ b/index.js | |
@@ -6,6 +6,7 @@ const path = require('path'); | |
function usage() { | |
console.log( | |
'Backpack usage:\n' + | |
+ ' List files in backpack: bp -l\n' + | |
' Put in backpack: bp -i <filename>\n' + | |
' Get from backpack: bp -e <filename>\n') | |
} | |
@@ -29,12 +30,30 @@ function exportFile(filename, backpackDir) { | |
fs.renameSync(path.join(backpackDir, filename), filename); | |
} | |
+function getStoredFiles(backpackDir) { | |
+ const files = fs.readdirSync(backpackDir); | |
+ files.sort((a, b) => fs.statSync(path.join(backpackDir, b)).ctime.getTime() - | |
+ fs.statSync(path.join(backpackDir, a)).ctime.getTime() | |
+ ); | |
+ return files; | |
+} | |
+ | |
+function listFiles(backpackDir) { | |
+ const files = getStoredFiles(backpackDir); | |
+ files.forEach((f, i) => { | |
+ const index = files.length - i; | |
+ console.log(`${index}: ${f}`); | |
+ }); | |
+} | |
+ | |
const backpackDir = initializeBackpackDir(); | |
const args = process.argv.slice(2); | |
if (args.length === 2 && args[0] === '-i') { | |
importFile(args[1], backpackDir); | |
} else if (args.length === 2 && args[0] === '-e') { | |
exportFile(args[1], backpackDir); | |
+} else if (args.length === 1 && args[0] === '-l') { | |
+ listFiles(backpackDir); | |
} else { | |
usage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment