@ptkl/toolkit 0.10.0 → 0.10.2
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/dist/commands/forge.js +41 -12
- package/package.json +1 -1
package/dist/commands/forge.js
CHANGED
|
@@ -5,7 +5,7 @@ import { Writable } from "stream";
|
|
|
5
5
|
import { writeFileSync, readFileSync, rmSync, existsSync } from "fs";
|
|
6
6
|
import axios from 'axios';
|
|
7
7
|
import Util from "../lib/util.js";
|
|
8
|
-
import { join } from 'path';
|
|
8
|
+
import { extname, isAbsolute, join } from 'path';
|
|
9
9
|
import { forgeDevKernelPlugin } from '../lib/forgeDevKernelPlugin.js';
|
|
10
10
|
function formatBuildError(reason) {
|
|
11
11
|
const lines = [];
|
|
@@ -30,6 +30,36 @@ function formatBuildError(reason) {
|
|
|
30
30
|
}
|
|
31
31
|
return lines.join('\n') || String(reason);
|
|
32
32
|
}
|
|
33
|
+
function iconMimeType(iconPath) {
|
|
34
|
+
const extension = extname(iconPath).toLowerCase();
|
|
35
|
+
if (extension === '.png')
|
|
36
|
+
return 'image/png';
|
|
37
|
+
if (extension === '.jpg' || extension === '.jpeg')
|
|
38
|
+
return 'image/jpeg';
|
|
39
|
+
if (extension === '.svg')
|
|
40
|
+
return 'image/svg+xml';
|
|
41
|
+
if (extension === '.gif')
|
|
42
|
+
return 'image/gif';
|
|
43
|
+
if (extension === '.webp')
|
|
44
|
+
return 'image/webp';
|
|
45
|
+
if (extension === '.ico')
|
|
46
|
+
return 'image/x-icon';
|
|
47
|
+
return 'application/octet-stream';
|
|
48
|
+
}
|
|
49
|
+
function encodeIcon(icon, appPath) {
|
|
50
|
+
if (!icon) {
|
|
51
|
+
return icon;
|
|
52
|
+
}
|
|
53
|
+
if (icon.startsWith('data:')) {
|
|
54
|
+
return icon;
|
|
55
|
+
}
|
|
56
|
+
const iconPath = isAbsolute(icon) ? icon : join(appPath, icon);
|
|
57
|
+
if (!existsSync(iconPath)) {
|
|
58
|
+
throw new Error(`Icon file not found: ${iconPath}`);
|
|
59
|
+
}
|
|
60
|
+
const iconBuffer = readFileSync(iconPath);
|
|
61
|
+
return `data:${iconMimeType(iconPath)};base64,${iconBuffer.toString('base64')}`;
|
|
62
|
+
}
|
|
33
63
|
class ForgeCommand {
|
|
34
64
|
register() {
|
|
35
65
|
return new Command("forge")
|
|
@@ -75,6 +105,7 @@ class ForgeCommand {
|
|
|
75
105
|
process.chdir(path);
|
|
76
106
|
const module = await import(`${path}/ptkl.config.js`);
|
|
77
107
|
const { views, name, version, distPath, icon, type, label, permissions, install_permissions, runtime_permissions, entitlements, requires, scripts, ssrRenderer, } = module.default ?? {};
|
|
108
|
+
const iconBase64 = encodeIcon(icon, path);
|
|
78
109
|
// Clean dist folder before building to remove stale files
|
|
79
110
|
if (existsSync(distPath)) {
|
|
80
111
|
rmSync(distPath, { recursive: true, force: true });
|
|
@@ -91,7 +122,7 @@ class ForgeCommand {
|
|
|
91
122
|
version,
|
|
92
123
|
views: {},
|
|
93
124
|
label,
|
|
94
|
-
icon,
|
|
125
|
+
icon: iconBase64,
|
|
95
126
|
permissions,
|
|
96
127
|
install_permissions: install_permissions ?? [],
|
|
97
128
|
runtime_permissions: runtime_permissions ?? [],
|
|
@@ -101,13 +132,9 @@ class ForgeCommand {
|
|
|
101
132
|
type: type || 'platform', // default to 'platform' if not specified
|
|
102
133
|
ssrRenderer,
|
|
103
134
|
};
|
|
104
|
-
const client = Util.getClientForProfile();
|
|
105
|
-
// get base url of the platform cliente
|
|
106
|
-
const baseUrl = client.getPlatformBaseURL();
|
|
107
135
|
// Different build approach for public vs platform apps
|
|
108
136
|
if (type === 'public') {
|
|
109
137
|
// Public apps: standard SPA build with index.html
|
|
110
|
-
manifest.icon = icon;
|
|
111
138
|
console.log("Building public app...");
|
|
112
139
|
try {
|
|
113
140
|
await build({
|
|
@@ -164,14 +191,13 @@ class ForgeCommand {
|
|
|
164
191
|
}
|
|
165
192
|
else {
|
|
166
193
|
// Platform apps: custom bundle format with views and scripts
|
|
167
|
-
const
|
|
168
|
-
manifest.icon = `${base}${icon}`;
|
|
194
|
+
const bundleBasePath = `/luma/appservice/v1/forge/static/bundle/${name}/${version}/`;
|
|
169
195
|
const buildViews = Object.keys(views).map(async (view) => {
|
|
170
196
|
manifest.views[view] = `${view}.bundle.js`;
|
|
171
197
|
try {
|
|
172
198
|
const result = await build({
|
|
173
199
|
root: path,
|
|
174
|
-
base,
|
|
200
|
+
base: bundleBasePath,
|
|
175
201
|
build: {
|
|
176
202
|
outDir: distPath,
|
|
177
203
|
emptyOutDir: false,
|
|
@@ -196,10 +222,12 @@ class ForgeCommand {
|
|
|
196
222
|
for (const fileName in bundle) {
|
|
197
223
|
const chunk = bundle[fileName];
|
|
198
224
|
if (chunk.type === 'chunk' && chunk.code) {
|
|
225
|
+
const runtimeBaseResolver = `const __ptklForgeBundleBase=()=>{const env=typeof window!=='undefined'&&window.__ENV_VARIABLES__?window.__ENV_VARIABLES__.API_HOST:'';const host=typeof env==='string'?env.replace(/\\/$/,''):'';return host+'${bundleBasePath}';};\n`;
|
|
226
|
+
chunk.code = runtimeBaseResolver + chunk.code;
|
|
199
227
|
// Transform dynamic imports in the final bundled code
|
|
200
|
-
chunk.code = chunk.code.replace(/import\(['"`]\.\/([^'"`]+)['"`]\)/g, `dynamicImport('$
|
|
228
|
+
chunk.code = chunk.code.replace(/import\(['"`]\.\/([^'"`]+)['"`]\)/g, `dynamicImport(__ptklForgeBundleBase() + '$1')`);
|
|
201
229
|
// Also handle relative paths without ./
|
|
202
|
-
chunk.code = chunk.code.replace(/import\(['"`]([^'"`\/]+\.js)['"`]\)/g, `dynamicImport('$
|
|
230
|
+
chunk.code = chunk.code.replace(/import\(['"`]([^'"`\/]+\.js)['"`]\)/g, `dynamicImport(__ptklForgeBundleBase() + '$1')`);
|
|
203
231
|
}
|
|
204
232
|
}
|
|
205
233
|
}
|
|
@@ -217,7 +245,7 @@ class ForgeCommand {
|
|
|
217
245
|
try {
|
|
218
246
|
const result = await build({
|
|
219
247
|
root: path,
|
|
220
|
-
base,
|
|
248
|
+
base: bundleBasePath,
|
|
221
249
|
build: {
|
|
222
250
|
outDir: distPath,
|
|
223
251
|
emptyOutDir: false,
|
|
@@ -306,6 +334,7 @@ class ForgeCommand {
|
|
|
306
334
|
const manifestPath = `${distPath}/manifest.json`;
|
|
307
335
|
writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
|
|
308
336
|
if (upload) {
|
|
337
|
+
const client = Util.getClientForProfile();
|
|
309
338
|
let buffer = Buffer.alloc(0);
|
|
310
339
|
const bufferStream = new Writable({
|
|
311
340
|
write(chunk, encoding, callback) {
|