https://github.com/jainmohit2001/coding-challenges/tree/master/src/1
This was my first time setting up a complete TS project along with its structure, automated testing, npm scripts, linter, and prettier.
Here are a few articles that I went through to get a clear understanding of project structures in TS.
This is the final structure I went ahead with.
Here are some other resources as well that I referenced to get started with the above-mentioned tools:
Eslint & Prettier Setup
Jest using Babel
Project structure after completion of Challenge 1
I started by writing a function that would take in a filename
as an input and return the total number of bytes. Using the fs
module was pretty easy here. It exposes the statSync
method to get the information about a file (or statistics)[Ref].
IMHO, it's always good to abstract the functionality you want to achieve by understanding the inputs and outputs for the same.
function byteCount(filename: string): number {
return fs.statSync(filename).size;
}
Afterward, I used process.argv
to retrieve the command line arguments and find the file's name if present.
To ensure that the filename provided is correct, I used the existsSync
method to synchronously check whether a file already exists in the given path.[Ref]
Handled the case if the given file is invalid by displaying relevant information on the console and exiting the process with status code 1.