@openfairygui/backend 0.2.0-alpha.19 → 0.2.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { type FileSystem, type ProjectSourceFile, ProjectWriter } from '@openfairygui/core/project-io';
1
+ import type { ProjectSourceFile } from '@openfairygui/core/project-io';
2
2
  import {
3
3
  commitUamProjectSourcePaths,
4
4
  materializeUamProject,
@@ -28,69 +28,9 @@ import type {
28
28
  import type { CacheService } from './cache-service.js';
29
29
  import { type BackendContext, failure, success } from './context.js';
30
30
  import type { EventService } from './event-service.js';
31
+ import { writeSessionProject } from './session-project-writer.js';
31
32
  import { createSessionNotFoundError, createStaleWriteError, toSessionSnapshot } from './session-utils.js';
32
33
 
33
- function createWriterFileSystem(
34
- fileSystem: BackendFileSystem,
35
- committedPaths: string[],
36
- failedPaths: string[],
37
- ): FileSystem {
38
- async function trackWrite<T>(targetPath: string, fn: () => Promise<T>): Promise<T> {
39
- try {
40
- const result = await fn();
41
- committedPaths.push(targetPath);
42
- return result;
43
- } catch (error) {
44
- failedPaths.push(targetPath);
45
- throw error;
46
- }
47
- }
48
-
49
- return {
50
- async readFile(filePath: string): Promise<string> {
51
- return fileSystem.readFile(filePath);
52
- },
53
- async readFileRaw(filePath: string): Promise<Uint8Array> {
54
- return fileSystem.readFileRaw(filePath);
55
- },
56
- async writeFile(filePath: string, content: string): Promise<void> {
57
- await trackWrite(filePath, async () => {
58
- await fileSystem.mkdir(fileSystem.dirname(filePath), { recursive: true });
59
- await fileSystem.writeFile(filePath, content);
60
- });
61
- },
62
- async writeFileRaw(filePath: string, data: Uint8Array): Promise<void> {
63
- await trackWrite(filePath, async () => {
64
- await fileSystem.mkdir(fileSystem.dirname(filePath), { recursive: true });
65
- await fileSystem.writeFileRaw(filePath, data);
66
- });
67
- },
68
- async mkdir(dirPath: string): Promise<void> {
69
- await fileSystem.mkdir(dirPath, { recursive: true });
70
- },
71
- async readdir(dirPath: string): Promise<string[]> {
72
- return fileSystem.readdir(dirPath);
73
- },
74
- async exists(filePath: string): Promise<boolean> {
75
- try {
76
- await fileSystem.stat(filePath);
77
- return true;
78
- } catch {
79
- return false;
80
- }
81
- },
82
- join(...paths: string[]): string {
83
- return fileSystem.join(...paths);
84
- },
85
- dirname(filePath: string): string {
86
- return fileSystem.dirname(filePath);
87
- },
88
- async unlink(filePath: string): Promise<void> {
89
- await trackWrite(filePath, () => fileSystem.unlink(filePath));
90
- },
91
- };
92
- }
93
-
94
34
  function projectSourceFiles(project: UamProject): Map<string, ProjectSourceFile> {
95
35
  const result = new Map<string, ProjectSourceFile>();
96
36
  for (const pkg of project.packages) {
@@ -466,9 +406,13 @@ export class AuthoringService {
466
406
  revision: session.revision,
467
407
  });
468
408
  try {
469
- const writer = new ProjectWriter(createWriterFileSystem(fileSystem, committedPaths, failedPaths));
470
- await writer.write(materializeUamProject(session.project), session.fairyPath, {
409
+ await writeSessionProject({
410
+ fileSystem,
411
+ document: materializeUamProject(session.project),
412
+ fairyPath: session.fairyPath,
471
413
  staleSourceFiles: [...session.pendingStaleSourceFiles.values()],
414
+ writtenPaths: committedPaths,
415
+ failedPaths,
472
416
  });
473
417
  session.fileSystem ??= fileSystem;
474
418
  session.pendingStaleSourceFiles.clear();
@@ -700,10 +644,14 @@ export class AuthoringService {
700
644
  revision: session.revision,
701
645
  });
702
646
  try {
703
- const writer = new ProjectWriter(createWriterFileSystem(fileSystem, writtenPaths, failedPaths));
704
647
  const isSessionStorageTarget = fileSystem === session.fileSystem && fairyPath === session.fairyPath;
705
- await writer.write(document, fairyPath, {
648
+ await writeSessionProject({
649
+ fileSystem,
650
+ document,
651
+ fairyPath,
706
652
  staleSourceFiles: isSessionStorageTarget ? [...session.pendingStaleSourceFiles.values()] : [],
653
+ writtenPaths,
654
+ failedPaths,
707
655
  });
708
656
  if (isSessionStorageTarget) session.pendingStaleSourceFiles.clear();
709
657
  if (storageTarget && !isSessionStorageTarget) session.pendingStaleSourceFiles.clear();
@@ -0,0 +1,64 @@
1
+ import type { Document } from '@openfairygui/core';
2
+ import { type FileSystem, type ProjectSourceFile, ProjectWriter } from '@openfairygui/core/project-io';
3
+ import type { BackendFileSystem } from '../runtime.js';
4
+
5
+ function createWriterFileSystem(
6
+ fileSystem: BackendFileSystem,
7
+ writtenPaths: string[],
8
+ failedPaths: string[],
9
+ ): FileSystem {
10
+ async function trackWrite<T>(targetPath: string, write: () => Promise<T>): Promise<T> {
11
+ try {
12
+ const result = await write();
13
+ writtenPaths.push(targetPath);
14
+ return result;
15
+ } catch (error) {
16
+ failedPaths.push(targetPath);
17
+ throw error;
18
+ }
19
+ }
20
+
21
+ return {
22
+ readFile: (path) => fileSystem.readFile(path),
23
+ readFileRaw: (path) => fileSystem.readFileRaw(path),
24
+ writeFile: (path, content) =>
25
+ trackWrite(path, async () => {
26
+ await fileSystem.mkdir(fileSystem.dirname(path), { recursive: true });
27
+ await fileSystem.writeFile(path, content);
28
+ }),
29
+ writeFileRaw: (path, data) =>
30
+ trackWrite(path, async () => {
31
+ await fileSystem.mkdir(fileSystem.dirname(path), { recursive: true });
32
+ await fileSystem.writeFileRaw(path, data);
33
+ }),
34
+ mkdir: (path) => fileSystem.mkdir(path, { recursive: true }),
35
+ readdir: (path) => fileSystem.readdir(path),
36
+ async exists(path): Promise<boolean> {
37
+ try {
38
+ await fileSystem.stat(path);
39
+ return true;
40
+ } catch {
41
+ return false;
42
+ }
43
+ },
44
+ join: (...paths) => fileSystem.join(...paths),
45
+ dirname: (path) => fileSystem.dirname(path),
46
+ unlink: (path) => trackWrite(path, () => fileSystem.unlink(path)),
47
+ };
48
+ }
49
+
50
+ export async function writeSessionProject(input: {
51
+ fileSystem: BackendFileSystem;
52
+ document: Document;
53
+ fairyPath: string;
54
+ staleSourceFiles: ProjectSourceFile[];
55
+ writtenPaths: string[];
56
+ failedPaths: string[];
57
+ }): Promise<void> {
58
+ const writer = new ProjectWriter(
59
+ createWriterFileSystem(input.fileSystem, input.writtenPaths, input.failedPaths),
60
+ );
61
+ await writer.write(input.document, input.fairyPath, {
62
+ staleSourceFiles: input.staleSourceFiles,
63
+ });
64
+ }