@blocklet/pages-kit-block-studio 0.0.11 → 0.0.13

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.
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.initBlockStudioRouter = void 0;
16
+ const express_1 = require("express");
17
+ const fs_1 = __importDefault(require("fs"));
18
+ const path_1 = __importDefault(require("path"));
19
+ const helper_1 = require("../utils/helper");
20
+ exports.initBlockStudioRouter = (0, express_1.Router)();
21
+ const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.svg'];
22
+ exports.initBlockStudioRouter.get('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
23
+ if (!helper_1.isDev) {
24
+ return res.status(403).json({ error: 'Only available in development mode' });
25
+ }
26
+ const filePath = req.query.path;
27
+ if (!filePath) {
28
+ return res.status(400).json({ error: 'Path parameter is required' });
29
+ }
30
+ if (!(0, helper_1.isPathSafe)(filePath)) {
31
+ return res.status(403).json({ error: 'Invalid path' });
32
+ }
33
+ try {
34
+ if (!fs_1.default.existsSync(filePath)) {
35
+ return res.json(null);
36
+ }
37
+ const ext = path_1.default.extname(filePath).toLowerCase();
38
+ if (BINARY_EXTENSIONS.includes(ext)) {
39
+ // For images, stream the file directly
40
+ const mimeType = `image/${ext.slice(1)}`;
41
+ res.setHeader('Content-Type', mimeType);
42
+ return fs_1.default.createReadStream(filePath).pipe(res);
43
+ }
44
+ // For text files, try to parse as JSON
45
+ const content = fs_1.default.readFileSync(filePath, 'utf-8');
46
+ try {
47
+ return res.json(JSON.parse(content));
48
+ }
49
+ catch (_a) {
50
+ // If JSON parsing fails, return as plain text
51
+ return res.json(content);
52
+ }
53
+ }
54
+ catch (error) {
55
+ return res.status(500).json({ error: 'Failed to read file' });
56
+ }
57
+ }));
58
+ exports.initBlockStudioRouter.post('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
59
+ if (!helper_1.isDev) {
60
+ return res.status(403).json({ error: 'Only available in development mode' });
61
+ }
62
+ const { path: filePath, content } = req.body;
63
+ if (!filePath || content === undefined) {
64
+ return res.status(400).json({ error: 'Path and content are required' });
65
+ }
66
+ if (!(0, helper_1.isPathSafe)(filePath)) {
67
+ return res.status(403).json({ error: 'Invalid path' });
68
+ }
69
+ try {
70
+ const dir = path_1.default.dirname(filePath);
71
+ if (!fs_1.default.existsSync(dir)) {
72
+ fs_1.default.mkdirSync(dir, { recursive: true });
73
+ }
74
+ fs_1.default.writeFileSync(filePath, JSON.stringify(content, null, 2));
75
+ return res.json({ success: true });
76
+ }
77
+ catch (error) {
78
+ return res.status(500).json({ error: 'Failed to write file' });
79
+ }
80
+ }));
81
+ exports.default = exports.initBlockStudioRouter;
@@ -153,7 +153,7 @@ exports.initResourceRouter.post('/', (req, res) => __awaiter(void 0, void 0, voi
153
153
  fs_1.default.rmSync(dir, { recursive: true, force: true });
154
154
  fs_1.default.mkdirSync(dir, { recursive: true });
155
155
  const rootDir = process.cwd();
156
- const tmpPackage = (0, path_1.join)(rootDir, 'lib');
156
+ const tmpPackage = (0, path_1.join)(rootDir, helper_1.libDir);
157
157
  yield copyRecursive(tmpPackage, dir);
158
158
  fs_1.default.rmSync(tmpPackage, { recursive: true, force: true });
159
159
  res.json({ success: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ // init uploader router
@@ -157,16 +157,22 @@ function initBlockStudioPlugins(options) {
157
157
  const isHtml = relative.endsWith('.html');
158
158
  const blockName = (0, helper_1.getBlockName)(relative);
159
159
  const pageId = `/${blockName}`;
160
+ const dirPath = path.dirname(filePath);
161
+ const metadataPath = path.join(dirPath, '@metadata.json');
160
162
  api.addPageData({
161
163
  pageId,
162
- dataPath: isHtml ? `${vite_plugin_html_transform_1.VIRTUAL_MODULE_ID}?dir=${path.dirname(filePath)}` : filePath,
164
+ dataPath: isHtml ? `${vite_plugin_html_transform_1.VIRTUAL_MODULE_ID}?dir=${dirPath}` : filePath,
163
165
  staticData: isHtml
164
166
  ? {
165
167
  isHtmlPreview: true,
166
168
  dataPath: filePath,
167
169
  blockName,
170
+ dirPath,
171
+ metadataPath,
168
172
  }
169
- : Object.assign(Object.assign({}, (yield helpers.extractStaticData(file))), { code: (0, fs_1.readFileSync)(file.path, 'utf-8'), dataPath: filePath, blockName }),
173
+ : Object.assign(Object.assign({}, (yield helpers.extractStaticData(file))), { code: (0, fs_1.readFileSync)(file.path, 'utf-8'), dataPath: filePath, blockName,
174
+ dirPath,
175
+ metadataPath }),
170
176
  });
171
177
  });
172
178
  });