Skip to content

Commit

Permalink
feat(store-ftp): return url for createFile and updateFile
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Sep 17, 2023
1 parent f557bc4 commit 9c31153
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
20 changes: 15 additions & 5 deletions packages/store-ftp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ export default class FtpStore {
await client.mkdir(directory, true);
}

return await client.put(readableStream, absolutePath);
await client.put(readableStream, absolutePath);

const url = new URL(this.info.uid);
url.pathname = path.join(url.pathname, filePath);

return url.href;
} catch (error) {
throw new IndiekitError(error.message, {
cause: error,
Expand Down Expand Up @@ -178,11 +183,16 @@ export default class FtpStore {
const readableStream = this.#createReadableStream(content);
const absolutePath = this.#absolutePath(filePath);

const update = await client.put(readableStream, absolutePath);
await client.put(readableStream, absolutePath);

if (options?.newPath) {
await client.rename(absolutePath, this.#absolutePath(options.newPath));
}

const url = new URL(this.info.uid);
url.pathname = path.join(url.pathname, options?.newPath || filePath);

return options?.newPath
? await client.rename(absolutePath, this.#absolutePath(options.newPath))
: update;
return url.href;
} catch (error) {
throw new IndiekitError(error.message, {
cause: error,
Expand Down
17 changes: 4 additions & 13 deletions packages/store-ftp/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ test("Throws error connecting to FTP server", async (t) => {
});

test.serial("Creates file", async (t) => {
t.is(
await ftp.createFile("foo.md", "foobar"),
"Uploaded data stream to foo.md",
);
t.is(await ftp.createFile("foo.md", "foobar"), "sftp://127.0.0.1/foo.md");
});

test("Throws error creating file", async (t) => {
Expand All @@ -81,26 +78,20 @@ test("Throws error reading file", async (t) => {
});

test.serial("Updates file", async (t) => {
t.is(
await ftp.updateFile("foo.md", "foobar"),
"Uploaded data stream to foo.md",
);
t.is(await ftp.updateFile("foo.md", "foobar"), "sftp://127.0.0.1/foo.md");
});

test("Updates and renames file", async (t) => {
t.is(
await ftp.updateFile("foo.md", "foobar", {
newPath: "bar.md",
}),
"Successfully renamed foo.md to bar.md",
"sftp://127.0.0.1/bar.md",
);
});

test("Creates file if original not found", async (t) => {
t.is(
await ftp.updateFile("qux.md", "foobar"),
"Uploaded data stream to qux.md",
);
t.is(await ftp.updateFile("qux.md", "foobar"), "sftp://127.0.0.1/qux.md");
});

test("Throws error updating file", async (t) => {
Expand Down

0 comments on commit 9c31153

Please sign in to comment.