Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
vikrantkalyan23 authored Nov 17, 2024
1 parent 31f0178 commit bbf28ee
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You are awesome
1 change: 1 addition & 0 deletions filenames.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["example.txt"]
40 changes: 40 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require('fs');
const yargs = require('yargs');

// Load filenames from `filenames.txt` if it exists
let filenames = [];
const filenamesPath = 'filenames.txt';

if (fs.existsSync(filenamesPath)) {
const data = fs.readFileSync(filenamesPath, 'utf-8');
filenames = data ? JSON.parse(data) : [];
}

// Configure yargs to take a filename as input
const argv = yargs
.option('filename', {
alias: 'f',
type: 'string',
description: 'Name of the file to create',
demandOption: true,
})
.help()
.argv;

const newFilename = argv.filename;

// Check if the file already exists
if (filenames.includes(newFilename)) {
console.log(`The file "${newFilename}" already exists. Please provide a new filename.`);
} else {
// Write 'You are awesome' to the new file
fs.writeFileSync(newFilename, 'You are awesome', 'utf-8');
console.log(`File "${newFilename}" created successfully!`);

// Add the new filename to the array
filenames.push(newFilename);

// Save updated filenames to `filenames.txt`
fs.writeFileSync(filenamesPath, JSON.stringify(filenames), 'utf-8');
console.log('Updated filenames list saved to filenames.txt.');
}
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "assignment-1",
"version": "1.0.0",
"description": "assignment 1",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "vikrant",
"license": "ISC",
"dependencies": {
"yargs": "^17.7.2"
}
}

0 comments on commit bbf28ee

Please sign in to comment.