diff --git a/codeExamples/week1/commandLineExample/.gitignore b/codeExamples/week1/commandLineExample/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..94374a6a35df952ae6bb401ff2d6f2c6eebee378 --- /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 0000000000000000000000000000000000000000..17ee5bce51ebd50d0b88d41c56873bca3a5c3361 --- /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 0000000000000000000000000000000000000000..5e0f233f0c95995096aa115a7efbeb2bb668adba --- /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 0000000000000000000000000000000000000000..b300681700281b06127e6a114ad4641d9f227b52 --- /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 0000000000000000000000000000000000000000..a5668fd8e80a2f6bb32907f3563b6b6d0114f1e5 --- /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 0000000000000000000000000000000000000000..b7783f041dce707d4ff1f7e5fbb408f43bf358ae --- /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 0000000000000000000000000000000000000000..eca25084775e90f63a5efd0ab01d325457eb2a85 --- /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 0000000000000000000000000000000000000000..64ac975df7fbf14cdb8fdd5621fac91b95770e57 --- /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 0000000000000000000000000000000000000000..2a8fafb87f494f44dc4d615d163200144458cb05 --- /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" + } +}