@dhlotter/ai-usage 0.2.6 → 0.2.7
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.mjs +97 -18
- package/package.json +1 -1
package/cli.mjs
CHANGED
|
@@ -2,18 +2,21 @@
|
|
|
2
2
|
import { execFile } from 'node:child_process';
|
|
3
3
|
import { createHash } from 'node:crypto';
|
|
4
4
|
import { readFileSync } from 'node:fs';
|
|
5
|
-
import { lstat, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
6
|
-
import { join, resolve } from 'node:path';
|
|
5
|
+
import { lstat, mkdir, mkdtemp, rename, rm, writeFile } from 'node:fs/promises';
|
|
6
|
+
import { dirname, join, resolve } from 'node:path';
|
|
7
7
|
import { parseArgs, promisify } from 'node:util';
|
|
8
8
|
|
|
9
9
|
const { version } = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
|
10
|
-
|
|
10
|
+
// Installer releases can change without a new Mac build.
|
|
11
|
+
const appVersion = '0.2.6';
|
|
12
|
+
const asset = `https://github.com/dhlotter/ai-usage/releases/download/v${appVersion}/AI.Usage_${appVersion}_universal.dmg`;
|
|
11
13
|
const sha256 = '7d34a4707588183e1b7b17b4cd83e0d9642de148dca0490b7fca5c60ca266ae7';
|
|
12
14
|
const app = 'AI Usage.app';
|
|
15
|
+
const bundleId = 'com.dhlotter.ai-usage';
|
|
13
16
|
const run = promisify(execFile);
|
|
14
17
|
const usage = `Usage: ai-usage install [--app-dir DIRECTORY]
|
|
15
|
-
Install AI Usage ${
|
|
16
|
-
Defaults to /Applications.
|
|
18
|
+
Install or update AI Usage to ${appVersion} on macOS 14 or later (Apple Silicon or Intel).
|
|
19
|
+
Defaults to /Applications. Current and newer versions are left alone.`;
|
|
17
20
|
|
|
18
21
|
export async function downloadVerified(fetchRelease = fetch) {
|
|
19
22
|
const response = await fetchRelease(asset, { signal: AbortSignal.timeout(120_000) });
|
|
@@ -31,13 +34,66 @@ export async function downloadVerified(fetchRelease = fetch) {
|
|
|
31
34
|
return Buffer.concat(chunks, size);
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
async function
|
|
37
|
+
async function pathInfo(path) {
|
|
35
38
|
try {
|
|
36
|
-
await lstat(path);
|
|
37
|
-
return true;
|
|
39
|
+
return await lstat(path);
|
|
38
40
|
} catch (error) {
|
|
39
41
|
if (error.code !== 'ENOENT') throw error;
|
|
40
|
-
return
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function readBundle(path) {
|
|
47
|
+
const info = await pathInfo(path);
|
|
48
|
+
if (!info) return null;
|
|
49
|
+
const unrecognised = `The item at ${path} is not a recognised AI Usage app. It was not changed.`;
|
|
50
|
+
if (!info.isDirectory()) throw new Error(unrecognised);
|
|
51
|
+
|
|
52
|
+
const plist = join(path, 'Contents', 'Info.plist');
|
|
53
|
+
const readKey = async key => (await run('/usr/bin/plutil', ['-extract', key, 'raw', '-expect', 'string', '-o', '-', plist])).stdout.trim();
|
|
54
|
+
const id = await readKey('CFBundleIdentifier').catch(() => '');
|
|
55
|
+
if (id !== bundleId) throw new Error(unrecognised);
|
|
56
|
+
const installedVersion = await readKey('CFBundleShortVersionString').catch(() => '');
|
|
57
|
+
if (!/^\d+\.\d+\.\d+$/.test(installedVersion)) {
|
|
58
|
+
throw new Error(`Cannot determine the AI Usage version at ${path}. It was not changed.`);
|
|
59
|
+
}
|
|
60
|
+
return { info, version: installedVersion };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function requireClosedApp() {
|
|
64
|
+
try {
|
|
65
|
+
await run('/usr/bin/pgrep', ['-x', 'ai-usage']);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (error.code === 1) return;
|
|
68
|
+
throw new Error('Could not check if AI Usage is running. No update was made.', { cause: error });
|
|
69
|
+
}
|
|
70
|
+
throw new Error('Quit AI Usage from the menu bar, then run this command again to update.');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function moveIntoPlace(source, destination) {
|
|
74
|
+
await run('/bin/mv', ['-n', source, dirname(destination)]);
|
|
75
|
+
if (await pathInfo(source)) throw new Error(`The item at ${destination} already exists. It was not overwritten.`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isSameBundle(current, previous) {
|
|
79
|
+
return current && current.version === previous.version &&
|
|
80
|
+
current.info.dev === previous.info.dev && current.info.ino === previous.info.ino;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function replaceApp(staged, destination, backup, installed) {
|
|
84
|
+
await rename(destination, backup);
|
|
85
|
+
try {
|
|
86
|
+
if (!isSameBundle(await readBundle(backup), installed)) {
|
|
87
|
+
throw new Error('The installed app changed during the update. Run this command again.');
|
|
88
|
+
}
|
|
89
|
+
await moveIntoPlace(staged, destination);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
try {
|
|
92
|
+
await moveIntoPlace(backup, destination);
|
|
93
|
+
} catch {
|
|
94
|
+
throw new Error(`Update failed and the old app could not be restored. Its backup is at ${backup}.`, { cause: error });
|
|
95
|
+
}
|
|
96
|
+
throw new Error(`Update failed. The old app was restored. ${error.message}`, { cause: error });
|
|
41
97
|
}
|
|
42
98
|
}
|
|
43
99
|
|
|
@@ -60,8 +116,20 @@ async function main(args) {
|
|
|
60
116
|
if (values['app-dir'] === '') throw new Error('--app-dir must not be empty.');
|
|
61
117
|
const directory = resolve(values['app-dir'] ?? '/Applications');
|
|
62
118
|
const destination = join(directory, app);
|
|
63
|
-
const
|
|
64
|
-
if (
|
|
119
|
+
const installed = await readBundle(destination);
|
|
120
|
+
if (installed) {
|
|
121
|
+
const available = appVersion.split('.').map(BigInt);
|
|
122
|
+
const order = installed.version.split('.').map((part, i) => BigInt(part) - available[i]).find(part => part !== 0n) ?? 0n;
|
|
123
|
+
if (order === 0n) {
|
|
124
|
+
console.log(`AI Usage ${appVersion} is already up to date.`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (order > 0n) {
|
|
128
|
+
console.log(`Installed AI Usage ${installed.version} is newer than ${appVersion}. No changes made.`);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
await requireClosedApp();
|
|
132
|
+
}
|
|
65
133
|
|
|
66
134
|
let temp;
|
|
67
135
|
try {
|
|
@@ -77,26 +145,37 @@ async function main(args) {
|
|
|
77
145
|
const mount = join(temp, 'mount');
|
|
78
146
|
const dmg = join(temp, 'release.dmg');
|
|
79
147
|
const staged = join(temp, app);
|
|
148
|
+
const backup = join(temp, 'previous', app);
|
|
80
149
|
let mounted = false;
|
|
150
|
+
let installedSuccessfully = false;
|
|
81
151
|
try {
|
|
82
|
-
console.log(`Downloading AI Usage ${
|
|
152
|
+
console.log(`Downloading AI Usage ${appVersion} from GitHub...`);
|
|
83
153
|
await writeFile(dmg, await downloadVerified(), { flag: 'wx', mode: 0o600 });
|
|
84
154
|
console.log('SHA-256 verified.');
|
|
85
155
|
await mkdir(mount);
|
|
86
156
|
await run('/usr/bin/hdiutil', ['attach', '-readonly', '-nobrowse', '-quiet', '-mountpoint', mount, dmg]);
|
|
87
157
|
mounted = true;
|
|
88
158
|
await run('/usr/bin/ditto', [join(mount, app), staged]);
|
|
89
|
-
await run('/usr/bin/codesign', ['--verify', '--deep', '--strict', '-R',
|
|
159
|
+
await run('/usr/bin/codesign', ['--verify', '--deep', '--strict', '-R', `=identifier "${bundleId}"`, staged]);
|
|
160
|
+
if ((await readBundle(staged))?.version !== appVersion) throw new Error('The verified app has an unexpected version.');
|
|
90
161
|
|
|
91
162
|
// npm downloads are not tagged as browser downloads. Keep first-launch assessment with macOS.
|
|
92
163
|
await run('/usr/bin/xattr', ['-w', 'com.apple.quarantine', `0081;${Math.floor(Date.now() / 1000).toString(16)};AI Usage Installer;`, staged]);
|
|
93
164
|
await run('/usr/bin/hdiutil', ['detach', '-quiet', mount]);
|
|
94
165
|
mounted = false;
|
|
95
166
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
167
|
+
if (installed) {
|
|
168
|
+
await mkdir(dirname(backup));
|
|
169
|
+
if (!isSameBundle(await readBundle(destination), installed)) {
|
|
170
|
+
throw new Error('The installed app changed during the download. Run this command again.');
|
|
171
|
+
}
|
|
172
|
+
await requireClosedApp();
|
|
173
|
+
await replaceApp(staged, destination, backup, installed);
|
|
174
|
+
} else {
|
|
175
|
+
await moveIntoPlace(staged, destination);
|
|
176
|
+
}
|
|
177
|
+
installedSuccessfully = true;
|
|
178
|
+
console.log(`${installed ? `Updated AI Usage from ${installed.version} to ${appVersion} at` : 'Installed'} ${destination}. Open it from Finder.`);
|
|
100
179
|
console.log('This release is not notarized. macOS may ask you to review it in Privacy & Security.');
|
|
101
180
|
} finally {
|
|
102
181
|
if (mounted) {
|
|
@@ -107,7 +186,7 @@ async function main(args) {
|
|
|
107
186
|
console.error(`Could not eject ${mount}. The temporary files were left in place.`);
|
|
108
187
|
}
|
|
109
188
|
}
|
|
110
|
-
if (!mounted) await rm(temp, { recursive: true, force: true });
|
|
189
|
+
if (!mounted && (installedSuccessfully || !(await pathInfo(backup)))) await rm(temp, { recursive: true, force: true });
|
|
111
190
|
}
|
|
112
191
|
}
|
|
113
192
|
|