Skip to content

Commit

Permalink
refactor: Refined logic for deciding when to run command, print help …
Browse files Browse the repository at this point in the history
…or version
  • Loading branch information
d4rkr00t committed Apr 24, 2019
1 parent 20579f1 commit 47033e6
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 69 deletions.
2 changes: 1 addition & 1 deletion packages/core/dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 93 additions & 32 deletions packages/core/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,112 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const fs_1 = require("fs");
const minimist_1 = __importDefault(require("minimist"));
const minimist_options_1 = __importDefault(require("minimist-options"));
const help_1 = require("./commands/help");
async function createCLI(rawArgv, dir, packageJson) {
let argv = rawArgv.slice(2);
let commandName = argv[0];
let isCommand = commandName && !commandName.startsWith("-");
let commandsDirPath = path.join(dir, "commands");
// If command is passed check if file exists
// Otherwise load index.js
let commandPath;
if (isCommand) {
commandPath = path.resolve(path.join(commandsDirPath, commandName));
}
else {
commandPath = path.resolve(path.join(commandsDirPath, "index"));
let commands = fs_1.readdirSync(commandsDirPath);
let isCommand = commandName && !commandName.startsWith("-");
let isVersion = argv.find(f => f === "-v") || argv.find(f => f === "--version");
let isHelp = argv.find(f => f === "--help");
// 0. If no commands -> blow up! // TODO
// 1. If --version and command is not passed -> print version
// 2. If --help
// 2.1. If command is not passed -> help
// 2.2. If command is passed and exists -> help
// 2.3. If command is passed and doesn't exist -> error
// 3. If command is passed and exists -> run
// 4. If command doesn't exist and single command cli
// 4.1. Check if index exists -> run
// 4.2. If index doesn't exist -> show help
// 5. If command doesn't exist and multi command cli
// 5.1. If index exists -> run
// 5.2. If index doesn't exist -> show help
let action = "help";
let payload; // TODO: any
// # 0
if (!commands.length) {
action = "error";
payload = `Need to add at least 1 command to ${commandsDirPath}...`;
}
let command;
try {
command = require(commandPath);
// # 1
else if (isVersion && !isCommand) {
action = "version";
}
catch (e) { }
let { _: rawInputs, ...flags } = minimist_1.default(argv, minimist_options_1.default((command || {}).options || {}));
let inputs = isCommand ? rawInputs.slice(1) : rawInputs;
// If not a command and --version was passed, print version
if (!isCommand && (flags.v || flags.version)) {
console.log(packageJson.version);
process.exit(0);
// # 2
else if (isHelp) {
if (!isCommand) {
action = "help";
}
else if (isCommand &&
commands.find(c => c.startsWith(`${commandName}.`))) {
payload = commandName;
action = "help";
}
else if (isCommand &&
!commands.find(c => c.startsWith(`${commandName}.`))) {
action = "error";
payload = `Command "${commandName}" doesn't exist, help can't be printed for it. Try cli --help`; // TODO: proper name for cli
}
}
// If not a command and --help was passed, or command wasn't found, print help
if (!command || (!isCommand && flags.help)) {
help_1.helpCommand({ commandsDirPath, packageJson });
process.exit(0);
// # 3
else if (isCommand && commands.find(c => c.startsWith(`${commandName}.`))) {
action = "run";
payload = commandName;
}
// If a command and --help was passed, print command help
if (command && flags.help) {
console.log(`${commandName} HEEEEELP!`);
process.exit(0);
// # 4 – single command cli
else if (commands.length === 1) {
// # 4.1 | 4.2
if (commands.find(c => c.startsWith("index."))) {
action = "run";
payload = "index";
}
else {
action = "help";
}
}
try {
await command(inputs, flags);
process.exit(0);
// # 5 – multi-command cli
else if (commands.length > 1) {
// # 5.1 | 5.2
if (commands.find(c => c.startsWith("index."))) {
action = "run";
payload = "index";
}
else {
action = "help";
}
}
catch (error) {
process.exit(1);
switch (action) {
case "run":
let commandPath = path.resolve(path.join(commandsDirPath, payload));
let command = require(commandPath);
let { _: rawInputs, ...flags } = minimist_1.default(argv, minimist_options_1.default((command || {}).options || {}));
let inputs = isCommand ? rawInputs.slice(1) : rawInputs;
try {
await command(inputs, flags);
process.exit(0);
}
catch (error) {
process.exit(1);
}
break;
case "version":
console.log(packageJson.version);
process.exit(0);
break;
case "error":
console.error(payload); // TODO: logging
process.exit(1);
break;
case "help":
default:
help_1.helpCommand({ commandsDirPath, packageJson });
process.exit(0);
break;
}
}
exports.default = createCLI;
141 changes: 105 additions & 36 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from "path";
import { readdirSync } from "fs";
import minimist from "minimist";
import buildOptions from "minimist-options";
import { helpCommand } from "./commands/help";
Expand All @@ -10,52 +11,120 @@ export default async function createCLI(
) {
let argv = rawArgv.slice(2);
let commandName = argv[0];
let isCommand = commandName && !commandName.startsWith("-");
let commandsDirPath = path.join(dir, "commands");
let commands = readdirSync(commandsDirPath);
let isCommand = commandName && !commandName.startsWith("-");
let isVersion =
argv.find(f => f === "-v") || argv.find(f => f === "--version");
let isHelp = argv.find(f => f === "--help");

// 0. If no commands -> blow up! // TODO
// 1. If --version and command is not passed -> print version
// 2. If --help
// 2.1. If command is not passed -> help
// 2.2. If command is passed and exists -> help
// 2.3. If command is passed and doesn't exist -> error
// 3. If command is passed and exists -> run
// 4. If command doesn't exist and single command cli
// 4.1. Check if index exists -> run
// 4.2. If index doesn't exist -> show help
// 5. If command doesn't exist and multi command cli
// 5.1. If index exists -> run
// 5.2. If index doesn't exist -> show help

// If command is passed check if file exists
// Otherwise load index.js
let action: "run" | "help" | "error" | "version" = "help";
let payload: any; // TODO: any

let commandPath;
if (isCommand) {
commandPath = path.resolve(path.join(commandsDirPath, commandName));
} else {
commandPath = path.resolve(path.join(commandsDirPath, "index"));
// # 0
if (!commands.length) {
action = "error";
payload = `Need to add at least 1 command to ${commandsDirPath}...`;
}

let command;
try {
command = require(commandPath);
} catch (e) {}

let { _: rawInputs, ...flags } = minimist(
argv,
buildOptions((command || {}).options || {})
);
let inputs = isCommand ? rawInputs.slice(1) : rawInputs;

// If not a command and --version was passed, print version
if (!isCommand && (flags.v || flags.version)) {
console.log(packageJson.version);
process.exit(0);
// # 1
else if (isVersion && !isCommand) {
action = "version";
}

// If not a command and --help was passed, or command wasn't found, print help
if (!command || (!isCommand && flags.help)) {
helpCommand({ commandsDirPath, packageJson });
process.exit(0);
// # 2
else if (isHelp) {
if (!isCommand) {
action = "help";
} else if (
isCommand &&
commands.find(c => c.startsWith(`${commandName}.`))
) {
payload = commandName;
action = "help";
} else if (
isCommand &&
!commands.find(c => c.startsWith(`${commandName}.`))
) {
action = "error";
payload = `Command "${commandName}" doesn't exist, help can't be printed for it. Try cli --help`; // TODO: proper name for cli
}
}

// If a command and --help was passed, print command help
if (command && flags.help) {
console.log(`${commandName} HEEEEELP!`);
process.exit(0);
// # 3
else if (isCommand && commands.find(c => c.startsWith(`${commandName}.`))) {
action = "run";
payload = commandName;
}

try {
await command(inputs, flags);
process.exit(0);
} catch (error) {
process.exit(1);
// # 4 – single command cli
else if (commands.length === 1) {
// # 4.1 | 4.2
if (commands.find(c => c.startsWith("index."))) {
action = "run";
payload = "index";
} else {
action = "help";
}
}

// # 5 – multi-command cli
else if (commands.length > 1) {
// # 5.1 | 5.2
if (commands.find(c => c.startsWith("index."))) {
action = "run";
payload = "index";
} else {
action = "help";
}
}

switch (action) {
case "run":
let commandPath = path.resolve(path.join(commandsDirPath, payload));
let command = require(commandPath);

let { _: rawInputs, ...flags } = minimist(
argv,
buildOptions((command || {}).options || {})
);
let inputs = isCommand ? rawInputs.slice(1) : rawInputs;
try {
await command(inputs, flags);
process.exit(0);
} catch (error) {
process.exit(1);
}
break;

case "version":
console.log(packageJson.version);
process.exit(0);
break;

case "error":
console.error(payload); // TODO: logging
process.exit(1);
break;

case "help":
default:
helpCommand({ commandsDirPath, packageJson });
process.exit(0);
break;
}
}
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,

"incremental": true,
"composite": true /* Enable project compilation */,

/* Strict Type-Checking Options */
Expand Down

0 comments on commit 47033e6

Please sign in to comment.