redux-feature-generator
Version:
Generate Redux feature code from the command line
158 lines (157 loc) • 6.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.rfgApi = exports.RFG_STATUS = void 0;
/**
* @file rfgArgs.ts
* @version 1.4.0
* @fileoverview Redux Feature Generator (RFG) Argument Processor is used to process and verify any cli user input and return a command status.
*/
const fs_1 = require("fs");
const path_1 = require("path");
const helpers_1 = require("./helpers");
/**
* Simple status enumeration of the all feature generator commands
*/
var RFG_STATUS;
(function (RFG_STATUS) {
RFG_STATUS[RFG_STATUS["OK"] = 0] = "OK";
RFG_STATUS[RFG_STATUS["VERSION"] = 1] = "VERSION";
RFG_STATUS[RFG_STATUS["HELP"] = 2] = "HELP";
RFG_STATUS[RFG_STATUS["ERROR"] = 3] = "ERROR";
RFG_STATUS[RFG_STATUS["UNKNOWN_ERROR"] = 400] = "UNKNOWN_ERROR";
RFG_STATUS[RFG_STATUS["FEATURE_NAMING_ERROR"] = 401] = "FEATURE_NAMING_ERROR";
RFG_STATUS[RFG_STATUS["UNKNOWN_FLAG_ERROR"] = 402] = "UNKNOWN_FLAG_ERROR";
RFG_STATUS[RFG_STATUS["FOLDER_NOT_FOUND_ERROR"] = 403] = "FOLDER_NOT_FOUND_ERROR";
})(RFG_STATUS = exports.RFG_STATUS || (exports.RFG_STATUS = {}));
;
/**
* Self explanatory
*/
const MAX_ARGUMENT_LIMIT = 4;
/**
* Main cli argument handler
* @param argv list of args from the cli
* @returns ArgumentPayload
*/
const readArgs = (argv) => {
// Keep track of the status
let commandPayload = {
status: RFG_STATUS.UNKNOWN_ERROR,
argv: []
};
// Types of templates allowed
const templateTypesArr = [
"redux-typescript",
"redux-javascript"
];
/**
* Fail on incorrect length of argv
*/
if (argv.length === 0 || argv.length > MAX_ARGUMENT_LIMIT) {
return createStatusPayload(RFG_STATUS.ERROR, "Error: Incorrect Amount of Arguments");
}
/**
* For each argument given on cli
*/
for (let index = 0; index < argv.length; index++) {
const cliArgListItem = argv[index];
let isOption = false;
if (cliArgListItem[0] == '-') {
isOption = true;
}
switch (index) {
case 0: // Check that first argument is a valid name
if (!isOption) {
if ((0, helpers_1.validName)(cliArgListItem)) {
commandPayload.argv[index] = cliArgListItem;
commandPayload.status = RFG_STATUS.OK;
break;
}
else {
return createStatusPayload(RFG_STATUS.FEATURE_NAMING_ERROR, "Naming Error: The feature name must be valid for a variable in your code");
}
} // else Pass and handle in default
case 1: // Check that second argument is a valid path
if (!isOption) {
if ((0, fs_1.existsSync)((0, path_1.join)(cliArgListItem))) {
commandPayload.argv[index] = cliArgListItem;
commandPayload.status = RFG_STATUS.OK;
break;
}
else {
return createStatusPayload(RFG_STATUS.FOLDER_NOT_FOUND_ERROR, "Folder Path Error: The folder must exist to generate the features");
}
} // else Pass and handle in default
default:
if (isOption) { // Handle arguments or unknown cases
switch (cliArgListItem) {
case "-h":
case "--help":
return createStatusPayload(RFG_STATUS.HELP);
case "-v":
case "--version":
return createStatusPayload(RFG_STATUS.VERSION);
case "-t":
case "--template":
if (templateTypesArr.includes(argv[++index])) {
commandPayload.argv[1] = argv[index];
}
else {
return createStatusPayload(RFG_STATUS.ERROR, `Argument Error: Invalid template pattern attempted please provide one of the options seen here [${templateTypesArr}]`);
}
break;
default:
return createStatusPayload(RFG_STATUS.UNKNOWN_FLAG_ERROR, "Error: Unknown Command Provided");
}
}
else {
return createStatusPayload(RFG_STATUS.UNKNOWN_ERROR, "Error: Unknown Command Provided");
}
break;
}
}
return commandPayload;
};
/**
* Builds and returns help text
* @returns {string} The help text that should be printed to console
*/
const generateHelp = () => {
let helpString = "redux-feature-generator:\t\tA simple command line tool to speed up the time it takes to setup a new redux feature.\n\n";
helpString = helpString.concat("\tUsage:\tgenerate-feature <featureName> <generationPath> [--template redux-typescript]\n\n", "\tExample:\n", "\t\tgenerate-feature <featureName>\n", "\t\tgenerate-feature <featureName> <generationPath>\n", "\t\tgenerate-feature <featureName> <generationPath> -t <template>\n\n", "\tcustomName\t\t\tThe name of your new feature.\n", "\tfeatureFolder\t\t\tThe folder location that you want the files generated in.\n", "\t-h --help\t\t\tPrint command help.\n", "\t-v --version\t\t\tPrint command help.\n", "\t--template <template>\t\tThe template structure you would like use for generation.\n\n");
return helpString;
};
/**
* Retrieve the version string from the config file
* @param cfg
* @returns
*/
const getVersion = (cfg) => {
if (cfg !== undefined && cfg.version !== undefined) {
const rfgVersion = `v${cfg.version}`;
return rfgVersion;
}
};
/**
* Create the payload to return to the cli functions
* @param status {RFG_STATUS} the status of the command
* @param message {string | undefined} if a message must be printed, include it here
* @returns ArgumentPayload
*/
const createStatusPayload = (status, message) => {
if (message) {
console.log(message);
}
return {
status: status,
argv: []
};
};
/**
* API
*/
exports.rfgApi = {
processCommand: readArgs,
getHelp: generateHelp,
getVersion: getVersion
};