@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.
- package/lib/cjs/middlewares/init-block-studio-router.js +81 -0
- package/lib/cjs/middlewares/init-resource-router.js +1 -1
- package/lib/cjs/middlewares/init-uploader-router.js +2 -0
- package/lib/cjs/plugins/vite-plugin-block-studio.js +8 -2
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/build-lib.js +3 -4
- package/lib/cjs/utils/helper.js +9 -1
- package/lib/esm/middlewares/init-block-studio-router.js +75 -0
- package/lib/esm/middlewares/init-resource-router.js +2 -2
- package/lib/esm/middlewares/init-uploader-router.js +2 -0
- package/lib/esm/plugins/vite-plugin-block-studio.js +8 -2
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/build-lib.js +1 -2
- package/lib/esm/utils/helper.js +7 -0
- package/lib/types/middlewares/init-block-studio-router.d.ts +2 -0
- package/lib/types/middlewares/init-uploader-router.d.ts +0 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/helper.d.ts +3 -0
- package/package.json +13 -9
|
@@ -31,14 +31,13 @@ function buildLib(options) {
|
|
|
31
31
|
: ((_b = process.env.BLOCK_FILTER) === null || _b === void 0 ? void 0 : _b.split(',')) || null;
|
|
32
32
|
const ignoreViteLog = false; // process.argv.includes('--log') ? false : process.env.BLOCK_LOG !== 'true';
|
|
33
33
|
const multiMode = process.argv.includes('--multi') || process.env.BLOCK_MULTI === 'true';
|
|
34
|
-
const libDir = 'lib';
|
|
35
34
|
const blocks = allBlocks.filter((name) => !filterModules || filterModules.includes(name || ''));
|
|
36
35
|
if (!blocks.length) {
|
|
37
36
|
console.log(chalk_1.default.yellow('No blocks founds'));
|
|
38
37
|
return;
|
|
39
38
|
}
|
|
40
39
|
// Clean up the lib directory
|
|
41
|
-
yield (0, child_process_1.execSync)(`rm -rf ${libDir}`, { cwd: workingDir });
|
|
40
|
+
yield (0, child_process_1.execSync)(`rm -rf ${helper_1.libDir}`, { cwd: workingDir });
|
|
42
41
|
console.log(chalk_1.default.gray('Clean up the lib directory'));
|
|
43
42
|
// Start to build
|
|
44
43
|
console.log(chalk_1.default.cyan(`Start to build ${blocks.length} blocks in parallel\n`));
|
|
@@ -57,7 +56,7 @@ function buildLib(options) {
|
|
|
57
56
|
'--filter',
|
|
58
57
|
`"${blockName}"`,
|
|
59
58
|
'--outDir',
|
|
60
|
-
libDir,
|
|
59
|
+
helper_1.libDir,
|
|
61
60
|
'--emptyOutDir=false',
|
|
62
61
|
hasConfig ? `--config ${configFile}.*` : '',
|
|
63
62
|
], {
|
|
@@ -77,7 +76,7 @@ function buildLib(options) {
|
|
|
77
76
|
});
|
|
78
77
|
}));
|
|
79
78
|
console.log(chalk_1.default.green(`\nBuild ${blocks.length} blocks successfully`));
|
|
80
|
-
console.log(chalk_1.default.gray(`Generate the ${libDir} directory successfully`));
|
|
79
|
+
console.log(chalk_1.default.gray(`Generate the ${helper_1.libDir} directory successfully`));
|
|
81
80
|
process.exit(0);
|
|
82
81
|
}
|
|
83
82
|
catch (err) {
|
package/lib/cjs/utils/helper.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.logger = void 0;
|
|
6
|
+
exports.isDev = exports.isPathSafe = exports.libDir = exports.logger = void 0;
|
|
7
7
|
exports.setBlockEntryFilesPattern = setBlockEntryFilesPattern;
|
|
8
8
|
exports.getBlockEntryFilesPattern = getBlockEntryFilesPattern;
|
|
9
9
|
exports.findComponentFiles = findComponentFiles;
|
|
@@ -53,3 +53,11 @@ function getBlockName(entry) {
|
|
|
53
53
|
}
|
|
54
54
|
exports.logger = console;
|
|
55
55
|
function generateBlockYml() { }
|
|
56
|
+
exports.libDir = 'lib';
|
|
57
|
+
const isPathSafe = (filePath) => {
|
|
58
|
+
const normalizedPath = path_1.default.normalize(filePath);
|
|
59
|
+
const pwd = process.env.PWD || process.cwd();
|
|
60
|
+
return (!normalizedPath.includes('../') && !normalizedPath.includes('..\\') && path_1.default.resolve(normalizedPath).startsWith(pwd));
|
|
61
|
+
};
|
|
62
|
+
exports.isPathSafe = isPathSafe;
|
|
63
|
+
exports.isDev = process.env.BLOCKLET_MODE === 'development';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Router } from 'express';
|
|
11
|
+
import fs from 'fs';
|
|
12
|
+
import path from 'path';
|
|
13
|
+
import { isPathSafe, isDev } from '../utils/helper';
|
|
14
|
+
export const initBlockStudioRouter = Router();
|
|
15
|
+
const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.svg'];
|
|
16
|
+
initBlockStudioRouter.get('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
if (!isDev) {
|
|
18
|
+
return res.status(403).json({ error: 'Only available in development mode' });
|
|
19
|
+
}
|
|
20
|
+
const filePath = req.query.path;
|
|
21
|
+
if (!filePath) {
|
|
22
|
+
return res.status(400).json({ error: 'Path parameter is required' });
|
|
23
|
+
}
|
|
24
|
+
if (!isPathSafe(filePath)) {
|
|
25
|
+
return res.status(403).json({ error: 'Invalid path' });
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
if (!fs.existsSync(filePath)) {
|
|
29
|
+
return res.json(null);
|
|
30
|
+
}
|
|
31
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
32
|
+
if (BINARY_EXTENSIONS.includes(ext)) {
|
|
33
|
+
// For images, stream the file directly
|
|
34
|
+
const mimeType = `image/${ext.slice(1)}`;
|
|
35
|
+
res.setHeader('Content-Type', mimeType);
|
|
36
|
+
return fs.createReadStream(filePath).pipe(res);
|
|
37
|
+
}
|
|
38
|
+
// For text files, try to parse as JSON
|
|
39
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
40
|
+
try {
|
|
41
|
+
return res.json(JSON.parse(content));
|
|
42
|
+
}
|
|
43
|
+
catch (_a) {
|
|
44
|
+
// If JSON parsing fails, return as plain text
|
|
45
|
+
return res.json(content);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
return res.status(500).json({ error: 'Failed to read file' });
|
|
50
|
+
}
|
|
51
|
+
}));
|
|
52
|
+
initBlockStudioRouter.post('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
if (!isDev) {
|
|
54
|
+
return res.status(403).json({ error: 'Only available in development mode' });
|
|
55
|
+
}
|
|
56
|
+
const { path: filePath, content } = req.body;
|
|
57
|
+
if (!filePath || content === undefined) {
|
|
58
|
+
return res.status(400).json({ error: 'Path and content are required' });
|
|
59
|
+
}
|
|
60
|
+
if (!isPathSafe(filePath)) {
|
|
61
|
+
return res.status(403).json({ error: 'Invalid path' });
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const dir = path.dirname(filePath);
|
|
65
|
+
if (!fs.existsSync(dir)) {
|
|
66
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
67
|
+
}
|
|
68
|
+
fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
|
|
69
|
+
return res.json({ success: true });
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
return res.status(500).json({ error: 'Failed to write file' });
|
|
73
|
+
}
|
|
74
|
+
}));
|
|
75
|
+
export default initBlockStudioRouter;
|
|
@@ -12,7 +12,7 @@ import { spawn } from 'child_process';
|
|
|
12
12
|
import { Router } from 'express';
|
|
13
13
|
import fs from 'fs';
|
|
14
14
|
import path, { join } from 'path';
|
|
15
|
-
import { findComponentFiles } from '../utils/helper';
|
|
15
|
+
import { findComponentFiles, libDir } from '../utils/helper';
|
|
16
16
|
const DID = 'z2qa7rr3eUyVnWp2PCxEVARuUfLFh6cE5V2xV';
|
|
17
17
|
const RESOURCE_TYPE = 'page';
|
|
18
18
|
const allTag = '@ALL_COMPONENTS';
|
|
@@ -111,7 +111,7 @@ initResourceRouter.post('/', (req, res) => __awaiter(void 0, void 0, void 0, fun
|
|
|
111
111
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
112
112
|
fs.mkdirSync(dir, { recursive: true });
|
|
113
113
|
const rootDir = process.cwd();
|
|
114
|
-
const tmpPackage = join(rootDir,
|
|
114
|
+
const tmpPackage = join(rootDir, libDir);
|
|
115
115
|
yield copyRecursive(tmpPackage, dir);
|
|
116
116
|
fs.rmSync(tmpPackage, { recursive: true, force: true });
|
|
117
117
|
res.json({ success: true });
|
|
@@ -121,16 +121,22 @@ export function initBlockStudioPlugins(options) {
|
|
|
121
121
|
const isHtml = relative.endsWith('.html');
|
|
122
122
|
const blockName = getBlockName(relative);
|
|
123
123
|
const pageId = `/${blockName}`;
|
|
124
|
+
const dirPath = path.dirname(filePath);
|
|
125
|
+
const metadataPath = path.join(dirPath, '@metadata.json');
|
|
124
126
|
api.addPageData({
|
|
125
127
|
pageId,
|
|
126
|
-
dataPath: isHtml ? `${VIRTUAL_MODULE_ID}?dir=${
|
|
128
|
+
dataPath: isHtml ? `${VIRTUAL_MODULE_ID}?dir=${dirPath}` : filePath,
|
|
127
129
|
staticData: isHtml
|
|
128
130
|
? {
|
|
129
131
|
isHtmlPreview: true,
|
|
130
132
|
dataPath: filePath,
|
|
131
133
|
blockName,
|
|
134
|
+
dirPath,
|
|
135
|
+
metadataPath,
|
|
132
136
|
}
|
|
133
|
-
: Object.assign(Object.assign({}, (yield helpers.extractStaticData(file))), { code: readFileSync(file.path, 'utf-8'), dataPath: filePath, blockName
|
|
137
|
+
: Object.assign(Object.assign({}, (yield helpers.extractStaticData(file))), { code: readFileSync(file.path, 'utf-8'), dataPath: filePath, blockName,
|
|
138
|
+
dirPath,
|
|
139
|
+
metadataPath }),
|
|
134
140
|
});
|
|
135
141
|
});
|
|
136
142
|
});
|