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

cli get random face when no command provided #13

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ console.log(facetxt.list);
```
### CLI (Command line)

#### Install

`$ npm install -g facetxt`

#### Usage

`$ facetxt [commands]`

```
Commands:
version toutput the version number
Expand All @@ -83,12 +89,15 @@ Commands:
all get an array with all faces
like get one face by name; if more than one, will be return randomly
likes get an array of faces by name

Note: all commands also supports "--" e.g. $ facetext --version
```

```bash
# get one random face
$ facetxt rand
$ facetxt
(°レ°)
# same as: "$ facetxt rand"

# get one random face with description
$ facetxt randDesc
Expand Down
22 changes: 17 additions & 5 deletions bin/facetxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ const pckg = JSON.parse(

const usage = `${pckg.name}@${pckg.version} with <3 by char@omg.lol\n` +
`Commands:\n${
'\tversion'.padEnd(12) }toutput the version number\n${
'\tversion'.padEnd(12) }output the version number\n${
'\thelp'.padEnd(12) }display this text\n\n${
'\trand'.padEnd(12) }get a random face\n${
'\trandDesc'.padEnd(12) }get a random face with description\n${
'\tlist'.padEnd(12) }pretty prints all the faces\n${
'\tall'.padEnd(12) }get an array with all faces\n${
'\tlike'.padEnd(12) }get one face by name; if more than one, will` +
` be return randomly\n${
'\tlikes'.padEnd(12) }get an array of faces by name\n`;
'\tlikes'.padEnd(12) }get an array of faces by name\n\n` +
'Note: all commands also supports "--" e.g. $ facetext --version';
const UNKNOWN_COMMAND_FACE = 'shrug';
const NOT_FOUND_FACE = 'sad';
const unknownCommand = 'Unknown command: ' +
Expand All @@ -30,24 +31,33 @@ const faceNotFound = 'Could not find a face like: ' +

/* eslint no-console: "off" */
(() => {
if (process.argv.length === 2) {
console.log(facetxt.rand);
}
if (process.argv.length === 3) {
switch (process.argv[2]) {
case 'help':
case '--help':
console.log(usage);
break;
case 'version':
case '--version':
console.log(`${pckg.name}@${pckg.version}`);
break;
case 'list':
case '--list':
console.log(facetxt.list);
break;
case 'all':
case '--all':
console.log(facetxt.all);
break;
case 'rand':
case '--rand':
console.log(facetxt.rand);
break;
case 'randDesc': {
case 'randDesc':
case '--randDesc': {
const randFace = facetxt.randDesc;
console.log(`${randFace.face} --${randFace.description}--`);
break;
Expand All @@ -58,7 +68,8 @@ const faceNotFound = 'Could not find a face like: ' +
}
if (process.argv.length === 4) {
switch (process.argv[2]) {
case 'like': {
case 'like':
case '--like': {
const faceLike = facetxt.like(process.argv[3]);
if (typeof faceLike === 'undefined') {
console.log(faceNotFound);
Expand All @@ -67,7 +78,8 @@ const faceNotFound = 'Could not find a face like: ' +
}
break;
}
case 'likes': {
case 'likes':
case '--likes': {
const facesLike = facetxt.likes(process.argv[3]);
if (!facesLike.length) {
console.log(faceNotFound);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "facetxt",
"version": "1.2.0",
"version": "1.3.0",
"description": "",
"main": "./src/index.js",
"type": "module",
Expand Down
29 changes: 28 additions & 1 deletion tests/facetxt-cli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@
run ./bin/facetxt.js rand
[ "${#output}" -gt 0 ]
[ "${output}" != "undefined" ]
run ./bin/facetxt.js --rand
[ "${#output}" -gt 0 ]
[ "${output}" != "undefined" ]
run ./bin/facetxt.js
[ "${#output}" -gt 0 ]
[ "${output}" != "undefined" ]
}

@test "should retrieve a random face with description" {
description=$(./bin/facetxt.js randDesc | sed 's/--//g' | awk '{print $NF}');
found=$(grep $description src/faces.js)
[ "${#found}" -gt 0 ]
description=$(./bin/facetxt.js --randDesc | sed 's/--//g' | awk '{print $NF}');
found=$(grep $description src/faces.js)
[ "${#found}" -gt 0 ]
}

@test "should retrieve an array with all faces" {
run ./bin/facetxt.js all
[ "${#output}" -gt 0 ]
[ "${output}" != "undefined" ]
run ./bin/facetxt.js --all
[ "${#output}" -gt 0 ]
[ "${output}" != "undefined" ]
}

@test "should retrieve a pretty print of facess" {
Expand All @@ -24,12 +36,20 @@
[ $(expr "${lines[1]}" : ".*sideways latin only emoticons:") -ne 0 ]
[ $(expr "${lines[2]}" : ".*smiley happy face:") -ne 0 ]
[ $(expr "${lines[3]}" : ".*:‑) :) :-] :] :-> :> 8-) 8) :-} :} :o) :c) :^) =] =)") -ne 0 ]
run ./bin/facetxt.js --list
[ "${lines[0]}" = "western:" ]
[ $(expr "${lines[1]}" : ".*sideways latin only emoticons:") -ne 0 ]
[ $(expr "${lines[2]}" : ".*smiley happy face:") -ne 0 ]
[ $(expr "${lines[3]}" : ".*:‑) :) :-] :] :-> :> 8-) 8) :-} :} :o) :c) :^) =] =)") -ne 0 ]
}

@test "should retrieve a cthulhu like face" {
run ./bin/facetxt.js like cthulhu
[ "$output" == "^(;,;)^" ]
[ "${output}" != "undefined" ]
run ./bin/facetxt.js --like cthulhu
[ "$output" == "^(;,;)^" ]
[ "${output}" != "undefined" ]
}

@test "should not retrieve a foobar like face, since does not exists" {
Expand All @@ -42,6 +62,9 @@
run ./bin/facetxt.js likes cthulhu
[ "$output" == "[ '^(;,;)^' ]" ]
[ "${output}" != "undefined" ]
run ./bin/facetxt.js --likes cthulhu
[ "$output" == "[ '^(;,;)^' ]" ]
[ "${output}" != "undefined" ]
}

@test "should not retrieve a foobar likes face, since does not exists" {
Expand All @@ -54,14 +77,16 @@
run ./bin/facetxt.js help
[ $(expr "${lines[0]}" : "facetxt@.* with <3 by char@omg.lol") -ne 0 ]
[ "${lines[1]}" = "Commands:" ]
[ $(expr "${lines[2]}" : ".*version.*toutput the version number") -ne 0 ]
[ $(expr "${lines[2]}" : ".*version.*output the version number") -ne 0 ]
[ $(expr "${lines[3]}" : ".*help.*display this text") -ne 0 ]
[ $(expr "${lines[4]}" : ".*rand.*get a random face") -ne 0 ]
[ $(expr "${lines[5]}" : ".*randDesc.*get a random face with description") -ne 0 ]
[ $(expr "${lines[6]}" : ".*list.*pretty prints all the faces") -ne 0 ]
[ $(expr "${lines[7]}" : ".*all.*get an array with all faces") -ne 0 ]
[ $(expr "${lines[8]}" : ".*like.*get one face by name; if more than one, will be return randomly") -ne 0 ]
[ $(expr "${lines[9]}" : ".*likes.*get an array of faces by name") -ne 0 ]
run ./bin/facetxt.js --help
[ $(expr "${lines[0]}" : "facetxt@.* with <3 by char@omg.lol") -ne 0 ]
}

@test "should retrieve help on unknown command" {
Expand All @@ -73,6 +98,8 @@
}

@test "should retrieve version" {
run ./bin/facetxt.js --version
[ $(expr "${lines[0]}" : "facetxt@.*") -ne 0 ]
run ./bin/facetxt.js version
[ $(expr "${lines[0]}" : "facetxt@.*") -ne 0 ]
}
Loading