doclyft
Version:
CLI for DocLyft - Interactive documentation generator with hosted documentation support
47 lines (46 loc) • 1.74 kB
JavaScript
;
/**
* Function to apply authentication protection to all CLI commands
* This will be called at the end of the CLI initialization
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyAuthProtection = applyAuthProtection;
const command_protection_1 = require("../utils/command-protection");
const command_protection_2 = require("../utils/command-protection");
/**
* Apply authentication protection to all commands in the program
* that require it based on the PROTECTED_COMMANDS list
*/
function applyAuthProtection(program) {
// First get all commands and subcommands
const commands = [];
collectCommands(program, commands);
// Apply protection to commands that need it
for (const command of commands) {
const commandName = command.name();
// Skip public commands
if (command_protection_2.PUBLIC_COMMANDS.includes(commandName)) {
continue;
}
// Check if this is a protected command or subcommand of protected command
const shouldProtect = command_protection_2.PROTECTED_COMMANDS.some(protectedCmd => commandName === protectedCmd || commandName.startsWith(`${protectedCmd} `));
if (shouldProtect) {
(0, command_protection_1.protectCommand)(command);
}
}
}
/**
* Recursively collect all commands and subcommands
*/
function collectCommands(command, result) {
// Add current command
result.push(command);
// Add all subcommands
const subcommands = command.commands;
if (subcommands && subcommands.length > 0) {
for (const subcommand of subcommands) {
collectCommands(subcommand, result);
}
}
}
exports.default = applyAuthProtection;