@cccarv82/freya 2.6.1 → 2.7.1
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/cli/auto-update.js +134 -0
- package/cli/web.js +8 -0
- package/package.json +1 -1
- package/scripts/lib/DataLayer.js +47 -5
- package/templates/base/scripts/lib/DataLayer.js +47 -5
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* auto-update.js
|
|
3
|
+
* Transparently keeps workspace scripts and dependencies in sync
|
|
4
|
+
* with the installed Freya version.
|
|
5
|
+
*
|
|
6
|
+
* Called automatically on every `freya` CLI invocation.
|
|
7
|
+
* - Compares installed Freya version vs. workspace's last-synced version
|
|
8
|
+
* - If different, runs initWorkspace to copy updated scripts
|
|
9
|
+
* - Runs `npm install` if dependencies changed
|
|
10
|
+
* - Writes a .freya-version marker so it only runs once per upgrade
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
'use strict';
|
|
14
|
+
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
const { execSync } = require('child_process');
|
|
18
|
+
|
|
19
|
+
function getInstalledVersion() {
|
|
20
|
+
try {
|
|
21
|
+
const pkg = require('../package.json');
|
|
22
|
+
return pkg.version || '0.0.0';
|
|
23
|
+
} catch {
|
|
24
|
+
return '0.0.0';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function getWorkspaceVersion(workspaceDir) {
|
|
29
|
+
const markerPath = path.join(workspaceDir, '.freya-version');
|
|
30
|
+
try {
|
|
31
|
+
return fs.readFileSync(markerPath, 'utf8').trim();
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function setWorkspaceVersion(workspaceDir, version) {
|
|
38
|
+
const markerPath = path.join(workspaceDir, '.freya-version');
|
|
39
|
+
try {
|
|
40
|
+
fs.writeFileSync(markerPath, version + '\n', 'utf8');
|
|
41
|
+
} catch {
|
|
42
|
+
// non-fatal
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function needsNpmInstall(workspaceDir) {
|
|
47
|
+
// Check if sql.js is installed in the workspace's node_modules
|
|
48
|
+
const sqlJsPath = path.join(workspaceDir, 'node_modules', 'sql.js');
|
|
49
|
+
try {
|
|
50
|
+
fs.accessSync(sqlJsPath);
|
|
51
|
+
return false;
|
|
52
|
+
} catch {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function runNpmInstall(workspaceDir) {
|
|
58
|
+
try {
|
|
59
|
+
console.log('[FREYA] Installing workspace dependencies...');
|
|
60
|
+
const opts = {
|
|
61
|
+
cwd: workspaceDir,
|
|
62
|
+
stdio: 'pipe',
|
|
63
|
+
timeout: 120000, // 2 min max
|
|
64
|
+
env: { ...process.env }
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
if (process.platform === 'win32') {
|
|
68
|
+
execSync('npm install --no-audit --no-fund', opts);
|
|
69
|
+
} else {
|
|
70
|
+
execSync('npm install --no-audit --no-fund', opts);
|
|
71
|
+
}
|
|
72
|
+
console.log('[FREYA] Dependencies installed successfully.');
|
|
73
|
+
return true;
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.error('[FREYA] Warning: npm install failed:', err.message || String(err));
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Auto-update the workspace if the installed Freya version is newer.
|
|
82
|
+
* This is designed to be silent and non-disruptive.
|
|
83
|
+
*
|
|
84
|
+
* @param {string} workspaceDir - Resolved absolute path to the workspace
|
|
85
|
+
* @returns {Promise<{updated: boolean, version: string}>}
|
|
86
|
+
*/
|
|
87
|
+
async function autoUpdate(workspaceDir) {
|
|
88
|
+
const installedVersion = getInstalledVersion();
|
|
89
|
+
const workspaceVersion = getWorkspaceVersion(workspaceDir);
|
|
90
|
+
|
|
91
|
+
// Already in sync
|
|
92
|
+
if (workspaceVersion === installedVersion) {
|
|
93
|
+
return { updated: false, version: installedVersion };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Workspace doesn't look like a Freya workspace (no scripts/ dir)
|
|
97
|
+
const scriptsDir = path.join(workspaceDir, 'scripts');
|
|
98
|
+
try {
|
|
99
|
+
fs.accessSync(scriptsDir);
|
|
100
|
+
} catch {
|
|
101
|
+
// Not a workspace, skip
|
|
102
|
+
return { updated: false, version: installedVersion };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
console.log(`[FREYA] Updating workspace from ${workspaceVersion || 'unknown'} → ${installedVersion}...`);
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
// Use initWorkspace to sync templates (scripts, etc.)
|
|
109
|
+
const { initWorkspace } = require('./init');
|
|
110
|
+
await initWorkspace({
|
|
111
|
+
targetDir: workspaceDir,
|
|
112
|
+
force: false, // Don't force non-script files
|
|
113
|
+
forceData: false,
|
|
114
|
+
forceLogs: false
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Check if npm install is needed
|
|
118
|
+
if (needsNpmInstall(workspaceDir)) {
|
|
119
|
+
runNpmInstall(workspaceDir);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Mark workspace as up-to-date
|
|
123
|
+
setWorkspaceVersion(workspaceDir, installedVersion);
|
|
124
|
+
console.log(`[FREYA] Workspace updated to v${installedVersion} ✓`);
|
|
125
|
+
|
|
126
|
+
return { updated: true, version: installedVersion };
|
|
127
|
+
} catch (err) {
|
|
128
|
+
console.error('[FREYA] Warning: auto-update failed:', err.message || String(err));
|
|
129
|
+
// Non-fatal — don't block the user
|
|
130
|
+
return { updated: false, version: installedVersion, error: err.message };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = { autoUpdate, getInstalledVersion };
|
package/cli/web.js
CHANGED
|
@@ -2238,6 +2238,14 @@ function seedDevWorkspace(workspaceDir) {
|
|
|
2238
2238
|
|
|
2239
2239
|
async function cmdWeb({ port, dir, open, dev }) {
|
|
2240
2240
|
await ready;
|
|
2241
|
+
|
|
2242
|
+
// Auto-update workspace scripts/deps if Freya version changed
|
|
2243
|
+
try {
|
|
2244
|
+
const { autoUpdate } = require('./auto-update');
|
|
2245
|
+
const wsDir = normalizeWorkspaceDir(dir || './freya');
|
|
2246
|
+
await autoUpdate(wsDir);
|
|
2247
|
+
} catch { /* non-fatal */ }
|
|
2248
|
+
|
|
2241
2249
|
const host = '127.0.0.1';
|
|
2242
2250
|
|
|
2243
2251
|
const server = http.createServer(async (req, res) => {
|
package/package.json
CHANGED
package/scripts/lib/DataLayer.js
CHANGED
|
@@ -72,7 +72,10 @@ class PreparedStatement {
|
|
|
72
72
|
const db = this._wrapper._db;
|
|
73
73
|
db.run(this._sql, this._flattenParams(params));
|
|
74
74
|
const changes = db.getRowsModified();
|
|
75
|
-
|
|
75
|
+
// Only save to disk if NOT inside a transaction (save happens at COMMIT)
|
|
76
|
+
if (!this._wrapper._inTransaction) {
|
|
77
|
+
this._wrapper._save();
|
|
78
|
+
}
|
|
76
79
|
return { changes };
|
|
77
80
|
}
|
|
78
81
|
|
|
@@ -92,7 +95,7 @@ class SqlJsDatabase {
|
|
|
92
95
|
constructor(sqlJsDb, filePath) {
|
|
93
96
|
this._db = sqlJsDb;
|
|
94
97
|
this._filePath = filePath;
|
|
95
|
-
this.
|
|
98
|
+
this._inTransaction = false;
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
/**
|
|
@@ -126,7 +129,9 @@ class SqlJsDatabase {
|
|
|
126
129
|
*/
|
|
127
130
|
exec(sql) {
|
|
128
131
|
this._db.run(sql);
|
|
129
|
-
this.
|
|
132
|
+
if (!this._inTransaction) {
|
|
133
|
+
this._save();
|
|
134
|
+
}
|
|
130
135
|
}
|
|
131
136
|
|
|
132
137
|
/**
|
|
@@ -144,13 +149,16 @@ class SqlJsDatabase {
|
|
|
144
149
|
transaction(fn) {
|
|
145
150
|
const self = this;
|
|
146
151
|
return function (...args) {
|
|
152
|
+
self._inTransaction = true;
|
|
147
153
|
self._db.run('BEGIN TRANSACTION');
|
|
148
154
|
try {
|
|
149
155
|
const result = fn(...args);
|
|
150
156
|
self._db.run('COMMIT');
|
|
157
|
+
self._inTransaction = false;
|
|
151
158
|
self._save();
|
|
152
159
|
return result;
|
|
153
160
|
} catch (e) {
|
|
161
|
+
self._inTransaction = false;
|
|
154
162
|
try { self._db.run('ROLLBACK'); } catch { /* already rolled back */ }
|
|
155
163
|
throw e;
|
|
156
164
|
}
|
|
@@ -192,6 +200,40 @@ class DataLayer {
|
|
|
192
200
|
const initSqlJs = require('sql.js');
|
|
193
201
|
const SQL = await initSqlJs();
|
|
194
202
|
|
|
203
|
+
// --- WAL consolidation (migration from better-sqlite3) ---
|
|
204
|
+
// better-sqlite3 used WAL mode, which may leave a -wal file with uncommitted data.
|
|
205
|
+
// sql.js cannot read WAL files directly, so we need to checkpoint first.
|
|
206
|
+
const walPath = this._dbPath + '-wal';
|
|
207
|
+
const shmPath = this._dbPath + '-shm';
|
|
208
|
+
if (fs.existsSync(this._dbPath) && fs.existsSync(walPath)) {
|
|
209
|
+
try {
|
|
210
|
+
console.log('[DataLayer] Consolidating WAL file from previous better-sqlite3 usage...');
|
|
211
|
+
// Open the database including the WAL data
|
|
212
|
+
const fileBuffer = fs.readFileSync(this._dbPath);
|
|
213
|
+
const tempDb = new SQL.Database(fileBuffer);
|
|
214
|
+
|
|
215
|
+
// Try to checkpoint the WAL (merge pending writes into main file)
|
|
216
|
+
try { tempDb.run('PRAGMA wal_checkpoint(TRUNCATE)'); } catch { /* may not work in WASM */ }
|
|
217
|
+
|
|
218
|
+
// Switch to DELETE journal mode (fully compatible with sql.js)
|
|
219
|
+
try { tempDb.run('PRAGMA journal_mode = DELETE'); } catch { /* ignore */ }
|
|
220
|
+
|
|
221
|
+
// Export the consolidated database and save it
|
|
222
|
+
const consolidated = tempDb.export();
|
|
223
|
+
fs.writeFileSync(this._dbPath, Buffer.from(consolidated));
|
|
224
|
+
tempDb.close();
|
|
225
|
+
|
|
226
|
+
// Clean up WAL/SHM files
|
|
227
|
+
try { fs.unlinkSync(walPath); } catch { /* ignore */ }
|
|
228
|
+
try { fs.unlinkSync(shmPath); } catch { /* ignore */ }
|
|
229
|
+
|
|
230
|
+
console.log('[DataLayer] WAL consolidation complete.');
|
|
231
|
+
} catch (err) {
|
|
232
|
+
console.error('[DataLayer] Warning: WAL consolidation failed:', err.message);
|
|
233
|
+
// Continue anyway — the main .sqlite file may still be usable
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
195
237
|
// Load existing database from disk, or create new one
|
|
196
238
|
let sqlJsDb;
|
|
197
239
|
if (fs.existsSync(this._dbPath)) {
|
|
@@ -206,8 +248,8 @@ class DataLayer {
|
|
|
206
248
|
}
|
|
207
249
|
|
|
208
250
|
_initSchema() {
|
|
209
|
-
//
|
|
210
|
-
this.db.pragma('journal_mode =
|
|
251
|
+
// Use DELETE journal mode (compatible with sql.js WASM)
|
|
252
|
+
this.db.pragma('journal_mode = DELETE');
|
|
211
253
|
|
|
212
254
|
this.db.exec(`
|
|
213
255
|
CREATE TABLE IF NOT EXISTS projects (
|
|
@@ -72,7 +72,10 @@ class PreparedStatement {
|
|
|
72
72
|
const db = this._wrapper._db;
|
|
73
73
|
db.run(this._sql, this._flattenParams(params));
|
|
74
74
|
const changes = db.getRowsModified();
|
|
75
|
-
|
|
75
|
+
// Only save to disk if NOT inside a transaction (save happens at COMMIT)
|
|
76
|
+
if (!this._wrapper._inTransaction) {
|
|
77
|
+
this._wrapper._save();
|
|
78
|
+
}
|
|
76
79
|
return { changes };
|
|
77
80
|
}
|
|
78
81
|
|
|
@@ -92,7 +95,7 @@ class SqlJsDatabase {
|
|
|
92
95
|
constructor(sqlJsDb, filePath) {
|
|
93
96
|
this._db = sqlJsDb;
|
|
94
97
|
this._filePath = filePath;
|
|
95
|
-
this.
|
|
98
|
+
this._inTransaction = false;
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
/**
|
|
@@ -126,7 +129,9 @@ class SqlJsDatabase {
|
|
|
126
129
|
*/
|
|
127
130
|
exec(sql) {
|
|
128
131
|
this._db.run(sql);
|
|
129
|
-
this.
|
|
132
|
+
if (!this._inTransaction) {
|
|
133
|
+
this._save();
|
|
134
|
+
}
|
|
130
135
|
}
|
|
131
136
|
|
|
132
137
|
/**
|
|
@@ -144,13 +149,16 @@ class SqlJsDatabase {
|
|
|
144
149
|
transaction(fn) {
|
|
145
150
|
const self = this;
|
|
146
151
|
return function (...args) {
|
|
152
|
+
self._inTransaction = true;
|
|
147
153
|
self._db.run('BEGIN TRANSACTION');
|
|
148
154
|
try {
|
|
149
155
|
const result = fn(...args);
|
|
150
156
|
self._db.run('COMMIT');
|
|
157
|
+
self._inTransaction = false;
|
|
151
158
|
self._save();
|
|
152
159
|
return result;
|
|
153
160
|
} catch (e) {
|
|
161
|
+
self._inTransaction = false;
|
|
154
162
|
try { self._db.run('ROLLBACK'); } catch { /* already rolled back */ }
|
|
155
163
|
throw e;
|
|
156
164
|
}
|
|
@@ -192,6 +200,40 @@ class DataLayer {
|
|
|
192
200
|
const initSqlJs = require('sql.js');
|
|
193
201
|
const SQL = await initSqlJs();
|
|
194
202
|
|
|
203
|
+
// --- WAL consolidation (migration from better-sqlite3) ---
|
|
204
|
+
// better-sqlite3 used WAL mode, which may leave a -wal file with uncommitted data.
|
|
205
|
+
// sql.js cannot read WAL files directly, so we need to checkpoint first.
|
|
206
|
+
const walPath = this._dbPath + '-wal';
|
|
207
|
+
const shmPath = this._dbPath + '-shm';
|
|
208
|
+
if (fs.existsSync(this._dbPath) && fs.existsSync(walPath)) {
|
|
209
|
+
try {
|
|
210
|
+
console.log('[DataLayer] Consolidating WAL file from previous better-sqlite3 usage...');
|
|
211
|
+
// Open the database including the WAL data
|
|
212
|
+
const fileBuffer = fs.readFileSync(this._dbPath);
|
|
213
|
+
const tempDb = new SQL.Database(fileBuffer);
|
|
214
|
+
|
|
215
|
+
// Try to checkpoint the WAL (merge pending writes into main file)
|
|
216
|
+
try { tempDb.run('PRAGMA wal_checkpoint(TRUNCATE)'); } catch { /* may not work in WASM */ }
|
|
217
|
+
|
|
218
|
+
// Switch to DELETE journal mode (fully compatible with sql.js)
|
|
219
|
+
try { tempDb.run('PRAGMA journal_mode = DELETE'); } catch { /* ignore */ }
|
|
220
|
+
|
|
221
|
+
// Export the consolidated database and save it
|
|
222
|
+
const consolidated = tempDb.export();
|
|
223
|
+
fs.writeFileSync(this._dbPath, Buffer.from(consolidated));
|
|
224
|
+
tempDb.close();
|
|
225
|
+
|
|
226
|
+
// Clean up WAL/SHM files
|
|
227
|
+
try { fs.unlinkSync(walPath); } catch { /* ignore */ }
|
|
228
|
+
try { fs.unlinkSync(shmPath); } catch { /* ignore */ }
|
|
229
|
+
|
|
230
|
+
console.log('[DataLayer] WAL consolidation complete.');
|
|
231
|
+
} catch (err) {
|
|
232
|
+
console.error('[DataLayer] Warning: WAL consolidation failed:', err.message);
|
|
233
|
+
// Continue anyway — the main .sqlite file may still be usable
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
195
237
|
// Load existing database from disk, or create new one
|
|
196
238
|
let sqlJsDb;
|
|
197
239
|
if (fs.existsSync(this._dbPath)) {
|
|
@@ -206,8 +248,8 @@ class DataLayer {
|
|
|
206
248
|
}
|
|
207
249
|
|
|
208
250
|
_initSchema() {
|
|
209
|
-
//
|
|
210
|
-
this.db.pragma('journal_mode =
|
|
251
|
+
// Use DELETE journal mode (compatible with sql.js WASM)
|
|
252
|
+
this.db.pragma('journal_mode = DELETE');
|
|
211
253
|
|
|
212
254
|
this.db.exec(`
|
|
213
255
|
CREATE TABLE IF NOT EXISTS projects (
|