Skip to content

Commit 46c052c

Browse files
committed
docs(changeset): Fix Node FS adapter and Translation replacments
1 parent efb70c7 commit 46c052c

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

.changeset/empty-poems-obey.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@storybooker/adapter-fs": patch
3+
"@storybooker/core": patch
4+
---
5+
6+
Fix Node FS adapter and Translation replacments

packages/adapter-fs/src/fs-storage.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// oxlint-disable no-await-in-loop
2+
13
import * as fs from "node:fs";
24
import * as fsp from "node:fs/promises";
35
import * as path from "node:path";
4-
import { Readable } from "node:stream";
5-
import type { ReadableStream } from "node:stream/web";
6+
import { Readable, type Stream } from "node:stream";
7+
import type { ReadableStream as WebReadableStream } from "node:stream/web";
68
import type { StorageService } from "@storybooker/core/types";
79

810
export class LocalFileStorage implements StorageService {
@@ -89,19 +91,45 @@ export class LocalFileStorage implements StorageService {
8991
) => {
9092
for (const file of files) {
9193
const filepath = this.#genPath(containerId, file.path);
92-
const data =
93-
// oxlint-disable-next-line no-nested-ternary
94-
typeof file.content === "string"
95-
? file.content
96-
: // oxlint-disable-next-line no-nested-ternary
97-
file.content instanceof Blob
98-
? Readable.fromWeb(file.content.stream() as ReadableStream)
99-
: Readable.fromWeb(file.content as ReadableStream);
100-
// oxlint-disable-next-line no-await-in-loop
101-
await fsp.writeFile(filepath, data, {
102-
encoding: "utf8",
103-
signal: options.abortSignal,
104-
});
94+
const dirpath = path.dirname(filepath);
95+
96+
await fsp.mkdir(dirpath, { recursive: true });
97+
if (file.content instanceof ReadableStream) {
98+
await writeWebStreamToFile(file.content, filepath);
99+
} else {
100+
const data: string | Stream =
101+
// oxlint-disable-next-line no-nested-ternary
102+
typeof file.content === "string"
103+
? file.content
104+
: await file.content.text();
105+
106+
await fsp.writeFile(filepath, data, {
107+
encoding: "utf8",
108+
signal: options.abortSignal,
109+
});
110+
}
105111
}
106112
};
107113
}
114+
115+
async function writeWebStreamToFile(
116+
webReadableStream: ReadableStream,
117+
outputPath: string,
118+
): Promise<null> {
119+
// Convert WebReadableStream to Node.js Readable stream
120+
const nodeReadableStream = Readable.fromWeb(
121+
webReadableStream as WebReadableStream,
122+
);
123+
124+
// Create a writable file stream
125+
const fileWritableStream = fs.createWriteStream(outputPath);
126+
127+
// Pipe the Node.js readable stream to the writable file stream
128+
nodeReadableStream.pipe(fileWritableStream);
129+
130+
// Return a promise that resolves when writing is finished
131+
return new Promise((resolve, reject) => {
132+
fileWritableStream.on("finish", () => resolve(null));
133+
fileWritableStream.on("error", reject);
134+
});
135+
}

packages/core/src/builds/model.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ export class BuildsModel extends Model<BuildType> {
337337
dirpath: string,
338338
prefix: string,
339339
): Promise<StoryBookerFile[]> => {
340+
const { streaming } = getStore();
341+
340342
const allEntriesInDir = await fsp.readdir(dirpath, {
341343
encoding: "utf8",
342344
recursive: true,
@@ -348,13 +350,15 @@ export class BuildsModel extends Model<BuildType> {
348350

349351
return allFilesInDir.map((filepath): StoryBookerFile => {
350352
const relativePath = filepath.replace(`${dirpath}/`, "");
351-
const stream = Readable.toWeb(
352-
fs.createReadStream(filepath, { encoding: "binary" }),
353-
);
354-
// const content = await fsp.readFile(filepath, { encoding: "binary" });
353+
const content =
354+
streaming === false
355+
? fs.readFileSync(filepath, { encoding: "binary" })
356+
: (Readable.toWeb(
357+
fs.createReadStream(filepath, { encoding: "binary" }),
358+
) as ReadableStream);
355359

356360
return {
357-
content: stream as ReadableStream,
361+
content,
358362
mimeType: getMimeType(filepath),
359363
path: path.posix.join(prefix, relativePath),
360364
};

packages/core/src/builds/ui/render.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export function renderBuildDetailsPage({
162162
download={`storybook-${projectId}-${build.sha}.zip`}
163163
target="_blank"
164164
>
165-
{commonT.Upload()} {commonT.StoryBook()}
165+
{commonT.Download()} {commonT.StoryBook()}
166166
</a>
167167
) : null}
168168
</DocumentSidebar>

packages/core/src/utils/i18n.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ export function getT<
1111
replacements?: Record<string, string>,
1212
): Translation[Category][Key] {
1313
const value = getStore().translation[category][key];
14-
if (replacements && typeof value === "string") {
14+
15+
let translation: string = typeof value === "string" ? value : String(value);
16+
if (replacements) {
1517
for (const rep of Object.entries(replacements)) {
16-
value.replace(`{{${rep[0]}}}`, rep[1]);
18+
translation = translation.replace(`{{${rep[0]}}}`, rep[1]);
1719
}
1820
}
1921

20-
return value;
22+
return translation as typeof value;
2123
}
2224

2325
export const commonT = {

0 commit comments

Comments
 (0)