@sonicjs-cms/core 2.3.1 → 2.3.3
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/bin/db-reset.js +240 -0
- package/dist/{chunk-5QJX2VMP.cjs → chunk-33WVERFH.cjs} +90 -90
- package/dist/{chunk-5QJX2VMP.cjs.map → chunk-33WVERFH.cjs.map} +1 -1
- package/dist/{chunk-WK5EUGBO.js → chunk-73KQVU4R.js} +3 -3
- package/dist/{chunk-WK5EUGBO.js.map → chunk-73KQVU4R.js.map} +1 -1
- package/dist/{chunk-23VPL6VI.cjs → chunk-FNODBJ4I.cjs} +31 -7
- package/dist/chunk-FNODBJ4I.cjs.map +1 -0
- package/dist/{chunk-6RJU7HL5.js → chunk-KID6B3NC.js} +7 -3
- package/dist/chunk-KID6B3NC.js.map +1 -0
- package/dist/{chunk-44LBCF3B.cjs → chunk-NSGYDNNC.cjs} +7 -3
- package/dist/chunk-NSGYDNNC.cjs.map +1 -0
- package/dist/{chunk-3MPQII4R.js → chunk-POF4PJI2.js} +31 -7
- package/dist/chunk-POF4PJI2.js.map +1 -0
- package/dist/{chunk-ZPERJATF.js → chunk-S3HV6SMQ.js} +8 -8
- package/dist/{chunk-ZPERJATF.js.map → chunk-S3HV6SMQ.js.map} +1 -1
- package/dist/{chunk-5UUYHAZT.cjs → chunk-W5TX4DX4.cjs} +4 -4
- package/dist/{chunk-5UUYHAZT.cjs.map → chunk-W5TX4DX4.cjs.map} +1 -1
- package/dist/index.cjs +74 -74
- package/dist/index.js +7 -7
- package/dist/middleware.cjs +23 -23
- package/dist/middleware.js +2 -2
- package/dist/migrations-HMNQBOHY.js +4 -0
- package/dist/{migrations-CCLAGJGP.js.map → migrations-HMNQBOHY.js.map} +1 -1
- package/dist/migrations-MN533MCV.cjs +13 -0
- package/dist/{migrations-O6INOHRD.cjs.map → migrations-MN533MCV.cjs.map} +1 -1
- package/dist/routes.cjs +24 -24
- package/dist/routes.js +4 -4
- package/dist/services.cjs +2 -2
- package/dist/services.js +1 -1
- package/dist/utils.cjs +11 -11
- package/dist/utils.js +1 -1
- package/package.json +5 -1
- package/dist/chunk-23VPL6VI.cjs.map +0 -1
- package/dist/chunk-3MPQII4R.js.map +0 -1
- package/dist/chunk-44LBCF3B.cjs.map +0 -1
- package/dist/chunk-6RJU7HL5.js.map +0 -1
- package/dist/migrations-CCLAGJGP.js +0 -4
- package/dist/migrations-O6INOHRD.cjs +0 -13
package/bin/db-reset.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { execSync, spawn } from 'child_process';
|
|
4
|
+
import { readFileSync, writeFileSync, existsSync, rmSync } from 'fs';
|
|
5
|
+
import { createInterface } from 'readline';
|
|
6
|
+
import { join, dirname } from 'path';
|
|
7
|
+
|
|
8
|
+
const colors = {
|
|
9
|
+
reset: '\x1b[0m',
|
|
10
|
+
green: '\x1b[32m',
|
|
11
|
+
yellow: '\x1b[33m',
|
|
12
|
+
red: '\x1b[31m',
|
|
13
|
+
cyan: '\x1b[36m',
|
|
14
|
+
bold: '\x1b[1m',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function log(message, color = '') {
|
|
18
|
+
console.log(`${color}${message}${colors.reset}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function execCommand(command, options = {}) {
|
|
22
|
+
try {
|
|
23
|
+
return execSync(command, { encoding: 'utf-8', stdio: options.silent ? 'pipe' : 'inherit', ...options });
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (options.ignoreError) return '';
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function execCommandOutput(command) {
|
|
31
|
+
try {
|
|
32
|
+
return execSync(command, { encoding: 'utf-8', stdio: 'pipe' }).trim();
|
|
33
|
+
} catch {
|
|
34
|
+
return '';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function prompt(question) {
|
|
39
|
+
const rl = createInterface({
|
|
40
|
+
input: process.stdin,
|
|
41
|
+
output: process.stdout,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return new Promise((resolve) => {
|
|
45
|
+
rl.question(question, (answer) => {
|
|
46
|
+
rl.close();
|
|
47
|
+
resolve(answer.toLowerCase());
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function findWranglerToml() {
|
|
53
|
+
// Look for wrangler.toml in current directory or parent directories
|
|
54
|
+
let dir = process.cwd();
|
|
55
|
+
for (let i = 0; i < 5; i++) {
|
|
56
|
+
const tomlPath = join(dir, 'wrangler.toml');
|
|
57
|
+
if (existsSync(tomlPath)) {
|
|
58
|
+
return { path: tomlPath, dir };
|
|
59
|
+
}
|
|
60
|
+
dir = dirname(dir);
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function main() {
|
|
66
|
+
log('\n🔧 SonicJS Database Reset Tool', colors.cyan + colors.bold);
|
|
67
|
+
log('================================\n', colors.cyan);
|
|
68
|
+
|
|
69
|
+
// Find wrangler.toml
|
|
70
|
+
const wranglerInfo = findWranglerToml();
|
|
71
|
+
if (!wranglerInfo) {
|
|
72
|
+
log('Error: Could not find wrangler.toml in current or parent directories', colors.red);
|
|
73
|
+
log('Please run this command from your SonicJS project directory.', colors.yellow);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const { path: wranglerPath, dir: projectDir } = wranglerInfo;
|
|
78
|
+
log(`Found wrangler.toml at: ${wranglerPath}`, colors.green);
|
|
79
|
+
|
|
80
|
+
// Change to project directory
|
|
81
|
+
process.chdir(projectDir);
|
|
82
|
+
|
|
83
|
+
// Get the current branch name
|
|
84
|
+
let branchName;
|
|
85
|
+
try {
|
|
86
|
+
branchName = execCommandOutput('git rev-parse --abbrev-ref HEAD');
|
|
87
|
+
} catch {
|
|
88
|
+
branchName = '';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!branchName || branchName === 'HEAD') {
|
|
92
|
+
log('Error: Could not determine branch name', colors.red);
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Create a safe database name from branch
|
|
97
|
+
const safeBranch = branchName.replace(/[^a-zA-Z0-9-]/g, '-').slice(0, 50);
|
|
98
|
+
const dbName = `sonicjs-worktree-${safeBranch}`;
|
|
99
|
+
|
|
100
|
+
log(`Setting up fresh D1 database for worktree: ${branchName}`, colors.cyan);
|
|
101
|
+
log(`Database name: ${dbName}\n`, colors.cyan);
|
|
102
|
+
|
|
103
|
+
// Check if database already exists
|
|
104
|
+
log('Checking for existing database...', colors.yellow);
|
|
105
|
+
let existingDbId = '';
|
|
106
|
+
try {
|
|
107
|
+
const listOutput = execCommandOutput('npx wrangler d1 list --json 2>/dev/null');
|
|
108
|
+
if (listOutput) {
|
|
109
|
+
const databases = JSON.parse(listOutput);
|
|
110
|
+
const existing = databases.find((db) => db.name === dbName);
|
|
111
|
+
if (existing) {
|
|
112
|
+
existingDbId = existing.uuid;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
// Ignore errors - database may not exist
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let dbId = existingDbId;
|
|
120
|
+
|
|
121
|
+
if (existingDbId) {
|
|
122
|
+
log(`Database ${dbName} already exists with ID: ${existingDbId}`, colors.yellow);
|
|
123
|
+
|
|
124
|
+
const answer = await prompt('Delete existing database and create fresh one? (y/N): ');
|
|
125
|
+
if (answer === 'y' || answer === 'yes') {
|
|
126
|
+
log('\nDeleting existing database...', colors.yellow);
|
|
127
|
+
try {
|
|
128
|
+
execCommand(`npx wrangler d1 delete "${dbName}" --skip-confirmation`, { silent: true });
|
|
129
|
+
existingDbId = '';
|
|
130
|
+
dbId = '';
|
|
131
|
+
} catch (error) {
|
|
132
|
+
log(`Warning: Failed to delete database: ${error.message}`, colors.yellow);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (!existingDbId || !dbId) {
|
|
138
|
+
// Create new database
|
|
139
|
+
log(`\nCreating new D1 database: ${dbName}`, colors.cyan);
|
|
140
|
+
let createOutput = '';
|
|
141
|
+
try {
|
|
142
|
+
createOutput = execCommandOutput(`npx wrangler d1 create "${dbName}" 2>&1`);
|
|
143
|
+
console.log(createOutput);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
log(`Error creating database: ${error.message}`, colors.red);
|
|
146
|
+
process.exit(1);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Extract database ID from creation output
|
|
150
|
+
const idMatch = createOutput.match(/database_id\s*=\s*"([^"]+)"/);
|
|
151
|
+
if (idMatch) {
|
|
152
|
+
dbId = idMatch[1];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// If extraction failed, try listing databases
|
|
156
|
+
if (!dbId) {
|
|
157
|
+
try {
|
|
158
|
+
const listOutput = execCommandOutput('npx wrangler d1 list --json');
|
|
159
|
+
if (listOutput) {
|
|
160
|
+
const databases = JSON.parse(listOutput);
|
|
161
|
+
const created = databases.find((db) => db.name === dbName);
|
|
162
|
+
if (created) {
|
|
163
|
+
dbId = created.uuid;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
} catch {
|
|
167
|
+
// Ignore
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (!dbId) {
|
|
173
|
+
log('Error: Failed to get database ID', colors.red);
|
|
174
|
+
process.exit(1);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
log(`\nDatabase ID: ${dbId}`, colors.green);
|
|
178
|
+
|
|
179
|
+
// Update wrangler.toml with the new database ID
|
|
180
|
+
log('\nUpdating wrangler.toml...', colors.yellow);
|
|
181
|
+
try {
|
|
182
|
+
let wranglerContent = readFileSync(wranglerPath, 'utf-8');
|
|
183
|
+
wranglerContent = wranglerContent.replace(
|
|
184
|
+
/database_id\s*=\s*"[^"]*"/,
|
|
185
|
+
`database_id = "${dbId}"`
|
|
186
|
+
);
|
|
187
|
+
wranglerContent = wranglerContent.replace(
|
|
188
|
+
/database_name\s*=\s*"[^"]*"/,
|
|
189
|
+
`database_name = "${dbName}"`
|
|
190
|
+
);
|
|
191
|
+
writeFileSync(wranglerPath, wranglerContent);
|
|
192
|
+
log('Updated wrangler.toml successfully', colors.green);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
log(`Error updating wrangler.toml: ${error.message}`, colors.red);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Reset local database by removing it
|
|
199
|
+
log('\nResetting local database...', colors.yellow);
|
|
200
|
+
const localDbPath = join(projectDir, '.wrangler', 'state', 'v3', 'd1');
|
|
201
|
+
if (existsSync(localDbPath)) {
|
|
202
|
+
try {
|
|
203
|
+
rmSync(localDbPath, { recursive: true, force: true });
|
|
204
|
+
log('Local database cleared.', colors.green);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
log(`Warning: Could not clear local database: ${error.message}`, colors.yellow);
|
|
207
|
+
}
|
|
208
|
+
} else {
|
|
209
|
+
log('No local database to clear.', colors.green);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Run migrations on remote
|
|
213
|
+
log('\nRunning migrations on remote database...', colors.cyan);
|
|
214
|
+
try {
|
|
215
|
+
execCommand(`npx wrangler d1 migrations apply "${dbName}" --remote`);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
log(`Warning: Remote migrations may have failed: ${error.message}`, colors.yellow);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Run migrations on local
|
|
221
|
+
log('\nRunning migrations on local database...', colors.cyan);
|
|
222
|
+
try {
|
|
223
|
+
execCommand(`npx wrangler d1 migrations apply "${dbName}" --local`);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
log(`Warning: Local migrations may have failed: ${error.message}`, colors.yellow);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
log('\n==========================================', colors.green + colors.bold);
|
|
229
|
+
log('Database setup complete!', colors.green + colors.bold);
|
|
230
|
+
log(`Database name: ${dbName}`, colors.green);
|
|
231
|
+
log(`Database ID: ${dbId}`, colors.green);
|
|
232
|
+
log('Both remote and local databases are ready.', colors.green);
|
|
233
|
+
log('==========================================\n', colors.green + colors.bold);
|
|
234
|
+
log('You can now run: npm run dev', colors.cyan);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
main().catch((error) => {
|
|
238
|
+
log(`\nError: ${error.message}`, colors.red);
|
|
239
|
+
process.exit(1);
|
|
240
|
+
});
|