A tale of Two Runtimes: Node.js vs Browser
Global vs Window
In Node.js runtime, global object
is where global variables are stored.
In browsers, window objects
are where global variables are stored.
Browsers have access to the document
object, which contains the html for the page, node does not have this.
Browsers also have access to a location
that contains information about the web address, node doesn’t.
Node has the require
module, that we can use to import installed modules, and export our own files.
The FS Module
Node ships with a File System module that contains methods that allow us to interact with our pc’s FS.
require
will return to us a object with many properties that will enable us to do file I/O.
Creating a New File
Write File
: Method used to create an file in the FS module.
fs.writeFile("foo.txt", "Hello world!", "utf8", (err) => {
if (err) {
console.log(err);
}
console.log("write is complete");
});
callback chaining
to guarantee commands occur after a write completes or fails.Reading Existing Files
Read File : Method used to read the contents of a file.
Arguments go as follows:
Callback to signal end
You can do anything you want with the contents of a read file, since it is just one giant string!
Fancy File I/O
We can use Read File and Write file together to replace occurences of certain phrases with something else.
const fs = require("fs");
function replaceContents(file, oldStr, newStr) {
fs.readFile(file, "utf8", (err, data) => {
if (err) {
console.log(err);
}
let newData = data.split(oldStr).join(newStr);
writeContents(file, newData);
});
}
function writeContents(file, data) {
fs.writeFile(file, data, "utf8", (err) => {
if (err) {
console.log(err);
}
console.log("done!");
});
}
replaceContents("poetry.txt", "do not", "should");
Version Control Systems help us keep track of changes over time.
Git
is the most popular VCS in use today.
Repository
: Comprises all the source code for a particular project.
Commits
: collection of changes grouped towards a shared purpose.
Commit Messages & Commit Hashes
: Git id’s your commits with a specially generated series of letters and numbers called a Hash, and a detailed message describing the commit.
References
: Way to alias a commit in plain english.
HEAD
: default reference that points to the most recent commit.Working Directory
Staging Area
Commit History
Tracking Changes in a Repository
Git Init
: Allows you to start a repository in your current directory.
Git Add
: Adds content to your repository.
Git Status
: Used to check what has been staged in your repository.
Git Commit -m
: Used to commit your changes/additions into the commit history - flag is used to add a message to your commit.
Git Log
: Used to check your repo’s commit history.
Branches and Workflow
Branch
: Basically a seperate timeline in Git, reserved for it’s own changes - Master is the default branch.
Git branch [name of branch]
: Used to reate a new branch.
Git Checkout
: Used to switch to an existing branch.
Bringing it Back Together
Git Merge
: Used to integrate one branch into another.Connecting with the world via Github
DCVS
: Distributed Version Control System - built in support for managing code both locally and from a distant source.
We typically refer to the primary remote of a repo as the origin
.
We clone
a distant repo once, use pull
to update it and push
our own code into it.
Collaboration via Git and Github
> git branch add-my-new-file
> git add my-new-file.js
> git commit -m "Add new file"
> git push -u origin add-my-new-file
This is the typical push workflow.
-u
flag stands for --set-upstream
.Git Pull
: Used to update your files from remote repo - behind the scenes this is a combination of Git Fetch and Git Merge.
Git Diff
: Used to visualize any tracked differences in our repository.
---
& +++
: let’s us know there are both additions & subtractions in our js file.@@
: shows use where our chunk of changes are line-wise.
Diff Options
# See differences between the 'feature'
# branch and the 'master' branch.
> git diff master feature
# Compare two different commits
> git diff 1fc345a 2e3dff
# Compare a specific file across separate commits
> git diff 1fc345a 2e3dff my-file.js
Time Travel
# You can checkout a branch name.
# You'll be using this particular branch a lot!
> git checkout master
# You can also use commit hashes directly
> git checkout 7d3e2f1
# Using a hyphen instead of a hash will take
# you to the last branch you checked out
> git checkout -
# You can use "HEAD~N" to move N commits prior
# to the current HEAD
> git checkout HEAD~3
Rebase: An Alternative Form of Time Travel
The Golden Rule of Git
Never change the history of a branch that’s shared with others.
Conflict Resolution