forked from gSchool/fs-pet-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pets.js
executable file
·112 lines (99 loc) · 2.84 KB
/
pets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
'use strict'
const fs = require('fs');
const path = require('path');
const petsPath = path.join('./', 'pets.json');
const node = path.basename(process.argv[0]);
const file = path.basename(process.argv[1]);
const cmd = process.argv[2];
if (cmd === 'read'){
fs.readFile(petsPath, 'utf8', function (err, data){
if (err){
console.error('Shit done fucked up son');
process.exit(1);
}
const pets = JSON.parse(data);
const pet = process.argv[3];
if (!pet) {
console.log(pets);
} else if (pets[pet] === undefined){
console.error(`Usage: ${node} ${file} read PET`);
} else if (pet) {
console.log(pets[pet]);
}
})
} else if (cmd === 'create') {
fs.readFile(petsPath, 'utf8', function(readErr, data) {
if (readErr) {
console.error('Shit done fucked up son');
process.exit(1);
}
const pets = JSON.parse(data);
const age = parseInt(process.argv[3]);
const kind = process.argv[4];
const name = process.argv[5];
if (!age || !kind || !name) {
console.error(`Usage: ${node} ${file} ${cmd} AGE KIND NAME`);
process.exit(1);
}
const pet = {'age': age, 'kind': kind, 'name': name};
pets.push(pet);
const petsJSON = JSON.stringify(pets);
fs.writeFile(petsPath, petsJSON, function(writeErr) {
if (writeErr) {
throw writeErr;
}
console.log(pet);
});
});
} else if (cmd === 'update') {
fs.readFile(petsPath, 'utf8', function(readErr, data) {
if (readErr) {
console.error('Shit done fucked up son');
process.exit(1);
}
const pets = JSON.parse(data);
const index = parseInt(process.argv[3]);
const age = parseInt(process.argv[4]);
const kind = process.argv[5];
const name = process.argv[6];
if (!index || !age || !kind || !name) {
console.error(`Usage: ${node} ${file} ${cmd} INDEX AGE KIND NAME`);
process.exit(1);
}
const pet = {'age': age, 'kind': kind, 'name': name};
pets[index] = pet;
const petsJSON = JSON.stringify(pets);
fs.writeFile(petsPath, petsJSON, function(writeErr) {
if (writeErr) {
throw writeErr;
}
console.log(pet);
});
});
} else if (cmd === 'destroy') {
fs.readFile(petsPath, 'utf8', function(readErr, data) {
if (readErr) {
console.error('Shit done fucked up son');
process.exit(1);
}
const pets = JSON.parse(data);
const index = parseInt(process.argv[3]);
if (!index) {
console.error(`Usage: ${node} ${file} ${cmd} INDEX`);
process.exit(1);
}
const destroyedPet = pets[index];
pets.splice(index,1);
const petsJSON = JSON.stringify(pets);
fs.writeFile(petsPath, petsJSON, function(writeErr) {
if (writeErr) {
throw writeErr;
}
console.log(destroyedPet);
});
});
} else {
console.error(`Usage: ${node} ${file} [read | create | update | destroy]`);
process.exit(1);
}