Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #31

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
## Nobot - Automating with NodeJS ##
# Automating with Node.js - Nobot Examples

**Minimum Node version**: 6.9.1
Colour Print: http://amzn.eu/aA0cSnu

https://nodejs.org/docs/v6.9.1/api/
Kindle: http://amzn.eu/dVSykv1

Kobo: https://www.kobo.com/gb/en/ebook/automating-with-node-js

Leanpub: https://leanpub.com/automatingwithnodejs

![Node Bot](nobot.jpg)
Google Play: https://play.google.com/store/books/details/Automating_with_Node_js?id=9QFgDwAAQBAJ

**Minimum Node version**: 6.9.1

1. Make sure you have Node installed https://nodejs.org/en
1. Run `npm install`
1. Follow along in the book to work on examples in current `master` branch
1. View complete versions at branch complete `git checkout complete`
1. Follow along in the book to work on examples in `develop` branch
1. Complete versions are on `master` branch.

https://nodejs.org/docs/v6.9.1/api/
13 changes: 8 additions & 5 deletions examples/001/hello.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const args = process.argv.slice(2);
const [name] = args;
/* eslint-disable linebreak-style */
const args = process.argv.splice(2);
const [fname] = args;
// const args = process.argv.splice(2);
// const fname = args[0];

if (name === undefined) {
console.error('Please pass a name, e.g. node hello.js Shaun');
if (fname === undefined) {
console.error('Please pass a name, e.g., node hello.js Arie!');
process.exit(0);
}
console.log(`Good day, to you, ${fname}!`);

console.log(`Good day to you, ${name}`);
17 changes: 7 additions & 10 deletions examples/002/process.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
console.log(`This process is pid ${process.pid}`);
/* eslint-disable linebreak-style */
console.log(`This process is PID ${process.pid}`);

process.on('exit', (code) => {
console.log(`The process has now finished, exiting with code: ${code}`);
console.log(`The process has now finished, exiting with CODE ${code}`);
});

process.stdout.write('Hello, I am writing to standard output\n');
process.stdout.write(`The current working directory is ${process.cwd()}\n`);
process.stdout.write(`This script has now run for ${process.uptime()} seconds!\n`);

process.stdout.write(`Current working directory: ${process.cwd()}\n`);

console.log(`This script has been running for ${process.uptime()} seconds`);

process.stdout.write('Type something then hit enter: \n');

process.stdin.setEncoding('utf8');

process.stdout.write('Type something and hit enter!\n');
process.stdin.setEncoding('utf-8');
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
Expand Down
14 changes: 7 additions & 7 deletions examples/003/build-querystring.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const querystring = require('querystring');

// https://jira.my-company.com/rest/api/latest/search?jql="assignee=shaun.stone&startAt=2&maxResults=2"

/* eslint-disable linebreak-style */
/* eslint-disable import/newline-after-import */
/* global URLSearchParams, */
/* eslint no-undef: "error" */
const apiHost = 'https://jira.my-company.com/rest/api/latest/search?jql=';

const jqlParams = {
const jqlParams = new URLSearchParams({
assignee: 'shaun.stone',
startAt: 2,
maxResults: 2
};
});

const apiUrl = `${apiHost}"${querystring.stringify(jqlParams)}"`;
const apiUrl = `${apiHost}"${jqlParams.toString()}"`;

console.log(`My JQL api call is: ${apiUrl}`);
32 changes: 30 additions & 2 deletions examples/003/parse-querystring.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
/* eslint-disable linebreak-style */
/* global URL, */

const querystring = require('querystring');
const apiUrl = 'http://www.opencanvas.co.uk?myName=Shaun&myAge=28&comment=Yes+I+am+getting+old';
const parsedUrl = querystring.parse(apiUrl.substring(apiUrl.indexOf('?') + 1));

console.log(parsedUrl);

// console.log(parsedUrl);

// console.log(`href: ${apiUrl.href}`);

// console.log(`host: ${apiUrl.host}`);

// console.log(`hostname: ${apiUrl.hostname}`);

// console.log(`path: ${apiUrl.pathname}`);

// console.log(`port: ${apiUrl.port}`);

// console.log(`querystring: ${apiUrl.search}`);

// console.log(`JSON: ${JSON.stringify(apiUrl)}`);

// const querystring = apiUrl.search;

// console.log(`params: ${querystring}`);

const url = 'http://www.opencanvas.co.uk?myName=Shaun&myAge=28&comment=Yes+I+am+getting+old';
const parsedUrl = querystring.parse(url.substring(url.indexOf('?') + 1));
// const querystring = require('querystring');
// const url = 'http://www.opencanvas.co.uk?myName=Shaun&myAge=28&comment=Yes+I+am+getting+old';
// const parsedUrl = querystring.parse(url.substring(url.indexOf('?') + 1));

console.log(`Hi my name is ${parsedUrl.myName}`);
console.log(`I am ${parsedUrl.myAge}`);
Expand Down
5 changes: 3 additions & 2 deletions examples/004/url.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable linebreak-style */
/* eslint-disable import/newline-after-import */
const url = require('url');

const args = process.argv.slice(2);
const [urlEntered] = args;

if (urlEntered === undefined) {
console.error('Please pass a URL e.g. https://www.google.co.uk/search?q=stranger+things');
console.error('Please pass an URL, e.g., https://www.google.co.za/search?q=stranger+things');
process.exit(0);
}

Expand Down
11 changes: 9 additions & 2 deletions examples/005/os.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
/* eslint-disable linebreak-style */
/* eslint-disable no-unused-vars */
/* The os module allows us to query hardware specifications of our computer */
const os = require('os');

const homeDirectory = os.homedir();

console.log(`Your home directory is: ${homeDirectory}`);

/* You can identify your OS platform with the os.platform function */
const osPlatform = os.platform();
console.log(`The OS platform is: ${osPlatform}`);

console.log(`Your operating system platform is: ${osPlatform}`);

/* Identify CPU specs with the os.cpus function */
const cpuCores = os.cpus();
const coreCount = cpuCores.length;
const cpuModel = cpuCores[0].model;

console.log(`I can see your ${cpuModel} has ${coreCount} cores.`);
console.log(`Your ${cpuModel} has ${coreCount} cores.`);
13 changes: 5 additions & 8 deletions examples/006/open.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
/* eslint-disable linebreak-style */
const { platform } = require('os');

const { exec } = require('child_process');

const WINDOWS_PLATFORM = 'win32';

const osPlatform = platform();
const args = process.argv.slice(2);
const [url] = args;

if (url === undefined) {
console.error('Please enter a URL, e.g. "http://www.opencanvas.co.uk"');
console.log('Please enter an URL, e.g., "http://www.opencanvas.co.uk"');
process.exit(0);
}

let command;

if (osPlatform === WINDOWS_PLATFORM) {
command = `start microsoft-edge:${url}`;
command = `Start Microsoft-Edge:${url}`;
} else {
command = `open -a "Google Chrome" ${url}`;
}

console.log(`executing command: ${command}`);

console.log(`Executing command: ${command}`);
exec(command);
4 changes: 3 additions & 1 deletion examples/007/helpers/write-json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const fs = require('fs');

const JSON_WHITESPACE = 4;

const writeJson = (file, contents) => new Promise((resolve, reject) => {
fs.writeFile(file, JSON.stringify(contents, null, 4), (err) => {
fs.writeFile(file, JSON.stringify(contents, null, JSON_WHITESPACE), (err) => {
if (err) {
reject(err);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/012/new-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ shell.exec(`git checkout ${baseBranch}`);
// Making sure we have the latest changes from the remote origin
shell.exec(`git pull origin ${baseBranch}`);

// prompt for the ticket number
// prompt for the ticket ID
const ticketId = readLineSync.question('What is the ticket ID? ', {
limit: input => input.trim().length > 0,
limitMessage: 'Please enter a ticket number (e.g. GOT-123)'
limitMessage: 'Please enter a ticket ID (e.g. GOT-123)'
});

// Create a new branch
Expand Down
1 change: 0 additions & 1 deletion examples/020/releases/a-test/game.json

This file was deleted.

16 changes: 0 additions & 16 deletions examples/020/releases/a-test/index.html

This file was deleted.

1 change: 0 additions & 1 deletion examples/020/releases/blue-green-game/game.json

This file was deleted.

16 changes: 0 additions & 16 deletions examples/020/releases/blue-green-game/index.html

This file was deleted.

1 change: 0 additions & 1 deletion examples/020/releases/example/game.json

This file was deleted.

16 changes: 0 additions & 16 deletions examples/020/releases/example/index.html

This file was deleted.

1 change: 0 additions & 1 deletion examples/020/releases/shaun-the-sheep/game.json

This file was deleted.

16 changes: 0 additions & 16 deletions examples/020/releases/shaun-the-sheep/index.html

This file was deleted.

4 changes: 2 additions & 2 deletions examples/020/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{TEMPLATE}</title>
<title>Ball Paddle Game</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; display: block; margin: 0 auto; }
Expand All @@ -13,4 +13,4 @@
<canvas id="game" width="480" height="320"></canvas>
<script type="text/javascript" src="../../core/game-1.0.0.js"></script>
</body>
</html>
</html>
Loading