Talking To Telescope

Safrin
3 min readNov 8, 2020

This week was getting to know Telescope week.

Setting up Telescope

I used mac to set up Telescope because I was aware that how painful it is to setup Telescope in Windows. Hence, I avoid it and I am glad that I did that. Because of that, it went smoothly. I installed docker which makes easier to run Telescope. So far, I did not encounter any major issues 🤞

Lab 6

The task of this lab was working with Telescope REST API. We have to grab the last 10 blog posts and check all the link inside each blogs. I could have just done it simpler way and be done with it, but I wanted to try something different and MULTIPLE ways. Yeah, I implemented this feature in 3 different ways.

Version 1.0

My first idea was using my tool inside Telescope project. To do that, first installed my tool inside Telescope. Without any change, my tool only works as CLI tool, not as a library. So, I searched how to call a CLI tool programmatically. There is a library called child-process which can be used to run any of my link chekcer’s available options. My link checker already has option called fbl -f fileName to read file and report links from the file. The coding I did to call the options with correct files were:

  • grab the url form `http://localhost:3000/posts` which will give me an array of json object
  • loop through the array and fetch again `http://localhost:3000${post.url}` to get each blog post
  • create temporary file with the content of the file: I used tmp library and fs.appendFile
  • call -f option where you pass the tmp file

It took me a while with figuring out async call and how to call the options. That’s how I call the option from my link checker to Telescope.

const { exec } = require('child_process');
const fbl = exec(`fbl -f ${fileName}`, function(error, stdout){...}
Output of using fbl as CLI in Telescope

Version 1.1

I was not fulfilled the way I did it because using as CLI required me to install another library. So, I decided to turn my tool into library. I thought it won’t take that long, but later I found out how wrong I was.

All my function was in index.js To make my tool as library, I need to organize it better and export the functions so that Telescope can access the functions too only by installing my tool.

I did a huge refactoring on my code. After making my code more modular, I implemented the main functionality of this lab in a separate function. The implementation was not difficult, but the Promise and async again gave me difficult time. It took me awhile to figure out how to add all the tmp files into an array which can be passed to readFile function.

To test it, I decided why not make an option on my tool too to use this feature. Now, if I run fbl -t , the output is

Output of running fbl -t

Version 1.2

The second version made my tool as library. Now, I can use my tool from Telescope even more easily. All I did was literally 3 lines of coding and got the beautiful result:

const fbl = require("findbrokenurl/src/util")
fbl.setDefaultConfig()
fbl.handleTelsescope()
Output of using fbl as library in Telescope

You can see the full codes for different versions in the gist file. Do you think it was worth it?

--

--