Check out a free preview of the full Deno First Look course
The "CLI Options" Lesson is part of the full, Deno First Look course featured in this preview video. Here's what you'd learn in this lesson:
Burke talks about some interesting arguments that can be passed to the Deno CLI including a built-in formatting tool and explains how to pass in arguments to the Deno CLI to avoid errors. Deno commonly uses the built-in watcher using --unstable and --watch. Passing in watcher flags incorrectly will cause errors.
Transcript from the "CLI Options" Lesson
[00:00:00]
>> For Deno, if you actually go to Deno site here, one of the things that we did not talk about is they have a really good manual. If you just go to their manual, setting up your environment, and one of the things that Deno provides the shell auto completions.
[00:00:20]
So if we look at the CLR here, I'm back in my terminal. If I need Deno and then tab, it shows me all of the different directories that might be called Deno. But if I do space tab, then it gives me all of the options on the Deno object that I can actually run.
[00:00:38]
So these are all the things that you can pass to Deno, we've been doing Deno run, but these are all the other things that you can do. And these are enabled by the autocomplete, which you can look and dots on how to set that up. For any one of these, you can pass a help flag too so for instance, like, let's Deno run---H, and then it will tell you all of the different flags that you can pass in here.
[00:01:07]
It's rapping quite a bit because the text is so big, but here are the options. So any one of these items, any one of these Deno items, you can just pass that -H2, and it will give you more information on what you can do there. There are a lot of options.
[00:01:23]
I wanna talk about a few of them that I think are interesting, but we're not gonna talk about all. One of the things that Deno includes is a built in format. So let's look at it. So I'm gonna paste some code in here and I've actually got formatting turned on.
[00:01:43]
So I'm gonna turn off Toggle formatting. All right, so I'm gonna save this file. And you'll notice that this file has single quotes. If we were to run Deno format on this file, if you watch it's gonna change the single quotes to double quotes. I don't know if you just saw do that pretty quickly there.
[00:02:10]
This is very similar to prettier if you've used prettier before. It's sort of a version of prettier although it doesn't uses the D print library. So the formatting is a little bit different. Like for instance, the Deno format are really likes trailing commas, right? So if you have a list of items, all right?
[00:02:28]
So if you have, like const stuff is an array, and yet 1, 2, 3. I can't count 1, 2, 3 like if we format this with Deno, let's try this see what happens. It puts a trailing comma, see on the 3, it just likes trailing commas. Prettier doesn't do trailing commas.
[00:02:50]
So there are some differences there. In VS code to make this easier for yourself, there's an extension called formatting toggle, I think is what it's called. Yeah. And what this does is it just toggles on and off something called format on Save. And so you can see that down here in my status bar, it's X.
[00:03:12]
I'm gonna turn this back on this toggle formatting. And then I'm gonna go to my user settings for a second here and show you something. So we go, let's get settings Jason. And then I'm gonna take this off this default format or line and then look again at what happens here.
[00:03:35]
And so we have this file, it's in fact let's save. Now if we go to format this file, so I'm gonna say format document. It's gonna tell me there's multiple formatters, which one do you wanna use? And then we can configure. Now normally I use prettier but when you install Deno you now have this additional format or so you can choose Deno formatting as well.
[00:03:58]
I'm gonna choose Deno and then it adds this line to the settings file here where it sets the default format for TypeScript. Now I think one of the questions that's worth asking is why does Deno include a formatter? And I think the answer is that, according to Ryan, at least is that it seems to be primarily about two things.
[00:04:24]
One of them is speed. And so the format is written in rust and it's quite fast. In fact, if we were to come over here to the Deno, in fact I cloned earlier, I cloned all of Deno to my local machine. So if you got a Deno here oops, let's do Deno.
[00:04:44]
And then let's look at their standard library which is here. This is their entire standard library, right? So we could say, Deno format every file in the standard library, and it's gonna go through and do that. It just checked 475 just formatted 400. No, it didn't make any changes cuz there might have been formatted, but it's just significantly faster.
[00:05:07]
And so that's one of the reasons. The other reason is I think, I'm guessing here, but in current JavaScript and node projects, you don't get all of the things that you need to really have a good project out of the box. You have to vote on them all on, one of them is formatting, you have to vote on prettier.
[00:05:28]
If you want linting, you have to vote on eslint. And Deno is including these things out of the box. And so you don't have to add anything to your project to get that, it just works. Additionally, Deno will run tests for you, so it even includes a test runner.
[00:05:44]
So you can see that it's a much more comprehensive idea for your projects, so that you don't end up bolting a lot of dependencies on is trying to eliminate those dependencies for you. All right, that's the fourth matter, which is kind of cool. Let's look at how you pass in arguments to the Deno COI, because it's a little bit important to understand this.
[00:06:08]
Otherwise you can get confused in a hurry. And so before I think we were logging out, let's go back to our, we're logging out our args here. Actually, we're just logging out the first one. Let's just take this out to look at our Deno.args, let's just look at all of them.
[00:06:29]
So if we pass in, so we can do Deno run app.ts 1, 2, 3, and should return 1, 2, 3. Now you have to pass the args after your file name here, and this is super important. Now this is not unlike other systems, but it is especially finicky when it comes to flags.
[00:06:54]
So in Deno we've got these like so before we had these Deno run and then we did if y'all remember we did allow read. And then we did app.ts which this isn't gonna do anything but there we are. We didn't pass any parameters in. But if we had something where we needed to read so let's console log Deno current working directory, right, it's gonna we have to pass the flag in.
[00:07:20]
That flag has to come before the app.ts file. If you pass it after the file like this, so if you do this, right? And then you try to pass in allow read, it's just gonna act like you didn't do it, right? And it's very particular about that some CLI are some art, but just know that all of your flags have to come before the file name or they're not gonna work.
[00:07:45]
So if you're passing a flag and it's ignoring the flag, check and make sure that you're passing it in the right location. So we've talked about passing in arguments here, running programs, we talked about flags, they must be passed before the name of the file to execute. There are a lot of different flags that you're gonna end up passing to Deno, but there are two that you're gonna pass a lot.
[00:08:08]
Again, Deno has so much stuff that's built in and it's just a lot of fun to use. For instance, it has a built in watcher so we can do deno run, watch appt.ts. Now when I execute this it's gonna tell me that it can't, because I also need to pass in an unstable flag.
[00:08:28]
So Deno has this idea where Deno is stable, but a lot of the features in it are not and it gets around that by putting them behind an unstable flag. So in order to use the watch, we got to say run watch unstable app.ts. And now it's gonna run process we didn't allow, but we can change this out, so if we change this and said Hello World, And saved it, it changes and the boom, Hello World's logged out, right?
[00:08:59]
And so as you're going along developing, perhaps you don't wanna have to run it every time, you can just leave it running. We'll do this later on when we're actually building our project to see how that's helpful. So that's watching unstable, you're gonna use those a lot. The unstable flag you'll use quite a bit, a lot of stuff is still behind the unstable flag.
[00:09:18]
There are a lot of other flags that we can pass in, I think just as a summary we're gonna pass in H in order to get help. We're going to pass in all flags before the filename and we're gonna pass in our own arguments after the file name.
[00:09:35]
And that's basically what you need to know about the Deno COI, you can explore the different options on your own.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops