-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_manager.js
161 lines (126 loc) · 3.26 KB
/
file_manager.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import inquirer from 'inquirer';
import clear from 'clear-console';
import chalk from 'chalk';
import * as fs from 'fs';
import { exit } from 'process';
function makeFolder() {
inquirer
.prompt(
[{
type: 'text',
message: 'Folder Name',
name: 'name',
}]
)
.then((answers) => {
fs.mkdir(`${answers.name}`, (err) => {
if (err) {
console.log(err);
} else {
console.log(chalk.green('Folder Created Successfully'));
}
})
})
.catch((error) => {
if (error.isTtyError) {
console.log(chalk.red('Prompt couldn\'t be rendered in the current environment'));
} else {
console.log(chalk.red('Something went wrong'));
}
});
}
function makeFile() {
inquirer
.prompt(
[{
type: 'text',
message: 'File Name',
name: 'name',
}]
)
.then((answers1) => {
console.log(`${answers1.name}`);
inquirer
.prompt(
[{
type: 'text',
message: 'Content',
name: 'content',
}]
).then((answers2) => {
fs.writeFile(`${answers1.name}`, answers2.content, (err) => {
if (err) {
console.log(err);
} else {
console.log(chalk.green('File Created Successfully'));
}
})
});
})
.catch((error) => {
if (error.isTtyError) {
console.log(chalk.red('Prompt couldn\'t be rendered in the current environment'));
} else {
console.log(chalk.red('Something went wrong'));
}
});
}
function readFile() {
inquirer
.prompt(
[{
type: 'text',
message: 'File Name',
name: 'name',
}]
)
.then((answers) => {
fs.readFile(`${answers.name}`, { encoding: 'utf-8' }, (err, data) => {
if (err) {
console.log(err);
} else {
const structDatas = [
{ Content: data },
];
console.table(structDatas);
}
})
})
.catch((error) => {
if (error.isTtyError) {
console.log(chalk.red('Prompt couldn\'t be rendered in the current environment'));
} else {
console.log(chalk.red('Something went wrong'));
}
});
}
clear();
console.log(chalk.green('WELCOME TO FILE MANAGER'))
const choices = ['Add New Folder', 'Add New File', 'Read File', chalk.red('Exit')];
inquirer
.prompt([
{
type: 'list',
name: 'action',
message: chalk.white('What do you want?'),
choices: choices,
},
])
.then(answers => {
switch (answers.action) {
case choices[0]:
makeFolder();
break;
case choices[1]:
makeFile();
break;
case choices[2]:
readFile();
break;
case choices[3]:
clear();
exit();
default:
break;
}
});