The last weekend I built an open source CLI tool in less than 24 hours, for the first time and this how I did it meanwhile I tweeted
β¨ How I started
I noticed in my Ghost blog that when I upload any image, the blog creates another one optimized. But when you remove this image from the post or just delete the entire post, the images kept in the server.
So I just started to research what could I do
Today I'm was researching about remove uploaded images in Ghost after remove them from the post or delete the post
— David Burgos (@daburix) 12 de enero de 2019
π¨βπ» Coding to figure it out
I started to create a dirty script to validate if it would be possible
On one hand I fetched all posts & pages of the blog and on the other hand, get the list of all the uploaded images.
Then it matches if any uploaded image is being used on the post, page or metadata (twitter card, facebook card or post image)
Right now I have a dirty script to compare the uploads with all the content and meta images of the posts pic.twitter.com/sJq0d7DKYs
— David Burgos (@daburix) 12 de enero de 2019
π₯ Improving it
I got it running with success but needed some extra time to improve the code, turn into a CLI tool and test it.
So I ordered pizza and I kept working
I'm gonna order pizza and keep working on it to release as open source π
— David Burgos (@daburix) 12 de enero de 2019
I created a list action to just print the files list
Display action done, in order to simply print the files (informative, it does not remove) pic.twitter.com/xOmIOVE0ay
— David Burgos (@daburix) 12 de enero de 2019
Then I created the delete action
Purge command done, to delete the unused images π pic.twitter.com/0YQexDK2zy
— David Burgos (@daburix) 12 de enero de 2019
At this point I created the repository at Github
Pushing the code to this repo https://t.co/BaAAxqbOpd (still WIP) π¨βπ»
— David Burgos (@daburix) 12 de enero de 2019
π§ Learning on the fly
And started to turn it into a Command Line Interface (CLI). As I had no idea how to do it because I never did it before, I found some issues
Ok, I got it... I have to change the bin entry and add args handler pic.twitter.com/DryIdhJatE
— David Burgos (@daburix) 13 de enero de 2019
Some time later I went to bed
Well, I'm going to bed... I will continue π€
— David Burgos (@daburix) 13 de enero de 2019
And about 9h later I continued. Remember I have 2 kids and wife π
Morning guys! βοΈLet's continue
— David Burgos (@daburix) 13 de enero de 2019
π Testing
I fixed the previous issues but when testing in a real blog server I noticed I should update the script to don't remove the publication logo, cover and icon.
Ok, now it's working but noticed I have to skip some uploads like logo, header cover, etc. wondering how detect them because there is no endpoint π° pic.twitter.com/Cz6DH3ngoi
— David Burgos (@daburix) 13 de enero de 2019
Some time later was done for publication logo and cover
I think I got it, I added support to publicationCover and publicationLogo when proceed pic.twitter.com/wmZD6NXWHV
— David Burgos (@daburix) 13 de enero de 2019
About the publication icon (the blog favicon) was not a problem because Ghost creates the file favicon.ico
from the uploaded image and does not use it anywhere later
The image uploaded as publication icon is renamed to favicon.ico so can be removed, no problem π
— David Burgos (@daburix) 13 de enero de 2019
I tested it in this blog and also in the Ghostboard blog
I ran it on https://t.co/GVvO6NAnQK π
— David Burgos (@daburix) 13 de enero de 2019
With success! π
It works! pic.twitter.com/lcbMs3F32J
— David Burgos (@daburix) 13 de enero de 2019
π Final touches
Then I improved the README at the repo and added some screenshots
I improved the docs and add screenshots to the README https://t.co/BaAAxqbOpd
— David Burgos (@daburix) 13 de enero de 2019
And that's all!
It's done for now, I built an open source package in less than 24 hours π₯π
— David Burgos (@daburix) 13 de enero de 2019
π Resources
You can find:
Feel free to leave a comment with your feedback or any question π
π Thanks for reading!