<h2id="initialising-your-assignments-directory">Initialising your assignments directory</h2>
<p>What follows is a recipe to initialise a git repository for your class assignments.</p>
<h3id="git-init">git init</h3>
<p>Let's assume you have a directory on your machine <code>~/cth2016</code> where you intend to place all your assignments. To create a dedicated assignments directory:</p>
<pre><code>$ cd ~/cth2016
$ mkdir cth2016-assignments
$ cd cth2016-assignments</code></pre>
<p>This will create a new directory <code>~/cth2016/cth2016-assignments/</code> that will be turned into a git repository with the following commands:</p>
<pre><code>$ git init</code></pre>
<h3id="git-add">git add</h3>
<p>Your directory is now under version control, though there is nothing in there yet. Let's first create a README file describing what the repo is about:</p>
<pre><code>$ echo 'Repository of CTH2016 assignments'> README</code></pre>
<p>This command will create a new README file on your system and add to it the <code>echo</code> string. You can, of course, create the file manually using Sublime. To make sure git is keeping track of this new file, emit the following command:</p>
<pre><code>$ git status</code></pre>
<p>The above should display the new file in the list of changes to the repo since it has been created. Yet the README file <em>is not</em> currently added to the repo (marked in red). To add the file to the repo:</p>
<pre><code>$ git add README</code></pre>
<p>Now <code>git status</code> should list the file in green and mark it as new. Any files that will be added to the directory will need to be added to the repo using the <code>git add</code> command.</p>
<h3id="git-commit">git commit</h3>
<p>When adding new files or changing them, you need to commit these to the repo:</p>
<pre><code>$ git commit -a -m 'initial commit'</code></pre>
<p>From the git command above <code>-a</code> means to you are committing all changes listed by <code>git status</code> and <code>-m</code> is the message for the commit. If you do a <code>git status</code> you should now see that the README file dissapeared from the list of changes since it has just been committed. Changing the README file will re-introduce it to list of changes. A commit is like a recorded snapshot of the repo, any changes that occur after the commit will be flagged in <code>git status</code>.</p>
<h3id="github">github</h3>
<p>Now that you have added a README file and committed this change to your local repo, it is time to link it to Github so that your instructor and your peers can "clone" it. To do so, login to <ahref="https://github.com">Github</a> and on the top/right corner of the page select the '+' symbol and 'New repository'</p>
<p>where you replace "your-github-https-url" with your actual github url (for instance, on my repo it is: https://github.com/gauthiier/cth2016-assignments.git)</p>
<p>Your local repo and your github repo are now linked and ready to go!</p>
<hr/>
<h2id="creating-a-new-assignment-node-git">Creating a new assignment (Node + Git)</h2>
<p>What follows is a recipe to create a new assignment in your newly initialised git assignments repository.</p>
<h3id="assignment-x">assignment-x</h3>
<p>If you have followed the previous section, you should now have a directory named <code>~/cth2016/cth2016-assignments/</code> under which you will add a new <code>assignment-x</code>. To create the new directory:</p>
<pre><code>$ cd ~/cth2016/cth2016-assignments/
$ mkdir assignment-x
$ cd assignment-x</code></pre>
<h3id="npm-init">npm init</h3>
<p>Node has a package manager named <code>npm</code> (n-ode, p-ackage, m-anager) that automates the task of creating a project from scratch and configures it in a way that is compatible with other node projects. We will use <code>npm</code> during the course of the module.</p>
<p>To initialise a new project/assignment, simply emit the follow command:</p>
<pre><code>$ npm init</code></pre>
<p>and you will be asked various questions regarding your project's name, version, description, entry point, author, etc. which you can configure at will (press <'enter'> to select and move to the next configuration step).</p>
<p>A <code>package.json</code> file is created upon the completion of <code>npm init</code>. This file contains all the configurations you selected and can be edited manually.</p>
<p>This step is optional, yet recommended for your assignment. When making a node application, modules/libraries can be made part of your project using <code>npm</code>. Thousands of javascript libraries are available on <ahref="https://www.npmjs.com">www.npmjs.com</a> which you can install with a simple <code>npm</code> command.</p>
<p>As in the first session of the class on the Command Line Interface (CLI), we will be using <ahref="https://www.npmjs.com/package/commander">"commander"</a> for our applications. To install it for you assignment, use the following command:</p>
<p>This will create a <code>node_modules</code> directory next to <code>package.json</code> where the commander code is placed. All modules installed using <code>npm</code> will reside in this <code>node_modules</code> directory.</p>
<h3id="index.js">index.js</h3>
<p>Now that you have an npm project created and installed commander it is time to create an <code>index.js</code> file containing the code for the assignment. Though since I am not following a proper assignment per se, I will simply create a simple tri-lingual script that will output <code>"Hello!"</code>, <code>"Dag!"</code> or <code>"Allô!"</code> depending on a command line arguments passed to the script which can be of the type <code>en</code> (english), <code>nl</code> (nederlands), <code>fr</code> (français) or nothing.</p>
<p>To create <code>index.js</code> simply type (or, alternatively, create it with Sublime):</p>
<pre><code>$ touch index.js</code></pre>
<p>Now open the file and input the desired code:</p>
<spanclass="co">// simplest tri-lingual program </span>
<spanclass="kw">var</span> program <spanclass="op">=</span><spanclass="at">require</span>(<spanclass="st">'commander'</span>)<spanclass="op">;</span>
<p>where you can replace <code>fr</code> by <code>en</code> or <code>nl</code> or nothing. Try it out!</p>
<h3id="readme">README</h3>
<p>One of the interesting feature of commander is that it can auto generate a <code>"help"</code> output from your script based on the command line options you have listed in your program.</p>
<p>To see this help output, simply type:</p>
<pre><code>node index.js --help</code></pre>
<pre><code> Usage: index [options]
Options:
-h, --help output usage information
-V, --version output the version number
-l, --language [code] Language
</code></pre>
<p>It is very common for command line scripts to feature such <code>--help</code> argument that when executed, displays what the program expects as input. Thus, for this class, each README you will create (for both assignments and final project) is expected to have (at least) this type of "helper documentation".</p>
<p>To initialise a README and add the <code>help</code> output from your script:</p>
<p>You can, of course, edit this README manually. It is nonetheless a very good practice to start the redaction with the <code>help</code> as it signals how to use your script to people that may be interested in it.</p>
<h3id="git-add-1">git add</h3>
<p>Now that you have a proper project initialised, the next step is to add all its files to git.</p>
<p>Yet before we do so, there one important file you need to fetch from github which will instruct git not to include to you repository unwanted files that may be installed by <code>npm</code> or by your operating system. Git has a special file called <code>.gitignore</code> that lists files and file patterns that have to be ignored by git. Luckily, there is a <ahref="https://github.com/github/gitignore">github repository</a> maintained by the git community that lists common files to ignore depending on the programming language and environment your are using.</p>
<p>To download the Node <code>.gitignore</code> and install it under the current <code>assignment-x</code>, type the following command:</p>
<p>It is now time to look which file needs to be added to git. In order to do so type:</p>
<pre><code>$ git status</code></pre>
<p>This should indicate which directories and/or files that are untracked. If you have followed the previous steps, <code>git status</code> should simply signal that the current directory <code>./</code> is untracked. To add it to git:</p>
<pre><code>$ git add ./</code></pre>
<p>To make sure git properly added the new assignment, simply check with <code>git status</code> that your <code>package.json</code>, <code>index.js</code>, <code>README</code>, and various file in <code>node_modules</code> are marked as new files (green).</p>
<h3id="git-commit-1">git commit</h3>
<p>The next step is to commit these new files to your local git repo:</p>
<pre><code>$ git commit -a -m "initial commit for assignment-x"</code></pre>
<h3id="git-push-1">git push</h3>
<p>And push the commit to your assignments github repo:</p>
<pre><code>$ git push origin master</code></pre>
<p>You should now see these changes online in your new <code>cth2016-assignments/assignment-x/</code> directory:</p>
<li><ahref="https://help.github.com/articles/set-up-git/">Github Help - Set Up Git</a></li>
<li><ahref="https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/">Github Help - Adding an existing project</a></li>