@yousali/codetok 0.2.1-canary.20260219012136
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/README.md +26 -0
- package/bin/codetok.js +30 -0
- package/package.json +53 -0
- package/scripts/install.mjs +240 -0
- package/vendor/.gitkeep +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yousa
|
|
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/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# codetok (npm)
|
|
2
|
+
|
|
3
|
+
Install the `codetok` CLI through npm.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g codetok
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## How it works
|
|
12
|
+
|
|
13
|
+
- During `postinstall`, this package downloads the matching binary from GitHub Releases.
|
|
14
|
+
- The `codetok` command then runs that native binary.
|
|
15
|
+
|
|
16
|
+
Supported targets:
|
|
17
|
+
|
|
18
|
+
- macOS: `x64`, `arm64`
|
|
19
|
+
- Linux: `x64`, `arm64`
|
|
20
|
+
- Windows: `x64`, `arm64`
|
|
21
|
+
|
|
22
|
+
## Troubleshooting
|
|
23
|
+
|
|
24
|
+
If install fails due to network restrictions, you can download binaries directly from:
|
|
25
|
+
|
|
26
|
+
https://github.com/miss-you/codetok/releases
|
package/bin/codetok.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { spawn } = require('node:child_process');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
|
|
7
|
+
const binName = process.platform === 'win32' ? 'codetok.exe' : 'codetok';
|
|
8
|
+
const binPath = path.join(__dirname, '..', 'vendor', binName);
|
|
9
|
+
|
|
10
|
+
if (!fs.existsSync(binPath)) {
|
|
11
|
+
console.error('[codetok] native binary is missing. Reinstall with `npm install -g codetok`.');
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const child = spawn(binPath, process.argv.slice(2), {
|
|
16
|
+
stdio: 'inherit',
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
child.on('error', (err) => {
|
|
20
|
+
console.error(`[codetok] failed to start binary: ${err.message}`);
|
|
21
|
+
process.exit(1);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
child.on('exit', (code, signal) => {
|
|
25
|
+
if (signal) {
|
|
26
|
+
process.kill(process.pid, signal);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
process.exit(code ?? 1);
|
|
30
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yousali/codetok",
|
|
3
|
+
"version": "0.2.1-canary.20260219012136",
|
|
4
|
+
"description": "Track and aggregate token usage across AI coding CLI tools",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/miss-you/codetok#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/miss-you/codetok.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/miss-you/codetok/issues"
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"codetok": "bin/codetok.js"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"postinstall": "node scripts/install.mjs"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"extract-zip": "^2.0.1",
|
|
22
|
+
"tar": "^7.4.3"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"os": [
|
|
28
|
+
"darwin",
|
|
29
|
+
"linux",
|
|
30
|
+
"win32"
|
|
31
|
+
],
|
|
32
|
+
"cpu": [
|
|
33
|
+
"x64",
|
|
34
|
+
"arm64"
|
|
35
|
+
],
|
|
36
|
+
"keywords": [
|
|
37
|
+
"cli",
|
|
38
|
+
"go",
|
|
39
|
+
"token",
|
|
40
|
+
"usage",
|
|
41
|
+
"codetok"
|
|
42
|
+
],
|
|
43
|
+
"files": [
|
|
44
|
+
"bin/",
|
|
45
|
+
"scripts/",
|
|
46
|
+
"vendor/",
|
|
47
|
+
"README.md",
|
|
48
|
+
"LICENSE"
|
|
49
|
+
],
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { promises as fsp } from 'node:fs';
|
|
4
|
+
import https from 'node:https';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
|
|
9
|
+
import extract from 'extract-zip';
|
|
10
|
+
import tar from 'tar';
|
|
11
|
+
|
|
12
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
13
|
+
const __dirname = path.dirname(__filename);
|
|
14
|
+
const pkgRoot = path.resolve(__dirname, '..');
|
|
15
|
+
const pkgJSONPath = path.join(pkgRoot, 'package.json');
|
|
16
|
+
const vendorDir = path.join(pkgRoot, 'vendor');
|
|
17
|
+
|
|
18
|
+
const project = 'codetok';
|
|
19
|
+
const owner = 'miss-you';
|
|
20
|
+
const repo = 'codetok';
|
|
21
|
+
const maxRedirects = 5;
|
|
22
|
+
const requestTimeoutMs = 60_000;
|
|
23
|
+
|
|
24
|
+
const isWindows = process.platform === 'win32';
|
|
25
|
+
const binaryName = isWindows ? 'codetok.exe' : 'codetok';
|
|
26
|
+
|
|
27
|
+
if (process.env.CODETOK_SKIP_INSTALL === '1') {
|
|
28
|
+
console.log('[codetok] CODETOK_SKIP_INSTALL=1, skip binary install');
|
|
29
|
+
process.exit(0);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
main().catch((err) => {
|
|
33
|
+
console.error(`[codetok] install failed: ${err.message}`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
async function main() {
|
|
38
|
+
const pkg = JSON.parse(await fsp.readFile(pkgJSONPath, 'utf8'));
|
|
39
|
+
const version = pkg.version;
|
|
40
|
+
|
|
41
|
+
if (!version || version === '0.0.0-dev') {
|
|
42
|
+
console.log('[codetok] development package version detected, skip binary download');
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const target = resolveTarget(process.platform, process.arch);
|
|
47
|
+
const ext = target.goos === 'windows' ? 'zip' : 'tar.gz';
|
|
48
|
+
const archiveName = `${project}_${version}_${target.goos}_${target.goarch}.${ext}`;
|
|
49
|
+
const releaseTag = `v${version}`;
|
|
50
|
+
const baseURL = `https://github.com/${owner}/${repo}/releases/download/${releaseTag}`;
|
|
51
|
+
|
|
52
|
+
const tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), `${project}-npm-`));
|
|
53
|
+
const archivePath = path.join(tmpDir, archiveName);
|
|
54
|
+
const checksumsPath = path.join(tmpDir, 'checksums.txt');
|
|
55
|
+
const extractDir = path.join(tmpDir, 'extract');
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
await fsp.mkdir(extractDir, { recursive: true });
|
|
59
|
+
|
|
60
|
+
await downloadToFile(`${baseURL}/${archiveName}`, archivePath);
|
|
61
|
+
await downloadToFile(`${baseURL}/checksums.txt`, checksumsPath);
|
|
62
|
+
await verifySHA256(checksumsPath, archivePath, archiveName);
|
|
63
|
+
|
|
64
|
+
if (ext === 'tar.gz') {
|
|
65
|
+
await tar.x({
|
|
66
|
+
cwd: extractDir,
|
|
67
|
+
file: archivePath,
|
|
68
|
+
strict: true,
|
|
69
|
+
});
|
|
70
|
+
} else {
|
|
71
|
+
await extract(archivePath, { dir: extractDir });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const extractedBinary = await findFileByName(extractDir, binaryName);
|
|
75
|
+
if (!extractedBinary) {
|
|
76
|
+
throw new Error(`binary ${binaryName} not found in ${archiveName}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
await fsp.mkdir(vendorDir, { recursive: true });
|
|
80
|
+
const destPath = path.join(vendorDir, binaryName);
|
|
81
|
+
await fsp.copyFile(extractedBinary, destPath);
|
|
82
|
+
if (!isWindows) {
|
|
83
|
+
await fsp.chmod(destPath, 0o755);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
console.log(`[codetok] installed ${binaryName} (${target.goos}/${target.goarch})`);
|
|
87
|
+
} finally {
|
|
88
|
+
await fsp.rm(tmpDir, { recursive: true, force: true });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function resolveTarget(platform, arch) {
|
|
93
|
+
const goos =
|
|
94
|
+
platform === 'darwin'
|
|
95
|
+
? 'darwin'
|
|
96
|
+
: platform === 'linux'
|
|
97
|
+
? 'linux'
|
|
98
|
+
: platform === 'win32'
|
|
99
|
+
? 'windows'
|
|
100
|
+
: null;
|
|
101
|
+
|
|
102
|
+
const goarch =
|
|
103
|
+
arch === 'x64'
|
|
104
|
+
? 'amd64'
|
|
105
|
+
: arch === 'arm64'
|
|
106
|
+
? 'arm64'
|
|
107
|
+
: null;
|
|
108
|
+
|
|
109
|
+
if (!goos || !goarch) {
|
|
110
|
+
throw new Error(`unsupported platform/arch: ${platform}/${arch}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { goos, goarch };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function verifySHA256(checksumsPath, filePath, archiveName) {
|
|
117
|
+
const checksums = await fsp.readFile(checksumsPath, 'utf8');
|
|
118
|
+
const expected = parseChecksum(checksums, archiveName);
|
|
119
|
+
if (!expected) {
|
|
120
|
+
throw new Error(`checksum not found for ${archiveName}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const actual = await sha256(filePath);
|
|
124
|
+
if (actual !== expected) {
|
|
125
|
+
throw new Error(`checksum mismatch for ${archiveName}: expected ${expected}, got ${actual}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function parseChecksum(checksumsText, archiveName) {
|
|
130
|
+
const lines = checksumsText.split(/\r?\n/);
|
|
131
|
+
for (const line of lines) {
|
|
132
|
+
const trimmed = line.trim();
|
|
133
|
+
if (!trimmed) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const parts = trimmed.split(/\s+/);
|
|
137
|
+
if (parts.length < 2) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const hash = parts[0].toLowerCase();
|
|
142
|
+
const fileName = parts[parts.length - 1].replace(/^\*/, '');
|
|
143
|
+
if (fileName === archiveName) {
|
|
144
|
+
return hash;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return '';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function sha256(filePath) {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const hash = crypto.createHash('sha256');
|
|
153
|
+
const rs = fs.createReadStream(filePath);
|
|
154
|
+
rs.on('error', reject);
|
|
155
|
+
rs.on('data', (chunk) => hash.update(chunk));
|
|
156
|
+
rs.on('end', () => resolve(hash.digest('hex')));
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function downloadToFile(url, destPath, redirects = 0) {
|
|
161
|
+
if (redirects > maxRedirects) {
|
|
162
|
+
throw new Error(`too many redirects while downloading ${url}`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
let done = false;
|
|
167
|
+
const finish = (err) => {
|
|
168
|
+
if (done) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
done = true;
|
|
172
|
+
if (err) {
|
|
173
|
+
reject(err);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
resolve();
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const req = https.get(
|
|
180
|
+
url,
|
|
181
|
+
{
|
|
182
|
+
headers: {
|
|
183
|
+
'User-Agent': 'codetok-npm-installer',
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
(res) => {
|
|
187
|
+
if (
|
|
188
|
+
res.statusCode &&
|
|
189
|
+
[301, 302, 303, 307, 308].includes(res.statusCode) &&
|
|
190
|
+
res.headers.location
|
|
191
|
+
) {
|
|
192
|
+
const nextURL = new URL(res.headers.location, url).toString();
|
|
193
|
+
res.resume();
|
|
194
|
+
downloadToFile(nextURL, destPath, redirects + 1).then(() => finish()).catch(finish);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (res.statusCode !== 200) {
|
|
199
|
+
res.resume();
|
|
200
|
+
finish(new Error(`download failed (${res.statusCode}) for ${url}`));
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const ws = fs.createWriteStream(destPath);
|
|
205
|
+
ws.on('error', (err) => {
|
|
206
|
+
res.destroy();
|
|
207
|
+
finish(err);
|
|
208
|
+
});
|
|
209
|
+
ws.on('finish', () => finish());
|
|
210
|
+
res.on('error', (err) => {
|
|
211
|
+
ws.destroy();
|
|
212
|
+
finish(err);
|
|
213
|
+
});
|
|
214
|
+
res.pipe(ws);
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
req.setTimeout(requestTimeoutMs, () => {
|
|
219
|
+
req.destroy(new Error(`download timeout after ${requestTimeoutMs}ms for ${url}`));
|
|
220
|
+
});
|
|
221
|
+
req.on('error', finish);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function findFileByName(rootDir, name) {
|
|
226
|
+
const entries = await fsp.readdir(rootDir, { withFileTypes: true });
|
|
227
|
+
for (const entry of entries) {
|
|
228
|
+
const full = path.join(rootDir, entry.name);
|
|
229
|
+
if (entry.isFile() && entry.name === name) {
|
|
230
|
+
return full;
|
|
231
|
+
}
|
|
232
|
+
if (entry.isDirectory()) {
|
|
233
|
+
const found = await findFileByName(full, name);
|
|
234
|
+
if (found) {
|
|
235
|
+
return found;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return '';
|
|
240
|
+
}
|
package/vendor/.gitkeep
ADDED
|
File without changes
|