@cccarv82/freya 2.6.1 → 2.7.0
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
|
@@ -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