in packages/hub/cli.ts [240:487]
async function run() {
switch (mainCommandName) {
case undefined:
case "--help":
case "help": {
const helpArgs = mainCommandName === "help" ? process.argv.slice(3) : [];
if (helpArgs.length > 0) {
const cmdName = helpArgs[0] as TopLevelCommandName;
if (cmdName && commands[cmdName]) {
const cmdDef = commands[cmdName];
if ("subcommands" in cmdDef) {
if (helpArgs.length > 1) {
const subCmdName = helpArgs[1];
if (
subCmdName in cmdDef.subcommands &&
cmdDef.subcommands[subCmdName as keyof typeof cmdDef.subcommands]
) {
console.log(detailedUsageForSubcommand(cmdName, subCmdName as keyof typeof cmdDef.subcommands));
break;
} else {
console.error(`Error: Unknown subcommand '${subCmdName}' for command '${cmdName}'.`);
console.log(listSubcommands(cmdName, cmdDef));
process.exitCode = 1;
break;
}
} else {
console.log(listSubcommands(cmdName, cmdDef));
break;
}
} else {
console.log(detailedUsageForCommand(cmdName));
break;
}
} else {
console.error(`Error: Unknown command '${cmdName}' for help.`);
process.exitCode = 1;
}
} else {
// General help
console.log(
`Hugging Face CLI Tools (hfjs)\n\nAvailable commands:\n\n` +
typedEntries(commands)
.map(([name, def]) => ` ${usage(name)}: ${def.description}`)
.join("\n")
);
console.log("\nTo get help on a specific command, run `hfjs help <command>` or `hfjs <command> --help`");
console.log(
"For commands with subcommands (like 'branch'), run `hfjs help <command> <subcommand>` or `hfjs <command> <subcommand> --help`"
);
if (mainCommandName === undefined) {
process.exitCode = 1;
}
}
break;
}
case "upload": {
const cmdDef = commands.upload;
if (cliArgs[0] === "--help" || cliArgs[0] === "-h") {
console.log(detailedUsageForCommand("upload"));
break;
}
const parsedArgs = advParseArgs(cliArgs, cmdDef.args, "upload");
const {
repoName,
localFolder,
repoType,
revision,
token,
quiet,
commitMessage,
pathInRepo,
private: isPrivate,
} = parsedArgs;
const repoId = repoType ? { type: repoType as "model" | "dataset" | "space", name: repoName } : repoName;
if (
!(await repoExists({ repo: repoId, revision, accessToken: token, hubUrl: process.env.HF_ENDPOINT ?? HUB_URL }))
) {
if (!quiet) {
console.log(`Repo ${repoName} does not exist. Creating it...`);
}
await createRepo({
repo: repoId,
accessToken: token,
private: !!isPrivate,
hubUrl: process.env.HF_ENDPOINT ?? HUB_URL,
});
}
const isFile = (await stat(localFolder)).isFile();
const files = isFile
? [
{
content: pathToFileURL(localFolder),
path: join(pathInRepo, `${basename(localFolder)}`).replace(/^[.]?\//, ""),
},
]
: [{ content: pathToFileURL(localFolder), path: pathInRepo.replace(/^[.]?\//, "") }];
for await (const event of uploadFilesWithProgress({
repo: repoId,
files,
branch: revision,
accessToken: token,
commitTitle: commitMessage?.trim().split("\n")[0],
commitDescription: commitMessage?.trim().split("\n").slice(1).join("\n").trim(),
hubUrl: process.env.HF_ENDPOINT ?? HUB_URL,
})) {
if (!quiet) {
console.log(event);
}
}
break;
}
case "branch": {
const branchCommandGroup = commands.branch;
const currentSubCommandName = subCommandName as keyof typeof branchCommandGroup.subcommands | undefined;
if (cliArgs[0] === "--help" || cliArgs[0] === "-h") {
if (currentSubCommandName && branchCommandGroup.subcommands[currentSubCommandName]) {
console.log(detailedUsageForSubcommand("branch", currentSubCommandName));
} else {
console.log(listSubcommands("branch", branchCommandGroup));
}
break;
}
if (!currentSubCommandName || !branchCommandGroup.subcommands[currentSubCommandName]) {
console.error(`Error: Missing or invalid subcommand for 'branch'.`);
console.log(listSubcommands("branch", branchCommandGroup));
process.exitCode = 1;
break;
}
const subCmdDef = branchCommandGroup.subcommands[currentSubCommandName];
switch (currentSubCommandName) {
case "create": {
const parsedArgs = advParseArgs(cliArgs, subCmdDef.args, "branch create");
const { repoName, branch, revision, empty, repoType, token, force } = parsedArgs;
await createBranch({
repo: repoType ? { type: repoType as "model" | "dataset" | "space", name: repoName } : repoName,
branch,
accessToken: token,
revision,
empty: empty ?? undefined,
overwrite: force ?? undefined,
hubUrl: process.env.HF_ENDPOINT ?? HUB_URL,
});
console.log(`Branch '${branch}' created successfully in repo '${repoName}'.`);
break;
}
case "delete": {
const parsedArgs = advParseArgs(cliArgs, subCmdDef.args, "branch delete");
const { repoName, branch, repoType, token } = parsedArgs;
await deleteBranch({
repo: repoType ? { type: repoType as "model" | "dataset" | "space", name: repoName } : repoName,
branch,
accessToken: token,
hubUrl: process.env.HF_ENDPOINT ?? HUB_URL,
});
console.log(`Branch '${branch}' deleted successfully from repo '${repoName}'.`);
break;
}
default:
// Should be caught by the check above
console.error(`Error: Unknown subcommand '${currentSubCommandName}' for 'branch'.`);
console.log(listSubcommands("branch", branchCommandGroup));
process.exitCode = 1;
break;
}
break;
}
case "repo": {
const repoCommandGroup = commands.repo;
const currentSubCommandName = subCommandName as keyof typeof repoCommandGroup.subcommands | undefined;
if (cliArgs[0] === "--help" || cliArgs[0] === "-h") {
if (currentSubCommandName && repoCommandGroup.subcommands[currentSubCommandName]) {
console.log(detailedUsageForSubcommand("repo", currentSubCommandName));
} else {
console.log(listSubcommands("repo", repoCommandGroup));
}
break;
}
if (!currentSubCommandName || !repoCommandGroup.subcommands[currentSubCommandName]) {
console.error(`Error: Missing or invalid subcommand for 'repo'.`);
console.log(listSubcommands("repo", repoCommandGroup));
process.exitCode = 1;
break;
}
const subCmdDef = repoCommandGroup.subcommands[currentSubCommandName];
switch (currentSubCommandName) {
case "delete": {
const parsedArgs = advParseArgs(cliArgs, subCmdDef.args, `repo ${currentSubCommandName}`);
const { repoName, repoType, token } = parsedArgs;
const repoDesignation: Parameters<typeof deleteRepo>[0]["repo"] = repoType
? { type: repoType as "model" | "dataset" | "space", name: repoName }
: repoName;
await deleteRepo({
repo: repoDesignation,
accessToken: token,
hubUrl: process.env.HF_ENDPOINT ?? HUB_URL,
});
console.log(`Repository '${repoName}' deleted successfully.`);
break;
}
default:
// This case should ideally be caught by the check above
console.error(`Error: Unknown subcommand '${currentSubCommandName}' for 'repo'.`);
console.log(listSubcommands("repo", repoCommandGroup));
process.exitCode = 1;
break;
}
break;
}
case "version": {
if (cliArgs[0] === "--help" || cliArgs[0] === "-h") {
console.log(detailedUsageForCommand("version"));
break;
}
console.log(`hfjs version: ${version}`);
break;
}
default:
console.error("Command not found: " + mainCommandName);
// Print general help
console.log(
`\nAvailable commands:\n\n` +
typedEntries(commands)
.map(([name, def]) => ` ${usage(name)}: ${def.description}`)
.join("\n")
);
console.log("\nTo get help on a specific command, run `hfjs help <command>` or `hfjs <command> --help`");
process.exitCode = 1;
break;
}
}