Full Stack for Front-Ends Part 2 Creating a shell script with Node: Exercise
This course has been updated! We now recommend you take the Full Stack for Front-End Engineers, v3 course.
Transcript from the "Creating a shell script with Node: Exercise" Lesson
[00:00:15] You can do that too but this is kind of a lazier way of doing it, because it's just gonna execute without having to do Node name for your script.
>> Jem Young: But we have a bonus in here and I'll tell you about the bonus. What makes writing on node shell scripts so much fun.
[00:00:29] So let's just make a directory called workspace, call it whatever you want, make it your home folder. Just cuz the things we're gonna do, we don't wanna install, I won't give it away and you have access to the slides. But I won't give it away. But you wanna make your own folder for this.
[00:00:44] Cuz we're gonna make an untamed script. Yes?
>> Speaker 2: On our server?
>> Jem Young: Yes, on your server. Sorry.
>> Jem Young: So make a workspace folder and just move in to that and then touch index.js
>> Jem Young: Touch is a fun command because all it does is it change the modification time on a file.
[00:01:07] If that file doesn't exist than it makes it for you, that's what touch does. We can all say vi index.js, putting it to vi, open it, close it. It's just faster if you want to just create an empty file. Create it with touch. It's one of those final ones, right?
[00:01:20] Let's do it man touch, so many options, so many options. Remember I said we only do the basics everything has a man file everything has way more flags than we'll ever use. Wait 1fn man man, yes. The manual has a manual on how to use manual. Yes, Phoenix, but all right, I'm gonna make a directory.
[00:01:42] Sorry, I got a little distracted there. Make a directory, and call it workspace, and I'm just gonna move into workspace. Awesome, I'm gonna touch index.js, cool. Ls index.js, right? Everybody with me? Nothing complicated. Let's create a new project. I don't know if you've ever used npm init, but it's a pretty useful command, it sets up everything for you.
[00:02:11] So let's say, npm init, we're actually making an npm module as a node script. That's awesome. And call workspace, that's fine, that's fine. Defaults, defaults.
>> Jem Young: Cool. And if we look, it created a package.json for us. So cat, and it just creates a pretty basic npm package.json. It's kind useful, like npm init, or like npm.
>> Jem Young: So let's edit the package.json real fast because we want a link to the binary. The scripts were gonna execute that, executing is binary so it's like I don't need to actually run it as a node script. I'm gonna run it as a binary, as an executive log.
[00:02:59] So in package.json just add the line bin for binary and you can call it my script, you can call it whatever you want, you can call it load scripts, it doesn't really matter. So I'm just gonna do that here vi package.json and it scripts doesn't matter. Say,
>> Jem Young: Bin.
>> Jem Young: And have it always create the closing bracket.
>> Jem Young: And what do I wanna call it? I'm gonna call it my script, so, actually I'll call it loadscripts. That's how I remember what it is. And we're just gonna point to index.js as our scripts that we're gonna run, index.js.
>> Jem Young: So I added the reference to the binary, just index.js and we are just gonna write quit on that one. Everybody good? Cool.
>> Jem Young: Next, we're gonna do the same thing we just wrote with that shell script but we are gonna do it in node.
[00:04:13] And you'll note all the extra work we have to do the same work. Now, we're starting to see the benefit of writing in shell, writing in batch, versus writing in node. Because we have to execute these processes, we have to open all these other things, but let's go and do that.
[00:04:27] If you wanna cheat, because I know it's getting close to lunch, and I'm as hungry as you. In the comments I put all these codes and you just copy-paste it because I respect you, and I figure you know how to do this part, because I'm gonna do the same thing, don't judge me.
[00:04:42] Copy, paste.
>> Jem Young: Make you prettier.
>> Jem Young: So what does the scripts do? It does the same thing we did, it gets the load average from proc and it executes that. But now we have to worry about standard out, standard in, standard error. We're not gonna enable any standard error, standard in because we're not reading anything in this file, and it shouldn't throw any errors.
[00:05:15] Generally, you wanna handle standard error, otherwise you'll be running errors and nothing will be outputted. But because we don't know when this is gonna execute, it's gonna execute when it has free CPU cycles, we'd have to create an on function with a callback. Everybody knows what call backs are?
[00:05:29] Yes, Frontend Masters, you're masters of the call back, and/or promises. We could do this with probably async, but I don't wanna get too complicated with the promises and thing like that. But we're just saying on standard out,
>> Jem Young: It's gonna get some data and we're just gonna split out that data, nothing to it.
>> Jem Young: Now, if you wanna handle standard in or standard error, it's vstat.standarderror, stat.standardin, yeah. So let's go ahead and write and close that.
>> Jem Young: I didn't put the good part in here, I must have left it out. Now, the good part, this is the fun part. So if we npm i -g, nothing attended to it.
[00:06:24] We're gonna create a script that will run globally without us having it called node and we can run it from anywhere because it's a shell script. So if we npm i -g, all right? What did I call that script? Loadscript.
>> Jem Young: Yeah, so they'll load, it's all zeroes, we won't be for long.
[00:06:43] But now, we created an npm module that we can just run the shell script from anywhere in the system. That's the cool thing about why we're doing it in npm and not just a standalone system.
>> Jem Young: Important note, if you make changes to the file because npm is blinking a reference to the file, you have to npmg every time you update it.
[00:07:19] That's pretty cool. That's really useful.
>> Jem Young: And again, there is a course on Shell Scripting and VIM and Regex that I highly recommend you take if you wanna get more on your shell scripting and your nuances. Way more commands like calendar and date, time, things like that, if you want.
[00:07:36] This is enough knowledge for us for now.