@openfairygui/functions 0.2.0-alpha.2 → 0.2.0-alpha.21
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.
- package/README.md +48 -6
- package/dist/atlas-C6tbl7nn.d.ts +193 -0
- package/dist/atlas-CHsu2Y8i.d.cts +193 -0
- package/dist/index.cjs +16 -3603
- package/dist/index.d.cts +5 -294
- package/dist/index.d.ts +5 -294
- package/dist/index.js +3 -3594
- package/dist/node.cjs +256 -0
- package/dist/node.d.cts +36 -0
- package/dist/node.d.ts +36 -0
- package/dist/node.js +254 -0
- package/dist/publish-BJ_eelME.js +3267 -0
- package/dist/publish-xFWT9Slz.cjs +3338 -0
- package/dist/restore-BW2xacB3.cjs +936 -0
- package/dist/restore-BeWaJNjR.d.cts +288 -0
- package/dist/restore-Clk62n0O.js +931 -0
- package/dist/restore-Dh0-Nvms.d.ts +288 -0
- package/dist/uam-transaction.cjs +44 -1
- package/dist/uam-transaction.d.cts +16 -1
- package/dist/uam-transaction.d.ts +16 -1
- package/dist/uam-transaction.js +44 -1
- package/dist/web.cjs +274 -0
- package/dist/web.d.cts +41 -0
- package/dist/web.d.ts +41 -0
- package/dist/web.js +273 -0
- package/package.json +28 -4
- package/src/adapters/node/plugins.ts +82 -0
- package/src/adapters/node/publish.ts +130 -0
- package/src/adapters/node/restore.ts +187 -0
- package/src/adapters/web/publish.ts +159 -0
- package/src/adapters/web/raster.ts +251 -0
- package/src/atlas/font.ts +95 -0
- package/src/atlas/inputs.ts +515 -0
- package/src/atlas/jta.ts +211 -0
- package/src/atlas/packing.ts +767 -0
- package/src/atlas.ts +116 -1221
- package/src/codegen.ts +106 -67
- package/src/index.ts +43 -3
- package/src/node.ts +8 -0
- package/src/plugins/types.ts +56 -0
- package/src/publish/contracts.ts +80 -0
- package/src/publish/external-resources.ts +117 -0
- package/src/publish/options.ts +180 -0
- package/src/publish/package-context.ts +608 -0
- package/src/publish/resource-references.ts +210 -0
- package/src/publish.ts +290 -968
- package/src/restore-internals/font.ts +100 -0
- package/src/restore-internals/movie-clip.ts +104 -0
- package/src/restore-internals/output-transaction.ts +164 -0
- package/src/restore.ts +112 -311
- package/src/shared-types.ts +4 -8
- package/src/uam-transaction.ts +68 -0
- package/src/utils.ts +28 -0
- package/src/web.ts +11 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Document } from '@openfairygui/core';
|
|
2
|
+
import {
|
|
3
|
+
formatPluginError,
|
|
4
|
+
type LoadedPlugin,
|
|
5
|
+
type Plugin,
|
|
6
|
+
type PluginManifest,
|
|
7
|
+
type PluginModule,
|
|
8
|
+
} from '../../plugins/types.js';
|
|
9
|
+
|
|
10
|
+
interface PluginPackageJson extends Partial<PluginManifest> {
|
|
11
|
+
name?: string;
|
|
12
|
+
main?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Keep Node builtins out of the neutral bundle resolver while still loading plugins in Node.
|
|
16
|
+
const importNative = new Function('id', 'return import(id)') as <T>(id: string) => Promise<T>;
|
|
17
|
+
|
|
18
|
+
export async function loadPlugins(doc: Document, pluginsDir: string): Promise<LoadedPlugin[]> {
|
|
19
|
+
if (!pluginsDir) return [];
|
|
20
|
+
|
|
21
|
+
const fs = await importNative<typeof import('node:fs/promises')>('node:fs/promises');
|
|
22
|
+
const path = await importNative<typeof import('node:path')>('node:path');
|
|
23
|
+
let entries: Array<{ name: string; isDirectory(): boolean }>;
|
|
24
|
+
try {
|
|
25
|
+
entries = await fs.readdir(pluginsDir, { withFileTypes: true });
|
|
26
|
+
} catch {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const plugins: LoadedPlugin[] = [];
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
if (!entry.isDirectory()) continue;
|
|
33
|
+
const pluginDir = path.join(pluginsDir, entry.name);
|
|
34
|
+
try {
|
|
35
|
+
const manifest = await readPluginManifest(fs, path, pluginDir);
|
|
36
|
+
if (!manifest) continue;
|
|
37
|
+
|
|
38
|
+
const mainPath = resolvePluginMain(path, pluginDir, manifest);
|
|
39
|
+
const plugin = await loadPlugin(mainPath);
|
|
40
|
+
plugins.push({ name: manifest.name, plugin });
|
|
41
|
+
} catch (error) {
|
|
42
|
+
doc.getLogger().warn(`publish: Plugin "${entry.name}" was skipped: ${formatPluginError(error)}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return plugins;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function readPluginManifest(
|
|
50
|
+
fs: typeof import('node:fs/promises'),
|
|
51
|
+
path: typeof import('node:path'),
|
|
52
|
+
pluginDir: string,
|
|
53
|
+
): Promise<PluginPackageJson | null> {
|
|
54
|
+
const manifestPath = path.join(pluginDir, 'package.json');
|
|
55
|
+
const content = await fs.readFile(manifestPath, 'utf-8');
|
|
56
|
+
const manifest = JSON.parse(content) as PluginPackageJson;
|
|
57
|
+
if (!manifest.name) throw new Error(`Codegen plugin at ${pluginDir} is missing package.json name.`);
|
|
58
|
+
if (!manifest.main) throw new Error(`Codegen plugin "${manifest.name}" is missing package.json main.`);
|
|
59
|
+
return manifest;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function resolvePluginMain(path: typeof import('node:path'), pluginDir: string, manifest: PluginPackageJson): string {
|
|
63
|
+
const mainPath = path.resolve(pluginDir, manifest.main!);
|
|
64
|
+
const relative = path.relative(pluginDir, mainPath);
|
|
65
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
66
|
+
throw new Error(`Codegen plugin "${manifest.name}" main must resolve inside its plugin directory.`);
|
|
67
|
+
}
|
|
68
|
+
return mainPath;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function loadPlugin(mainPath: string): Promise<Plugin> {
|
|
72
|
+
const { createJiti } = await importNative<typeof import('jiti')>('jiti');
|
|
73
|
+
const jiti = createJiti(import.meta.url);
|
|
74
|
+
const mod = await jiti.import<PluginModule>(mainPath);
|
|
75
|
+
const defaultExport = mod.default;
|
|
76
|
+
const plugin = isObject(defaultExport) ? defaultExport : mod;
|
|
77
|
+
return plugin as Plugin;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
81
|
+
return value !== null && typeof value === 'object';
|
|
82
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { Document } from '@openfairygui/core';
|
|
2
|
+
import { resolveProjectBasePath } from '../../codegen.js';
|
|
3
|
+
import type { LoadedPlugin } from '../../plugins/types.js';
|
|
4
|
+
import type { AtlasRasterBackend, PublishFileSystem } from '../../publish/contracts.js';
|
|
5
|
+
import { type PublishOptions, publish } from '../../publish.js';
|
|
6
|
+
import { loadPlugins } from './plugins.js';
|
|
7
|
+
|
|
8
|
+
const importNative = new Function('id', 'return import(id)') as <T>(id: string) => Promise<T>;
|
|
9
|
+
|
|
10
|
+
interface NodePublishFileSystem extends PublishFileSystem {
|
|
11
|
+
readFileRaw(path: string): Promise<Uint8Array>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface PublishNodeOptions extends Omit<PublishOptions, 'atlas' | 'basePath' | 'encoder' | 'fs' | 'plugins'> {
|
|
15
|
+
document: Document;
|
|
16
|
+
/**
|
|
17
|
+
* Assets directory. Defaults to `<document project dir>/assets` when available.
|
|
18
|
+
*/
|
|
19
|
+
assetsPath?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Override the standard Sharp raster backend.
|
|
22
|
+
*/
|
|
23
|
+
encoder?: AtlasRasterBackend;
|
|
24
|
+
/**
|
|
25
|
+
* Supply already-loaded hooks. Pass an empty array to skip project plugin discovery.
|
|
26
|
+
*/
|
|
27
|
+
plugins?: LoadedPlugin[];
|
|
28
|
+
atlas?: Omit<NonNullable<PublishOptions['atlas']>, 'readFileRaw'>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function createNodePublishFileSystem(): Promise<NodePublishFileSystem> {
|
|
32
|
+
const [fs, path] = await Promise.all([
|
|
33
|
+
importNative<typeof import('node:fs/promises')>('node:fs/promises'),
|
|
34
|
+
importNative<typeof import('node:path')>('node:path'),
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
async readFileRaw(filePath: string): Promise<Uint8Array> {
|
|
39
|
+
const data = await fs.readFile(filePath);
|
|
40
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
41
|
+
},
|
|
42
|
+
async writeFileRaw(filePath: string, data: Uint8Array): Promise<void> {
|
|
43
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
44
|
+
await fs.writeFile(filePath, data);
|
|
45
|
+
},
|
|
46
|
+
async mkdir(dirPath: string): Promise<void> {
|
|
47
|
+
await fs.mkdir(dirPath, { recursive: true });
|
|
48
|
+
},
|
|
49
|
+
async readdir(dirPath: string): Promise<string[]> {
|
|
50
|
+
return fs.readdir(dirPath);
|
|
51
|
+
},
|
|
52
|
+
async deleteFile(filePath: string): Promise<void> {
|
|
53
|
+
await fs.rm(filePath, { force: true });
|
|
54
|
+
},
|
|
55
|
+
join(...paths: string[]): string {
|
|
56
|
+
return path.join(...paths);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function resolveNodeAssetsPath(document: Document, assetsPath: string | undefined): Promise<string | undefined> {
|
|
62
|
+
if (assetsPath) return assetsPath;
|
|
63
|
+
const projectDir = document.getProjectDir?.() ?? '';
|
|
64
|
+
if (!projectDir) return undefined;
|
|
65
|
+
const path = await importNative<typeof import('node:path')>('node:path');
|
|
66
|
+
return path.join(projectDir, 'assets');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function loadSharpBackend(): Promise<AtlasRasterBackend | undefined> {
|
|
70
|
+
try {
|
|
71
|
+
const loaded = await importNative<typeof import('sharp')>('sharp');
|
|
72
|
+
const sharp = loaded as unknown as { default?: AtlasRasterBackend };
|
|
73
|
+
return sharp.default ?? (loaded as unknown as AtlasRasterBackend);
|
|
74
|
+
} catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function loadNodePublishPlugins(document: Document, assetsPath: string | undefined): Promise<LoadedPlugin[]> {
|
|
80
|
+
const projectDir = document.getProjectDir?.() || (assetsPath ? resolveProjectBasePath(assetsPath) : '');
|
|
81
|
+
if (!projectDir) return [];
|
|
82
|
+
const path = await importNative<typeof import('node:path')>('node:path');
|
|
83
|
+
return loadPlugins(document, path.join(projectDir, 'plugins'));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Publish a FairyGUI project through the standard Node host adapter.
|
|
88
|
+
*
|
|
89
|
+
* The adapter owns Node filesystem, Sharp, and project plugin discovery.
|
|
90
|
+
* For custom environments, use the lower-level `publish()` core with explicit
|
|
91
|
+
* capabilities instead.
|
|
92
|
+
*/
|
|
93
|
+
export async function publishNode(options: PublishNodeOptions): Promise<void> {
|
|
94
|
+
const {
|
|
95
|
+
document,
|
|
96
|
+
assetsPath: configuredAssetsPath,
|
|
97
|
+
atlas,
|
|
98
|
+
encoder: configuredEncoder,
|
|
99
|
+
plugins: configuredPlugins,
|
|
100
|
+
...publishOptions
|
|
101
|
+
} = options;
|
|
102
|
+
const [fileSystem, assetsPath] = await Promise.all([
|
|
103
|
+
createNodePublishFileSystem(),
|
|
104
|
+
resolveNodeAssetsPath(document, configuredAssetsPath),
|
|
105
|
+
]);
|
|
106
|
+
const [encoder, plugins] = await Promise.all([
|
|
107
|
+
configuredEncoder === undefined ? loadSharpBackend() : Promise.resolve(configuredEncoder),
|
|
108
|
+
configuredPlugins === undefined
|
|
109
|
+
? loadNodePublishPlugins(document, assetsPath)
|
|
110
|
+
: Promise.resolve(configuredPlugins),
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
if (!encoder) {
|
|
114
|
+
throw new Error('publishNode: Sharp is required for a complete publish. Install sharp or provide an encoder.');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
await document.transform(
|
|
118
|
+
publish({
|
|
119
|
+
...publishOptions,
|
|
120
|
+
basePath: assetsPath,
|
|
121
|
+
encoder,
|
|
122
|
+
atlas: {
|
|
123
|
+
...atlas,
|
|
124
|
+
readFileRaw: fileSystem.readFileRaw,
|
|
125
|
+
},
|
|
126
|
+
fs: fileSystem,
|
|
127
|
+
plugins,
|
|
128
|
+
}),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RestoreFileSystem,
|
|
3
|
+
RestoreImageCropInput,
|
|
4
|
+
RestoreImageCropper,
|
|
5
|
+
RestoreImageExtractInput,
|
|
6
|
+
RestoreImageExtractor,
|
|
7
|
+
RestoreOptions,
|
|
8
|
+
RestoreResult,
|
|
9
|
+
} from '../../restore.js';
|
|
10
|
+
import { restore } from '../../restore.js';
|
|
11
|
+
|
|
12
|
+
const importNative = new Function('id', 'return import(id)') as <T>(id: string) => Promise<T>;
|
|
13
|
+
|
|
14
|
+
interface RestoreImageProcessors {
|
|
15
|
+
cropImage: RestoreImageCropper;
|
|
16
|
+
extractImage: RestoreImageExtractor;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RestoreNodeOptions extends Omit<RestoreOptions, 'fs' | 'cropImage' | 'extractImage'> {}
|
|
20
|
+
|
|
21
|
+
async function createNodeRestoreFileSystem(): Promise<RestoreFileSystem> {
|
|
22
|
+
const [fs, path] = await Promise.all([
|
|
23
|
+
importNative<typeof import('node:fs/promises')>('node:fs/promises'),
|
|
24
|
+
importNative<typeof import('node:path')>('node:path'),
|
|
25
|
+
]);
|
|
26
|
+
return {
|
|
27
|
+
async readFile(filePath: string): Promise<string> {
|
|
28
|
+
return fs.readFile(filePath, 'utf-8');
|
|
29
|
+
},
|
|
30
|
+
async readFileRaw(filePath: string): Promise<Uint8Array> {
|
|
31
|
+
const buffer = await fs.readFile(filePath);
|
|
32
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
33
|
+
},
|
|
34
|
+
async writeFile(filePath: string, content: string): Promise<void> {
|
|
35
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
36
|
+
await fs.writeFile(filePath, content, 'utf-8');
|
|
37
|
+
},
|
|
38
|
+
async writeFileRaw(filePath: string, data: Uint8Array): Promise<void> {
|
|
39
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
40
|
+
await fs.writeFile(filePath, data);
|
|
41
|
+
},
|
|
42
|
+
async mkdir(dirPath: string): Promise<void> {
|
|
43
|
+
await fs.mkdir(dirPath, { recursive: true });
|
|
44
|
+
},
|
|
45
|
+
async readdir(dirPath: string): Promise<string[]> {
|
|
46
|
+
return fs.readdir(dirPath);
|
|
47
|
+
},
|
|
48
|
+
async exists(filePath: string): Promise<boolean> {
|
|
49
|
+
try {
|
|
50
|
+
await fs.access(filePath);
|
|
51
|
+
return true;
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
async isFile(filePath: string): Promise<boolean> {
|
|
57
|
+
try {
|
|
58
|
+
return (await fs.stat(filePath)).isFile();
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
async resolvePath(filePath: string): Promise<string> {
|
|
64
|
+
try {
|
|
65
|
+
return await fs.realpath(filePath);
|
|
66
|
+
} catch {
|
|
67
|
+
return path.resolve(filePath);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
async rm(targetPath: string, options?: { recursive?: boolean; force?: boolean }): Promise<void> {
|
|
71
|
+
await fs.rm(targetPath, {
|
|
72
|
+
recursive: options?.recursive ?? false,
|
|
73
|
+
force: options?.force ?? false,
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
async rename(from: string, to: string): Promise<void> {
|
|
77
|
+
await fs.rename(from, to);
|
|
78
|
+
},
|
|
79
|
+
join(...paths: string[]): string {
|
|
80
|
+
return path.join(...paths);
|
|
81
|
+
},
|
|
82
|
+
dirname(filePath: string): string {
|
|
83
|
+
return path.dirname(filePath);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function createRestoreImageProcessors(): Promise<RestoreImageProcessors> {
|
|
89
|
+
let sharp: any;
|
|
90
|
+
try {
|
|
91
|
+
const loaded = await importNative<any>('sharp');
|
|
92
|
+
sharp = loaded.default ?? loaded;
|
|
93
|
+
} catch {
|
|
94
|
+
throw new Error('restoreNode: Sharp is required to crop atlas images. Install sharp before restoring.');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function extractImage(input: RestoreImageExtractInput): Promise<Uint8Array> {
|
|
98
|
+
const targetPath = (input as RestoreImageCropInput).outputPath ?? input.sourcePath;
|
|
99
|
+
let image = sharp(input.sourcePath).extract({
|
|
100
|
+
left: input.left,
|
|
101
|
+
top: input.top,
|
|
102
|
+
width: input.width,
|
|
103
|
+
height: input.height,
|
|
104
|
+
});
|
|
105
|
+
if (input.rotated) image = image.rotate(90);
|
|
106
|
+
const { data, info } = await image.png().toBuffer({ resolveWithObject: true });
|
|
107
|
+
const needsOriginalCanvas =
|
|
108
|
+
input.expectedWidth > 0 &&
|
|
109
|
+
input.expectedHeight > 0 &&
|
|
110
|
+
(input.offsetX !== 0 ||
|
|
111
|
+
input.offsetY !== 0 ||
|
|
112
|
+
info.width !== input.expectedWidth ||
|
|
113
|
+
info.height !== input.expectedHeight);
|
|
114
|
+
|
|
115
|
+
if (needsOriginalCanvas) {
|
|
116
|
+
if (
|
|
117
|
+
input.offsetX < 0 ||
|
|
118
|
+
input.offsetY < 0 ||
|
|
119
|
+
input.offsetX + info.width > input.expectedWidth ||
|
|
120
|
+
input.offsetY + info.height > input.expectedHeight
|
|
121
|
+
) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
`restore: Cropped image does not fit original canvas for ${targetPath}: ` +
|
|
124
|
+
`crop ${info.width}x${info.height} at ${input.offsetX},${input.offsetY}, ` +
|
|
125
|
+
`canvas ${input.expectedWidth}x${input.expectedHeight}`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
const composed = await sharp({
|
|
129
|
+
create: {
|
|
130
|
+
width: input.expectedWidth,
|
|
131
|
+
height: input.expectedHeight,
|
|
132
|
+
channels: 4,
|
|
133
|
+
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
134
|
+
},
|
|
135
|
+
})
|
|
136
|
+
.composite([{ input: data, left: input.offsetX, top: input.offsetY }])
|
|
137
|
+
.png()
|
|
138
|
+
.toBuffer({ resolveWithObject: true });
|
|
139
|
+
if (
|
|
140
|
+
composed.info.width !== input.expectedWidth ||
|
|
141
|
+
composed.info.height !== input.expectedHeight
|
|
142
|
+
) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`restore: Cropped image size mismatch for ${targetPath}: ` +
|
|
145
|
+
`expected ${input.expectedWidth}x${input.expectedHeight}, ` +
|
|
146
|
+
`got ${composed.info.width}x${composed.info.height}`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
return composed.data;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (
|
|
153
|
+
input.expectedWidth > 0 &&
|
|
154
|
+
input.expectedHeight > 0 &&
|
|
155
|
+
(info.width !== input.expectedWidth || info.height !== input.expectedHeight)
|
|
156
|
+
) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
`restore: Cropped image size mismatch for ${targetPath}: ` +
|
|
159
|
+
`expected ${input.expectedWidth}x${input.expectedHeight}, got ${info.width}x${info.height}`,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
return data;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const fs = await importNative<typeof import('node:fs/promises')>('node:fs/promises');
|
|
166
|
+
const path = await importNative<typeof import('node:path')>('node:path');
|
|
167
|
+
return {
|
|
168
|
+
extractImage,
|
|
169
|
+
cropImage: async (input: RestoreImageCropInput): Promise<void> => {
|
|
170
|
+
await fs.mkdir(path.dirname(input.outputPath), { recursive: true });
|
|
171
|
+
await fs.writeFile(input.outputPath, await extractImage(input));
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Restore trusted local published artifacts through the standard Node host adapter. */
|
|
177
|
+
export async function restoreNode(options: RestoreNodeOptions): Promise<RestoreResult> {
|
|
178
|
+
const [fs, imageProcessors] = await Promise.all([
|
|
179
|
+
createNodeRestoreFileSystem(),
|
|
180
|
+
createRestoreImageProcessors(),
|
|
181
|
+
]);
|
|
182
|
+
return restore({
|
|
183
|
+
...options,
|
|
184
|
+
fs,
|
|
185
|
+
...imageProcessors,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { type Document, type ILogger, ProjectType } from '@openfairygui/core';
|
|
2
|
+
import type { AtlasOptions } from '../../atlas.js';
|
|
3
|
+
import { publish } from '../../publish.js';
|
|
4
|
+
import type {
|
|
5
|
+
PublishFileSystem,
|
|
6
|
+
PublishOutputFileSystem,
|
|
7
|
+
PublishSourceFileSystem,
|
|
8
|
+
} from '../../publish/contracts.js';
|
|
9
|
+
import { assertBrowserImageSupport, createBrowserImageEncoder } from './raster.js';
|
|
10
|
+
|
|
11
|
+
export type BrowserPublishProjectType = 'layabox';
|
|
12
|
+
|
|
13
|
+
export type BrowserPublishAtlasOptions = Pick<
|
|
14
|
+
AtlasOptions,
|
|
15
|
+
| 'maxSize'
|
|
16
|
+
| 'fast'
|
|
17
|
+
| 'allowRotation'
|
|
18
|
+
| 'padding'
|
|
19
|
+
| 'powerOfTwo'
|
|
20
|
+
| 'square'
|
|
21
|
+
| 'multiPage'
|
|
22
|
+
| 'trimImage'
|
|
23
|
+
| 'extractAlpha'
|
|
24
|
+
>;
|
|
25
|
+
|
|
26
|
+
export type BrowserPublishSourceFileSystem = PublishSourceFileSystem;
|
|
27
|
+
|
|
28
|
+
export type BrowserPublishOutputFileSystem = PublishOutputFileSystem;
|
|
29
|
+
|
|
30
|
+
export interface BrowserPublishOptions {
|
|
31
|
+
document: Document;
|
|
32
|
+
sourceFileSystem: BrowserPublishSourceFileSystem;
|
|
33
|
+
outputFileSystem: BrowserPublishOutputFileSystem;
|
|
34
|
+
projectType: BrowserPublishProjectType;
|
|
35
|
+
output: string;
|
|
36
|
+
compressed?: boolean;
|
|
37
|
+
packages?: string[];
|
|
38
|
+
branch?: string;
|
|
39
|
+
atlas?: BrowserPublishAtlasOptions;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface BrowserPublishDiagnostic {
|
|
43
|
+
level: 'debug' | 'info' | 'warning' | 'error';
|
|
44
|
+
message: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface BrowserPublishedFile {
|
|
48
|
+
path: string;
|
|
49
|
+
size: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface BrowserPublishResult {
|
|
53
|
+
success: boolean;
|
|
54
|
+
files: BrowserPublishedFile[];
|
|
55
|
+
diagnostics: BrowserPublishDiagnostic[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function createTrackingFileSystem(
|
|
59
|
+
fileSystem: BrowserPublishOutputFileSystem,
|
|
60
|
+
files: Map<string, number>,
|
|
61
|
+
): PublishFileSystem {
|
|
62
|
+
const tracked: PublishFileSystem = {
|
|
63
|
+
join: (...paths) => fileSystem.join(...paths),
|
|
64
|
+
mkdir: (path) => fileSystem.mkdir(path),
|
|
65
|
+
writeFileRaw: async (path, data) => {
|
|
66
|
+
await fileSystem.writeFileRaw(path, data);
|
|
67
|
+
files.set(path, data.byteLength);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
return tracked;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function createDiagnosticLogger(logger: ILogger, diagnostics: BrowserPublishDiagnostic[]): ILogger {
|
|
74
|
+
return {
|
|
75
|
+
debug(message) {
|
|
76
|
+
diagnostics.push({ level: 'debug', message });
|
|
77
|
+
logger.debug(message);
|
|
78
|
+
},
|
|
79
|
+
info(message) {
|
|
80
|
+
diagnostics.push({ level: 'info', message });
|
|
81
|
+
logger.info(message);
|
|
82
|
+
},
|
|
83
|
+
warn(message) {
|
|
84
|
+
diagnostics.push({ level: 'warning', message });
|
|
85
|
+
logger.warn(message);
|
|
86
|
+
},
|
|
87
|
+
error(message) {
|
|
88
|
+
diagnostics.push({ level: 'error', message });
|
|
89
|
+
logger.error(message);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function toResult(
|
|
95
|
+
success: boolean,
|
|
96
|
+
files: Map<string, number>,
|
|
97
|
+
diagnostics: BrowserPublishDiagnostic[],
|
|
98
|
+
): BrowserPublishResult {
|
|
99
|
+
return {
|
|
100
|
+
success,
|
|
101
|
+
files: [...files].map(([path, size]) => ({ path, size })),
|
|
102
|
+
diagnostics,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Publish a loaded FairyGUI project to browser-provided storage.
|
|
108
|
+
*
|
|
109
|
+
* The adapter uses browser Canvas APIs for atlas composition, writes only through
|
|
110
|
+
* the supplied output filesystem, and intentionally skips Node publish plugins.
|
|
111
|
+
*/
|
|
112
|
+
export async function publishBrowser(options: BrowserPublishOptions): Promise<BrowserPublishResult> {
|
|
113
|
+
const files = new Map<string, number>();
|
|
114
|
+
const diagnostics: BrowserPublishDiagnostic[] = [];
|
|
115
|
+
const root = options.document.getRoot();
|
|
116
|
+
const previousProjectType = root.getProjectType();
|
|
117
|
+
const previousLogger = options.document.getLogger();
|
|
118
|
+
options.document.setLogger(createDiagnosticLogger(previousLogger, diagnostics));
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
if (options.projectType !== 'layabox') {
|
|
122
|
+
throw new Error(`publishBrowser: unsupported project type "${String(options.projectType)}".`);
|
|
123
|
+
}
|
|
124
|
+
assertBrowserImageSupport();
|
|
125
|
+
root.setProjectType(ProjectType.LayaBox);
|
|
126
|
+
const outputFileSystem = createTrackingFileSystem(options.outputFileSystem, files);
|
|
127
|
+
const sourceAssetsPath = options.sourceFileSystem.join(options.document.getProjectDir(), 'assets');
|
|
128
|
+
|
|
129
|
+
await options.document.transform(
|
|
130
|
+
publish({
|
|
131
|
+
output: options.output,
|
|
132
|
+
compressed: options.compressed,
|
|
133
|
+
fileExtension: 'fui',
|
|
134
|
+
packages: options.packages,
|
|
135
|
+
branch: options.branch,
|
|
136
|
+
basePath: sourceAssetsPath,
|
|
137
|
+
encoder: createBrowserImageEncoder(options.sourceFileSystem, outputFileSystem),
|
|
138
|
+
atlas: {
|
|
139
|
+
...options.atlas,
|
|
140
|
+
readFileRaw: (path) => options.sourceFileSystem.readFileRaw(path),
|
|
141
|
+
},
|
|
142
|
+
fs: outputFileSystem,
|
|
143
|
+
plugins: [],
|
|
144
|
+
codeGeneration: false,
|
|
145
|
+
}),
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
return toResult(true, files, diagnostics);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
diagnostics.push({
|
|
151
|
+
level: 'error',
|
|
152
|
+
message: error instanceof Error ? error.message : String(error),
|
|
153
|
+
});
|
|
154
|
+
return toResult(false, files, diagnostics);
|
|
155
|
+
} finally {
|
|
156
|
+
root.setProjectType(previousProjectType);
|
|
157
|
+
options.document.setLogger(previousLogger);
|
|
158
|
+
}
|
|
159
|
+
}
|