Skip to content

Commit d33d6a4

Browse files
authored
Merge pull request #1 from Saamu192/develop
Develop
2 parents 3a6d904 + 5c6da65 commit d33d6a4

File tree

9 files changed

+180
-32
lines changed

9 files changed

+180
-32
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules
2-
yarn.lock
2+
yarn.lock
3+
coverage
4+
.nyc_output
5+
.reify-cache

.nycrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"branches": 100,
3+
"lines": 100,
4+
"functions": 100,
5+
"statements": 100,
6+
"checkCoverage": true,
7+
"reporter": ["html", "text"]
8+
}

database.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
[
22
{
33
"id": 1,
4-
"vehicles": ["Motocicleta", "Carro", "Caminhão"],
4+
"vehicles": [
5+
"Motocicleta",
6+
"Carro",
7+
"Caminhão"
8+
],
59
"kmTraveled": 10000,
610
"from": "2009-01-01",
711
"to": "2020-11-26"
12+
},
13+
{
14+
"id": "2",
15+
"vehicles": [
16+
"aviao"
17+
],
18+
"kmTraveled": "2000",
19+
"from": "2022-02-02",
20+
"to": "2022-06-06"
821
}
9-
]
22+
]

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
"author": "Saamu192 <samuelpr192@gmail.com>",
77
"license": "MIT",
88
"scripts": {
9-
"dev": "npx nodemon --ignore database.json --exec node --experimental-json-modules src/index.js"
9+
"dev": "npx nodemon --ignore database.json --exec node --experimental-json-modules --experimental-top-level-await src/index.js",
10+
"test": "npx mocha -w --parallel test/*.test.js",
11+
"test:cov": "npx nyc npx mocha -r reify --parallel test/*.test.js"
1012
},
1113
"dependencies": {
1214
"chalk": "^5.1.2",
1315
"chalk-table": "^1.0.2",
1416
"draftlog": "^1.0.13"
1517
},
1618
"devDependencies": {
17-
"nodemon": "^2.0.20"
19+
"chai": "^4.2.0",
20+
"mocha": "^8.2.1",
21+
"nodemon": "^2.0.6",
22+
"nyc": "^15.1.0",
23+
"reify": "^0.20.12",
24+
"sinon": "^9.2.1"
1825
}
1926
}

src/index.js

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
import DraftLog from "draftlog";
2-
import chalk from "chalk";
3-
import chalkTable from "chalk-table";
4-
import readline from "readline";
51
import database from "./../database.json" assert { type: "json" };
62
import Person from "./persons.js";
3+
import { save } from "./repository.js";
4+
import TerminalController from "./terminal.controller.js";
75

8-
DraftLog(console).addLineListener(process.stdin);
6+
const DEFAULT_LANG = "pt-BR";
7+
const STOP_TERM = ":q";
98

10-
const options = {
11-
leftPad: 2,
12-
columns: [
13-
{ field: "id", name: chalk.cyan("ID") },
14-
{ field: "vehicles", name: chalk.magenta("vehicles") },
15-
{ field: "kmTraveled", name: chalk.green("km Traveled") },
16-
{ field: "from", name: chalk.cyan("From") },
17-
{ field: "to", name: chalk.cyan("To") },
18-
],
19-
};
9+
const terminalController = new TerminalController();
10+
terminalController.initializeTerminal(database, DEFAULT_LANG);
2011

21-
const table = chalkTable(
22-
options,
23-
database.map((element) => new Person(element).formatted())
24-
);
25-
const print = console.draft(table);
12+
async function mainLoop() {
13+
try {
14+
const answer = await terminalController.question();
15+
if (answer === STOP_TERM) {
16+
terminalController.closeTerminal();
17+
console.log("process finished!");
18+
return;
19+
}
20+
const person = Person.generateInstanceFromString(answer);
21+
terminalController.updateTable(person.formatted(DEFAULT_LANG));
22+
await save(person);
2623

27-
const terminal = readline.createInterface({
28-
input: process.stdin,
29-
output: process.stdout,
30-
});
24+
return mainLoop();
25+
} catch (error) {
26+
console.error("DEU RUIM**", error);
27+
return mainLoop();
28+
}
29+
}
3130

32-
// terminal.question("Qual é o seu nome?", (msg) => {
33-
// console.log("msg", msg.toString());
34-
// });
31+
await mainLoop();

src/persons.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ export default class Person {
3434
}).format(mapDate(this.to)),
3535
};
3636
}
37+
38+
static generateInstanceFromString(text) {
39+
const EMPTY_SPACE = " ";
40+
const [id, vehicles, kmTraveled, from, to] = text.split(EMPTY_SPACE);
41+
const person = new Person({
42+
id,
43+
from,
44+
kmTraveled,
45+
to,
46+
vehicles: vehicles.split(","),
47+
});
48+
return person;
49+
}
3750
}

src/repository.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { writeFile, readFile } from "fs/promises";
2+
3+
export const save = async (data) => {
4+
const currentData = JSON.parse(await readFile("./database.json"));
5+
currentData.push(data);
6+
7+
await writeFile("./database.json", JSON.stringify(currentData, null, 2));
8+
};

src/terminal.controller.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import DraftLog from "draftlog";
2+
import chalk from "chalk";
3+
import chalkTable from "chalk-table";
4+
import readline from "readline";
5+
import Person from "./persons.js";
6+
7+
export default class TerminalController {
8+
constructor() {
9+
this.print = {};
10+
this.data = {};
11+
}
12+
13+
initializeTerminal(database, language) {
14+
DraftLog(console).addLineListener(process.stdin);
15+
this.terminal = readline.createInterface({
16+
input: process.stdin,
17+
output: process.stdout,
18+
});
19+
20+
this.initializeTable(database, language);
21+
}
22+
23+
initializeTable(database, language) {
24+
const data = database.map((element) =>
25+
new Person(element).formatted(language)
26+
);
27+
const table = chalkTable(this.getTableOptions(), data);
28+
this.print = console.draft(table);
29+
this.data = data;
30+
}
31+
32+
updateTable(item) {
33+
this.data.push(item);
34+
this.print(chalkTable(this.getTableOptions(), this.data));
35+
}
36+
37+
question(msg = "") {
38+
return new Promise((resolve) => this.terminal.question(msg, resolve));
39+
}
40+
41+
closeTerminal() {
42+
this.terminal.close();
43+
}
44+
45+
getTableOptions() {
46+
return {
47+
leftPad: 2,
48+
columns: [
49+
{ field: "id", name: chalk.cyan("ID") },
50+
{ field: "vehicles", name: chalk.magenta("vehicles") },
51+
{ field: "kmTraveled", name: chalk.green("km Traveled") },
52+
{ field: "from", name: chalk.cyan("From") },
53+
{ field: "to", name: chalk.cyan("To") },
54+
],
55+
};
56+
}
57+
}

test/person.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import mocha from "mocha";
2+
const { describe, it } = mocha;
3+
import chai from "chai";
4+
const { expect } = chai;
5+
import Person from "../src/persons.js";
6+
7+
describe("Person", () => {
8+
it("should return a person instance from a string", () => {
9+
const person = Person.generateInstanceFromString(
10+
"1 Bike,Carro 20000 2020-01-01 2020-02-01"
11+
);
12+
const expected = {
13+
from: "2020-01-01",
14+
to: "2020-02-01",
15+
vehicles: ["Bike", "Carro"],
16+
kmTraveled: "20000",
17+
id: "1",
18+
};
19+
20+
expect(person).to.be.deep.equal(expected);
21+
});
22+
23+
it("should format values", () => {
24+
const person = new Person({
25+
from: "2020-01-01",
26+
to: "2020-02-01",
27+
vehicles: ["Bike", "Carro"],
28+
kmTraveled: "20000",
29+
id: "1",
30+
});
31+
32+
const result = person.formatted("pt-BR");
33+
const expected = {
34+
id: 1,
35+
vehicles: "Bike e Carro",
36+
kmTraveled: "20.000 km",
37+
from: "01 de janeiro de 2020",
38+
to: "01 de fevereiro de 2020",
39+
};
40+
expect(result).to.be.deep.equal(expected);
41+
});
42+
});

0 commit comments

Comments
 (0)