Source: bin/color-seek.js

#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const path = require("path");
const minimist2 = require("minimist2");
const file_system_1 = require("../lib/file-system");
const web_1 = require("../lib/web");
const helpers_1 = require("../lib/helpers");
const palette_1 = require("../lib/palette");
const command_1 = require("../lib/command");
/**
 * @summary Global.Command Object
 * @description Creates all Console switches and commands
 * @type {Command[]}
 * @member
 */
const commands = [
    new command_1.Command('-i, --input [PATH]      ', '[REQUIRED] The source file or url to search for color values   '),
    new command_1.Command('-o, --output [DIRECTORY]', 'The output file(s) directory                        '),
    new command_1.Command('-n, --name              ', 'The output file(s) name (no extension)              '),
    new command_1.Command('--css                   ', 'Create a Css rendering of the color palette         '),
    new command_1.Command('--gpl                   ', 'Create a Gimp Palette rendering of the color palette'),
    new command_1.Command('--less                  ', 'Create a Less rendering of the color palette        '),
    new command_1.Command('--scss                  ', 'Create a Sass rendering of the color palette        ')
];
/**
 * @summary Log Helper
 * @description Reference to the console.log method
 * @type {Console}
 * @member
 */
const log = console.log;
/**
 * @summary Application Exit Helper
 * @description Reference to the process.exit field
 * @type {any}
 * @member
 */
const exit = process.exit;
/**
 * @summary Console Helper
 * @description Displays 'Information' level messages to the console
 * @type {any}
 * @member
 */
const info = chalk_1.default.hex('#8CF069');
/**
 * @summary Console Helper
 * @description Displays bold 'Information' level messages to the console
 * @type {any}
 * @member
 */
const infoBold = chalk_1.default.bold.green;
/**
 * @summary Console Helper
 * @description Displays message sent to the printHelp method
 * @type {any}
 * @member
 */
const helpMessage = chalk_1.default.bold.hex('#69A0F0');
/**
 * @summary Console Helper
 * @description Displays 'Warning' level messages to the console
 * @type {any}
 * @member
 */
const warning = chalk_1.default.bold.hex('#F0EB69');
/**
 * @summary Command Line Arguments Helper
 * @description Returns the command line argument array starting on the third element
 * @type {any}
 * @member
 */
const args = minimist2(process.argv.slice(2));
/**
 *  @summary Color Format
 *  @description An array containing all colors found in the source file or URL
 *  @type {Array<string>}
 *  @member
 */
let hexColors = [];
/**
 * @summary Command Line Argument
 * @description Request for the help menu
 * @type {string}
 * @member
 */
const helpMe = args.h ? args.h : args.help;
/**
 * @summary Command Line Argument
 * @description The source file path or URL location
 * @type {string}
 * @member
 */
const inputPath = args.i ? args.i : args.input;
/**
 * @summary Command Line Argument
 * @description The directory to save all output files
 * @type {string}
 * @member
 */
let outputPath = args.o ? args.o : args.output;
/**
 * @summary Command Line Argument
 * @description Is CSS a requested output file type
 * @type {boolean}
 * @member
 */
const isCss = args.css;
/**
 * @summary Command Line Argument
 * @description Is Gimp Palette File a requested output file type
 * @type {boolean}
 * @member
 */
const isGimp = args.gimp;
/**
 * @summary Command Line Argument
 * @description Is LESS a requested output file type
 * @type {boolean}
 * @member
 */
const isLess = args.less;
/**
 * @summary Command Line Argument
 * @description Is SASS a requested output file type
 * @type {boolean}
 * @member
 */
const isSass = args.sass;
/**
 * @summary Command Line Argument
 * @description The color format to create (Hex, Gimp, RGB, HSL)
 * @type {string}
 * @member
 */
const colorFormat = args.rgb
    ? 'rgb'
    : args.hsl
        ? 'hsl'
        : 'hex';
/**
 * @summary Command Line Argument
 * @description The file name to assign all output files
 * @type {string}
 * @member
 */
let name = args.n ? args.n : args.name;
//- Enter the matrix
main();
/**
  * @public
  * @function
  * @summary Main
  * @description Entry function for Color Seek
  * @memberof Global
 */
function main() {
    try {
        //- Cry for HELP?
        if (helpMe) {
            printHelp();
            return;
        }
        //- Check if input source is valid
        if (inputPath) {
            if (inputPath.length) {
                //- Check if the file name needs to be set
                if (!name) {
                    name = path.basename(inputPath, path.extname(inputPath));
                }
                //- Determine if input is a file or a URL
                if (inputPath.toLowerCase().startsWith('http')) {
                    new web_1.Web.Http().getUrlData(inputPath, htmlTextHandler);
                }
                else {
                    new file_system_1.FileSystem.FileAccess(inputPath, outputPath, name).readFile(htmlTextHandler);
                }
            }
            else {
                helpers_1.Helpers.outputError(new Error('Missing input file'));
            }
        }
        else {
            printHelp('Input Path is Required');
        }
    }
    catch (e) {
        helpers_1.Helpers.outputError(e);
    }
}
/**
 * @public
 * @function
 * @summary Html Text Handler Callback
 * @description Handles the html data sent from Web.Html.getUrlData() to be
 *              written to disk
 * @memberof Global
 * @callback htmlTextHandler
 * @param {string} data - The file or URL text
 */
function htmlTextHandler(data) {
    const fs = new file_system_1.FileSystem.FileAccess(inputPath, outputPath, name);
    if (!outputPath) {
        outputPath = '.';
    }
    //- Create color palette and generate HTML
    hexColors = new palette_1.Palette.PaletteBuilder(inputPath, outputPath, name).buildHtmlOutput(data);
    //- Determine which output files to generate
    if (isCss) {
        fs.writeCss(outputPath, name, hexColors, colorFormat, 'css');
    }
    if (isGimp) {
        fs.writeCss(outputPath, name, hexColors, 'gpl', 'gpl');
    }
    if (isLess) {
        fs.writeCss(outputPath, name, hexColors, colorFormat, 'less');
    }
    if (isSass) {
        fs.writeCss(outputPath, name, hexColors, colorFormat, 'scss');
    }
}
/*
 * @public
 * @function
 * @summary Print Help
 * @description Print the CLI command list for Color Seek
 * @memberof Global
 */
function printHelp(message = '') {
    if (message.length) {
        log(helpMessage('-----------------------------------------------------'));
        log(helpMessage(`SYSTEM MESSAGE: ${message}`));
        log(helpMessage('-----------------------------------------------------'));
    }
    log(info('\nUsage: colorseek [OPTIONS]\n'));
    commands.forEach((c) => log(infoBold(' ' + c.Argument + '\t' + c.Description)));
    log(warning('\nIf no output type is specified then only a [DIRECTORY]/[NAME].html file will be created.'));
    log(warning('Multiple versions of the color palette can be created by specifying multiple output types (ex: --css --sass).'));
    exit();
}
//# sourceMappingURL=color-seek.js.map