From ba0e8cacd3e328d6f5b230f0edc5800f5c94e0a4 Mon Sep 17 00:00:00 2001
From: Andrej Rasevic <andrej@rasevicengineering.com>
Date: Thu, 7 Jan 2021 14:18:35 -0500
Subject: [PATCH] command line example

---
 .../week1/commandLineExample/.gitignore       |  5 +++
 .../week1/commandLineExample/bin/weather      |  5 +++
 .../week1/commandLineExample/cmds/foo.js      |  1 +
 .../week1/commandLineExample/cmds/help.js     | 21 +++++++++++
 .../week1/commandLineExample/cmds/today.js    |  3 ++
 .../week1/commandLineExample/cmds/version.js  |  5 +++
 .../week1/commandLineExample/index.js         | 35 +++++++++++++++++++
 .../commandLineExample/package-lock.json      | 13 +++++++
 .../week1/commandLineExample/package.json     | 18 ++++++++++
 9 files changed, 106 insertions(+)
 create mode 100644 codeExamples/week1/commandLineExample/.gitignore
 create mode 100755 codeExamples/week1/commandLineExample/bin/weather
 create mode 100644 codeExamples/week1/commandLineExample/cmds/foo.js
 create mode 100644 codeExamples/week1/commandLineExample/cmds/help.js
 create mode 100644 codeExamples/week1/commandLineExample/cmds/today.js
 create mode 100644 codeExamples/week1/commandLineExample/cmds/version.js
 create mode 100644 codeExamples/week1/commandLineExample/index.js
 create mode 100644 codeExamples/week1/commandLineExample/package-lock.json
 create mode 100644 codeExamples/week1/commandLineExample/package.json

diff --git a/codeExamples/week1/commandLineExample/.gitignore b/codeExamples/week1/commandLineExample/.gitignore
new file mode 100644
index 0000000..94374a6
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/.gitignore
@@ -0,0 +1,5 @@
+# only relevant for Mac systems
+.DS_STORE
+
+#ingore npm packages
+node_modules
diff --git a/codeExamples/week1/commandLineExample/bin/weather b/codeExamples/week1/commandLineExample/bin/weather
new file mode 100755
index 0000000..17ee5bc
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/bin/weather
@@ -0,0 +1,5 @@
+#!/usr/bin/env node
+// require('../')()
+var index = require('../index')
+index()
+//console.log("Another way to run a node application");
\ No newline at end of file
diff --git a/codeExamples/week1/commandLineExample/cmds/foo.js b/codeExamples/week1/commandLineExample/cmds/foo.js
new file mode 100644
index 0000000..5e0f233
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/cmds/foo.js
@@ -0,0 +1 @@
+console.log("executing the foo command");
\ No newline at end of file
diff --git a/codeExamples/week1/commandLineExample/cmds/help.js b/codeExamples/week1/commandLineExample/cmds/help.js
new file mode 100644
index 0000000..b300681
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/cmds/help.js
@@ -0,0 +1,21 @@
+const menus = {
+  main: `
+    weather [command] <options>
+
+    today .............. show weather for today
+    version ............ show package version
+    help ............... show help menu for a command`,
+
+  today: `
+    weather today <options>
+
+    --location, -l ..... the location to use`,
+}
+
+module.exports = (args) => {
+  console.log("***********")
+  console.log(args._[0])
+  const subCmd = args._[0] === 'help' ? args._[1] : args._[0]
+  console.log(subCmd)
+  console.log(menus[subCmd] || menus.main)
+}
\ No newline at end of file
diff --git a/codeExamples/week1/commandLineExample/cmds/today.js b/codeExamples/week1/commandLineExample/cmds/today.js
new file mode 100644
index 0000000..a5668fd
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/cmds/today.js
@@ -0,0 +1,3 @@
+module.exports = (args) => {
+  console.log('today is sunny')
+}
diff --git a/codeExamples/week1/commandLineExample/cmds/version.js b/codeExamples/week1/commandLineExample/cmds/version.js
new file mode 100644
index 0000000..b7783f0
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/cmds/version.js
@@ -0,0 +1,5 @@
+const { version } = require('../package.json')
+
+module.exports = (args) => {
+  console.log(`v${version}`)
+}
diff --git a/codeExamples/week1/commandLineExample/index.js b/codeExamples/week1/commandLineExample/index.js
new file mode 100644
index 0000000..eca2508
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/index.js
@@ -0,0 +1,35 @@
+const minimist = require('minimist')
+
+module.exports = () => {
+  const args = minimist(process.argv.slice(2))
+  console.log("args is: ");
+  console.log(args);
+  let cmd = args._[0] || 'help'
+
+  if (args.version || args.v) {
+    cmd = 'version'
+  }
+
+  if (args.help || args.h) {
+    cmd = 'help'
+  }
+  console.log("------------")
+  console.log(cmd);
+  switch (cmd) {
+    case 'today':
+      require('./cmds/today')(args)
+      break
+    case 'foo':
+      require('./cmds/foo')
+      break
+    case 'version':
+      require('./cmds/version')(args)
+      break
+    case 'help':
+      require('./cmds/help')(args)
+      break
+    default:
+      console.error(`"${cmd}" is not a valid command!`)
+      break
+  }
+}
diff --git a/codeExamples/week1/commandLineExample/package-lock.json b/codeExamples/week1/commandLineExample/package-lock.json
new file mode 100644
index 0000000..64ac975
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/package-lock.json
@@ -0,0 +1,13 @@
+{
+  "name": "command_line_example",
+  "version": "1.0.0",
+  "lockfileVersion": 1,
+  "requires": true,
+  "dependencies": {
+    "minimist": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
+      "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ="
+    }
+  }
+}
diff --git a/codeExamples/week1/commandLineExample/package.json b/codeExamples/week1/commandLineExample/package.json
new file mode 100644
index 0000000..2a8fafb
--- /dev/null
+++ b/codeExamples/week1/commandLineExample/package.json
@@ -0,0 +1,18 @@
+{
+  "name": "command_line_example",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "devstart": "node bin/weather",
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "bin": {
+    "weather": "bin/weather"
+  },
+  "dependencies": {
+    "minimist": "^1.2.0"
+  }
+}
-- 
GitLab