@blocklet/pages-kit-block-studio 0.6.14 → 0.6.15
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-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-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,53 +1,13 @@
|
|
|
1
1
|
import { getResourceExportDir } from '@blocklet/sdk/lib/component';
|
|
2
|
-
import { getMediaKitFileStream } from '@blocklet/uploader-server';
|
|
3
2
|
import { spawn } from 'child_process';
|
|
4
3
|
import { Router } from 'express';
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
import path, { join, basename } from 'path';
|
|
9
|
-
import ts from 'typescript';
|
|
10
|
-
import { findComponentFiles, libDir, getPreviewImageRelativePath, generateYaml, logger, getComponentScriptName, getEditComponentBlockName, getAigneOutputValueSchemaBlockName, getPropertiesSchemaBlockName, getGetServerSidePropsBlockName, } from '../utils/helper';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import { findComponentFiles } from '../utils/helper';
|
|
6
|
+
const allTag = '@ALL_COMPONENTS';
|
|
11
7
|
const DID = 'z2qa7BQdkEb3TwYyEYC1psK6uvmGnHSUHt5RM';
|
|
12
8
|
const RESOURCE_TYPE = 'page';
|
|
13
|
-
const allTag = '@ALL_COMPONENTS';
|
|
14
9
|
// use for tracking build process
|
|
15
10
|
let currentBuildProcess = null;
|
|
16
|
-
export function copyFile(src, dest) {
|
|
17
|
-
return new Promise((resolve, reject) => {
|
|
18
|
-
const readStream = fs.createReadStream(src);
|
|
19
|
-
const writeStream = fs.createWriteStream(dest);
|
|
20
|
-
readStream.on('error', reject);
|
|
21
|
-
writeStream.on('error', reject);
|
|
22
|
-
writeStream.on('finish', resolve);
|
|
23
|
-
readStream.pipe(writeStream);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
export async function copyDirectory(src, dest) {
|
|
27
|
-
await fs.promises.mkdir(dest, { recursive: true });
|
|
28
|
-
const entries = await fs.promises.readdir(src, { withFileTypes: true });
|
|
29
|
-
for (const entry of entries) {
|
|
30
|
-
const srcPath = path.join(src, entry.name);
|
|
31
|
-
const destPath = path.join(dest, entry.name);
|
|
32
|
-
if (entry.isDirectory()) {
|
|
33
|
-
// eslint-disable-next-line no-await-in-loop
|
|
34
|
-
await copyDirectory(srcPath, destPath);
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
// eslint-disable-next-line no-await-in-loop
|
|
38
|
-
await copyFile(srcPath, destPath);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
export async function copyRecursive(src, dest) {
|
|
43
|
-
const srcStats = await fs.promises.stat(src);
|
|
44
|
-
if (srcStats.isDirectory()) {
|
|
45
|
-
await copyDirectory(src, dest);
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
await copyFile(src, dest);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
11
|
const getExportDir = (projectId, releaseId) => {
|
|
52
12
|
const exportDir = getResourceExportDir({ projectId, releaseId });
|
|
53
13
|
const dir = join(exportDir, DID, RESOURCE_TYPE);
|
|
@@ -59,8 +19,6 @@ initResourceRouter.get('/', async (_req, res) => {
|
|
|
59
19
|
const rootDir = process.cwd();
|
|
60
20
|
const files = findComponentFiles({ cwd: rootDir, strictExistMetadata: true });
|
|
61
21
|
for (const file of files) {
|
|
62
|
-
// const filePath = path.join(rootDir, file);
|
|
63
|
-
// const fileContent = fs.readFileSync(filePath, 'utf8');
|
|
64
22
|
const { blockName } = file;
|
|
65
23
|
resources.push({
|
|
66
24
|
id: blockName,
|
|
@@ -94,9 +52,11 @@ initResourceRouter.post('/', async (req, res) => {
|
|
|
94
52
|
currentBuildProcess = null;
|
|
95
53
|
}
|
|
96
54
|
if (componentIds.length === 0) {
|
|
97
|
-
throw new Error('No components
|
|
55
|
+
throw new Error('No components found, please check has any component been selected');
|
|
98
56
|
}
|
|
99
|
-
//
|
|
57
|
+
// Get export directory
|
|
58
|
+
const exportDir = getExportDir(projectId, releaseId);
|
|
59
|
+
// Execute build command with export directory
|
|
100
60
|
const buildProcess = spawn('pnpm', ['run', 'build-lib'], {
|
|
101
61
|
stdio: 'inherit',
|
|
102
62
|
shell: true,
|
|
@@ -104,6 +64,7 @@ initResourceRouter.post('/', async (req, res) => {
|
|
|
104
64
|
...process.env,
|
|
105
65
|
FORCE_COLOR: '1',
|
|
106
66
|
BLOCK_FILTER: componentIds.join(','),
|
|
67
|
+
EXPORT_DIR: exportDir,
|
|
107
68
|
NODE_OPTIONS: '--max_old_space_size=16384',
|
|
108
69
|
NODE_ENV: 'production',
|
|
109
70
|
},
|
|
@@ -122,183 +83,6 @@ initResourceRouter.post('/', async (req, res) => {
|
|
|
122
83
|
reject(error);
|
|
123
84
|
});
|
|
124
85
|
});
|
|
125
|
-
const dir = getExportDir(projectId, releaseId);
|
|
126
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
127
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
128
|
-
const rootDir = process.cwd();
|
|
129
|
-
const distDir = join(rootDir, libDir);
|
|
130
|
-
const codeDir = join(distDir, 'es');
|
|
131
|
-
const tmpPackage = join(distDir, 'resource-blocklet');
|
|
132
|
-
fs.mkdirSync(tmpPackage, { recursive: true });
|
|
133
|
-
const pagesDir = join(tmpPackage, 'pages');
|
|
134
|
-
fs.mkdirSync(pagesDir, { recursive: true });
|
|
135
|
-
const componentsDir = join(tmpPackage, 'components');
|
|
136
|
-
fs.mkdirSync(componentsDir, { recursive: true });
|
|
137
|
-
const chunksDir = join(tmpPackage, 'chunks');
|
|
138
|
-
fs.mkdirSync(chunksDir, { recursive: true });
|
|
139
|
-
const chunksMapPath = join(codeDir, 'chunks-map.json');
|
|
140
|
-
const chunksMap = JSON.parse(fs.readFileSync(chunksMapPath, 'utf8'));
|
|
141
|
-
// get @metadata.json by glob
|
|
142
|
-
const canUseComponents = findComponentFiles({ cwd: rootDir, filter: componentIds, strictExistMetadata: true });
|
|
143
|
-
// Filter and process metadata files
|
|
144
|
-
const metadataList = await Promise.all(canUseComponents.map(async ({ fullPath, blockName, metadata: _metadata }) => {
|
|
145
|
-
// get metadata
|
|
146
|
-
const metadata = _metadata;
|
|
147
|
-
const jsName = `${blockName}.js`;
|
|
148
|
-
// set version to 2
|
|
149
|
-
set(metadata, 'version', 2);
|
|
150
|
-
// get code to metadata
|
|
151
|
-
const code = fs.readFileSync(join(codeDir, `${getComponentScriptName(blockName, 'esm')}.js`), 'utf8');
|
|
152
|
-
if (code) {
|
|
153
|
-
set(metadata, 'renderer.script', code);
|
|
154
|
-
set(metadata, 'renderer.type', 'react-component');
|
|
155
|
-
set(metadata, 'renderer.chunks', chunksMap[jsName] || []);
|
|
156
|
-
}
|
|
157
|
-
// get cjs by code with transpileModule
|
|
158
|
-
try {
|
|
159
|
-
const cjsCode = ts.transpileModule(code, {
|
|
160
|
-
compilerOptions: {
|
|
161
|
-
jsx: ts.JsxEmit.React,
|
|
162
|
-
target: ts.ScriptTarget.ES2016,
|
|
163
|
-
module: ts.ModuleKind.CommonJS,
|
|
164
|
-
moduleResolution: ts.ModuleResolutionKind.Node16,
|
|
165
|
-
},
|
|
166
|
-
}).outputText;
|
|
167
|
-
if (cjsCode) {
|
|
168
|
-
set(metadata, 'renderer.cjsScript', cjsCode);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
catch (error) {
|
|
172
|
-
// ignore error
|
|
173
|
-
}
|
|
174
|
-
// get edit component code
|
|
175
|
-
try {
|
|
176
|
-
const editComponentJsName = `${getEditComponentBlockName(blockName)}.js`;
|
|
177
|
-
const editComponentCode = fs.readFileSync(join(codeDir, editComponentJsName), 'utf8');
|
|
178
|
-
if (editComponentCode) {
|
|
179
|
-
// add edit component code
|
|
180
|
-
set(metadata, 'renderer.editComponent', editComponentCode);
|
|
181
|
-
// append edit component chunks
|
|
182
|
-
set(metadata, 'renderer.chunks', Array.from(new Set([...get(metadata, 'renderer.chunks', []), ...(chunksMap[editComponentJsName] || [])])));
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
catch (error) {
|
|
186
|
-
// ignore error
|
|
187
|
-
}
|
|
188
|
-
// get aigne output value schema
|
|
189
|
-
try {
|
|
190
|
-
const aigneOutputValueSchemaJsName = `${getAigneOutputValueSchemaBlockName(blockName)}.js`;
|
|
191
|
-
const aigneOutputValueSchemaCode = fs.readFileSync(join(codeDir, aigneOutputValueSchemaJsName), 'utf8');
|
|
192
|
-
if (aigneOutputValueSchemaCode) {
|
|
193
|
-
set(metadata, 'renderer.aigneOutputValueSchema', aigneOutputValueSchemaCode);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
catch (error) {
|
|
197
|
-
// ignore error
|
|
198
|
-
}
|
|
199
|
-
// get properties schema
|
|
200
|
-
try {
|
|
201
|
-
const propertiesSchemaJsName = `${getPropertiesSchemaBlockName(blockName)}.js`;
|
|
202
|
-
const propertiesSchemaCode = fs.readFileSync(join(codeDir, propertiesSchemaJsName), 'utf8');
|
|
203
|
-
if (propertiesSchemaCode) {
|
|
204
|
-
set(metadata, 'renderer.PROPERTIES_SCHEMA', propertiesSchemaCode);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
catch (error) {
|
|
208
|
-
// ignore error
|
|
209
|
-
}
|
|
210
|
-
// get getServerSideProps
|
|
211
|
-
try {
|
|
212
|
-
const getServerSidePropsJsName = `${getGetServerSidePropsBlockName(blockName)}.js`;
|
|
213
|
-
const getServerSidePropsCode = fs.readFileSync(join(codeDir, getServerSidePropsJsName), 'utf8');
|
|
214
|
-
if (getServerSidePropsCode) {
|
|
215
|
-
set(metadata, 'renderer.getServerSideProps', getServerSidePropsCode);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
// ignore error
|
|
220
|
-
}
|
|
221
|
-
// Handle preview image if exists
|
|
222
|
-
if (metadata.previewImage) {
|
|
223
|
-
const imagePath = path.join(path.dirname(fullPath), getPreviewImageRelativePath(metadata.previewImage));
|
|
224
|
-
const imageDestPath = path.join(componentsDir, metadata.previewImage);
|
|
225
|
-
if (fs.existsSync(imagePath)) {
|
|
226
|
-
fs.copyFileSync(imagePath, imageDestPath);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
// Handle type:url properties url
|
|
230
|
-
if (metadata.properties) {
|
|
231
|
-
const downloadPromises = [];
|
|
232
|
-
Object.keys(metadata.properties).forEach((key) => {
|
|
233
|
-
const property = metadata.properties[key].data;
|
|
234
|
-
if (property.type === 'url') {
|
|
235
|
-
// 如果每个 locales 的默认值里面,存在 mediaKitUrl 的值,需要把图片下载放到 imagePath 里面
|
|
236
|
-
Object.keys(property.locales).forEach((locale) => {
|
|
237
|
-
if (property.locales[locale].defaultValue?.mediaKitUrl?.startsWith('mediakit://')) {
|
|
238
|
-
const downloadPromise = (async () => {
|
|
239
|
-
try {
|
|
240
|
-
const fileName = basename(property.locales[locale].defaultValue.mediaKitUrl);
|
|
241
|
-
const targetPath = path.join(componentsDir, fileName);
|
|
242
|
-
// Check if file already exists to avoid redundant downloads
|
|
243
|
-
if (fs.existsSync(targetPath)) {
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
const resWithStream = await getMediaKitFileStream(property.locales[locale].defaultValue.mediaKitUrl);
|
|
247
|
-
if (!resWithStream.data) {
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
// create write stream
|
|
251
|
-
const writeStream = fs.createWriteStream(targetPath);
|
|
252
|
-
resWithStream.data.pipe(writeStream);
|
|
253
|
-
// wait for write stream to finish
|
|
254
|
-
await new Promise((resolve, reject) => {
|
|
255
|
-
writeStream.on('finish', () => {
|
|
256
|
-
// 修改 property.locales[locale].defaultValue.url 变为 mediakit:// 开头
|
|
257
|
-
property.locales[locale].defaultValue.url = property.locales[locale].defaultValue.mediaKitUrl;
|
|
258
|
-
resolve();
|
|
259
|
-
});
|
|
260
|
-
writeStream.on('error', reject);
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
catch (error) {
|
|
264
|
-
console.error('Download media kit file failed:', error.message);
|
|
265
|
-
}
|
|
266
|
-
})();
|
|
267
|
-
downloadPromises.push(downloadPromise);
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
// Wait for all downloads to complete for this metadata
|
|
273
|
-
if (downloadPromises.length > 0) {
|
|
274
|
-
await Promise.allSettled(downloadPromises);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
// write metadata to metadataPath
|
|
278
|
-
const metadataYmlPath = path.join(componentsDir, `${metadata.name || 'unnamed'}.${metadata.id}.yml`);
|
|
279
|
-
fs.writeFileSync(metadataYmlPath, generateYaml(metadata));
|
|
280
|
-
return metadata;
|
|
281
|
-
}));
|
|
282
|
-
// cp chunks dir
|
|
283
|
-
await copyRecursive(join(codeDir, 'chunks'), chunksDir);
|
|
284
|
-
// write pages.config.yml
|
|
285
|
-
const pagesConfigPath = path.join(tmpPackage, '.blocklet/pages/pages.config.yml');
|
|
286
|
-
fs.mkdirSync(path.dirname(pagesConfigPath), { recursive: true });
|
|
287
|
-
const pagesConfig = {
|
|
288
|
-
version: 2,
|
|
289
|
-
pages: [],
|
|
290
|
-
components: metadataList.map((metadata) => ({
|
|
291
|
-
id: metadata.id,
|
|
292
|
-
name: metadata.name,
|
|
293
|
-
})),
|
|
294
|
-
supportedLocales: [],
|
|
295
|
-
config: {},
|
|
296
|
-
};
|
|
297
|
-
fs.writeFileSync(pagesConfigPath, generateYaml(pagesConfig));
|
|
298
|
-
logger.info('generate resource blocklet block count:', metadataList.length);
|
|
299
|
-
await copyRecursive(tmpPackage, dir);
|
|
300
|
-
// remove tmpPackage
|
|
301
|
-
// fs.rmSync(tmpPackage, { recursive: true, force: true });
|
|
302
86
|
res.json({ success: true });
|
|
303
87
|
}
|
|
304
88
|
catch (error) {
|