Skip to content

Commit f6e9893

Browse files
committed
create repo when it does not exist
1 parent ea0e2bc commit f6e9893

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

packages/hub/cli.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { parseArgs } from "node:util";
44
import { typedEntries } from "./src/utils/typedEntries";
5-
import { createBranch, deleteBranch, uploadFilesWithProgress } from "./src";
5+
import { createBranch, createRepo, deleteBranch, repoExists, uploadFilesWithProgress } from "./src";
66
import { pathToFileURL } from "node:url";
77
import { stat } from "node:fs/promises";
88
import { basename, join } from "node:path";
@@ -219,7 +219,33 @@ async function run() {
219219
break;
220220
}
221221
const parsedArgs = advParseArgs(args, "upload");
222-
const { repoName, localFolder, repoType, revision, token, quiet, commitMessage, pathInRepo } = parsedArgs;
222+
const {
223+
repoName,
224+
localFolder,
225+
repoType,
226+
revision,
227+
token,
228+
quiet,
229+
commitMessage,
230+
pathInRepo,
231+
private: isPrivate,
232+
} = parsedArgs;
233+
234+
const repoId = repoType ? { type: repoType as "model" | "dataset" | "space", name: repoName } : repoName;
235+
236+
if (
237+
!(await repoExists({ repo: repoId, revision, accessToken: token, hubUrl: process.env.HF_ENDPOINT ?? HUB_URL }))
238+
) {
239+
if (!quiet) {
240+
console.log(`Repo ${repoName} does not exist. Creating it...`);
241+
}
242+
await createRepo({
243+
repo: repoId,
244+
accessToken: token,
245+
private: !!isPrivate,
246+
hubUrl: process.env.HF_ENDPOINT ?? HUB_URL,
247+
});
248+
}
223249

224250
const isFile = (await stat(localFolder)).isFile();
225251
const files = isFile
@@ -232,7 +258,7 @@ async function run() {
232258
: [{ content: pathToFileURL(localFolder), path: pathInRepo.replace(/^[.]?\//, "") }];
233259

234260
for await (const event of uploadFilesWithProgress({
235-
repo: repoType ? { type: repoType as "model" | "dataset" | "space", name: repoName } : repoName,
261+
repo: repoId,
236262
files,
237263
branch: revision,
238264
accessToken: token,

0 commit comments

Comments
 (0)