Skip to content

Commit

Permalink
Merge pull request #12 from stanislavt/refactor/cli
Browse files Browse the repository at this point in the history
Add bin script and do code refactoring
  • Loading branch information
stanislavt authored Aug 23, 2018
2 parents 31e5905 + c6ffe4c commit 5ed53d1
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 79 deletions.
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
#ATB Plastic bag
# ATB Plastic bag

<img src="http://www.comserv.com.ua/images/stories/klienty/12.jpg" />
![Plastic bag](http://www.comserv.com.ua/images/stories/klienty/12.jpg)

ATB Plastic Bag likes to travel around the world and take a lot of photos. The next photo will be taken in your project. So, how to do it:
## Installation:

ATB Plactic Bag likes to travel around the world.
And we bring it to your project
just run
```
npm install atbmarket --save
```
and add to your entry JS script:
```
require('atbmarket')();
npm install atbmarket -g
```

ATB has solutions for all situations:
## Add to your entry JS script:

```
require('atbmarket')('big');
require('atbmarket')('little');
require('atbmarket')(); //render default size bag
const Provision = require('atbmarket');
const bag = new Provision();
bag.draw() // Draw the bag in terminal
bag.caption() // Show a caption in terminal
bag.chalk() // Show both bag and caption in terminal
```

## Or just run it in terminal:

`atbmarket` draws plastic bag in terminal.

`atbmarket --help` shows more details.

### Options:

```
-V, --version output the version number
-S, --size <size> Choose size (default: medium)
--no-colorized [value] Is supposed to be not colorized?
-h, --help output usage information
```

Enjoy and show ATB the world of your production!
16 changes: 16 additions & 0 deletions bin/atbmarket
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node

const program = require('commander');
const Provision = require('../lib/bags/provision');
const { version } = require('../package');

program
.version(version)
.option('-S, --size <size>', 'Choose size', /^(large|medium|small)$/i, 'medium')
.option('--no-colorized [value]', 'Is supposed to be not colorized?')
.parse(process.argv);

const { size, colorized } = program;

const factory = new Provision({ size, colorized });
factory.chalk()
41 changes: 41 additions & 0 deletions lib/bags/bag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const chalk = require('chalk');

module.exports = class Bag {
/**
* @param {object} options
* @param {boolean=true} options.colorized
*/
constructor({ colorized = true }) {
this.colorized = colorized;
}

/**
* Print the bag with capture
*/
chalk(){
this.draw().caption();
}

/**
* Set colors of the bag
*/
get colors() {
if (this.colorized) {
const { bgWhite, bgBlue, white: tWhite, blue: tBlue, magenta: tRed } = chalk;

return { bgWhite, bgBlue, tWhite, tBlue, tRed };
} else {
const { bgWhite, bgBlackBright: bgBlue, white: tWhite, black: tBlue, black: tRed } = chalk;

return { bgWhite, bgBlue, tWhite, tBlue, tRed };
}
}

set colorized(value) {
this._colorized = Boolean(value);
}

get colorized() {
return this._colorized;
}
};
50 changes: 50 additions & 0 deletions lib/bags/large.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const Bag = require('./bag')

module.exports = class MediumBag extends Bag {
/**
* @param {object} options
* @param {boolean=true} options.colorized
*/
constructor({ colorized = true }) {
super({ colorized })
}

/**
* Draw the package in console
*/
draw() {
const { bgWhite, bgBlue, tWhite, tBlue, tRed } = this.colors;

console.log(`
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')}
${bgWhite(' ' + tBlue('▥▥▥') + ' ')}
${bgWhite(' ')}
${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('᛫¯¯᨞') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('⁄ ') + tRed('АТБ') + tWhite(' ⁄') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('᨞__᛫') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}
${bgWhite(' ')}
`);

return this;
}

caption() {
console.log('Even an ATB market plastic bag (L size) was there... What about you?!')

return this;
}
};
43 changes: 43 additions & 0 deletions lib/bags/medium.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const Bag = require('./bag')

module.exports = class MediumBag extends Bag {
/**
* @param {object} options
* @param {boolean=true} options.colorized
*/
constructor({ colorized = true }) {
super({ colorized })
}

/**
* Draw the package in console
*/
draw() {
const { bgWhite, bgBlue, tWhite, tBlue, tRed } = this.colors;

console.log(`
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ' + tBlue('▥▥') + ' ')}
${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('᛫¯¯᨞') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('⁄ ') + tRed('АТБ') + tWhite(' ⁄') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('᨞__᛫') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}
`);

return this;
}

caption() {
console.log('Even an ATB market plastic bag (M size) was there... What about you?!')

return this;
}
};
51 changes: 51 additions & 0 deletions lib/bags/provision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const SIZE = require('./size');

module.exports = class Provision {
/**
* Create a new instance of plastic bag
* @param {object} options
* @param {string='middle'} options.size
* @param {boolean=true} options.colorized
*/
constructor({ size = SIZE.MIDDLE, colorized = true } = {}) {
this.registerBags();
this.size = size;
this.colorized = colorized;

return new this.currentBag({ colorized: this.colorized });
}

registerBags() {
const sizes = [
[SIZE.SMALL, require('./small')],
[SIZE.MEDIUM, require('./medium')],
[SIZE.LARGE, require('./large')],
];

this._bags = new Map(sizes);
}

set colorized(value) {
this._colorized = Boolean(value);
}

get colorized() {
return this._colorized;
}

set size(value) {
this._size = value;
}

get size() {
return this._size;
}

get currentBag() {
if (this._bags.has(this.size)) {
return this._bags.get(this.size);
}

return this._bags.get(SIZE.MEDIUM);
}
};
5 changes: 5 additions & 0 deletions lib/bags/size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
SMALL: 'small',
MEDIUM: 'medium',
LARGE: 'large'
};
40 changes: 40 additions & 0 deletions lib/bags/small.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Bag = require('./bag')

module.exports = class SmallBag extends Bag {
/**
* @param {object} options
* @param {boolean=true} options.colorized
*/
constructor({ colorized = true }) {
super({ colorized })
}

/**
* Draw the package in console
*/
draw() {
const { bgWhite, bgBlue, tWhite, tBlue, tRed } = this.colors;

console.log(`
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ')} ${bgWhite(' ')}
${bgWhite(' ' + tBlue('▥') + ' ')}
${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('᛫¯¯᨞') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('⁄ ') + tRed('АТБ') + tWhite(' ⁄') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ' + tWhite('᨞__᛫') + ' ')}${bgWhite(' ')}
${bgWhite(' ')}${bgBlue(' ')}${bgWhite(' ')}
${bgWhite(' ')}
`);

return this;
}

caption() {
console.log('Even an ATB market plastic bag (S size) was there... What about you?!');

return this;
}
};
59 changes: 1 addition & 58 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1 @@
'use strict';

const chalk = require('chalk');

let init = function (size) {
switch (size) {
case 'little':
littleSize();
break;
case 'big':
bigSize();
break;
default:
middleSize();
break;
}
};

function littleSize() {
console.info(`
${chalk.bgWhite(' ')} ${chalk.bgWhite(' ')}
${chalk.bgWhite(' ')} ${chalk.bgWhite(' ' + chalk.black('▥') + ' ')}
${chalk.bgWhite(' ' + chalk.blue('-') + ' ')}
${chalk.bgWhite(' ' + chalk.blue('(') + chalk.red('ATB') + chalk.blue(')') + ' ')}
${chalk.bgWhite(' ' + chalk.blue('-') + ' ')}
${chalk.bgWhite(' ')}
${chalk.blue('Even an ATB market plastic bag (L) was there... What about you?!')}
`);
}

function middleSize() {
console.info(`
${chalk.bgWhite(' ')} ${chalk.bgWhite(' ')}
${chalk.bgWhite(' ')} ${chalk.bgWhite(' ')}
${chalk.bgWhite(' ')} ${chalk.bgWhite(' ' + chalk.black('▥▥') + ' ')}
${chalk.bgWhite(' ' + chalk.blue('-') + ' ')}
${chalk.bgWhite(' ' + chalk.blue('(') + chalk.red('ATB') + chalk.blue(')') + ' ')}
${chalk.bgWhite(' ' + chalk.blue('-') + ' ')}
${chalk.bgWhite(' ')}
${chalk.blue('Even an ATB market plastic bag (M) was there... What about you?! ')}
`);
}

function bigSize() {
console.info(`
${chalk.bgWhite(' ')} ${chalk.bgWhite(' ')}
${chalk.bgWhite(' ')} ${chalk.bgWhite(' ')}
${chalk.bgWhite(' ')} ${chalk.bgWhite(' ' + chalk.black('▥▥▥') + ' ')}
${chalk.bgWhite(' ')}
${chalk.bgWhite(' ' + chalk.blue('-') + ' ')}
${chalk.bgWhite(' ' + chalk.blue('(') + chalk.red('ATB') + chalk.blue(')') + ' ')}
${chalk.bgWhite(' ' + chalk.blue('-') + ' ')}
${chalk.bgWhite(' ')}
${chalk.blue('Even an ATB market plastic bag (XL) was there... What about you?!')}
`);
}

module.exports = init;
module.exports = require('./bags/provision');
Loading

0 comments on commit 5ed53d1

Please sign in to comment.