-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
118 lines (106 loc) · 4.01 KB
/
main.cpp
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
#include <cstring>
#include <iostream>
#include "./commands/commands.hpp"
#include "./main/minify.hpp"
using namespace std;
bool mini(char* inputFileName , char* outputFileName);
bool mini(char* inputFileName);
int main(int argc , char** argv) {
using namespace colors;
// Single argument passed
if (argc == 1) {
cout << yellow << "Please specify the CSS file" << '\n' << reset;
}
// The second argument is either the css file name or a flag
else if (argc == 2) {
// example : mini input.css
char* secondArg = argv[1];
// Check if it is a command
if (strlen(secondArg) > 2 && secondArg[0] == '-' && secondArg[1] == '-') {
executeCommand(secondArg);
} else {
// Minify CSS
mini(secondArg);
}
} else if (argc == 3) {
// example : mini input.css output.css
char* secondArg = argv[1];
char* thirdArg = argv[2];
if (strlen(secondArg) > 2 && secondArg[0] == '-' && secondArg[1] == '-') {
executeCommand(secondArg);
} else {
// Minify CSS
mini(secondArg , thirdArg);
}
} else if (argc == 4) {
// example : mini input.css -o output.css
char* secondArg = argv[1];
char* thirdArg = argv[2];
char* fourthArg = argv[3];
if (strlen(secondArg) > 2 && secondArg[0] == '-' && secondArg[1] == '-') {
executeCommand(secondArg);
} else if (thirdArg[0] == '-' && thirdArg[1] == 'o') {
// Specifying the output file
mini(secondArg , fourthArg);
} else {
std::cout << red << "Error!!!!\nInvalid arguments" << reset;
std::cout << bright_blue << "\nWrite `mini input.css -o output.css` to minify" << '\n' << reset;
}
} else {
cout << yellow << "Too many arguments" << '\n' << reset;
}
return 0;
}
bool mini(char* inputFileName) {
using namespace colors;
size_t len = strlen(inputFileName);
char* outputFileName = new char[len + 6];
// Check if the file name is valid
if (len >= 4 && inputFileName[len - 1] == 's' && inputFileName[len - 2] == 's' && inputFileName[len - 3] == 'c' && inputFileName[len - 4] == '.') {
// The file name is valid
// Make it fileName.mini.css
for (size_t i = 0; i < len - 4; i++) {
outputFileName[i] = inputFileName[i];
}
outputFileName[len - 4] = '.';
outputFileName[len - 3] = 'm';
outputFileName[len - 2] = 'i';
outputFileName[len - 1] = 'n';
outputFileName[len] = 'i';
outputFileName[len + 1] = '.';
outputFileName[len + 2] = 'c';
outputFileName[len + 3] = 's';
outputFileName[len + 4] = 's';
outputFileName[len + 5] = '\0';
} else {
cout << bright_magenta << "Warning : Invalid file name" << '\n' << reset;
cout << "Maybe it is not a CSS file. Still trying to minify... " << '\n';
// Make it fileName.ext.mini.css
outputFileName = new char[len + 9];
for (size_t i = 0; i < len; i++) {
outputFileName[i] = inputFileName[i];
}
outputFileName[len] = '.';
outputFileName[len + 1] = 'm';
outputFileName[len + 2] = 'i';
outputFileName[len + 3] = 'n';
outputFileName[len + 4] = '.';
outputFileName[len + 5] = 'c';
outputFileName[len + 6] = 's';
outputFileName[len + 7] = 's';
outputFileName[len + 8] = '\0';
}
return mini(inputFileName , outputFileName);
}
bool mini(char* inputFileName , char* outputFileName) {
using namespace colors;
cout << "Minifying: " << bright_blue << inputFileName << '\n' << reset;
bool isMinified = minify(inputFileName , outputFileName);
if (isMinified) {
cout << "Done: " << bright_green << outputFileName << '\n' << reset;
cout << bright_red <<"Mini" << bright_blue << " :)" << '\n' << reset;
} else {
// cout << "Error: Minification Failed!" << '\n';
}
return true;
}