UNPKG

embed-support

Version:

Create embed codes for a static web project

77 lines (64 loc) 2.16 kB
#! /usr/bin/env node 'use strict'; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /* eslint-disable no-console */ var fileExists = require('file-exists'); var minimist = require('minimist'); var embedSupport = require('./main'); var path = require('path'); var chalk = require('chalk'); var Table = require('cli-table2'); var _ = require('lodash'); var validateOptions = require('./validate-options'); var args = minimist(process.argv.slice(2)); var configFile = args.config || 'embed-support.config.js'; var hrstart = process.hrtime(); // merge options from possible config file with command line arguments var opts = {}; if (fileExists(configFile)) { var 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) { (function () { var table = new Table(); _.forOwn(opts, function (value, key) { table.push(_defineProperty({}, 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; }