@dhlotter/ai-usage 0.0.0-stage → 0.2.6
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/LICENSE +21 -0
- package/cli.mjs +119 -0
- package/package.json +16 -4
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hermann Lotter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/cli.mjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { readFileSync } from 'node:fs';
|
|
5
|
+
import { lstat, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
6
|
+
import { join, resolve } from 'node:path';
|
|
7
|
+
import { parseArgs, promisify } from 'node:util';
|
|
8
|
+
|
|
9
|
+
const { version } = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
|
10
|
+
const asset = `https://github.com/dhlotter/ai-usage/releases/download/v${version}/AI.Usage_${version}_universal.dmg`;
|
|
11
|
+
const sha256 = '7d34a4707588183e1b7b17b4cd83e0d9642de148dca0490b7fca5c60ca266ae7';
|
|
12
|
+
const app = 'AI Usage.app';
|
|
13
|
+
const run = promisify(execFile);
|
|
14
|
+
const usage = `Usage: ai-usage install [--app-dir DIRECTORY]
|
|
15
|
+
Install AI Usage ${version} on macOS 14 or later (Apple Silicon or Intel).
|
|
16
|
+
Defaults to /Applications. An existing copy is never replaced.`;
|
|
17
|
+
|
|
18
|
+
export async function downloadVerified(fetchRelease = fetch) {
|
|
19
|
+
const response = await fetchRelease(asset, { signal: AbortSignal.timeout(120_000) });
|
|
20
|
+
if (!response.ok || !response.body) throw new Error(`Release download failed (HTTP ${response.status}).`);
|
|
21
|
+
const hash = createHash('sha256');
|
|
22
|
+
const chunks = [];
|
|
23
|
+
let size = 0;
|
|
24
|
+
for await (const chunk of response.body) {
|
|
25
|
+
size += chunk.byteLength;
|
|
26
|
+
if (size > 32 * 1024 * 1024) throw new Error('Release download exceeded 32 MiB.');
|
|
27
|
+
hash.update(chunk);
|
|
28
|
+
chunks.push(chunk);
|
|
29
|
+
}
|
|
30
|
+
if (hash.digest('hex') !== sha256) throw new Error('SHA-256 mismatch. The disk image was not installed.');
|
|
31
|
+
return Buffer.concat(chunks, size);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function exists(path) {
|
|
35
|
+
try {
|
|
36
|
+
await lstat(path);
|
|
37
|
+
return true;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error.code !== 'ENOENT') throw error;
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function main(args) {
|
|
45
|
+
const { values, positionals } = parseArgs({
|
|
46
|
+
args,
|
|
47
|
+
options: { 'app-dir': { type: 'string' }, help: { type: 'boolean', short: 'h' }, version: { type: 'boolean' } },
|
|
48
|
+
allowPositionals: true,
|
|
49
|
+
});
|
|
50
|
+
if (values.version || values.help || args.length === 0) {
|
|
51
|
+
console.log(values.version ? version : usage);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (positionals.length !== 1 || positionals[0] !== 'install') throw new Error(usage);
|
|
55
|
+
if (process.platform !== 'darwin') throw new Error('AI Usage requires macOS 14 or later.');
|
|
56
|
+
if (process.getuid() === 0) throw new Error('Run without sudo. Use --app-dir "$HOME/Applications" if needed.');
|
|
57
|
+
const { stdout: osVersion } = await run('/usr/bin/sw_vers', ['-productVersion']);
|
|
58
|
+
if (Number.parseInt(osVersion, 10) < 14) throw new Error('AI Usage requires macOS 14 or later.');
|
|
59
|
+
|
|
60
|
+
if (values['app-dir'] === '') throw new Error('--app-dir must not be empty.');
|
|
61
|
+
const directory = resolve(values['app-dir'] ?? '/Applications');
|
|
62
|
+
const destination = join(directory, app);
|
|
63
|
+
const existing = `AI Usage already exists at ${destination}. Quit it and move it to the Trash before reinstalling.`;
|
|
64
|
+
if (await exists(destination)) throw new Error(existing);
|
|
65
|
+
|
|
66
|
+
let temp;
|
|
67
|
+
try {
|
|
68
|
+
await mkdir(directory, { recursive: true });
|
|
69
|
+
temp = await mkdtemp(join(directory, '.ai-usage-'));
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (error.code === 'EACCES' || error.code === 'EPERM') {
|
|
72
|
+
throw new Error('Cannot write to the chosen folder. Try --app-dir "$HOME/Applications" without sudo.');
|
|
73
|
+
}
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const mount = join(temp, 'mount');
|
|
78
|
+
const dmg = join(temp, 'release.dmg');
|
|
79
|
+
const staged = join(temp, app);
|
|
80
|
+
let mounted = false;
|
|
81
|
+
try {
|
|
82
|
+
console.log(`Downloading AI Usage ${version} from GitHub...`);
|
|
83
|
+
await writeFile(dmg, await downloadVerified(), { flag: 'wx', mode: 0o600 });
|
|
84
|
+
console.log('SHA-256 verified.');
|
|
85
|
+
await mkdir(mount);
|
|
86
|
+
await run('/usr/bin/hdiutil', ['attach', '-readonly', '-nobrowse', '-quiet', '-mountpoint', mount, dmg]);
|
|
87
|
+
mounted = true;
|
|
88
|
+
await run('/usr/bin/ditto', [join(mount, app), staged]);
|
|
89
|
+
await run('/usr/bin/codesign', ['--verify', '--deep', '--strict', '-R', '=identifier "com.dhlotter.ai-usage"', staged]);
|
|
90
|
+
|
|
91
|
+
// npm downloads are not tagged as browser downloads. Keep first-launch assessment with macOS.
|
|
92
|
+
await run('/usr/bin/xattr', ['-w', 'com.apple.quarantine', `0081;${Math.floor(Date.now() / 1000).toString(16)};AI Usage Installer;`, staged]);
|
|
93
|
+
await run('/usr/bin/hdiutil', ['detach', '-quiet', mount]);
|
|
94
|
+
mounted = false;
|
|
95
|
+
|
|
96
|
+
// Same filesystem, with no overwrite if another installer got there first.
|
|
97
|
+
await run('/bin/mv', ['-n', staged, directory]);
|
|
98
|
+
if (await exists(staged)) throw new Error(existing);
|
|
99
|
+
console.log(`Installed ${destination}. Open it from Finder.`);
|
|
100
|
+
console.log('This release is not notarized. macOS may ask you to review it in Privacy & Security.');
|
|
101
|
+
} finally {
|
|
102
|
+
if (mounted) {
|
|
103
|
+
try {
|
|
104
|
+
await run('/usr/bin/hdiutil', ['detach', '-quiet', mount]);
|
|
105
|
+
mounted = false;
|
|
106
|
+
} catch {
|
|
107
|
+
console.error(`Could not eject ${mount}. The temporary files were left in place.`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (!mounted) await rm(temp, { recursive: true, force: true });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (import.meta.main) {
|
|
115
|
+
main(process.argv.slice(2)).catch(error => {
|
|
116
|
+
console.error(`Error: ${error.message}`);
|
|
117
|
+
process.exitCode = 1;
|
|
118
|
+
});
|
|
119
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dhlotter/ai-usage",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.6",
|
|
4
|
+
"description": "Install the AI Usage menu bar app for macOS",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/dhlotter/ai-usage.git",
|
|
9
|
+
"directory": "scripts/npm-installer"
|
|
10
|
+
},
|
|
11
|
+
"bin": { "ai-usage": "./cli.mjs" },
|
|
12
|
+
"engines": { "node": ">=22.18.0" },
|
|
13
|
+
"os": ["darwin"],
|
|
14
|
+
"cpu": ["arm64", "x64"],
|
|
15
|
+
"files": ["cli.mjs"],
|
|
16
|
+
"publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" },
|
|
17
|
+
"scripts": { "test": "node --test" }
|
|
18
|
+
}
|
package/README.md
DELETED