@openfairygui/cli 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.
- package/README.md +1 -0
- package/dist/cli.mjs +5794 -5734
- package/package.json +4 -4
- package/src/commands/restore.ts +2 -165
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfairygui/cli",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.20",
|
|
4
4
|
"description": "FairyGUI Headless Authoring SDK — command-line interface.",
|
|
5
5
|
"author": "OpenFairyGUI Contributors",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,13 +33,13 @@
|
|
|
33
33
|
"restore"
|
|
34
34
|
],
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@openfairygui/
|
|
37
|
-
"@openfairygui/
|
|
36
|
+
"@openfairygui/core": "0.2.0-alpha.20",
|
|
37
|
+
"@openfairygui/functions": "0.2.0-alpha.20"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"commander": "^14.0.2",
|
|
41
41
|
"jiti": "^2.7.0",
|
|
42
|
-
"@openfairygui/backend": "0.2.0-alpha.
|
|
42
|
+
"@openfairygui/backend": "0.2.0-alpha.20"
|
|
43
43
|
},
|
|
44
44
|
"optionalDependencies": {
|
|
45
45
|
"sharp": ">=0.33.0"
|
package/src/commands/restore.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
1
|
import path from 'node:path';
|
|
3
|
-
import {
|
|
4
|
-
type RestoreFileSystem,
|
|
5
|
-
type RestoreImageCropInput,
|
|
6
|
-
type RestoreImageCropper,
|
|
7
|
-
type RestoreImageExtractInput,
|
|
8
|
-
type RestoreImageExtractor,
|
|
9
|
-
restore,
|
|
10
|
-
} from '@openfairygui/functions';
|
|
2
|
+
import { restoreNode } from '@openfairygui/functions/node';
|
|
11
3
|
import type { Command } from 'commander';
|
|
12
4
|
import { parseProjectType } from '../utils/project-type.js';
|
|
13
5
|
|
|
@@ -18,11 +10,6 @@ type RestoreCommandOptions = {
|
|
|
18
10
|
projectType?: string;
|
|
19
11
|
};
|
|
20
12
|
|
|
21
|
-
interface RestoreImageProcessors {
|
|
22
|
-
cropImage: RestoreImageCropper;
|
|
23
|
-
extractImage: RestoreImageExtractor;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
13
|
export function registerRestoreCommand(program: Command): void {
|
|
27
14
|
program
|
|
28
15
|
.command('restore')
|
|
@@ -42,18 +29,14 @@ export function registerRestoreCommand(program: Command): void {
|
|
|
42
29
|
.filter(Boolean)
|
|
43
30
|
: undefined;
|
|
44
31
|
const projectType = parseProjectType(options.projectType);
|
|
45
|
-
const { cropImage, extractImage } = await createRestoreImageProcessors();
|
|
46
32
|
|
|
47
33
|
console.log(`Restoring published FairyGUI project: ${inputDir}`);
|
|
48
|
-
const result = await
|
|
34
|
+
const result = await restoreNode({
|
|
49
35
|
inputDir,
|
|
50
36
|
output: outputDir,
|
|
51
|
-
fs: createNodeRestoreFs(),
|
|
52
37
|
packages: pkgFilter,
|
|
53
38
|
force: options.force,
|
|
54
39
|
projectType,
|
|
55
|
-
cropImage,
|
|
56
|
-
extractImage,
|
|
57
40
|
});
|
|
58
41
|
|
|
59
42
|
const packages = result.document.getRoot().listPackages();
|
|
@@ -64,149 +47,3 @@ export function registerRestoreCommand(program: Command): void {
|
|
|
64
47
|
}
|
|
65
48
|
});
|
|
66
49
|
}
|
|
67
|
-
|
|
68
|
-
function createNodeRestoreFs(): RestoreFileSystem {
|
|
69
|
-
return {
|
|
70
|
-
async readFile(filePath: string): Promise<string> {
|
|
71
|
-
return fs.readFile(filePath, 'utf-8');
|
|
72
|
-
},
|
|
73
|
-
async readFileRaw(filePath: string): Promise<Uint8Array> {
|
|
74
|
-
const buf = await fs.readFile(filePath);
|
|
75
|
-
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
76
|
-
},
|
|
77
|
-
async writeFile(filePath: string, content: string): Promise<void> {
|
|
78
|
-
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
79
|
-
await fs.writeFile(filePath, content, 'utf-8');
|
|
80
|
-
},
|
|
81
|
-
async writeFileRaw(filePath: string, data: Uint8Array): Promise<void> {
|
|
82
|
-
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
83
|
-
await fs.writeFile(filePath, data);
|
|
84
|
-
},
|
|
85
|
-
async mkdir(dirPath: string): Promise<void> {
|
|
86
|
-
await fs.mkdir(dirPath, { recursive: true });
|
|
87
|
-
},
|
|
88
|
-
async readdir(dirPath: string): Promise<string[]> {
|
|
89
|
-
return fs.readdir(dirPath);
|
|
90
|
-
},
|
|
91
|
-
async exists(filePath: string): Promise<boolean> {
|
|
92
|
-
try {
|
|
93
|
-
await fs.access(filePath);
|
|
94
|
-
return true;
|
|
95
|
-
} catch {
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
async isFile(filePath: string): Promise<boolean> {
|
|
100
|
-
try {
|
|
101
|
-
return (await fs.stat(filePath)).isFile();
|
|
102
|
-
} catch {
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
async resolvePath(filePath: string): Promise<string> {
|
|
107
|
-
try {
|
|
108
|
-
return await fs.realpath(filePath);
|
|
109
|
-
} catch {
|
|
110
|
-
return path.resolve(filePath);
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
async rm(targetPath: string, options?: { recursive?: boolean; force?: boolean }): Promise<void> {
|
|
114
|
-
await fs.rm(targetPath, { recursive: options?.recursive ?? false, force: options?.force ?? false });
|
|
115
|
-
},
|
|
116
|
-
async rename(from: string, to: string): Promise<void> {
|
|
117
|
-
await fs.rename(from, to);
|
|
118
|
-
},
|
|
119
|
-
join(...paths: string[]): string {
|
|
120
|
-
return path.join(...paths);
|
|
121
|
-
},
|
|
122
|
-
dirname(filePath: string): string {
|
|
123
|
-
return path.dirname(filePath);
|
|
124
|
-
},
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async function createRestoreImageProcessors(): Promise<RestoreImageProcessors> {
|
|
129
|
-
let sharp: any;
|
|
130
|
-
try {
|
|
131
|
-
const mod = await import('sharp');
|
|
132
|
-
sharp = mod.default ?? mod;
|
|
133
|
-
} catch {
|
|
134
|
-
throw new Error('restore: sharp is required to crop atlas images. Install it with: pnpm add sharp');
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async function extractImage(input: RestoreImageExtractInput): Promise<Uint8Array> {
|
|
138
|
-
const targetPath = (input as RestoreImageCropInput).outputPath ?? input.sourcePath;
|
|
139
|
-
let image = sharp(input.sourcePath).extract({
|
|
140
|
-
left: input.left,
|
|
141
|
-
top: input.top,
|
|
142
|
-
width: input.width,
|
|
143
|
-
height: input.height,
|
|
144
|
-
});
|
|
145
|
-
if (input.rotated) image = image.rotate(90);
|
|
146
|
-
const { data, info } = await image.png().toBuffer({ resolveWithObject: true });
|
|
147
|
-
const needsOriginalCanvas =
|
|
148
|
-
input.expectedWidth > 0 &&
|
|
149
|
-
input.expectedHeight > 0 &&
|
|
150
|
-
(input.offsetX !== 0 ||
|
|
151
|
-
input.offsetY !== 0 ||
|
|
152
|
-
info.width !== input.expectedWidth ||
|
|
153
|
-
info.height !== input.expectedHeight);
|
|
154
|
-
|
|
155
|
-
if (needsOriginalCanvas) {
|
|
156
|
-
if (
|
|
157
|
-
input.offsetX < 0 ||
|
|
158
|
-
input.offsetY < 0 ||
|
|
159
|
-
input.offsetX + info.width > input.expectedWidth ||
|
|
160
|
-
input.offsetY + info.height > input.expectedHeight
|
|
161
|
-
) {
|
|
162
|
-
throw new Error(
|
|
163
|
-
`restore: Cropped image does not fit original canvas for ${targetPath}: ` +
|
|
164
|
-
`crop ${info.width}x${info.height} at ${input.offsetX},${input.offsetY}, ` +
|
|
165
|
-
`canvas ${input.expectedWidth}x${input.expectedHeight}`,
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
const composed = await sharp({
|
|
169
|
-
create: {
|
|
170
|
-
width: input.expectedWidth,
|
|
171
|
-
height: input.expectedHeight,
|
|
172
|
-
channels: 4,
|
|
173
|
-
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
174
|
-
},
|
|
175
|
-
})
|
|
176
|
-
.composite([{ input: data, left: input.offsetX, top: input.offsetY }])
|
|
177
|
-
.png()
|
|
178
|
-
.toBuffer({ resolveWithObject: true });
|
|
179
|
-
if (
|
|
180
|
-
input.expectedWidth > 0 &&
|
|
181
|
-
input.expectedHeight > 0 &&
|
|
182
|
-
(composed.info.width !== input.expectedWidth || composed.info.height !== input.expectedHeight)
|
|
183
|
-
) {
|
|
184
|
-
throw new Error(
|
|
185
|
-
`restore: Cropped image size mismatch for ${targetPath}: ` +
|
|
186
|
-
`expected ${input.expectedWidth}x${input.expectedHeight}, got ${composed.info.width}x${composed.info.height}`,
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
return composed.data;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (
|
|
193
|
-
input.expectedWidth > 0 &&
|
|
194
|
-
input.expectedHeight > 0 &&
|
|
195
|
-
(info.width !== input.expectedWidth || info.height !== input.expectedHeight)
|
|
196
|
-
) {
|
|
197
|
-
throw new Error(
|
|
198
|
-
`restore: Cropped image size mismatch for ${targetPath}: ` +
|
|
199
|
-
`expected ${input.expectedWidth}x${input.expectedHeight}, got ${info.width}x${info.height}`,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
return data;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return {
|
|
206
|
-
extractImage,
|
|
207
|
-
cropImage: async (input: RestoreImageCropInput): Promise<void> => {
|
|
208
|
-
await fs.mkdir(path.dirname(input.outputPath), { recursive: true });
|
|
209
|
-
await fs.writeFile(input.outputPath, await extractImage(input));
|
|
210
|
-
},
|
|
211
|
-
};
|
|
212
|
-
}
|