@lordcat/claw 0.0.0-stage → 1.0.0-alpha.1
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 +686 -0
- package/README.md +19 -2
- package/bin/claw.js +2 -0
- package/dist/assets/927d672925e7b99f7813735c484c6922.svg +3 -0
- package/dist/commands/build.js +59 -0
- package/dist/commands/ci.js +25 -0
- package/dist/commands/init.js +47 -0
- package/dist/commands/install.js +210 -0
- package/dist/commands/login.js +109 -0
- package/dist/commands/logout.js +6 -0
- package/dist/commands/publish.js +91 -0
- package/dist/commands/uninstall.js +40 -0
- package/dist/compiler/src/assets/927d672925e7b99f7813735c484c6922.svg +3 -0
- package/dist/compiler/src/compiler.js +8829 -0
- package/dist/compiler/src/extension-metadata-worker.js +2439 -0
- package/dist/config.js +11 -0
- package/dist/index.js +72 -0
- package/dist/internal/lists.claw +263 -0
- package/dist/schemas.js +57 -0
- package/dist/util/claw-file.js +26 -0
- package/dist/util/claw-lock.js +23 -0
- package/dist/util/credentials.js +65 -0
- package/dist/util/oauth.js +20 -0
- package/package.json +44 -4
package/dist/config.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
export const packageDirectory = path.resolve('claw_modules');
|
|
3
|
+
export function getBackendURL() {
|
|
4
|
+
return 'https://claw-backend.up.railway.app';
|
|
5
|
+
}
|
|
6
|
+
export function getClerkOAuthConfig() {
|
|
7
|
+
return {
|
|
8
|
+
issuer: 'https://clerk.claw.lordcat.dev',
|
|
9
|
+
clientId: 'TTEmU1oWc9sZ89iW'
|
|
10
|
+
};
|
|
11
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import meow from 'meow';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import build from './commands/build.js';
|
|
4
|
+
import ci from './commands/ci.js';
|
|
5
|
+
import init from './commands/init.js';
|
|
6
|
+
import install from './commands/install.js';
|
|
7
|
+
import login from './commands/login.js';
|
|
8
|
+
import logout from './commands/logout.js';
|
|
9
|
+
import publish from './commands/publish.js';
|
|
10
|
+
import uninstall from './commands/uninstall.js';
|
|
11
|
+
const help = `
|
|
12
|
+
Usage
|
|
13
|
+
$ claw build
|
|
14
|
+
$ claw ci
|
|
15
|
+
$ claw init [project|package] [name]
|
|
16
|
+
$ claw install [package[@version] ...]
|
|
17
|
+
$ claw login
|
|
18
|
+
$ claw logout
|
|
19
|
+
$ claw publish
|
|
20
|
+
$ claw uninstall <package> ...
|
|
21
|
+
`;
|
|
22
|
+
const cli = meow({
|
|
23
|
+
importMeta: import.meta,
|
|
24
|
+
commands: [
|
|
25
|
+
'build',
|
|
26
|
+
'ci',
|
|
27
|
+
'init',
|
|
28
|
+
'install',
|
|
29
|
+
'login',
|
|
30
|
+
'logout',
|
|
31
|
+
'publish',
|
|
32
|
+
'uninstall'
|
|
33
|
+
],
|
|
34
|
+
help,
|
|
35
|
+
allowUnknownFlags: false
|
|
36
|
+
});
|
|
37
|
+
const args = cli.input;
|
|
38
|
+
const handleError = (error) => {
|
|
39
|
+
const message = error instanceof Error ? error.message : error;
|
|
40
|
+
console.error(chalk.red(`Error: ${chalk.underline(message)}`));
|
|
41
|
+
process.exitCode = 1;
|
|
42
|
+
};
|
|
43
|
+
process.on('uncaughtException', handleError);
|
|
44
|
+
process.on('unhandledRejection', handleError);
|
|
45
|
+
switch (cli.command) {
|
|
46
|
+
case 'build':
|
|
47
|
+
await build();
|
|
48
|
+
break;
|
|
49
|
+
case 'ci':
|
|
50
|
+
await ci();
|
|
51
|
+
break;
|
|
52
|
+
case 'init':
|
|
53
|
+
await init(args);
|
|
54
|
+
break;
|
|
55
|
+
case 'install':
|
|
56
|
+
await install(args);
|
|
57
|
+
break;
|
|
58
|
+
case 'login':
|
|
59
|
+
await login();
|
|
60
|
+
break;
|
|
61
|
+
case 'logout':
|
|
62
|
+
await logout();
|
|
63
|
+
break;
|
|
64
|
+
case 'publish':
|
|
65
|
+
await publish();
|
|
66
|
+
break;
|
|
67
|
+
case 'uninstall':
|
|
68
|
+
await uninstall(args);
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
cli.showHelp();
|
|
72
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
type Scalar = number | string | boolean;
|
|
2
|
+
|
|
3
|
+
var reserving: number;
|
|
4
|
+
var allocated: number;
|
|
5
|
+
|
|
6
|
+
function* allocateFrame(bucket: number, size: number): number {
|
|
7
|
+
waitUntil(reserving == 0);
|
|
8
|
+
reserving = 1;
|
|
9
|
+
|
|
10
|
+
// A free frame's first slot points to the next free frame in its bucket.
|
|
11
|
+
var head = lists.item("$frameFree", bucket);
|
|
12
|
+
if (head > 0) {
|
|
13
|
+
allocated = head;
|
|
14
|
+
lists.replace("$frameFree", bucket, lists.item("$frames", head));
|
|
15
|
+
} else {
|
|
16
|
+
if (lists.length("$frames") + size > 199999) {
|
|
17
|
+
fail("Function memory is full.");
|
|
18
|
+
}
|
|
19
|
+
allocated = lists.length("$frames") + 1;
|
|
20
|
+
repeat (size + 1) {
|
|
21
|
+
lists.add("$frames", "");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
reserving = 0;
|
|
26
|
+
return allocated;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function* releaseFrame(frame: number, bucket: number): void {
|
|
30
|
+
lists.replace("$frames", frame, lists.item("$frameFree", bucket));
|
|
31
|
+
lists.replace("$frameFree", bucket, frame);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function* initialize(frameBuckets: number): void {
|
|
35
|
+
reserving = 0;
|
|
36
|
+
repeat (frameBuckets) {
|
|
37
|
+
lists.add("$frameFree", 0);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function* fail(message: string): never {
|
|
42
|
+
print(message);
|
|
43
|
+
project.stopAll();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function* integer(value: Scalar): number {
|
|
47
|
+
if (value != math.floor(value)) {
|
|
48
|
+
fail("Expected a finite integer.");
|
|
49
|
+
}
|
|
50
|
+
if (value > 200000 || value < -200000) {
|
|
51
|
+
fail("List index or length exceeds the Scratch limit.");
|
|
52
|
+
}
|
|
53
|
+
return value - 0;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function* bound(inputIndex: Scalar, count: number): number {
|
|
57
|
+
var index = integer(inputIndex);
|
|
58
|
+
if (index < 0) {
|
|
59
|
+
index += count;
|
|
60
|
+
}
|
|
61
|
+
if (index < 0) {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
if (index > count) {
|
|
65
|
+
return count;
|
|
66
|
+
}
|
|
67
|
+
return index;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The compiler binds source and target to specific Scratch lists.
|
|
71
|
+
function* listHelpers(source: Scalar[], target: Scalar[]): void {
|
|
72
|
+
function* listAt(input: Scalar): Scalar {
|
|
73
|
+
var index = integer(input);
|
|
74
|
+
if (index < 0) {
|
|
75
|
+
index += lists.length(source);
|
|
76
|
+
}
|
|
77
|
+
// Scratch list positions start at 1.
|
|
78
|
+
return lists.item(source, index + 1);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function* listSet(input: Scalar, value: Scalar): void {
|
|
82
|
+
var index = integer(input);
|
|
83
|
+
var count = lists.length(source);
|
|
84
|
+
if (index < 0 || index > count) {
|
|
85
|
+
fail("List assignment is out of bounds.");
|
|
86
|
+
}
|
|
87
|
+
if (index == count) {
|
|
88
|
+
if (count == 200000) {
|
|
89
|
+
fail("List length exceeds the Scratch limit.");
|
|
90
|
+
}
|
|
91
|
+
lists.add(source, value);
|
|
92
|
+
} else {
|
|
93
|
+
lists.replace(source, index + 1, value);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function* listResize(input: Scalar): void {
|
|
98
|
+
var count = integer(input);
|
|
99
|
+
if (count < 0) {
|
|
100
|
+
fail("List length cannot be negative.");
|
|
101
|
+
}
|
|
102
|
+
var length = lists.length(source);
|
|
103
|
+
repeat (length - count) {
|
|
104
|
+
lists.delete(source, "last");
|
|
105
|
+
}
|
|
106
|
+
repeat (count - length) {
|
|
107
|
+
lists.add(source, "");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function* listAppend(startInput: Scalar, endInput: Scalar): void {
|
|
112
|
+
var index = bound(startInput, lists.length(source)) + 1;
|
|
113
|
+
var end = bound(endInput, lists.length(source)) + 1;
|
|
114
|
+
if (lists.length(target) + end - index > 200000) {
|
|
115
|
+
fail("List length exceeds the Scratch limit.");
|
|
116
|
+
}
|
|
117
|
+
repeat (end - index) {
|
|
118
|
+
lists.add(target, lists.item(source, index));
|
|
119
|
+
index++;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function* listDeleteCount(start: number, inputCount: Scalar): number {
|
|
124
|
+
var count = integer(inputCount);
|
|
125
|
+
var remaining = lists.length(source) - start;
|
|
126
|
+
if (count > remaining) {
|
|
127
|
+
return remaining;
|
|
128
|
+
}
|
|
129
|
+
return count;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function* listDelete(start: number, count: number): void {
|
|
133
|
+
if (count <= 0) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (count == 1) {
|
|
137
|
+
lists.delete(source, start + 1);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
var index = start + 1;
|
|
141
|
+
var length = lists.length(source);
|
|
142
|
+
while (index + count <= length) {
|
|
143
|
+
lists.replace(source, index, lists.item(source, index + count));
|
|
144
|
+
index++;
|
|
145
|
+
}
|
|
146
|
+
repeat (count) {
|
|
147
|
+
lists.delete(source, "last");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function* listTake(start: number, count: number): void {
|
|
152
|
+
var index = start + 1;
|
|
153
|
+
repeat (count) {
|
|
154
|
+
lists.add(target, lists.item(source, index));
|
|
155
|
+
index++;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function* listReverse(): void {
|
|
160
|
+
var left = 1;
|
|
161
|
+
var right = lists.length(source);
|
|
162
|
+
while (left < right) {
|
|
163
|
+
var value = lists.item(source, left);
|
|
164
|
+
lists.replace(source, left, lists.item(source, right));
|
|
165
|
+
lists.replace(source, right, value);
|
|
166
|
+
left++;
|
|
167
|
+
right--;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function* listFill(value: Scalar, startInput: Scalar, endInput: Scalar): void {
|
|
172
|
+
var index = bound(startInput, lists.length(source)) + 1;
|
|
173
|
+
var end = bound(endInput, lists.length(source)) + 1;
|
|
174
|
+
repeat (end - index) {
|
|
175
|
+
lists.replace(source, index, value);
|
|
176
|
+
index++;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function* listIndexOf(value: Scalar, startInput: Scalar): number {
|
|
181
|
+
var index = bound(startInput, lists.length(source)) + 1;
|
|
182
|
+
var end = lists.length(source) + 1;
|
|
183
|
+
while (index < end) {
|
|
184
|
+
if (lists.item(source, index) == value) {
|
|
185
|
+
return index - 1;
|
|
186
|
+
}
|
|
187
|
+
index++;
|
|
188
|
+
}
|
|
189
|
+
return -1;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function* listLastIndexOf(value: Scalar, startInput: Scalar): number {
|
|
193
|
+
var index = integer(startInput);
|
|
194
|
+
var count = lists.length(source);
|
|
195
|
+
if (index < 0) {
|
|
196
|
+
index += count;
|
|
197
|
+
}
|
|
198
|
+
if (index >= count) {
|
|
199
|
+
index = count - 1;
|
|
200
|
+
}
|
|
201
|
+
while (index >= 0) {
|
|
202
|
+
if (lists.item(source, index + 1) == value) {
|
|
203
|
+
return index;
|
|
204
|
+
}
|
|
205
|
+
index--;
|
|
206
|
+
}
|
|
207
|
+
return -1;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function* listJoin(separator: Scalar): Scalar {
|
|
211
|
+
var result = lists.item(source, 1);
|
|
212
|
+
var index = 2;
|
|
213
|
+
repeat (lists.length(source) - 1) {
|
|
214
|
+
result = compiler.join(
|
|
215
|
+
compiler.join(result, separator),
|
|
216
|
+
lists.item(source, index)
|
|
217
|
+
);
|
|
218
|
+
index++;
|
|
219
|
+
}
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function* listSort(): void {
|
|
224
|
+
var length = lists.length(source);
|
|
225
|
+
var width = 1;
|
|
226
|
+
|
|
227
|
+
// Merge adjacent runs, doubling their width on each pass.
|
|
228
|
+
while (width < length) {
|
|
229
|
+
lists.clear(target);
|
|
230
|
+
var start = 1;
|
|
231
|
+
while (start <= length) {
|
|
232
|
+
const var middle = start + width;
|
|
233
|
+
var end = middle + width;
|
|
234
|
+
if (end > length + 1) {
|
|
235
|
+
end = length + 1;
|
|
236
|
+
}
|
|
237
|
+
var left = start;
|
|
238
|
+
var right = middle;
|
|
239
|
+
repeat (end - start) {
|
|
240
|
+
if (
|
|
241
|
+
right >= end ||
|
|
242
|
+
left < middle && !(lists.item(source, right) < lists.item(source, left))
|
|
243
|
+
) {
|
|
244
|
+
lists.add(target, lists.item(source, left));
|
|
245
|
+
left++;
|
|
246
|
+
} else {
|
|
247
|
+
lists.add(target, lists.item(source, right));
|
|
248
|
+
right++;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
start = end;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
var index = 1;
|
|
255
|
+
repeat (length) {
|
|
256
|
+
lists.replace(source, index, lists.item(target, index));
|
|
257
|
+
index++;
|
|
258
|
+
}
|
|
259
|
+
width *= 2;
|
|
260
|
+
}
|
|
261
|
+
lists.clear(target);
|
|
262
|
+
}
|
|
263
|
+
}
|
package/dist/schemas.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { valid } from 'semver';
|
|
3
|
+
const semverSchema = z.string().refine((value) => valid(value) === value.split('+')[0], { error: 'Invalid semantic version' });
|
|
4
|
+
export const packageNameSchema = z.string()
|
|
5
|
+
.regex(/^[a-z0-9][a-z0-9_-]*$/);
|
|
6
|
+
const lockedDependencySchema = z.object({
|
|
7
|
+
specifier: z.string(),
|
|
8
|
+
version: semverSchema,
|
|
9
|
+
license: z.string(),
|
|
10
|
+
dependencies: z.optional(z.record(packageNameSchema, z.string())),
|
|
11
|
+
integrity: z.string().regex(/^sha512-[A-Za-z0-9+/]+={0,2}$/)
|
|
12
|
+
});
|
|
13
|
+
export const lockFileSchema = z.object({
|
|
14
|
+
lockfileVersion: z.literal(1),
|
|
15
|
+
dependencies: z.record(packageNameSchema, lockedDependencySchema)
|
|
16
|
+
});
|
|
17
|
+
export const packageVersionSchema = z.object({
|
|
18
|
+
version: semverSchema,
|
|
19
|
+
license: z.string(),
|
|
20
|
+
dependencies: z.record(packageNameSchema, z.string()),
|
|
21
|
+
dist: z.object({
|
|
22
|
+
tarball: z.string().startsWith('/packages/'),
|
|
23
|
+
integrity: z.string().regex(/^sha512-[A-Za-z0-9+/]+={0,2}$/)
|
|
24
|
+
})
|
|
25
|
+
});
|
|
26
|
+
export const packageVersionsSchema = z.object({
|
|
27
|
+
versions: z.array(semverSchema)
|
|
28
|
+
});
|
|
29
|
+
const baseSchema = z.object({
|
|
30
|
+
name: z.string(),
|
|
31
|
+
version: semverSchema,
|
|
32
|
+
author: z.optional(z.string()),
|
|
33
|
+
license: z.optional(z.string()),
|
|
34
|
+
dependencies: z.optional(z.record(packageNameSchema, z.string()))
|
|
35
|
+
});
|
|
36
|
+
export const projectSchema = z.object({
|
|
37
|
+
...baseSchema.shape,
|
|
38
|
+
type: z.literal('project'),
|
|
39
|
+
sprites: z.record(z.string(), z.strictObject({
|
|
40
|
+
costume: z.optional(z.string()),
|
|
41
|
+
script: z.string()
|
|
42
|
+
})),
|
|
43
|
+
output: z.optional(z.string())
|
|
44
|
+
});
|
|
45
|
+
export const packageSchema = z.object({
|
|
46
|
+
...baseSchema.shape,
|
|
47
|
+
type: z.literal('package'),
|
|
48
|
+
description: z.optional(z.string()),
|
|
49
|
+
repository: z.optional(z.string()),
|
|
50
|
+
homepage: z.optional(z.string()),
|
|
51
|
+
keywords: z.optional(z.array(z.string())),
|
|
52
|
+
main: z.string()
|
|
53
|
+
});
|
|
54
|
+
export const packageFiles = z.object({
|
|
55
|
+
version: z.string(),
|
|
56
|
+
files: z.record(z.string().regex(/^(?:[a-zA-Z0-9_-][a-zA-Z0-9._-]*\/)*[a-zA-Z0-9_-][a-zA-Z0-9._-]*\.claw$/), z.string())
|
|
57
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { writeFile } from 'node:fs/promises';
|
|
3
|
+
import { projectSchema, packageSchema } from '../schemas.js';
|
|
4
|
+
export function getClawFile() {
|
|
5
|
+
if (!existsSync('claw.json')) {
|
|
6
|
+
throw new Error('claw.json not found.');
|
|
7
|
+
}
|
|
8
|
+
let clawFile;
|
|
9
|
+
try {
|
|
10
|
+
clawFile = JSON.parse(readFileSync('claw.json', 'utf-8'));
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
throw new Error('Invalid claw.json file.');
|
|
14
|
+
}
|
|
15
|
+
const isProject = typeof clawFile === 'object' && clawFile
|
|
16
|
+
&& 'type' in clawFile && clawFile.type === 'project';
|
|
17
|
+
const schema = isProject ? projectSchema : packageSchema;
|
|
18
|
+
const result = schema.safeParse(clawFile);
|
|
19
|
+
if (!result.success) {
|
|
20
|
+
throw new Error('Invalid claw.json file.');
|
|
21
|
+
}
|
|
22
|
+
return result.data;
|
|
23
|
+
}
|
|
24
|
+
export function writeClawFile(clawFile) {
|
|
25
|
+
return writeFile('claw.json', `${JSON.stringify(clawFile, null, 2)}\n`);
|
|
26
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { writeFile } from 'node:fs/promises';
|
|
3
|
+
import { lockFileSchema } from '../schemas.js';
|
|
4
|
+
export function getClawLock() {
|
|
5
|
+
if (!existsSync('claw-lock.json')) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
let lockFile;
|
|
9
|
+
try {
|
|
10
|
+
lockFile = JSON.parse(readFileSync('claw-lock.json', 'utf-8'));
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
throw new Error('Invalid claw-lock.json file.');
|
|
14
|
+
}
|
|
15
|
+
const result = lockFileSchema.safeParse(lockFile);
|
|
16
|
+
if (!result.success) {
|
|
17
|
+
throw new Error('Invalid claw-lock.json file.');
|
|
18
|
+
}
|
|
19
|
+
return result.data;
|
|
20
|
+
}
|
|
21
|
+
export function writeClawLock(lockFile) {
|
|
22
|
+
return writeFile('claw-lock.json', `${JSON.stringify(lockFile, null, 2)}\n`);
|
|
23
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { chmod, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { requestOAuthToken } from './oauth.js';
|
|
5
|
+
// todo: is there a better place to put this
|
|
6
|
+
const credentialsDirectory = join(homedir(), '.claw');
|
|
7
|
+
const credentialsFile = join(credentialsDirectory, 'credentials.json');
|
|
8
|
+
async function getCredential() {
|
|
9
|
+
let credentials;
|
|
10
|
+
try {
|
|
11
|
+
credentials = JSON.parse(await readFile(credentialsFile, 'utf8'));
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
|
|
15
|
+
throw new Error('Not signed in. Run claw login.');
|
|
16
|
+
}
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
// big
|
|
20
|
+
if (!credentials || typeof credentials !== 'object'
|
|
21
|
+
|| !('accessToken' in credentials)
|
|
22
|
+
|| typeof credentials.accessToken !== 'string'
|
|
23
|
+
|| !('refreshToken' in credentials)
|
|
24
|
+
|| typeof credentials.refreshToken !== 'string'
|
|
25
|
+
|| !('expiresAt' in credentials)
|
|
26
|
+
|| typeof credentials.expiresAt !== 'number'
|
|
27
|
+
|| !('clientId' in credentials)
|
|
28
|
+
|| typeof credentials.clientId !== 'string'
|
|
29
|
+
|| !('tokenEndpoint' in credentials)
|
|
30
|
+
|| typeof credentials.tokenEndpoint !== 'string') {
|
|
31
|
+
throw new Error('Invalid credentials. Run claw login again.');
|
|
32
|
+
}
|
|
33
|
+
return credentials;
|
|
34
|
+
}
|
|
35
|
+
export async function writeCredential(credentials) {
|
|
36
|
+
await mkdir(credentialsDirectory, {
|
|
37
|
+
recursive: true,
|
|
38
|
+
mode: 0o700
|
|
39
|
+
});
|
|
40
|
+
await chmod(credentialsDirectory, 0o700);
|
|
41
|
+
await writeFile(credentialsFile, `${JSON.stringify(credentials, null, 2)}\n`, { mode: 0o600 });
|
|
42
|
+
await chmod(credentialsFile, 0o600);
|
|
43
|
+
}
|
|
44
|
+
export async function getAccessToken() {
|
|
45
|
+
const credentials = await getCredential();
|
|
46
|
+
if (credentials.expiresAt > Date.now() + 60_000) {
|
|
47
|
+
return credentials.accessToken;
|
|
48
|
+
}
|
|
49
|
+
const token = await requestOAuthToken(credentials.tokenEndpoint, new URLSearchParams({
|
|
50
|
+
grant_type: 'refresh_token',
|
|
51
|
+
client_id: credentials.clientId,
|
|
52
|
+
refresh_token: credentials.refreshToken
|
|
53
|
+
}));
|
|
54
|
+
const updatedCredentials = {
|
|
55
|
+
...credentials,
|
|
56
|
+
accessToken: token.accessToken,
|
|
57
|
+
refreshToken: token.refreshToken || credentials.refreshToken,
|
|
58
|
+
expiresAt: Date.now() + token.expiresIn * 1000
|
|
59
|
+
};
|
|
60
|
+
await writeCredential(updatedCredentials);
|
|
61
|
+
return updatedCredentials.accessToken;
|
|
62
|
+
}
|
|
63
|
+
export function removeCredential() {
|
|
64
|
+
return rm(credentialsFile, { force: true });
|
|
65
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export async function requestOAuthToken(endpoint, parameters) {
|
|
2
|
+
const response = await fetch(endpoint, {
|
|
3
|
+
method: 'POST',
|
|
4
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
5
|
+
body: parameters
|
|
6
|
+
});
|
|
7
|
+
const body = await response.json();
|
|
8
|
+
const error = body.error_description || body.error;
|
|
9
|
+
if (!response.ok) {
|
|
10
|
+
throw new Error(error || `Authentication failed with HTTP ${response.status}.`);
|
|
11
|
+
}
|
|
12
|
+
if (!body.access_token || typeof body.expires_in !== 'number') {
|
|
13
|
+
throw new Error('Clerk returned an invalid OAuth token response.');
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
accessToken: body.access_token,
|
|
17
|
+
refreshToken: body.refresh_token,
|
|
18
|
+
expiresIn: body.expires_in
|
|
19
|
+
};
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lordcat/claw",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=24"
|
|
7
|
+
},
|
|
8
|
+
"bin": {
|
|
9
|
+
"claw": "bin/claw.js"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/LordCat0/claw.git",
|
|
14
|
+
"directory": "packages/cli"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"bin",
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public",
|
|
22
|
+
"tag": "alpha"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prebuild": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
26
|
+
"build": "tsc && node scripts/bundle-compiler.js && node ../compiler/scripts/bundle-metadata-worker.js dist/compiler/src",
|
|
27
|
+
"prepack": "npm run build --workspace compiler && npm run build",
|
|
28
|
+
"test": "node --test test/*.test.ts"
|
|
29
|
+
},
|
|
30
|
+
"license": "GPL-3.0-only",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"chalk": "^6.0.1",
|
|
33
|
+
"jszip": "^3.10.2",
|
|
34
|
+
"meow": "^14.1.0",
|
|
35
|
+
"semver": "^7.8.6",
|
|
36
|
+
"tar-stream": "^3.3.0",
|
|
37
|
+
"zod": "^4.6.7"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^26.6.11",
|
|
41
|
+
"@types/semver": "^7.8.0",
|
|
42
|
+
"compiler": "^1.0.0-alpha.1",
|
|
43
|
+
"esbuild": "^0.28.2",
|
|
44
|
+
"typescript": "^7.0.2"
|
|
45
|
+
}
|
|
46
|
+
}
|