2017-11-26 20:45:40 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const program = require('commander');
|
|
|
|
const path = require('path');
|
|
|
|
const importers = require('../index');
|
|
|
|
const fs = require('fs');
|
2018-06-25 17:42:50 +00:00
|
|
|
const { version } = require('../package.json');
|
2017-11-26 20:45:40 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
module.exports.go = async function() {
|
2017-11-26 20:45:40 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Configure the arguments parsing //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
program
|
|
|
|
.version(version, '-v, --version')
|
|
|
|
.usage('[options] <input>')
|
|
|
|
.option('-o, --output <path>', 'output directory')
|
|
|
|
.parse(process.argv);
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Set up the directory to work on //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
const inputPath = program.args[0];
|
|
|
|
const outputPath = program.output || program.args[1];
|
|
|
|
|
|
|
|
if (!inputPath) {
|
|
|
|
console.log('Input path not specified');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Convert the input file //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
const fullInputPath = path.resolve(inputPath);
|
|
|
|
const fileContents = fs.readFileSync(fullInputPath, 'utf8');
|
|
|
|
|
2018-01-16 05:51:28 +00:00
|
|
|
const result = await importers.convert(fileContents);
|
2017-11-26 20:45:40 +00:00
|
|
|
const exportContents = JSON.stringify(result.data, null, 2);
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~ //
|
|
|
|
// Write the output //
|
|
|
|
// ~~~~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
if (outputPath) {
|
|
|
|
const fullOutputPath = path.resolve(outputPath);
|
|
|
|
fs.writeFileSync(fullOutputPath, exportContents);
|
|
|
|
} else {
|
|
|
|
console.log(exportContents);
|
|
|
|
}
|
|
|
|
};
|