embed-support
Version:
Create embed codes for a static web project
74 lines (61 loc) • 1.9 kB
JavaScript
/* eslint-disable no-console */
const fileExists = require('file-exists');
const minimist = require('minimist');
const embedSupport = require('./main');
const path = require('path');
const chalk = require('chalk');
const Table = require('cli-table2');
const _ = require('lodash');
const validateOptions = require('./validate-options');
const args = minimist(process.argv.slice(2));
const configFile = args.config || 'embed-support.config.js';
const hrstart = process.hrtime();
// merge options from possible config file with command line arguments
let opts = {};
if (fileExists(configFile)) {
const resolvedPath = path.resolve(configFile);
if (args.verbose) {
console.log(`Using config from ${chalk.blue(resolvedPath)}`); // eslint-disable-line
}
opts = require(resolvedPath);
} else if (args.config) {
console.log(`[Error] ${configFile} does not exist`); // eslint-disable-line
process.exit(1);
}
opts = Object.assign(opts, args);
delete opts._;
// set default for scriptsUrl
if (!opts.scriptsUrl) {
opts.scriptsUrl = opts.iframeUrl;
}
if (!opts.template) {
opts.template = path.resolve(path.join(__dirname, 'embed-codes.hbs'));
}
// check that options are valid
if (!validateOptions(opts)) {
process.exit(1);
}
if (!args.silent) {
console.log(`${chalk.cyan('▶')} Creating embed codes and supporting assets`);
}
if (args.verbose) {
if (args.verbose) {
const table = new Table();
_.forOwn(opts, (value, key) => {
table.push({ [key]: value });
});
console.log(table.toString());
}
}
try {
embedSupport(opts);
if (!args.silent) {
console.log(`${chalk.green('✓')} ` // eslint-disable-line
+ 'Finished creating embed support assets in '
+ chalk.magenta(`${(process.hrtime(hrstart)[1] / 1000000).toFixed(2)} ms`));
}
} catch (err) {
console.log(`${chalk.red('[Error]')} ${err}`);
throw err;
}