Skip to content

Commit 4aefa05

Browse files
committed
Also support pathInRepo positional arg
1 parent 04d7fdd commit 4aefa05

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

packages/hub/cli.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { typedEntries } from "./src/utils/typedEntries";
55
import { createBranch, uploadFilesWithProgress } from "./src";
66
import { pathToFileURL } from "node:url";
77
import { HUB_URL } from "./src/consts";
8+
import { stat } from "node:fs/promises";
9+
import { basename, join } from "node:path";
810

911
// Didn't find the import from "node:util", so duplicated it here
1012
type OptionToken =
@@ -39,12 +41,12 @@ const commands = {
3941
positional: true,
4042
default: () => process.cwd(),
4143
},
42-
// {
43-
// name: "path-in-repo" as const,
44-
// description: "The path in the repo to upload the folder to. Defaults to the root of the repo",
45-
// positional: true,
46-
// default: "/",
47-
// },
44+
{
45+
name: "path-in-repo" as const,
46+
description: "The path in the repo to upload the folder to. Defaults to the root of the repo",
47+
positional: true,
48+
default: ".",
49+
},
4850
{
4951
name: "quiet" as const,
5052
short: "q",
@@ -158,6 +160,7 @@ async function run() {
158160
quiet,
159161
commitMessage,
160162
hubUrl,
163+
pathInRepo,
161164
} = parsedArgs;
162165

163166
if (revision && revision !== "main") {
@@ -172,9 +175,19 @@ async function run() {
172175
});
173176
}
174177

178+
const isFile = (await stat(localFolder)).isFile();
179+
const files = isFile
180+
? [
181+
{
182+
content: pathToFileURL(localFolder),
183+
path: join(pathInRepo, `${basename(localFolder)}`).replace(/^[.]?\//, ""),
184+
},
185+
]
186+
: [{ content: pathToFileURL(localFolder), path: pathInRepo.replace(/^[.]?\//, "") }];
187+
175188
for await (const event of uploadFilesWithProgress({
176189
repo: repoType ? { type: repoType as "model" | "dataset" | "space", name: repoName } : repoName,
177-
files: [pathToFileURL(localFolder)],
190+
files,
178191
branch: revision,
179192
accessToken: token,
180193
commitTitle: commitMessage?.trim().split("\n")[0],

packages/hub/src/utils/createBlobs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ export async function createBlobs(
3838

3939
return Promise.all(
4040
paths.map(async (path) => ({
41-
path: `${destPath}/${path.relativePath}`.replace(/\/[.]$/, "").replaceAll("//", "/"),
41+
path: `${destPath}/${path.relativePath}`
42+
.replace(/\/[.]$/, "")
43+
.replaceAll("//", "/")
44+
.replace(/^[.]?\//, ""),
4245
blob: await FileBlob.create(new URL(path.path)),
4346
}))
4447
);

0 commit comments

Comments
 (0)