@blocklet/pages-kit-block-studio 0.0.16 → 0.0.17

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,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MEDIA_KIT_RESOURCE_TYPE = exports.MEDIA_KIT_DID = exports.PAGES_KIT_BLOCK_STUDIO_RESOURCE_TYPE = exports.PAGES_KIT_BLOCK_STUDIO_DID = exports.PAGES_KIT_RESOURCE_TYPE = exports.PAGES_KIT_DID = void 0;
4
+ exports.PAGES_KIT_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
5
+ exports.PAGES_KIT_RESOURCE_TYPE = 'page';
6
+ exports.PAGES_KIT_BLOCK_STUDIO_DID = 'z2qa7rr3eUyVnWp2PCxEVARuUfLFh6cE5V2xV';
7
+ exports.PAGES_KIT_BLOCK_STUDIO_RESOURCE_TYPE = 'page';
8
+ exports.MEDIA_KIT_DID = 'z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9';
9
+ exports.MEDIA_KIT_RESOURCE_TYPE = 'imgpack';
@@ -15,6 +15,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.initBlockStudioRouter = void 0;
16
16
  const express_1 = require("express");
17
17
  const fs_1 = __importDefault(require("fs"));
18
+ const lodash_1 = require("lodash");
19
+ const lodash_2 = require("lodash");
20
+ const lodash_3 = require("lodash");
18
21
  const path_1 = __importDefault(require("path"));
19
22
  const helper_1 = require("../utils/helper");
20
23
  exports.initBlockStudioRouter = (0, express_1.Router)();
@@ -31,25 +34,23 @@ exports.initBlockStudioRouter.get('/', (req, res) => __awaiter(void 0, void 0, v
31
34
  return res.status(403).json({ error: 'Invalid path' });
32
35
  }
33
36
  try {
34
- if (!fs_1.default.existsSync(filePath)) {
35
- return res.json(null);
36
- }
37
37
  const ext = path_1.default.extname(filePath).toLowerCase();
38
38
  if (BINARY_EXTENSIONS.includes(ext)) {
39
+ if (!fs_1.default.existsSync(filePath)) {
40
+ return res.json(null);
41
+ }
39
42
  // For images, stream the file directly
40
43
  const mimeType = `image/${ext.slice(1)}`;
41
44
  res.setHeader('Content-Type', mimeType);
42
45
  return fs_1.default.createReadStream(filePath).pipe(res);
43
46
  }
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);
47
+ const metadata = (0, helper_1.initializeMetadata)(filePath);
48
+ const code = fs_1.default.readFileSync(filePath, 'utf8');
49
+ if (code) {
50
+ (0, lodash_3.set)(metadata, 'renderer.script', code);
51
+ (0, lodash_3.set)(metadata, 'renderer.type', 'react-component');
52
52
  }
53
+ return res.json(metadata);
53
54
  }
54
55
  catch (error) {
55
56
  return res.status(500).json({ error: 'Failed to read file' });
@@ -71,11 +72,59 @@ exports.initBlockStudioRouter.post('/', (req, res) => __awaiter(void 0, void 0,
71
72
  if (!fs_1.default.existsSync(dir)) {
72
73
  fs_1.default.mkdirSync(dir, { recursive: true });
73
74
  }
74
- fs_1.default.writeFileSync(filePath, JSON.stringify(content, null, 2));
75
- return res.json({ success: true });
75
+ const currentMetadata = (0, helper_1.initializeMetadata)(filePath);
76
+ const mergedContent = Object.assign(Object.assign({}, currentMetadata), content);
77
+ // remove renderer
78
+ delete mergedContent.renderer;
79
+ if ((0, lodash_1.isEqual)(currentMetadata, mergedContent)) {
80
+ return res.json({ success: true, content: mergedContent, message: 'No changes' });
81
+ }
82
+ mergedContent.updatedAt = new Date().toISOString();
83
+ // Check if this is a metadata file and has previewImage update
84
+ if ((0, helper_1.isMetadataFile)(filePath) && content.previewImage && currentMetadata.previewImage !== content.previewImage) {
85
+ const previewImagePath = path_1.default.join(dir, (0, helper_1.getPreviewImageRelativePath)(content.previewImage));
86
+ if (!fs_1.default.existsSync(previewImagePath)) {
87
+ yield (0, helper_1.downloadAsset)({
88
+ asset: content.previewImage,
89
+ savePath: previewImagePath,
90
+ componentDid: process.env.BLOCKLET_COMPONENT_DID || '',
91
+ });
92
+ }
93
+ }
94
+ fs_1.default.writeFileSync(filePath, JSON.stringify(mergedContent, null, 2));
95
+ return res.json({ success: true, content: mergedContent, message: 'Updated' });
76
96
  }
77
97
  catch (error) {
78
98
  return res.status(500).json({ error: 'Failed to write file' });
79
99
  }
80
100
  }));
101
+ exports.initBlockStudioRouter.get('/all', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
102
+ const { withBlockletData = true } = req.query;
103
+ const allBlocks = yield (0, helper_1.findComponentFiles)();
104
+ // get code to metadata
105
+ const allBlocksWithCode = allBlocks.map((block) => {
106
+ const code = fs_1.default.readFileSync(block.fullPath, 'utf8');
107
+ if (code) {
108
+ (0, lodash_3.set)(block.metadata, 'renderer.script', code);
109
+ (0, lodash_3.set)(block.metadata, 'renderer.type', 'react-component');
110
+ }
111
+ return block;
112
+ });
113
+ if (withBlockletData) {
114
+ const allBlocksWithBlockletData = allBlocksWithCode.map((block) => {
115
+ const blockletInfo = (0, helper_1.getBlockStudioInfo)() || {
116
+ title: '',
117
+ did: '',
118
+ };
119
+ const item = {
120
+ blockletTitle: blockletInfo.title,
121
+ blockletId: blockletInfo.did,
122
+ data: block.metadata,
123
+ };
124
+ return item;
125
+ });
126
+ return res.json((0, lodash_2.keyBy)(allBlocksWithBlockletData, 'data.id'));
127
+ }
128
+ res.json((0, lodash_2.keyBy)(allBlocksWithCode, 'id'));
129
+ }));
81
130
  exports.default = exports.initBlockStudioRouter;
@@ -53,6 +53,7 @@ const component_1 = require("@blocklet/sdk/lib/component");
53
53
  const child_process_1 = require("child_process");
54
54
  const express_1 = require("express");
55
55
  const fs_1 = __importDefault(require("fs"));
56
+ const set_1 = __importDefault(require("lodash/set"));
56
57
  const path_1 = __importStar(require("path"));
57
58
  const helper_1 = require("../utils/helper");
58
59
  const DID = 'z2qa7rr3eUyVnWp2PCxEVARuUfLFh6cE5V2xV';
@@ -153,9 +154,55 @@ exports.initResourceRouter.post('/', (req, res) => __awaiter(void 0, void 0, voi
153
154
  fs_1.default.rmSync(dir, { recursive: true, force: true });
154
155
  fs_1.default.mkdirSync(dir, { recursive: true });
155
156
  const rootDir = process.cwd();
156
- const tmpPackage = (0, path_1.join)(rootDir, helper_1.libDir);
157
+ const distDir = (0, path_1.join)(rootDir, helper_1.libDir);
158
+ const tmpPackage = (0, path_1.join)(distDir, 'resource-blocklet');
159
+ fs_1.default.mkdirSync(tmpPackage, { recursive: true });
160
+ const pagesDir = (0, path_1.join)(tmpPackage, 'pages');
161
+ fs_1.default.mkdirSync(pagesDir, { recursive: true });
162
+ const componentsDir = (0, path_1.join)(tmpPackage, 'components');
163
+ fs_1.default.mkdirSync(componentsDir, { recursive: true });
164
+ // get @metadata.json by glob
165
+ const canUseComponents = (0, helper_1.findComponentFiles)({ cwd: rootDir, filter: componentIds });
166
+ // Filter and process metadata files
167
+ const metadataList = canUseComponents.map(({ fullPath, blockName, metadata: _metadata }) => {
168
+ // get metadata
169
+ const metadata = _metadata;
170
+ // get code to metadata
171
+ const code = fs_1.default.readFileSync((0, path_1.join)(distDir, 'es', `${blockName}.js`), 'utf8');
172
+ if (code) {
173
+ (0, set_1.default)(metadata, 'renderer.script', code);
174
+ (0, set_1.default)(metadata, 'renderer.type', 'react-component');
175
+ }
176
+ // write metadata to metadataPath
177
+ const metadataYmlPath = path_1.default.join(componentsDir, `${metadata.name || 'unnamed'}.${metadata.id}.yml`);
178
+ fs_1.default.writeFileSync(metadataYmlPath, (0, helper_1.generateYaml)(metadata));
179
+ // Handle preview image if exists
180
+ if (metadata.previewImage) {
181
+ const imagePath = path_1.default.join(path_1.default.dirname(fullPath), (0, helper_1.getPreviewImageRelativePath)(metadata.previewImage));
182
+ const imageDestPath = path_1.default.join(componentsDir, metadata.previewImage);
183
+ if (fs_1.default.existsSync(imagePath)) {
184
+ fs_1.default.copyFileSync(imagePath, imageDestPath);
185
+ }
186
+ }
187
+ return metadata;
188
+ });
189
+ // write pages.config.yml
190
+ const pagesConfigPath = path_1.default.join(tmpPackage, '.blocklet/pages/pages.config.yml');
191
+ fs_1.default.mkdirSync(path_1.default.dirname(pagesConfigPath), { recursive: true });
192
+ const pagesConfig = {
193
+ pages: [],
194
+ components: metadataList.map((metadata) => ({
195
+ id: metadata.id,
196
+ name: metadata.name,
197
+ })),
198
+ supportedLocales: [],
199
+ config: {},
200
+ };
201
+ fs_1.default.writeFileSync(pagesConfigPath, (0, helper_1.generateYaml)(pagesConfig));
202
+ helper_1.logger.info('generate resource blocklet block count:', metadataList.length);
157
203
  yield copyRecursive(tmpPackage, dir);
158
- fs_1.default.rmSync(tmpPackage, { recursive: true, force: true });
204
+ // remove tmpPackage
205
+ // fs.rmSync(tmpPackage, { recursive: true, force: true });
159
206
  res.json({ success: true });
160
207
  }
161
208
  catch (error) {
@@ -37,6 +37,7 @@ exports.initUploaderRouter = void 0;
37
37
  // @ts-ignore
38
38
  const uploader_server_1 = require("@blocklet/uploader-server");
39
39
  const express_1 = __importStar(require("express"));
40
+ const constants_1 = require("../constants");
40
41
  // init uploader router
41
42
  exports.initUploaderRouter = (0, express_1.Router)();
42
43
  exports.initUploaderRouter.use('/', (0, uploader_server_1.initProxyToMediaKitUploadsMiddleware)({
@@ -46,20 +47,20 @@ exports.initUploaderRouter.use('/', (0, uploader_server_1.initProxyToMediaKitUpl
46
47
  resourceTypes: [
47
48
  // image bin resource
48
49
  {
49
- type: 'imgpack',
50
- did: 'z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9',
50
+ type: constants_1.MEDIA_KIT_RESOURCE_TYPE,
51
+ did: constants_1.MEDIA_KIT_DID,
51
52
  },
52
53
  // pages kit resource (pages and components folder)
53
54
  {
54
- type: 'page',
55
- did: 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o',
55
+ type: constants_1.PAGES_KIT_RESOURCE_TYPE,
56
+ did: constants_1.PAGES_KIT_DID,
56
57
  folder: ['pages', 'components'],
57
58
  blacklist: ['.yml'],
58
59
  },
59
60
  // pages kit block studio resource
60
61
  {
61
- type: 'page',
62
- did: 'z2qa7rr3eUyVnWp2PCxEVARuUfLFh6cE5V2xV',
62
+ type: constants_1.PAGES_KIT_BLOCK_STUDIO_RESOURCE_TYPE,
63
+ did: constants_1.PAGES_KIT_BLOCK_STUDIO_DID,
63
64
  folder: ['pages', 'components'],
64
65
  blacklist: ['.yml'],
65
66
  },
@@ -121,6 +121,9 @@ function initBlockStudioPlugins(options) {
121
121
  '@arcblock/did-connect',
122
122
  ],
123
123
  output: {
124
+ chunkFileNames: () => {
125
+ return '[format]/_chunks/[name]-[hash].js';
126
+ },
124
127
  // 为所有外部依赖提供全局变量
125
128
  globals: {
126
129
  react: 'React',