olly-molly 0.1.8 ā 0.1.10
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/cli.js +28 -45
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -6,25 +6,28 @@ const fs = require('fs');
|
|
|
6
6
|
const os = require('os');
|
|
7
7
|
const https = require('https');
|
|
8
8
|
|
|
9
|
+
const PACKAGE_NAME = 'olly-molly';
|
|
9
10
|
const REPO = 'ruucm/olly-molly';
|
|
10
11
|
const APP_DIR = path.join(os.homedir(), '.olly-molly');
|
|
11
12
|
const DB_DIR = path.join(APP_DIR, 'db');
|
|
12
13
|
const TARBALL_URL = `https://github.com/${REPO}/archive/refs/heads/main.tar.gz`;
|
|
13
|
-
const VERSION_URL = `https://raw.githubusercontent.com/${REPO}/main/package.json`;
|
|
14
14
|
|
|
15
15
|
console.log('\nš Olly Molly\n');
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
17
|
+
// Get latest version from npm registry
|
|
18
|
+
function getNpmVersion() {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
https.get(`https://registry.npmjs.org/${PACKAGE_NAME}/latest`, (res) => {
|
|
21
|
+
let data = '';
|
|
22
|
+
res.on('data', c => data += c);
|
|
23
|
+
res.on('end', () => {
|
|
24
|
+
try {
|
|
25
|
+
resolve(JSON.parse(data).version);
|
|
26
|
+
} catch {
|
|
27
|
+
resolve(null);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}).on('error', () => resolve(null));
|
|
28
31
|
});
|
|
29
32
|
}
|
|
30
33
|
|
|
@@ -52,12 +55,10 @@ function download(url, destDir) {
|
|
|
52
55
|
|
|
53
56
|
function getLocalVersion() {
|
|
54
57
|
try {
|
|
55
|
-
|
|
56
|
-
return pkg.version;
|
|
58
|
+
return JSON.parse(fs.readFileSync(path.join(APP_DIR, 'package.json'), 'utf8')).version;
|
|
57
59
|
} catch { return null; }
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
// Backup and restore user's database
|
|
61
62
|
function backupDB() {
|
|
62
63
|
const backupDir = path.join(os.tmpdir(), 'olly-molly-db-backup');
|
|
63
64
|
if (fs.existsSync(DB_DIR)) {
|
|
@@ -70,10 +71,8 @@ function backupDB() {
|
|
|
70
71
|
function restoreDB(backupDir) {
|
|
71
72
|
if (backupDir && fs.existsSync(backupDir)) {
|
|
72
73
|
fs.mkdirSync(DB_DIR, { recursive: true });
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
for (const file of files) {
|
|
76
|
-
if (file.endsWith('.sqlite') || file.endsWith('.sqlite-shm') || file.endsWith('.sqlite-wal')) {
|
|
74
|
+
for (const file of fs.readdirSync(backupDir)) {
|
|
75
|
+
if (file.includes('.sqlite')) {
|
|
77
76
|
fs.copyFileSync(path.join(backupDir, file), path.join(DB_DIR, file));
|
|
78
77
|
}
|
|
79
78
|
}
|
|
@@ -85,54 +84,38 @@ async function main() {
|
|
|
85
84
|
let needsInstall = false;
|
|
86
85
|
let needsBuild = false;
|
|
87
86
|
|
|
88
|
-
// Check for updates
|
|
89
87
|
const localVersion = getLocalVersion();
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
remoteVersion = (await fetchJSON(VERSION_URL)).version;
|
|
94
|
-
} catch {
|
|
95
|
-
// Offline - continue with local
|
|
96
|
-
}
|
|
88
|
+
const npmVersion = await getNpmVersion();
|
|
97
89
|
|
|
98
|
-
// Update if version
|
|
99
|
-
if (localVersion &&
|
|
100
|
-
console.log(`š Updating ${localVersion} ā ${
|
|
101
|
-
|
|
102
|
-
// Backup DB before update
|
|
90
|
+
// Update if npm version is newer
|
|
91
|
+
if (localVersion && npmVersion && localVersion !== npmVersion) {
|
|
92
|
+
console.log(`š Updating ${localVersion} ā ${npmVersion}\n`);
|
|
103
93
|
const dbBackup = backupDB();
|
|
104
|
-
|
|
105
|
-
// Remove app (but DB is backed up)
|
|
106
94
|
fs.rmSync(APP_DIR, { recursive: true, force: true });
|
|
107
|
-
|
|
108
|
-
// Download new version
|
|
109
95
|
console.log('š„ Downloading...');
|
|
110
96
|
await download(TARBALL_URL, APP_DIR);
|
|
111
|
-
console.log('ā
|
|
112
|
-
|
|
113
|
-
// Restore DB
|
|
97
|
+
console.log('ā
Downloaded\n');
|
|
114
98
|
restoreDB(dbBackup);
|
|
115
|
-
|
|
116
99
|
needsInstall = true;
|
|
117
100
|
needsBuild = true;
|
|
118
101
|
}
|
|
119
102
|
|
|
120
|
-
// First time
|
|
103
|
+
// First time
|
|
121
104
|
if (!fs.existsSync(APP_DIR)) {
|
|
122
105
|
console.log('š„ Downloading...');
|
|
123
106
|
await download(TARBALL_URL, APP_DIR);
|
|
124
|
-
console.log('ā
|
|
107
|
+
console.log('ā
Downloaded\n');
|
|
125
108
|
needsInstall = true;
|
|
126
109
|
needsBuild = true;
|
|
127
110
|
}
|
|
128
111
|
|
|
129
|
-
// Install
|
|
112
|
+
// Install
|
|
130
113
|
if (needsInstall || !fs.existsSync(path.join(APP_DIR, 'node_modules'))) {
|
|
131
|
-
console.log('š¦ Installing
|
|
114
|
+
console.log('š¦ Installing...\n');
|
|
132
115
|
execSync('npm install --omit=dev', { cwd: APP_DIR, stdio: 'inherit' });
|
|
133
116
|
}
|
|
134
117
|
|
|
135
|
-
// Build
|
|
118
|
+
// Build
|
|
136
119
|
if (needsBuild || !fs.existsSync(path.join(APP_DIR, '.next'))) {
|
|
137
120
|
console.log('\nšØ Building...\n');
|
|
138
121
|
execSync('npm run build', { cwd: APP_DIR, stdio: 'inherit' });
|