22
33import { parseArgs } from "node:util" ;
44import { typedEntries } from "./src/utils/typedEntries" ;
5- import { createBranch , deleteBranch , uploadFilesWithProgress } from "./src" ;
5+ import { createBranch , createRepo , deleteBranch , repoExists , uploadFilesWithProgress } from "./src" ;
66import { pathToFileURL } from "node:url" ;
77import { stat } from "node:fs/promises" ;
88import { 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