-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.js
executable file
Β·103 lines (89 loc) Β· 2.41 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
const glob = require('glob');
const yargs = require('yargs');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const isThere = require('is-there');
const convert = require('./convert');
/**
* @deprecated
* @code const cwd = require('shelljs').pwd().toString();
*/
const convertAndWrite = (inputFile, outputPath, single) => {
let outputData = fs.readFileSync(inputFile, 'utf8');
try {
outputData = convert(outputData);
} catch ( e ) {
console.error('ERROR: convert error. Input code is too complex to convert.');
return 1;
}
let realOutputPath = undefined;
if (single) {
realOutputPath = outputPath;
mkdirp(path.parse(realOutputPath).dir);
} else {
realOutputPath = outputPath + path.basename(inputFile);
mkdirp(outputPath);
}
try {
fs.writeFileSync(realOutputPath, outputData);
console.log('SUCCESS: generated file `' + realOutputPath + '`');
} catch ( e ) {
console.error('ERROR: write data to `' + realOutputPath + '` failed.');
return 1;
}
return 0;
};
const cli = (args) => {
if (typeof args === 'undefined') {
return 1;
}
if (typeof args === 'object') {
args = args.join(' ');
}
if (typeof args !== 'string') {
return 1;
}
args = yargs(args)
.describe('output', 'Output file')
.describe('version', 'Print version number and exit')
.help('help')
.alias('o', 'output')
.alias('h', 'help')
.alias('v', 'version')
.argv;
if (args.version || args.v) {
const json = require('./package.json');
console.log(json.name + ' ' + json.version);
return 0;
}
if (args._.length > 0) {
const inputFiles = args._;
let outputPath = undefined;
if (args.output || args.o) {
outputPath = args.outputPath || args.o;
} else {
console.error('ERROR: Invalid output');
return 1;
}
for (const inputFile of inputFiles) {
if (!isThere(inputFile)) {
glob(inputFile, (err, globFiles) => {
for (const globFile of globFiles) {
convertAndWrite(globFile, outputPath, false);
}
});
} else {
convertAndWrite(inputFile, outputPath, inputFiles.length===1);
}
}
return 0;
}
if ((args.output || args.o) && args._.length === 0) {
console.error('ERROR: Invalid input');
return 1;
}
console.log('Usage: kaomojify <input> -o <output>');
return 0;
};
module.exports = cli;