-
Notifications
You must be signed in to change notification settings - Fork 2
/
lottie-to-avd.js
61 lines (58 loc) · 1.74 KB
/
lottie-to-avd.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
#!/usr/bin/env node
var avd_converter = require('./src/index.js');
var fs = require('fs');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const path = require('path');
function changeFileExtension(filePath, newExtension) {
const currentExtension = path.extname(filePath);
const baseName = path.basename(filePath, currentExtension);
return path.join(path.dirname(filePath), baseName + newExtension);
}
//Parsing args
const argv = yargs(hideBin(process.argv))
.epilog('Lottie to AVD converter.')
.example('lottie-to-avd -i sample.json -o sample.xml')
.usage('Usage: lottie-to-avd -i <input> -o <output>')
.version('0.0.1')
.alias('v', 'version')
.option('i', {
alias: 'input',
describe: 'Input file path(required)',
type: 'string',
demandOption: true,
})
.option('o', {
alias: 'output',
describe: 'Output file path(optional)',
type: 'string',
demandOption: false,
})
.help('h')
.alias('h', 'help')
.argv;
// Access the arguments
const inputFilePath = argv.input;
var outputFilePath = argv.output;
if (!outputFilePath) {
outputFilePath = changeFileExtension(inputFilePath, '.xml');
}
//Converting...
fs.readFile(inputFilePath, "utf8", function(error, data){
process.on('unhandledRejection', function(err, promise) {
console.error('Unhandled rejection (promise: ', promise, ', reason: ', err, ').');
});
var prom = avd_converter(JSON.parse(data))
prom.then(function(xml){
fs.writeFile(outputFilePath, xml, function(err) {
if(err) {
return console.log(err);
}
console.log("Successfully converted!");
console.log('Output file path:', path.resolve(outputFilePath));
});
}).catch(function (err) {
console.log(err);
console.log('Convertion failed!!!');
});
})