@blocklet/pages-kit-block-studio 0.6.14 → 0.6.16
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/lib/cjs/middlewares/init-block-studio-router.js +34 -0
- package/lib/cjs/middlewares/init-resource-router.js +7 -262
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/build-lib.js +291 -42
- package/lib/cjs/utils/helper.js +38 -0
- package/lib/esm/middlewares/init-block-studio-router.js +34 -0
- package/lib/esm/middlewares/init-resource-router.js +8 -224
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/build-lib.js +259 -43
- package/lib/esm/utils/helper.js +35 -0
- package/lib/types/middlewares/init-resource-router.d.ts +0 -3
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/build-lib.d.ts +4 -2
- package/lib/types/utils/helper.d.ts +3 -0
- package/package.json +3 -3
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
37
|
};
|
|
@@ -6,25 +39,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
39
|
exports.buildLib = buildLib;
|
|
7
40
|
/* eslint-disable no-await-in-loop */
|
|
8
41
|
/* eslint-disable no-console */
|
|
42
|
+
const uploader_server_1 = require("@blocklet/uploader-server");
|
|
9
43
|
const chalk_1 = __importDefault(require("chalk"));
|
|
10
44
|
const child_process_1 = require("child_process");
|
|
45
|
+
const fs_1 = __importDefault(require("fs"));
|
|
46
|
+
const get_1 = __importDefault(require("lodash/get"));
|
|
47
|
+
const set_1 = __importDefault(require("lodash/set"));
|
|
48
|
+
const path_1 = __importStar(require("path"));
|
|
49
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
11
50
|
const helper_1 = require("./helper");
|
|
12
|
-
async function
|
|
13
|
-
const workingDir = options?.cwd || process.cwd();
|
|
51
|
+
async function performBuild(workingDir, componentIds) {
|
|
14
52
|
// Scan for all index files
|
|
15
53
|
const allBlocks = (0, helper_1.findComponentFiles)({ cwd: workingDir }).map((entry) => {
|
|
16
54
|
return entry.blockName;
|
|
17
55
|
});
|
|
18
|
-
const filterModules = process.argv.includes('--filter')
|
|
19
|
-
? process.argv[process.argv.indexOf('--filter') + 1]?.split(',')
|
|
20
|
-
: process.env.BLOCK_FILTER?.split(',') || null;
|
|
21
56
|
const ignoreViteLog = process.argv.includes('--log') ? false : process.env.BLOCK_LOG !== 'true';
|
|
22
57
|
const multiMode = process.argv.includes('--multi') || process.env.BLOCK_MULTI === 'true';
|
|
23
|
-
const blocks = allBlocks.filter((name) => !
|
|
58
|
+
const blocks = allBlocks.filter((name) => !componentIds || componentIds.includes(name || ''));
|
|
24
59
|
if (!blocks.length) {
|
|
25
|
-
console.log(chalk_1.default.yellow('No components
|
|
26
|
-
throw new Error('No components
|
|
27
|
-
return;
|
|
60
|
+
console.log(chalk_1.default.yellow('No components found, please check has any component been selected'));
|
|
61
|
+
throw new Error('No components found, please check has any component been selected');
|
|
28
62
|
}
|
|
29
63
|
// Clean up the lib directory
|
|
30
64
|
await (0, child_process_1.execSync)(`rm -rf ${helper_1.libDir}`, { cwd: workingDir });
|
|
@@ -32,51 +66,266 @@ async function buildLib(options) {
|
|
|
32
66
|
// Start to build
|
|
33
67
|
console.log(chalk_1.default.cyan(`Start to build ${blocks.length} blocks in parallel\n`));
|
|
34
68
|
const canBuildBlocks = multiMode ? blocks : [blocks.join(',')];
|
|
35
|
-
const configFile = 'vite-lib.config';
|
|
36
|
-
const hasConfig = (0, child_process_1.execSync)(`ls ${configFile}
|
|
69
|
+
const configFile = 'vite-lib.config.ts';
|
|
70
|
+
const hasConfig = (0, child_process_1.execSync)(`ls ${configFile} 2>/dev/null || true`, {
|
|
37
71
|
cwd: workingDir,
|
|
38
72
|
encoding: 'utf-8',
|
|
39
73
|
}).trim() !== '';
|
|
74
|
+
await Promise.all(canBuildBlocks.map((blockName) => {
|
|
75
|
+
console.log(chalk_1.default.blue(`Building ${blockName} Block...`));
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
const build = (0, child_process_1.spawn)('vite', [
|
|
78
|
+
'build',
|
|
79
|
+
'--filter',
|
|
80
|
+
`"${blockName}"`,
|
|
81
|
+
'--outDir',
|
|
82
|
+
helper_1.libDir,
|
|
83
|
+
'--emptyOutDir=false',
|
|
84
|
+
hasConfig ? `--config ${configFile}.*` : '',
|
|
85
|
+
], {
|
|
86
|
+
stdio: ignoreViteLog ? ['inherit', 'ignore', 'ignore'] : 'inherit',
|
|
87
|
+
shell: true,
|
|
88
|
+
cwd: workingDir,
|
|
89
|
+
});
|
|
90
|
+
build.on('close', (code) => {
|
|
91
|
+
if (code === 0) {
|
|
92
|
+
console.log(chalk_1.default.green(`Build ${blockName} successfully`));
|
|
93
|
+
resolve();
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
reject(new Error(`Build failed with code ${code}`));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}));
|
|
101
|
+
console.log(chalk_1.default.green(`\nBuild ${blocks.length} blocks successfully`));
|
|
102
|
+
console.log(chalk_1.default.gray(`Generate the ${helper_1.libDir} directory successfully`));
|
|
103
|
+
}
|
|
104
|
+
async function performPackaging({ exportDir, componentIds, workingDir }) {
|
|
105
|
+
if (exportDir) {
|
|
106
|
+
// Clear and create export directory
|
|
107
|
+
fs_1.default.rmSync(exportDir, { recursive: true, force: true });
|
|
108
|
+
fs_1.default.mkdirSync(exportDir, { recursive: true });
|
|
109
|
+
}
|
|
110
|
+
const distDir = (0, path_1.join)(workingDir, helper_1.libDir);
|
|
111
|
+
const codeDir = (0, path_1.join)(distDir, 'es');
|
|
112
|
+
const tmpPackage = (0, path_1.join)(distDir, 'resource-blocklet');
|
|
113
|
+
fs_1.default.mkdirSync(tmpPackage, { recursive: true });
|
|
114
|
+
const pagesDir = (0, path_1.join)(tmpPackage, 'pages');
|
|
115
|
+
fs_1.default.mkdirSync(pagesDir, { recursive: true });
|
|
116
|
+
const componentsDir = (0, path_1.join)(tmpPackage, 'components');
|
|
117
|
+
fs_1.default.mkdirSync(componentsDir, { recursive: true });
|
|
118
|
+
const chunksDir = (0, path_1.join)(tmpPackage, 'chunks');
|
|
119
|
+
fs_1.default.mkdirSync(chunksDir, { recursive: true });
|
|
120
|
+
const chunksMapPath = (0, path_1.join)(codeDir, 'chunks-map.json');
|
|
121
|
+
const chunksMap = JSON.parse(fs_1.default.readFileSync(chunksMapPath, 'utf8'));
|
|
122
|
+
// Get components with metadata
|
|
123
|
+
const canUseComponents = (0, helper_1.findComponentFiles)({
|
|
124
|
+
cwd: workingDir,
|
|
125
|
+
filter: componentIds,
|
|
126
|
+
strictExistMetadata: true,
|
|
127
|
+
});
|
|
128
|
+
// Process metadata files
|
|
129
|
+
const metadataList = await Promise.all(canUseComponents.map(async ({ fullPath, blockName, metadata: _metadata }) => {
|
|
130
|
+
const metadata = _metadata;
|
|
131
|
+
const jsName = `${blockName}.js`;
|
|
132
|
+
// Set version to 2
|
|
133
|
+
(0, set_1.default)(metadata, 'version', 2);
|
|
134
|
+
// Get main component code
|
|
135
|
+
const code = fs_1.default.readFileSync((0, path_1.join)(codeDir, `${(0, helper_1.getComponentScriptName)(blockName, 'esm')}.js`), 'utf8');
|
|
136
|
+
if (code) {
|
|
137
|
+
(0, set_1.default)(metadata, 'renderer.script', code);
|
|
138
|
+
(0, set_1.default)(metadata, 'renderer.type', 'react-component');
|
|
139
|
+
(0, set_1.default)(metadata, 'renderer.chunks', chunksMap[jsName] || []);
|
|
140
|
+
}
|
|
141
|
+
// Transpile to CJS
|
|
142
|
+
try {
|
|
143
|
+
const cjsCode = typescript_1.default.transpileModule(code, {
|
|
144
|
+
compilerOptions: {
|
|
145
|
+
jsx: typescript_1.default.JsxEmit.React,
|
|
146
|
+
target: typescript_1.default.ScriptTarget.ES2016,
|
|
147
|
+
module: typescript_1.default.ModuleKind.CommonJS,
|
|
148
|
+
moduleResolution: typescript_1.default.ModuleResolutionKind.Node16,
|
|
149
|
+
},
|
|
150
|
+
}).outputText;
|
|
151
|
+
if (cjsCode) {
|
|
152
|
+
(0, set_1.default)(metadata, 'renderer.cjsScript', cjsCode);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
// ignore error
|
|
157
|
+
}
|
|
158
|
+
// Handle edit component code
|
|
159
|
+
try {
|
|
160
|
+
const editComponentJsName = `${(0, helper_1.getEditComponentBlockName)(blockName)}.js`;
|
|
161
|
+
const editComponentCode = fs_1.default.readFileSync((0, path_1.join)(codeDir, editComponentJsName), 'utf8');
|
|
162
|
+
if (editComponentCode) {
|
|
163
|
+
(0, set_1.default)(metadata, 'renderer.editComponent', editComponentCode);
|
|
164
|
+
(0, set_1.default)(metadata, 'renderer.chunks', Array.from(new Set([...(0, get_1.default)(metadata, 'renderer.chunks', []), ...(chunksMap[editComponentJsName] || [])])));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
// ignore error
|
|
169
|
+
}
|
|
170
|
+
// Handle aigne output value schema
|
|
171
|
+
try {
|
|
172
|
+
const aigneOutputValueSchemaJsName = `${(0, helper_1.getAigneOutputValueSchemaBlockName)(blockName)}.js`;
|
|
173
|
+
const aigneOutputValueSchemaCode = fs_1.default.readFileSync((0, path_1.join)(codeDir, aigneOutputValueSchemaJsName), 'utf8');
|
|
174
|
+
if (aigneOutputValueSchemaCode) {
|
|
175
|
+
(0, set_1.default)(metadata, 'renderer.aigneOutputValueSchema', aigneOutputValueSchemaCode);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
// ignore error
|
|
180
|
+
}
|
|
181
|
+
// Handle properties schema
|
|
182
|
+
try {
|
|
183
|
+
const propertiesSchemaJsName = `${(0, helper_1.getPropertiesSchemaBlockName)(blockName)}.js`;
|
|
184
|
+
const propertiesSchemaCode = fs_1.default.readFileSync((0, path_1.join)(codeDir, propertiesSchemaJsName), 'utf8');
|
|
185
|
+
if (propertiesSchemaCode) {
|
|
186
|
+
(0, set_1.default)(metadata, 'renderer.PROPERTIES_SCHEMA', propertiesSchemaCode);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
// ignore error
|
|
191
|
+
}
|
|
192
|
+
// Handle getServerSideProps
|
|
193
|
+
try {
|
|
194
|
+
const getServerSidePropsJsName = `${(0, helper_1.getGetServerSidePropsBlockName)(blockName)}.js`;
|
|
195
|
+
const getServerSidePropsCode = fs_1.default.readFileSync((0, path_1.join)(codeDir, getServerSidePropsJsName), 'utf8');
|
|
196
|
+
if (getServerSidePropsCode) {
|
|
197
|
+
(0, set_1.default)(metadata, 'renderer.getServerSideProps', getServerSidePropsCode);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
// ignore error
|
|
202
|
+
}
|
|
203
|
+
// Handle preview image
|
|
204
|
+
if (metadata.previewImage) {
|
|
205
|
+
const imagePath = path_1.default.join(path_1.default.dirname(fullPath), (0, helper_1.getPreviewImageRelativePath)(metadata.previewImage));
|
|
206
|
+
const imageDestPath = path_1.default.join(componentsDir, metadata.previewImage);
|
|
207
|
+
if (fs_1.default.existsSync(imagePath)) {
|
|
208
|
+
fs_1.default.copyFileSync(imagePath, imageDestPath);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// Handle type:url properties
|
|
212
|
+
if (metadata.properties) {
|
|
213
|
+
const downloadPromises = [];
|
|
214
|
+
Object.keys(metadata.properties).forEach((key) => {
|
|
215
|
+
const property = metadata.properties[key].data;
|
|
216
|
+
if (property.type === 'url') {
|
|
217
|
+
Object.keys(property.locales).forEach((locale) => {
|
|
218
|
+
if (property.locales[locale].defaultValue?.mediaKitUrl?.startsWith('mediakit://')) {
|
|
219
|
+
const downloadPromise = (async () => {
|
|
220
|
+
try {
|
|
221
|
+
const fileName = (0, path_1.basename)(property.locales[locale].defaultValue.mediaKitUrl);
|
|
222
|
+
const targetPath = path_1.default.join(componentsDir, fileName);
|
|
223
|
+
if (fs_1.default.existsSync(targetPath)) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
// try to get image from preview image
|
|
227
|
+
const imagePathInPreview = path_1.default.join(path_1.default.dirname(fullPath), (0, helper_1.getPreviewImageRelativePath)(fileName));
|
|
228
|
+
if (fs_1.default.existsSync(imagePathInPreview)) {
|
|
229
|
+
fs_1.default.copyFileSync(imagePathInPreview, targetPath);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const resWithStream = await (0, uploader_server_1.getMediaKitFileStream)(property.locales[locale].defaultValue.mediaKitUrl);
|
|
233
|
+
if (!resWithStream.data) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const writeStream = fs_1.default.createWriteStream(targetPath);
|
|
237
|
+
resWithStream.data.pipe(writeStream);
|
|
238
|
+
await new Promise((resolve, reject) => {
|
|
239
|
+
writeStream.on('finish', () => {
|
|
240
|
+
property.locales[locale].defaultValue.url = property.locales[locale].defaultValue.mediaKitUrl;
|
|
241
|
+
resolve();
|
|
242
|
+
});
|
|
243
|
+
writeStream.on('error', reject);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
console.error('Download media kit file failed:', error.message);
|
|
248
|
+
}
|
|
249
|
+
})();
|
|
250
|
+
downloadPromises.push(downloadPromise);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
if (downloadPromises.length > 0) {
|
|
256
|
+
await Promise.allSettled(downloadPromises);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
// Write metadata file
|
|
260
|
+
const metadataYmlPath = path_1.default.join(componentsDir, `${metadata.name || 'unnamed'}.${metadata.id}.yml`);
|
|
261
|
+
fs_1.default.writeFileSync(metadataYmlPath, (0, helper_1.generateYaml)(metadata));
|
|
262
|
+
return metadata;
|
|
263
|
+
}));
|
|
264
|
+
// Copy chunks directory
|
|
265
|
+
await (0, helper_1.copyRecursive)((0, path_1.join)(codeDir, 'chunks'), chunksDir);
|
|
266
|
+
// Generate pages.config.yml
|
|
267
|
+
const pagesConfigPath = path_1.default.join(tmpPackage, '.blocklet/pages/pages.config.yml');
|
|
268
|
+
fs_1.default.mkdirSync(path_1.default.dirname(pagesConfigPath), { recursive: true });
|
|
269
|
+
const pagesConfig = {
|
|
270
|
+
version: 2,
|
|
271
|
+
pages: [],
|
|
272
|
+
components: metadataList.map((metadata) => ({
|
|
273
|
+
id: metadata.id,
|
|
274
|
+
name: metadata.name,
|
|
275
|
+
})),
|
|
276
|
+
supportedLocales: [],
|
|
277
|
+
config: {},
|
|
278
|
+
};
|
|
279
|
+
fs_1.default.writeFileSync(pagesConfigPath, (0, helper_1.generateYaml)(pagesConfig));
|
|
280
|
+
helper_1.logger.info('generate resource blocklet block count:', metadataList.length);
|
|
281
|
+
if (exportDir) {
|
|
282
|
+
// Copy to target directory
|
|
283
|
+
await (0, helper_1.copyRecursive)(tmpPackage, exportDir);
|
|
284
|
+
console.log(chalk_1.default.green(`Package exported to: ${exportDir}`));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
async function buildLib(options = {}) {
|
|
288
|
+
const workingDir = options?.cwd || process.cwd();
|
|
289
|
+
const filterModules = process.argv.includes('--filter')
|
|
290
|
+
? process.argv[process.argv.indexOf('--filter') + 1]?.split(',')
|
|
291
|
+
: process.env.BLOCK_FILTER?.split(',');
|
|
292
|
+
// Get parameters from environment variables
|
|
293
|
+
const componentIds = filterModules ?? null;
|
|
294
|
+
const exportDir = process.argv.includes('--export-dir')
|
|
295
|
+
? process.argv[process.argv.indexOf('--export-dir') + 1]
|
|
296
|
+
: process.env.EXPORT_DIR;
|
|
40
297
|
try {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
'--emptyOutDir=false',
|
|
51
|
-
hasConfig ? `--config ${configFile}.*` : '',
|
|
52
|
-
], {
|
|
53
|
-
stdio: ignoreViteLog ? ['inherit', 'ignore', 'ignore'] : 'inherit',
|
|
54
|
-
shell: true,
|
|
55
|
-
cwd: workingDir,
|
|
56
|
-
});
|
|
57
|
-
build.on('close', (code) => {
|
|
58
|
-
if (code === 0) {
|
|
59
|
-
console.log(chalk_1.default.green(`Build ${blockName} successfully`));
|
|
60
|
-
resolve();
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
reject(new Error(`Build failed with code ${code}`));
|
|
64
|
-
}
|
|
65
|
-
});
|
|
298
|
+
// Phase 1: Build components
|
|
299
|
+
await performBuild(workingDir, componentIds);
|
|
300
|
+
console.log(chalk_1.default.green('Build completed successfully'));
|
|
301
|
+
// Phase 2: Package resources (if export directory is specified)
|
|
302
|
+
if (exportDir) {
|
|
303
|
+
await performPackaging({
|
|
304
|
+
exportDir,
|
|
305
|
+
componentIds,
|
|
306
|
+
workingDir,
|
|
66
307
|
});
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
console.log(chalk_1.default.gray(`Generate the ${helper_1.libDir} directory successfully`));
|
|
70
|
-
process.exit(0);
|
|
308
|
+
console.log(chalk_1.default.green('Package completed successfully'));
|
|
309
|
+
}
|
|
71
310
|
}
|
|
72
311
|
catch (err) {
|
|
73
312
|
console.error(chalk_1.default.red('Build failed:'), err);
|
|
74
|
-
process.
|
|
313
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
314
|
+
process.exit(1);
|
|
315
|
+
}
|
|
316
|
+
throw err;
|
|
317
|
+
}
|
|
318
|
+
finally {
|
|
319
|
+
process.exit(0);
|
|
75
320
|
}
|
|
76
321
|
}
|
|
77
322
|
const cwdArg = process.argv.indexOf('--cwd');
|
|
78
323
|
const cwd = cwdArg !== -1 ? process.argv[cwdArg + 1] : undefined;
|
|
79
|
-
buildLib({ cwd })
|
|
324
|
+
buildLib({ cwd })
|
|
325
|
+
.catch((err) => {
|
|
80
326
|
console.error(chalk_1.default.red('Build failed:'), err);
|
|
81
327
|
process.exit(1);
|
|
328
|
+
})
|
|
329
|
+
.finally(() => {
|
|
330
|
+
process.exit(0);
|
|
82
331
|
});
|
package/lib/cjs/utils/helper.js
CHANGED
|
@@ -48,6 +48,9 @@ exports.getBlockName = getBlockName;
|
|
|
48
48
|
exports.initializeMetadata = initializeMetadata;
|
|
49
49
|
exports.generateYaml = generateYaml;
|
|
50
50
|
exports.getBlockStudioInfo = getBlockStudioInfo;
|
|
51
|
+
exports.copyFile = copyFile;
|
|
52
|
+
exports.copyDirectory = copyDirectory;
|
|
53
|
+
exports.copyRecursive = copyRecursive;
|
|
51
54
|
const component_1 = require("@blocklet/sdk/lib/component");
|
|
52
55
|
const config_1 = __importDefault(require("@blocklet/sdk/lib/config"));
|
|
53
56
|
const fs_1 = __importDefault(require("fs"));
|
|
@@ -263,3 +266,38 @@ const getGetServerSidePropsBlockName = (blockName) => {
|
|
|
263
266
|
return `${blockName}(${exports.GET_SERVER_SIDE_PROPS_NAME})`;
|
|
264
267
|
};
|
|
265
268
|
exports.getGetServerSidePropsBlockName = getGetServerSidePropsBlockName;
|
|
269
|
+
function copyFile(src, dest) {
|
|
270
|
+
return new Promise((resolve, reject) => {
|
|
271
|
+
const readStream = fs_1.default.createReadStream(src);
|
|
272
|
+
const writeStream = fs_1.default.createWriteStream(dest);
|
|
273
|
+
readStream.on('error', reject);
|
|
274
|
+
writeStream.on('error', reject);
|
|
275
|
+
writeStream.on('finish', resolve);
|
|
276
|
+
readStream.pipe(writeStream);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
async function copyDirectory(src, dest) {
|
|
280
|
+
await fs_1.default.promises.mkdir(dest, { recursive: true });
|
|
281
|
+
const entries = await fs_1.default.promises.readdir(src, { withFileTypes: true });
|
|
282
|
+
for (const entry of entries) {
|
|
283
|
+
const srcPath = path_1.default.join(src, entry.name);
|
|
284
|
+
const destPath = path_1.default.join(dest, entry.name);
|
|
285
|
+
if (entry.isDirectory()) {
|
|
286
|
+
// eslint-disable-next-line no-await-in-loop
|
|
287
|
+
await copyDirectory(srcPath, destPath);
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
// eslint-disable-next-line no-await-in-loop
|
|
291
|
+
await copyFile(srcPath, destPath);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
async function copyRecursive(src, dest) {
|
|
296
|
+
const srcStats = await fs_1.default.promises.stat(src);
|
|
297
|
+
if (srcStats.isDirectory()) {
|
|
298
|
+
await copyDirectory(src, dest);
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
await copyFile(src, dest);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
@@ -159,6 +159,40 @@ initBlockStudioRouter.post('/', async (req, res) => {
|
|
|
159
159
|
});
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
|
+
// check if the properties has mediaKitUrl
|
|
163
|
+
if (isMetadataFile(filePath) && content.properties) {
|
|
164
|
+
const downloadPromises = [];
|
|
165
|
+
Object.keys(content.properties).forEach((key) => {
|
|
166
|
+
const property = content.properties[key].data;
|
|
167
|
+
if (property.type === 'url') {
|
|
168
|
+
Object.keys(property.locales).forEach((locale) => {
|
|
169
|
+
if (property.locales[locale].defaultValue?.mediaKitUrl?.startsWith('mediakit://')) {
|
|
170
|
+
const downloadPromise = (async () => {
|
|
171
|
+
try {
|
|
172
|
+
const fileName = path.basename(property.locales[locale].defaultValue.mediaKitUrl);
|
|
173
|
+
const targetPath = path.join(dir, getPreviewImageRelativePath(fileName));
|
|
174
|
+
if (fs.existsSync(targetPath)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
await downloadAsset({
|
|
178
|
+
asset: fileName,
|
|
179
|
+
savePath: targetPath,
|
|
180
|
+
componentDid: process.env.BLOCKLET_COMPONENT_DID || '',
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
console.error('Download media kit file failed:', error.message);
|
|
185
|
+
}
|
|
186
|
+
})();
|
|
187
|
+
downloadPromises.push(downloadPromise);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
if (downloadPromises.length > 0) {
|
|
193
|
+
await Promise.allSettled(downloadPromises);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
162
196
|
fs.writeFileSync(filePath, JSON.stringify(mergedContent, null, 2));
|
|
163
197
|
return res.json({ success: true, content: mergedContent, message: 'Updated' });
|
|
164
198
|
}
|