-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.js
183 lines (160 loc) · 5.51 KB
/
cli.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const TailwindConverter = require('./build/TailwindConverter');
const yargs = require('yargs');
const async = require('async');
const through = require('through2');
const split = require('split2');
const pumpify = require('pumpify');
const _ = require('lodash');
const fs = require('fs');
var path = require('path');
var argv = yargs
.usage( "Usage: tailwindo <path/to/directory/>" )
.command( "dir", "a file path/a folder path/Bootstrap CSS classes", { alias: "directory", type: "string" } )
.option( "r", { alias: "recursive", demand: false, describe: "Recursively convert a directory", type: "boolean"} )
.option( "e", { alias: "extensions", demand: false, describe: "Convert different file extensions", type: "string",example:'php,html' } )
.option( "p", { alias: "replace", demand: false, describe: "Overwrite the original files", type: "boolean" } )
.help( "?" )
.alias( "?", "help" )
.epilog( "By Riaz Ali Laskar" )
.argv;
const arg = argv._[ 0 ];
const converter = new TailwindConverter();
// List all files in a directory in Node.js recursively in a synchronous fashion
function walkSync (dir, filelist = []) {
fs.readdirSync(dir).forEach(file => {
var dirFile = path.join(dir, file);
try {
filelist = walkSync(dirFile, filelist);
}
catch (err) {
if (err.code === 'ENOTDIR' || err.code === 'EBUSY') filelist.push(dirFile);
else throw err;
}
});
return filelist;
}
/**
*
*/
if(fs.existsSync(arg) && fs.lstatSync(arg).isFile())
{
var input = fs.createReadStream(arg);
let filename = path.basename(arg);
let file_comp =arg.split('.');
let ext = file_comp.pop();
file_comp.push('tw')
file_comp.push(ext);
let out_file = file_comp.join('.');
let extensions = [];
if(argv.e)
{
extensions = argv.e.split(',')
if(!extensions.length)
{
console.error("Empty extension list");
process.exit();
}
}
var output = fs.createWriteStream(out_file);
input.pipe((function(){
return pumpify(split(), through.obj(function(line, enc, cb){
if(!line) return cb();
var twline = converter.setContent(line).convert().get()+"\n";;
if(!twline.trim()) return cb();
cb(null, twline);
}));
})()).pipe(output)
/**
* replace files
*/
if(argv.p)
{
output.on('finish',function(){
fs.unlink(arg,function(err){
if (err) throw err;
fs.rename(out_file,arg,function (err) {
if (err) throw err;
});
})
})
}
}
/**
*
*/
else if(fs.existsSync(arg) && fs.lstatSync(arg).isDirectory())
{
if(argv.r)
{
var filelist = walkSync(arg);
}
else
{
let extensions = [];
if(argv.e)
{
extensions = argv.e.split(',')
if(!extensions.length)
{
console.error("Empty extension list");
process.exit();
}
}
var filelist = [];
fs.readdirSync(arg).forEach(file => {
let file_path = path.join(arg, file);
if(fs.existsSync(file_path) && fs.lstatSync(file_path).isFile())
{
if(_.includes(extensions,path.extname(file_path)))
{
filelist.push(file_path);
}
}
});
}
if(!filelist.length){
console.log("No file with matching extensions found");
process.exit();
}
async.map(filelist, function(file){
var input = fs.createReadStream(file);
let file_comp = file.split('.');
let ext = file_comp.pop();
file_comp.push('tw')
file_comp.push(ext);
let out_file = file_comp.join('.');
var output = fs.createWriteStream(out_file);
input.pipe((function(){
return pumpify(split(), through.obj(function(line, enc, cb){
if(!line) return cb();
var twline = converter.setContent(line).convert().get()+"\n";
if(!twline.trim()) return cb();
cb(null, twline);
}));
})())
.pipe(output);
/**
* replace files
*/
if(argv.p)
{
output.on('finish',function(){
fs.unlink(file,function(err){
if (err) throw err;
fs.rename(out_file,file,function (err) {
if (err) throw err;
});
})
})
}
}, function(err, results) {
if(err) return console.error(err);
});
}
else
{
/**
*
*/
console.log(converter.setContent(arg.toString()).convert().get())
}