@nextbridgehq/payload-block-builder 0.1.5 → 0.1.6
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/dist/bin/init.js +135 -1
- package/package.json +1 -1
package/dist/bin/init.js
CHANGED
|
@@ -42,7 +42,131 @@ function findAppDir() {
|
|
|
42
42
|
}
|
|
43
43
|
return null;
|
|
44
44
|
}
|
|
45
|
+
function findPayloadConfig() {
|
|
46
|
+
const candidates = [
|
|
47
|
+
path.join(process.cwd(), "src", "payload.config.ts"),
|
|
48
|
+
path.join(process.cwd(), "payload.config.ts")
|
|
49
|
+
];
|
|
50
|
+
for (const c of candidates) {
|
|
51
|
+
if (fs.existsSync(c)) return c;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
function detectDbAdapter(content) {
|
|
56
|
+
if (/postgresAdapter|db-postgres/.test(content)) return "postgres";
|
|
57
|
+
if (/sqliteAdapter|db-sqlite/.test(content)) return "sqlite";
|
|
58
|
+
return "other";
|
|
59
|
+
}
|
|
60
|
+
function findClosingBracket(content, openPos) {
|
|
61
|
+
let depth = 0;
|
|
62
|
+
for (let i = openPos; i < content.length; i++) {
|
|
63
|
+
if (content[i] === "[") depth++;
|
|
64
|
+
else if (content[i] === "]") {
|
|
65
|
+
depth--;
|
|
66
|
+
if (depth === 0) return i;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return -1;
|
|
70
|
+
}
|
|
71
|
+
function addImport(content) {
|
|
72
|
+
const newImport = `import { dynamicBlocksPlugin } from '@nextbridgehq/payload-block-builder'`;
|
|
73
|
+
const lastFromRegex = /^.*from\s+['"][^'"]+['"]\s*;?\s*$/gm;
|
|
74
|
+
let lastMatch = null;
|
|
75
|
+
let m;
|
|
76
|
+
while ((m = lastFromRegex.exec(content)) !== null) lastMatch = m;
|
|
77
|
+
if (!lastMatch) return newImport + "\n" + content;
|
|
78
|
+
const insertPos = lastMatch.index + lastMatch[0].length;
|
|
79
|
+
return content.slice(0, insertPos) + "\n" + newImport + content.slice(insertPos);
|
|
80
|
+
}
|
|
81
|
+
function insertIntoPluginsArray(content, collectionsArg) {
|
|
82
|
+
const pluginsMatch = /\bplugins\s*:\s*\[/.exec(content);
|
|
83
|
+
if (!pluginsMatch) return null;
|
|
84
|
+
const openPos = content.indexOf("[", pluginsMatch.index);
|
|
85
|
+
const closePos = findClosingBracket(content, openPos);
|
|
86
|
+
if (closePos === -1) return null;
|
|
87
|
+
const beforeClose = content.slice(0, closePos);
|
|
88
|
+
const prevNL = beforeClose.lastIndexOf("\n");
|
|
89
|
+
const closingIndent = beforeClose.slice(prevNL + 1).match(/^([ \t]*)/)?.[1] ?? " ";
|
|
90
|
+
const entryIndent = closingIndent + " ";
|
|
91
|
+
const newLine = `${entryIndent}dynamicBlocksPlugin({ collections: [${collectionsArg}] }),
|
|
92
|
+
`;
|
|
93
|
+
return content.slice(0, prevNL + 1) + newLine + content.slice(prevNL + 1);
|
|
94
|
+
}
|
|
95
|
+
function injectPluginsBlock(content, collectionsArg) {
|
|
96
|
+
const collMatch = /\bcollections\s*:\s*\[/.exec(content);
|
|
97
|
+
if (!collMatch) return null;
|
|
98
|
+
const collOpen = content.indexOf("[", collMatch.index);
|
|
99
|
+
const collClose = findClosingBracket(content, collOpen);
|
|
100
|
+
if (collClose === -1) return null;
|
|
101
|
+
const afterCollLine = content.indexOf("\n", collClose);
|
|
102
|
+
if (afterCollLine === -1) return null;
|
|
103
|
+
const beforeColl = content.slice(0, collMatch.index);
|
|
104
|
+
const collLineStart = beforeColl.lastIndexOf("\n") + 1;
|
|
105
|
+
const outerIndent = beforeColl.slice(collLineStart).match(/^([ \t]*)/)?.[1] ?? " ";
|
|
106
|
+
const entryIndent = outerIndent + " ";
|
|
107
|
+
const pluginsBlock = `${outerIndent}plugins: [
|
|
108
|
+
${entryIndent}dynamicBlocksPlugin({ collections: [${collectionsArg}] }),
|
|
109
|
+
${outerIndent}],`;
|
|
110
|
+
return content.slice(0, afterCollLine) + "\n" + pluginsBlock + content.slice(afterCollLine);
|
|
111
|
+
}
|
|
112
|
+
function modifyPayloadConfig(configPath, collectionsArg) {
|
|
113
|
+
let content = fs.readFileSync(configPath, "utf8");
|
|
114
|
+
if (content.includes("dynamicBlocksPlugin")) {
|
|
115
|
+
console.log(`Skipped: dynamicBlocksPlugin already present in ${configPath}`);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
content = addImport(content);
|
|
119
|
+
const noComments = content.replace(/\/\/[^\n]*/g, "");
|
|
120
|
+
const hasPluginsArray = /\bplugins\s*:\s*\[/.test(noComments);
|
|
121
|
+
const hasPluginsShorthand = /^\s*plugins\s*,/m.test(noComments);
|
|
122
|
+
if (hasPluginsShorthand && !hasPluginsArray) {
|
|
123
|
+
fs.writeFileSync(configPath, content, "utf8");
|
|
124
|
+
console.log(`Updated: ${configPath} (added import)`);
|
|
125
|
+
console.log(` Note: 'plugins' is imported from another file.`);
|
|
126
|
+
console.log(` Add dynamicBlocksPlugin({ collections: ['pages'] }) to that file manually.`);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
let result = null;
|
|
130
|
+
if (hasPluginsArray) {
|
|
131
|
+
result = insertIntoPluginsArray(content, collectionsArg);
|
|
132
|
+
} else {
|
|
133
|
+
result = injectPluginsBlock(content, collectionsArg);
|
|
134
|
+
}
|
|
135
|
+
if (result === null) {
|
|
136
|
+
fs.writeFileSync(configPath, content, "utf8");
|
|
137
|
+
console.log(`Updated: ${configPath} (added import only)`);
|
|
138
|
+
console.log(` Could not auto-detect plugins array. Add manually:`);
|
|
139
|
+
console.log(` plugins: [ dynamicBlocksPlugin({ collections: [${collectionsArg}] }) ]`);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
fs.writeFileSync(configPath, result, "utf8");
|
|
143
|
+
console.log(`Updated: ${configPath} (added dynamicBlocksPlugin)`);
|
|
144
|
+
}
|
|
145
|
+
function printNextSteps(dbAdapter) {
|
|
146
|
+
console.log("\n--- Next Steps ---");
|
|
147
|
+
console.log("1. Regenerate the Payload import map:");
|
|
148
|
+
console.log(" pnpm generate:importmap");
|
|
149
|
+
if (dbAdapter === "postgres") {
|
|
150
|
+
console.log("\n2. PostgreSQL detected. Start the dev server \u2014 Payload will auto-push schema:");
|
|
151
|
+
console.log(" pnpm dev");
|
|
152
|
+
console.log("\n Or if you prefer migrations:");
|
|
153
|
+
console.log(" pnpm payload migrate:create --name=add_block_builder");
|
|
154
|
+
console.log(" pnpm payload migrate");
|
|
155
|
+
} else if (dbAdapter === "sqlite") {
|
|
156
|
+
console.log("\n2. Start the dev server \u2014 Payload will auto-migrate SQLite:");
|
|
157
|
+
console.log(" pnpm dev");
|
|
158
|
+
} else {
|
|
159
|
+
console.log("\n2. Start the dev server:");
|
|
160
|
+
console.log(" pnpm dev");
|
|
161
|
+
}
|
|
162
|
+
console.log("\nThen visit: http://localhost:3000/block-builder");
|
|
163
|
+
console.log("------------------");
|
|
164
|
+
}
|
|
45
165
|
function main() {
|
|
166
|
+
const args = process.argv.slice(2);
|
|
167
|
+
const collectionsFlag = args.find((a) => a.startsWith("--collections="));
|
|
168
|
+
const collectionsValue = collectionsFlag ? collectionsFlag.replace("--collections=", "").split(",").map((s) => s.trim()) : ["pages"];
|
|
169
|
+
const collectionsArg = collectionsValue.map((c) => `'${c}'`).join(", ");
|
|
46
170
|
const appDir = findAppDir();
|
|
47
171
|
if (!appDir) {
|
|
48
172
|
console.error("Could not find app directory. Make sure you are in the root of a Next.js project.");
|
|
@@ -77,6 +201,16 @@ function main() {
|
|
|
77
201
|
console.log(`Skipped: ${customScssPath} already has block-builder imports`);
|
|
78
202
|
}
|
|
79
203
|
}
|
|
80
|
-
|
|
204
|
+
const configPath = findPayloadConfig();
|
|
205
|
+
if (!configPath) {
|
|
206
|
+
console.log("\nNote: payload.config.ts not found. Add the plugin manually:");
|
|
207
|
+
console.log(` import { dynamicBlocksPlugin } from '@nextbridgehq/payload-block-builder'`);
|
|
208
|
+
console.log(` plugins: [ dynamicBlocksPlugin({ collections: [${collectionsArg}] }) ]`);
|
|
209
|
+
printNextSteps("other");
|
|
210
|
+
} else {
|
|
211
|
+
const dbAdapter = detectDbAdapter(fs.readFileSync(configPath, "utf8"));
|
|
212
|
+
modifyPayloadConfig(configPath, collectionsArg);
|
|
213
|
+
printNextSteps(dbAdapter);
|
|
214
|
+
}
|
|
81
215
|
}
|
|
82
216
|
main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextbridgehq/payload-block-builder",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Block Builder for Payload CMS",
|
|
5
5
|
"keywords": ["payload", "payload-plugin", "cms", "block-builder", "dynamic-blocks"],
|
|
6
6
|
"homepage": "https://github.com/nextbridgehq/block-builder",
|