-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (94 loc) · 2.84 KB
/
index.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
const yargs = require('yargs');
const path = require('path');
const readFile = require('./lib/readFile');
const writeFile = require('./lib/writeFile');
const minify = require('./lib/minify');
const format = require('./lib/format');
const parse = require('./lib/parse');
yargs
.command({
command: 'minify <file>',
aliases: ['m'],
desc: 'Minify a file',
builder: (yargs) => {
yargs.positional('file', {
describe: 'A xml or json file',
type: 'string'
});
},
handler: (argv) => {
const file = argv.file;
readFile(file)
.then((data) => {
return minify(file, data);
})
.then((writeData) => {
return writeFile(file, writeData);
})
.then(() => {
console.log('The file has been minified!');
})
.catch((error) => {
console.error(error.message);
});
}
})
.command({
command: 'format <file>',
aliases: ['f'],
desc: 'Format a file',
builder: (yargs) => {
return yargs.positional('file', {
describe: 'A xml or json file',
type: 'string'
});
},
handler: (argv) => {
const file = argv.file;
readFile(file)
.then((data) => {
return format(file, data);
})
.then((writeData) => {
return writeFile(file, writeData);
})
.then(() => {
console.log('The file has been formatted!');
})
.catch((error) => {
console.error(error.message);
});
}
})
.command({
command: 'parse <file>',
aliases: ['p'],
desc: 'Parse a file',
builder: (yargs) => {
yargs.positional('file', {
describe: 'A xml or json file',
type: 'string'
});
},
handler: (argv) => {
const file = argv.file;
const extension = path.extname(file);
const newFile = (extension === '.xml') ? file.replace(extension, '.json') : file.replace(extension, '.xml');
readFile(file)
.then((data) => {
return parse(file, data);
})
.then((writeData) => {
return writeFile(newFile, writeData);
})
.then(() => {
console.log('The file has been formatted to ' + path.basename(newFile) + '!');
})
.catch((error) => {
console.error(error.message);
})
}
})
.demandCommand(1, 'You need at least one command before moving on')
.help()
.argv