Week 28 Homework đť đŚ
New stuff we learned this week: đ¤
Vim
- in visual mode (and visual block mode) you can switch the âcursorâ back and forth from either side of the selection using
o
â think of it as short for other â switch to the other side. - if you want to reselect the last thing you had visually selected you can type
gv
from normal mode - use
<CTRL-v>
to ender visual block mode. This mode lets you edit multiple lines at once. - in visual block mode typing
I
will allow you to insert text to the left of all the lines you have selected at once. (Remember, when youâre changing stuff in visual mode, youâll only see one line changing until you hit<esc>
to go back to normal mode). - in visual block mode typing
A
will allow you to insert text to the RIGHT of all the lines you have selected. - in visual block mode typing
c
will allow you to change all the lines at once, substituting the selected part on each line for the new stuff you type in insert mode. - in visual block mode typing
d
will delete everything you have selected. - in visual block mode typing
r
will replace everything you have selected with the character you type. - you can also select lines with âraggedâ (uneven) right edges, by using the âend-of-lineâ motion character, which is
$
and then moving up or down withj
ork
.
Git
- after youâve made modifications to a file and you want to throw away those changes, you can do
git checkout -- <file>
â to discard changes to that file. Think of it like âchecking outâ the file from the last commit. There is NO undo for this. - once youâve âstagedâ a file (added it to the truck) you can unstage it by doing
git reset HEAD <file>
. This doesnât change the file at all or lose any work, it just removes it from going into the next commit. 99% of the time you can get away with leaving out theHEAD
part, so you can just typegit reset <file>
. - if you do
git reset <commit-sha>
then git throws away the commits after that SHA but keeps the changes. So your files stay exactly the same, but any changes made after that commit now show up as modifications in your working directory. - if you do
git reset --hard <commit-sha>
then git removes the commits after that SHA AND deletes all the changes from those commits. Use this when you really want to throw away forever some work youâve done.
Node.js
- if you pass the node
require()
function a string that immediately starts with a letter, likerequire('cowsay')
rather thanrequire('./my-module')
â then that is called passing a Bare String. You can remember it by thinking, that âitâs not wearing itâs dot-slash.â - when they tiny Ryan Dahl inside your computer gets a bare string he first checks if there is a built-in node core module of that name. If there is,
require
returns the core module. - if there is no core module named the same as the bare string, then Ryan looks in your current directory for a
./node_modules
dir, and tries to find a module in there. If the module is namedfoobar
then./node_modules/foobar.js
would be loaded, or./node_modules/foobar/index.js
would also work. - if there is no file in
node_modules
that matches, Ryan starts climbing up the directory structure of your computer, looking for othernode_modules
dirs, until he quits and errors if he canât find a matching module. npm
is a program (which I already installed on our HTC computer) that allows you to install free javascript modules from the internet by doingnpm install <module-name>
.npm
puts downloaded modules into thenode_modules
directory.
Core Modules â Node.js
- the
fs
module is a core module in node for working with the file system. - you can access it by requiring the bare string
fs
:let fs = require('fs');
- you can read a file into memory by calling the
readFileSync()
method, which returns a Buffer of binary data, which can be converted to a string by calling.toString()
, like so:
let fs = require("fs");
let filePath = "./my-file.txt";
let buffer = fs.readFileSync(filePath);
let fileContent = buffer.toString();
- the
fs
module has awriteFileSync()
method which allows you to write out files to the filesystem as well:
let fs = require("fs");
fs.writeFileSync("./my-password.txt", "goatbanjorodeo");
- the
os
module is a core module that give you information about the host operating system, it has a bunch of nifty methods, like:
let os = require("os");
os.homedir();
os.cpus();
os.arch();
os.platform();
Useful Links:
Homework Plan:
- 1 day adding to and reviewing flashcards
- 2 days vim homework
- 1 day Node Core Modules homework
- 1 day Non-Core Modules homework
- 1 day watch CCCS #26
Homework day 1
Homework day 2
Homework day 3
Homework day 4
Flashcard Assignment
- add 2 new
vim
flashcardsgv
o
(when in visual or visual-block mode)
- add 4 new flashcards in the
git
category:git checkout -- <file>
git reset HEAD <file>
git reset <commit-sha>
git reset --hard <commit-sha>
- add 3 new
node
flashcardsfs.readFileSync(<filepath>)
- explain what it does, and what it returnsfs.writeFileSync(<filepath>, <data>)
Buffer.toString()
- carefully review all of your flashcards, stopping to reinforce any you get wrong
Vim Homework #1
- carefully review the
vim
section of âNew Stuffâ above ^. ssh
into your home dir, make and move into a new dir calledhtc-vimtutor
- copy down a file from
/vimtutor/visual-block-1.txt
into your current directory - open the file with
vim
, youâll see itâs like a addition tovimtutor
that I wrote. - go through all the steps carefully
- when you finish, delete the file, and copy it again, and complete all the steps a second time.
Vim Homework #2
- At least 1 day must have passed since you did Vim homework #1, STOP if it hasnât.
- do your Flashcard assignment first â STOP if you havenât
ssh
into your home dir, move into the dirhtc-vimtutor
- copy down a file from
/vimtutor/visual-block-2.txt
into your current directory â this file is mostly the same as the other, but removes the hints - open the file with
vim
- go through all the steps carefully
- when you finish, delete the file, and copy it again, and complete all the steps a second time.
Node Core Modules Homework
- carefully review the 2 sections on node, AND the git section in âNew Stuffâ above ^.
ssh
into your home dir and make and cd into a dir:~/node/week28
- initialize the new directory as a git repo
- create a new node script called
os.js
. To start, justrequire
the built-in'os'
module, and console.log out the whole module. - execute your script from the shell, and see if you can figure out which things seem like functions you might be able to call.
- before you do anything else, add the new file to git and commit your work.
- now, edit the file so that it outputs a nice summary of the system, that reads like this: (only the stuff to the left of the
:
on each line should be hardcoded, the real values come from calling functions on theos
module â you might need to console.log the module a bunch of times to find the right method names):
$ node os.js
Architechture: x64
Platform: linux
Your home dir: /home/ubuntu
Number of CPUs: 1
Running for seconds: 1032342
- when youâve got it working, stage the changes in
git
, but DONâT COMMIT YET. - next unstage (remove from truck) the file. (BE CAREFUL, I just want to you un-stage it, not throw away your work).
- once itâs unstaged, re-stage it with git add and then commit your work.
- now, modify your file so that it also adds a line at the bottom of the output that says something like:
Memory: 242342153
. - commit your work
- double-check that you did the above step â if you type
git status
it should say âworking tree cleanâ - now, from the shell, delete your
os.js
file. - ACKK! we lost our work, what should we do? Never fear, git will save us.
- type
git status
â git will show you that it noticed thatos.js
was deleted. - throw away the changes (which was the deleting of the file) by typing
git checkout -- os.js
- cat out the
os.js
file to see that itâs back. Thanks git! - now make one more change to
os.js
, adding the output of one moreos
function, it doesnât matter if you understand what it means. - commit your work
- next, letâs combine your last two commits into a single one using
git reset
. To do that, first:- type a command to see your commits and their shaâs
- copy a sha 2 or 3 commitâs back
- type
git reset <SHA>
with the sha you committed - type
git status
to see that your work is still all there - view git log again to see that the commits after the SHA you copied are gone
- git add and git commit, thus combining into one commit.
- next, with vim create a file called
facts.txt
two lines, each with one sentence, the sentences should read:- (line 1)
I believe Louisiana is the Pelican state.
- (line 2)
My phone number is 867-5309.
- (line 1)
- next, create a new file called
fs.js
, in it:- require the
fs
core module - read the contents of the
facts.txt
file into memory - console.log out the contents of
facts.txt
- require the
- save your work with git
- next, use a regular expression to modify the output of the script so that it prepends each line with
FACT:
like this:
$ node fs.js
> FACT: I believe Louisiana is the Pelican state.
> FACT: My phone number is 867-5309.
- save your work with git
- change the
fn.js
file so that instead of console.log-ing the output, it saves the output to a new file calledactual-facts.txt
. - save your work with git
- make another change to the
fs.js
file so that instead of saving the file toactual-facts.txt
that it saves the file to whatever the user supplies as an argument when invoking the script, so that for instance if it typednode fs.js foo.txt
it would save it to a file calledfoo.txt
. - save your work with git
- using
git reset
combine the last two commits into one. - make sure you committed properly, check git status â should be clean.
- destroy the code in
fn.js
by replacing it with the text âteddy bearâ â do this from the shell, without using vim. - commit your weird change.
- now do a âhardâ reset to throw away the last âteddy bearâ commit and throw away the work.
- cat out the
fs.js
file â it should be back to how it was before. - do a
git status
you should have a clean status, your teddy bear rampage is gone forever. - Extra Credit: ⨠To do the extra credit below, you have to know a new way to create a regular expression. So far Iâve only taught what is called a regular expression literal â but you can also construct a Regex out of a string, as shown here:
let regexLiteral = /^foo/gi;
// you can get the same result like this:
let regexConstructed = new RegExp("^foo", "gi");
// notice: the FLAGS go in the 2nd arg ----^^
- Extra Credit: ⨠- create a node script called
cat-replace.js
that takes three arguments, the first is the path to a file on the filesystem, the second is a search work, and the third is a replace word. When invoked it should print out the contents of the file (from the first arg), but, with all instances of the second arg replaced with the third arg. So for instance, I could do:
$ node cat-replace ./facts.txt is WAS
> I believe Louisiana WAS the Pelican state.
> My phone number WAS 867-5309.
Non-Core Modules Homework
- carefully review the 2 sections on node, AND the git section in âNew Stuffâ above ^.
ssh
into your home dir and move into the~/node/week28
dir- practice a couple of variations of
git log
- make and checkout a new branch in one git command, name the branch
user-module
- view git log â and make sure you understand how it is showing you that two branches are pointing at the same commit.
- where is
HEAD
pointing? Why? - type a git command to move
HEAD
so it points at master - move HEAD back to
user-module
- make a new node script called
module.js
- in the
module.js
file require a non-existent module called'user-name'
(bare string). - execute the script from the shell, you should get an error
- say out loud what different things node checked to try to find the
user-name
module before it gave up and errored. - now, create a file called
user-name.js
but put it in a special location to trick the tiny Ryan Dahl into finding it. - once youâve got it where you want it, edit the file with vim, and have it export a single string with your username.
- go back to your
module.js
file and console.log out the value of theuser-name
module, it should work something like this:
$ node module.js
> tabitha
- now, do a
git add .
and then agit status
â there should be two files added to the truck. DONâT COMMIT YET. - pretend you wanted to do a commit just creating the
user-name.js
module, so use your git skills to take themodule.js
change off of the truck. - now, commit just the new user-name module file.
- then commit the change to
module.js
- next, change the
user-name.js
file so that it doesnât return a hard-coded value, but actually determines the username somehow (hint, you could use theos
core module, or you could use theprocess
object). - review your work with
git diff
and then commit. - next, change the name of the the
user-name.js
file otindex.js
â and then change where it is located so that Ryan Dahl can still find it (hint: review New Stuff if youâre confusedâŚ) - make sure the script still works, and commit your work.
- using the new git techniques we learned, combine your last two commits into one.
- merge your
user-module
branch into master, then delete the branch - what type of merge happened?
- make and checkout a new branch called
filter-odd
- make a new node script called
filter-odd.js
that takes any number of numeric arguments from process.argv, and logs out all of the odd numbers. You donât know how to test if a number is odd yet, thatâs OK â use wishful thinking to imagine that you had a function calledisOdd
â that will be Georgeâs problem. - write the script, including using the faked-out
isOdd
function - run the script from the shell, you should get an error like
isOdd is not defined
, thatâs OK. - commit your work.
- now, instead of being George and writing an
isOdd
function, letâs let the internet help us! Install a package from the Node Package Manager (npm) calledis-odd
. To do that, type this command:npm install is-odd
. - after the command finishes, do
tree .
to see whatnpm
did for you. - go spelunking down into the
node_modules
dir and see whatâs in some of the files. You wonât understand all of them, but just poke around. - see how much of the code in
./node_modules/is-odd/index.js
you understand. What partâs donât you understand? - now, go back to your
filter-odd.js
file and import theisOdd
function from your nodemodules dir _using a bare string. - now that youâve got a function for
isOdd
you should be able to get your script working, so that it works like this (hint, use Array.map andNumber
to convert to numbers before filtering):
$ node filter-odd.js 1 2 5 8 10 11
> [1, 5, 11]
- commit your work
- merge your branch back into master
- Extra Credit: ⨠change the output so it reads like this:
$ node filter-odd.js 1 2 5 8 10 11
> The odd numbers are: 1, 5, 11
- Kiah Credit: ⨠doing the
npm install
in the previous step blew away our node_modules directory, so we lost theuser-name
module. Detach your HEAD and go back to a commit where that module existed, then temporarily copy that file to some other directory. Re-attach your head, and move the file into your working directory. Modifymodule.js
so that itrequire
s the module the correct way (now that itâs out ofnode_modules
), make sure it works, and commit your changes.