DEV Community

Kevan Y
Kevan Y

Posted on

Lab 5 - Refectory code and git rebase

Intro

For my fifth lab, we have to implement refactor our Static site generator CLI repo.

Refactor

Move handler file to handler folder

I moved my ssgHandler.js to src/utils/handler instead of being in the root folder. This helps to have everything in a respective folder.

Remove unused package

Removed some unused package import. They were some import declaration in the code that was never used. Removing it can make a cleaner code.

Remove duplicate code

In the code, they were some redundant code that wasn't necessary or can be simple to make it less redundant.

Add error handling for await

Add error handling for await, I wrapped most of the await call into a try/catch to handle the error. This makes code traceability easier.

Use promisified versions of fs

Removed promise wrapper for some node fs function. Instead changed to fs.promise.

Isolate function not related to ssgHandler to its own files

ssgHandler.js file was getting bigger and there is a lot of function that wasn't related such as reading files, data processing. I moved every unrelated function to its own file. So it is making it easier to maintain in the future.

Refactor names

In my code, there is some naming issue like inputPaths the name is in plural but the input path should only be a single path. To avoid some confusion I renamed it to inputPath

Rebase

After making all the commits for the above change. I wanted to squash into 1 commit. I first run the command git rebase -i master and pick the commit that I needed to combine together to squash and leave one commit to be pick. After saving the files I wasn't happy with the name of the commit, I run the command git commit --amend and changed the name. (b092da9)

Conclusion

Cleaning the code is something useful, it will help in the future as the project grows to have better maintainability.
Git rebase is super powerful too, It is like merge but can combine everything into 1 commit.

Top comments (0)