UNPKG

sandhog

Version:

A virtual Open Source project maintainer

404 lines (385 loc) 11.1 kB
import { Logger, getConfig } from "../chunk-SDA7SBLT.js"; import { SECTION_KINDS, findLicense, findPackage } from "../chunk-4KN5GL56.js"; import { LICENSE_NAMES, isKnownLicenseName } from "../chunk-GEXBMSSY.js"; import { listFormat } from "../chunk-VZLM7UUV.js"; import { CONSTANT } from "../chunk-VYUMLP7D.js"; // src/cli/command/create-program.ts import { Command } from "commander"; function createProgram() { return new Command(); } // src/cli/command/create-command/create-command.ts function coerceOptionValue(type, value) { switch (type) { case "string": if (value === null) return "null"; else if (value === void 0) return "undefined"; return String(value); case "number": if (typeof value === "number") return value; else if (value === true) return 1; else if (value === false) return 0; return parseFloat(value); case "boolean": if (value === "true" || value === "" || value === "1" || value === 1) return true; else if (value === "false" || value === "0" || value === 0) return false; return Boolean(value); } } function formatOptionFlags(shortHand, longHand) { const formattedLongHand = `${longHand} [arg]`; return shortHand != null ? `-${shortHand}, --${formattedLongHand}` : `--${formattedLongHand}`; } function formatCommandNameWithArgs(options) { const formattedArgs = Object.entries(options.args).map(([argName, { type, required }]) => { const left = required ? `<` : `[`; const right = required ? ">" : `]`; if (type === "string[]") { return `${left}${argName}...${right}`; } else { return `${left}${argName}${right}`; } }).join(" "); return `${options.name} ${formattedArgs}`; } function createCommand(program2, options, action) { const result = program2.command(formatCommandNameWithArgs(options)).description(options.description); Object.entries(options.options).forEach(([longhand, { shortHand, description, type, defaultValue }]) => { result.option(formatOptionFlags(shortHand, longhand), description, coerceOptionValue.bind(null, type), defaultValue); }); result.action((...args) => { const actionOptions = {}; let offset = 0; for (const key of Object.keys(options.args)) { actionOptions[key] = args[offset++]; } Object.assign(actionOptions, args[offset]); action(actionOptions); }); } // src/cli/command/shared/shared-options.ts var SHARED_OPTIONS = { config: { shortHand: "c", type: "string", description: "An (optional) path to the sandhog config to use" }, debug: { shortHand: "d", type: "boolean", description: "Whether to print debug information" }, verbose: { shortHand: "v", type: "boolean", description: "Whether to print verbose information" }, silent: { shortHand: "s", type: "boolean", description: "Whether to not print anything" }, yes: { shortHand: "y", type: "boolean", description: "Whether or not to auto-select 'yes' for all prompts" } }; // src/cli/task/generate-task-options/select-log-level/select-log-level.ts function selectLogLevel(options) { if (options.debug) { return 3 /* DEBUG */; } else if (options.verbose) { return 2 /* VERBOSE */; } else if (options.silent) { return 0 /* NONE */; } else { return 1 /* INFO */; } } // src/cli/task/generate-task-options/generate-task-options.ts import prettier from "prettier"; import fs from "fs"; async function generateTaskOptions(options) { const logLevel = selectLogLevel(options); const logger = new Logger(logLevel); if (logLevel === 2 /* VERBOSE */) logger.verbose(`Logging mode: VERBOSE`); else if (logLevel === 3 /* DEBUG */) logger.debug(`Logging mode: DEBUG`); const { pkg, root } = await findPackage({ logger }); const config = await getConfig({ root, pkg, filename: options.config, logger }); return { fs, pkg, config, logger, prettier, root, // eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare yes: options.yes === true }; } // src/cli/command/coc/coc-command.ts function createCocCommand(program2) { return createCommand( program2, { name: "coc", description: `Generates a ${CONSTANT.codeOfConductFilename} file`, args: {}, options: { ...SHARED_OPTIONS } }, async (args) => { const { cocTask } = await import("../coc-task-A6PEB6KH.js"); cocTask({ ...await generateTaskOptions(args) }); } ); } // src/cli/command/funding/funding-command.ts function createFundingCommand(program2) { return createCommand( program2, { name: "funding", description: `Generates a ${CONSTANT.fundingFilename} file`, args: {}, options: { ...SHARED_OPTIONS } }, async (args) => { const { fundingTask } = await import("../funding-task-IBAKCYYM.js"); fundingTask({ ...await generateTaskOptions(args) }); } ); } // src/cli/command/contributing/contributing-command.ts function createContributingCommand(program2) { return createCommand( program2, { name: "contributing", description: `Generates a ${CONSTANT.contributingFilename} file`, args: {}, options: { ...SHARED_OPTIONS } }, async (args) => { const { contributingTask } = await import("../contributing-task-3H5EDL6G.js"); contributingTask({ ...await generateTaskOptions(args) }); } ); } // src/util/prompt/radio.ts import { select } from "@inquirer/prompts"; async function radio(message, items) { const answer = await select({ message, choices: items.map((item) => ({ name: item, value: item })) }); return answer; } // src/license/get-license/get-license.ts async function getLicense(options) { const license = await findLicense(options); if (license == null) { return await radio(`No license could be found. Which one would you like to use?`, LICENSE_NAMES); } else { return license; } } // src/cli/command/license/license-command.ts function createLicenseCommand(program2) { return createCommand( program2, { name: "license", description: `Generates a ${CONSTANT.licenseFilename} file`, args: {}, options: { ...SHARED_OPTIONS, license: { description: "Override the license to use generate", type: "string", shortHand: "l" } } }, async (args) => { const { licenseTask } = await import("../license-task-AM6A6H5O.js"); const taskOptions = await generateTaskOptions(args); const license = await (async () => { if (args.license != null) { if (!isKnownLicenseName(args.license)) { throw new TypeError(`The license: '${args.license}' given via a CLI option is not supported`); } taskOptions.logger.debug(`License given as a CLI option: '${args.license}'`); return args.license; } else { return await getLicense(taskOptions); } })(); licenseTask({ ...taskOptions, license }); } ); } // src/section/ensure-section-kind/ensure-section-kind.ts function ensureSectionKind(sectionKind) { if (typeof sectionKind !== "string") return sectionKind; if (SECTION_KINDS.some((key) => key === sectionKind)) { return sectionKind; } else { throw new TypeError(`Could not parse string: '${sectionKind}' as a SectionKind. Possible values: ${listFormat(SECTION_KINDS, "and")}`); } } // src/badge/badge-kind.ts var BADGE_KINDS = [ "downloads", "dependencies", "npm", "contributors", "license", "patreon", "open_collective_donate", "open_collective_backers", "open_collective_sponsors", "code_style" ]; // src/badge/ensure-badge-kind/ensure-badge-kind.ts function ensureBadgeKind(badgeKind) { if (typeof badgeKind !== "string") return badgeKind; if (BADGE_KINDS.some((key) => key === badgeKind)) { return badgeKind; } else { throw new TypeError(`Could not parse string: '${badgeKind}' as a BadgeKind. Possible values: ${listFormat(BADGE_KINDS, "and")}`); } } // src/cli/command/readme/readme-command.ts function createReadmeCommand(program2) { return createCommand( program2, { name: "readme", description: `Generates a ${CONSTANT.readmeFilename} file`, args: {}, options: { ...SHARED_OPTIONS, "section.exclude": { description: `The comma-separated sections to exclude from the generated ${CONSTANT.readmeFilename}`, type: "string" }, "badge.exclude": { description: `The comma-separated badges to exclude from the generated ${CONSTANT.readmeFilename}`, type: "string" } } }, async (args) => { const { readmeTask } = await import("../readme-task-2DHDYMEZ.js"); const taskOptions = await generateTaskOptions(args); if (args["section.exclude"] != null) { const splitted = args["section.exclude"].split(","); taskOptions.config.readme.sections.exclude = splitted.map(ensureSectionKind); } if (args["badge.exclude"] != null) { const splitted = args["badge.exclude"].split(","); taskOptions.config.readme.badges.exclude = splitted.map(ensureBadgeKind); } readmeTask({ ...taskOptions }); } ); } // src/cli/command/all/all-command.ts function createAllCommand(program2) { return createCommand( program2, { name: "all", description: `Generates all of the files sandhog supports in one command`, args: {}, options: { ...SHARED_OPTIONS } }, async (args) => { const { cocTask } = await import("../coc-task-A6PEB6KH.js"); const { contributingTask } = await import("../contributing-task-3H5EDL6G.js"); const { fundingTask } = await import("../funding-task-IBAKCYYM.js"); const { licenseTask } = await import("../license-task-AM6A6H5O.js"); const { readmeTask } = await import("../readme-task-2DHDYMEZ.js"); const taskOptions = await generateTaskOptions(args); cocTask({ ...taskOptions }); contributingTask({ ...taskOptions }); fundingTask({ ...taskOptions }); licenseTask({ ...taskOptions, license: await getLicense(taskOptions) }); readmeTask({ ...taskOptions }); } ); } // src/cli/command/help/help-command.ts function createHelpCommand(program2) { program2.addHelpText("before", `Welcome to Sandhog! `); program2.addHelpText( "after", ` Example call: $ sandhog readme --yes` ); } // src/cli/index.ts var program = createProgram(); createCocCommand(program); createFundingCommand(program); createContributingCommand(program); createLicenseCommand(program); createReadmeCommand(program); createAllCommand(program); createHelpCommand(program); program.parse(); //# sourceMappingURL=index.js.map