@seguelac/aca-cli 0.1.0
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/CHANGELOG.md +23 -0
- package/LICENSE +21 -0
- package/README.md +171 -0
- package/dist/api.js +88 -0
- package/dist/commands/auth.js +41 -0
- package/dist/commands/autopilot.js +52 -0
- package/dist/commands/campaign.js +57 -0
- package/dist/commands/completion.js +69 -0
- package/dist/commands/config.js +48 -0
- package/dist/commands/contact.js +58 -0
- package/dist/commands/enroll.js +32 -0
- package/dist/commands/inbox.js +57 -0
- package/dist/commands/leads.js +47 -0
- package/dist/commands/lists.js +65 -0
- package/dist/commands/org.js +48 -0
- package/dist/commands/reply.js +47 -0
- package/dist/config.js +26 -0
- package/dist/constants.js +9 -0
- package/dist/index.js +45 -0
- package/dist/output.js +67 -0
- package/dist/paths.js +19 -0
- package/dist/prompt.js +52 -0
- package/dist/session.js +81 -0
- package/dist/update-check.js +64 -0
- package/dist/util.js +17 -0
- package/package.json +54 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { restGet } from '../api.js';
|
|
2
|
+
import { output } from '../output.js';
|
|
3
|
+
export function registerInbox(program) {
|
|
4
|
+
const inbox = program.command('inbox').description('Inspect CRM inbox conversations');
|
|
5
|
+
inbox
|
|
6
|
+
.command('unread')
|
|
7
|
+
.description('List conversations with unread messages')
|
|
8
|
+
.option('--channel <c>', 'linkedin | email | instagram | whatsapp | telegram | messenger | twitter')
|
|
9
|
+
.option('--status <s>', 'open | snoozed | closed | archived', 'open')
|
|
10
|
+
.option('--limit <n>', 'Max rows', '20')
|
|
11
|
+
.option('--json', 'Emit JSON')
|
|
12
|
+
.action(async (opts) => {
|
|
13
|
+
const params = new URLSearchParams();
|
|
14
|
+
params.append('select', 'id,channel,status,unread_count,last_message_at,last_message_preview,last_message_direction,priority_score,lead_intent,contact_id,crm_contacts(display_name,company,primary_email)');
|
|
15
|
+
params.append('unread_count', 'gt.0');
|
|
16
|
+
params.append('status', `eq.${opts.status}`);
|
|
17
|
+
if (opts.channel)
|
|
18
|
+
params.append('channel', `eq.${opts.channel}`);
|
|
19
|
+
params.append('order', 'priority_score.desc,last_message_at.desc');
|
|
20
|
+
params.append('limit', String(opts.limit));
|
|
21
|
+
const rows = await restGet(`crm_conversations?${params.toString()}`);
|
|
22
|
+
output(rows, { json: opts.json });
|
|
23
|
+
});
|
|
24
|
+
inbox
|
|
25
|
+
.command('show')
|
|
26
|
+
.description('Show a conversation with its recent messages')
|
|
27
|
+
.argument('<conversationId>')
|
|
28
|
+
.option('--limit <n>', 'Max messages', '20')
|
|
29
|
+
.option('--json', 'Emit JSON')
|
|
30
|
+
.action(async (conversationId, opts) => {
|
|
31
|
+
const conv = await restGet(`crm_conversations?id=eq.${conversationId}&select=*,crm_contacts(display_name,company,primary_email,primary_linkedin_url)`);
|
|
32
|
+
const messages = await restGet(`crm_messages?conversation_id=eq.${conversationId}&select=id,direction,content,sent_at,received_at,sender_external_id,read_at&order=sent_at.desc,received_at.desc&limit=${opts.limit}`);
|
|
33
|
+
output({ conversation: conv[0] ?? null, messages: messages.reverse() }, { json: opts.json });
|
|
34
|
+
});
|
|
35
|
+
inbox
|
|
36
|
+
.command('list')
|
|
37
|
+
.description('List conversations (any status)')
|
|
38
|
+
.option('--channel <c>', 'Filter by channel')
|
|
39
|
+
.option('--status <s>', 'Filter by status')
|
|
40
|
+
.option('--intent <i>', 'Filter by lead_intent (e.g. interested)')
|
|
41
|
+
.option('--limit <n>', 'Max rows', '20')
|
|
42
|
+
.option('--json', 'Emit JSON')
|
|
43
|
+
.action(async (opts) => {
|
|
44
|
+
const params = new URLSearchParams();
|
|
45
|
+
params.append('select', 'id,channel,status,unread_count,last_message_at,last_message_preview,lead_intent,priority_score,crm_contacts(display_name,company)');
|
|
46
|
+
params.append('order', 'last_message_at.desc');
|
|
47
|
+
params.append('limit', String(opts.limit));
|
|
48
|
+
if (opts.status)
|
|
49
|
+
params.append('status', `eq.${opts.status}`);
|
|
50
|
+
if (opts.channel)
|
|
51
|
+
params.append('channel', `eq.${opts.channel}`);
|
|
52
|
+
if (opts.intent)
|
|
53
|
+
params.append('lead_intent', `eq.${opts.intent}`);
|
|
54
|
+
const rows = await restGet(`crm_conversations?${params.toString()}`);
|
|
55
|
+
output(rows, { json: opts.json });
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { restGet } from '../api.js';
|
|
2
|
+
import { output } from '../output.js';
|
|
3
|
+
export function registerLeads(program) {
|
|
4
|
+
const leads = program.command('leads').description('Search and inspect leads / contacts');
|
|
5
|
+
leads
|
|
6
|
+
.command('search')
|
|
7
|
+
.description('Search crm_contacts with filters')
|
|
8
|
+
.option('-q, --query <text>', 'Free-text search across name + email + company')
|
|
9
|
+
.option('--tag <tag>', 'Filter by tag (exact match in tags array)')
|
|
10
|
+
.option('--status <status>', 'active | archived | bounced | unsubscribed | do_not_contact')
|
|
11
|
+
.option('--company <name>', 'Filter by company (ilike)')
|
|
12
|
+
.option('--has-email', 'Only contacts with a primary_email')
|
|
13
|
+
.option('--has-linkedin', 'Only contacts with a primary_linkedin_url')
|
|
14
|
+
.option('--limit <n>', 'Max rows', '50')
|
|
15
|
+
.option('--json', 'Emit JSON')
|
|
16
|
+
.action(async (opts) => {
|
|
17
|
+
const params = new URLSearchParams();
|
|
18
|
+
params.append('select', 'id,display_name,company,job_title,primary_email,primary_linkedin_url,status,tags,lead_score,last_activity_at');
|
|
19
|
+
params.append('order', 'last_activity_at.desc');
|
|
20
|
+
params.append('limit', String(opts.limit));
|
|
21
|
+
if (opts.status)
|
|
22
|
+
params.append('status', `eq.${opts.status}`);
|
|
23
|
+
if (opts.company)
|
|
24
|
+
params.append('company', `ilike.*${opts.company}*`);
|
|
25
|
+
if (opts.hasEmail)
|
|
26
|
+
params.append('primary_email', 'not.is.null');
|
|
27
|
+
if (opts.hasLinkedin)
|
|
28
|
+
params.append('primary_linkedin_url', 'not.is.null');
|
|
29
|
+
if (opts.tag)
|
|
30
|
+
params.append('tags', `cs.{${opts.tag}}`);
|
|
31
|
+
if (opts.query) {
|
|
32
|
+
const q = opts.query.replace(/[,()]/g, '');
|
|
33
|
+
params.append('or', `(display_name.ilike.*${q}*,primary_email.ilike.*${q}*,company.ilike.*${q}*)`);
|
|
34
|
+
}
|
|
35
|
+
const rows = await restGet(`crm_contacts?${params.toString()}`);
|
|
36
|
+
output(rows, { json: opts.json });
|
|
37
|
+
});
|
|
38
|
+
leads
|
|
39
|
+
.command('get')
|
|
40
|
+
.description('Fetch a single contact by ID')
|
|
41
|
+
.argument('<contactId>')
|
|
42
|
+
.option('--json', 'Emit JSON')
|
|
43
|
+
.action(async (contactId, opts) => {
|
|
44
|
+
const rows = await restGet(`crm_contacts?id=eq.${contactId}&select=*`);
|
|
45
|
+
output(rows[0] ?? null, { json: opts.json });
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { invokeFn, restGet, restPost, restDelete } from '../api.js';
|
|
2
|
+
import { output, fail } from '../output.js';
|
|
3
|
+
import { parseList } from '../util.js';
|
|
4
|
+
export function registerLists(program) {
|
|
5
|
+
const lists = program.command('lists').description('Manage lead lists');
|
|
6
|
+
lists
|
|
7
|
+
.command('ls')
|
|
8
|
+
.description('List lead lists for the current organization')
|
|
9
|
+
.option('--limit <n>', 'Max rows', '50')
|
|
10
|
+
.option('--json', 'Emit JSON')
|
|
11
|
+
.action(async (opts) => {
|
|
12
|
+
const rows = await restGet(`lead_lists?select=id,name,description,member_count,created_at&order=created_at.desc&limit=${opts.limit}`);
|
|
13
|
+
output(rows, { json: opts.json });
|
|
14
|
+
});
|
|
15
|
+
lists
|
|
16
|
+
.command('create')
|
|
17
|
+
.description('Create a new lead list')
|
|
18
|
+
.requiredOption('-n, --name <name>', 'List name')
|
|
19
|
+
.option('-d, --description <desc>', 'Description')
|
|
20
|
+
.option('--json', 'Emit JSON')
|
|
21
|
+
.action(async (opts) => {
|
|
22
|
+
const row = { name: opts.name };
|
|
23
|
+
if (opts.description)
|
|
24
|
+
row.description = opts.description;
|
|
25
|
+
const result = await restPost('lead_lists', row);
|
|
26
|
+
output(Array.isArray(result) ? result[0] : result, { json: opts.json });
|
|
27
|
+
});
|
|
28
|
+
lists
|
|
29
|
+
.command('add')
|
|
30
|
+
.description('Add contacts to a list (bulk, server-side)')
|
|
31
|
+
.requiredOption('-l, --list <listId>', 'Lead list ID')
|
|
32
|
+
.requiredOption('--contacts <ids>', 'Comma-separated contact IDs')
|
|
33
|
+
.option('--json', 'Emit JSON')
|
|
34
|
+
.action(async (opts) => {
|
|
35
|
+
const contactIds = parseList(opts.contacts);
|
|
36
|
+
if (contactIds.length === 0)
|
|
37
|
+
fail('No contact IDs provided');
|
|
38
|
+
const result = await invokeFn('bulk-add-to-list', { listId: opts.list, contactIds });
|
|
39
|
+
output(result, { json: opts.json });
|
|
40
|
+
});
|
|
41
|
+
lists
|
|
42
|
+
.command('members')
|
|
43
|
+
.description('List members of a lead list')
|
|
44
|
+
.requiredOption('-l, --list <listId>', 'Lead list ID')
|
|
45
|
+
.option('--limit <n>', 'Max rows', '50')
|
|
46
|
+
.option('--json', 'Emit JSON')
|
|
47
|
+
.action(async (opts) => {
|
|
48
|
+
const rows = await restGet(`lead_list_members?list_id=eq.${opts.list}&select=contact_id,added_at,crm_contacts(id,display_name,primary_email,primary_linkedin_url,company)&order=added_at.desc&limit=${opts.limit}`);
|
|
49
|
+
output(rows, { json: opts.json });
|
|
50
|
+
});
|
|
51
|
+
lists
|
|
52
|
+
.command('remove')
|
|
53
|
+
.description('Remove contacts from a list')
|
|
54
|
+
.requiredOption('-l, --list <listId>', 'Lead list ID')
|
|
55
|
+
.requiredOption('--contacts <ids>', 'Comma-separated contact IDs')
|
|
56
|
+
.option('--json', 'Emit JSON')
|
|
57
|
+
.action(async (opts) => {
|
|
58
|
+
const ids = parseList(opts.contacts);
|
|
59
|
+
if (ids.length === 0)
|
|
60
|
+
fail('No contact IDs provided');
|
|
61
|
+
const quoted = ids.map((s) => `"${s}"`).join(',');
|
|
62
|
+
await restDelete(`lead_list_members?list_id=eq.${opts.list}&contact_id=in.(${quoted})`);
|
|
63
|
+
output({ removed: ids.length }, { json: opts.json });
|
|
64
|
+
});
|
|
65
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { restGet } from '../api.js';
|
|
2
|
+
import { readConfig, updateConfig } from '../config.js';
|
|
3
|
+
import { output, fail } from '../output.js';
|
|
4
|
+
export function registerOrg(program) {
|
|
5
|
+
const org = program.command('org').description('Inspect and switch the active organization');
|
|
6
|
+
org
|
|
7
|
+
.command('ls')
|
|
8
|
+
.description('List organizations you belong to')
|
|
9
|
+
.option('--json', 'Emit JSON')
|
|
10
|
+
.action(async (opts) => {
|
|
11
|
+
const rows = await restGet('organization_members?select=organization_id,role,is_primary_owner,organizations(id,name)&order=joined_at.asc');
|
|
12
|
+
const active = readConfig().active_org_id;
|
|
13
|
+
const formatted = rows.map((r) => ({
|
|
14
|
+
organization_id: r.organization_id,
|
|
15
|
+
name: r.organizations?.name ?? '(unknown)',
|
|
16
|
+
role: r.role,
|
|
17
|
+
primary_owner: r.is_primary_owner,
|
|
18
|
+
active: r.organization_id === active,
|
|
19
|
+
}));
|
|
20
|
+
output(formatted, { json: opts.json });
|
|
21
|
+
});
|
|
22
|
+
org
|
|
23
|
+
.command('switch')
|
|
24
|
+
.description('Set the active organization for subsequent commands')
|
|
25
|
+
.argument('<organizationId>')
|
|
26
|
+
.option('--json', 'Emit JSON')
|
|
27
|
+
.action(async (organizationId, opts) => {
|
|
28
|
+
const rows = await restGet(`organization_members?organization_id=eq.${organizationId}&select=organization_id,organizations(name)`);
|
|
29
|
+
if (rows.length === 0) {
|
|
30
|
+
fail(`You don't belong to organization ${organizationId} (or it doesn't exist).`);
|
|
31
|
+
}
|
|
32
|
+
updateConfig({ active_org_id: organizationId });
|
|
33
|
+
output({ active_org_id: organizationId, name: rows[0].organizations?.name ?? null }, { json: opts.json });
|
|
34
|
+
});
|
|
35
|
+
org
|
|
36
|
+
.command('current')
|
|
37
|
+
.description('Show the currently active organization')
|
|
38
|
+
.option('--json', 'Emit JSON')
|
|
39
|
+
.action(async (opts) => {
|
|
40
|
+
const active = readConfig().active_org_id;
|
|
41
|
+
if (!active) {
|
|
42
|
+
output({ active_org_id: null, hint: 'Run `aca org ls` then `aca org switch <id>`.' }, { json: opts.json });
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const rows = await restGet(`organizations?id=eq.${active}&select=id,name`);
|
|
46
|
+
output(rows[0] ?? { active_org_id: active }, { json: opts.json });
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { invokeFn, restGet } from '../api.js';
|
|
2
|
+
import { output, fail } from '../output.js';
|
|
3
|
+
import { readStdinIfPiped } from '../util.js';
|
|
4
|
+
export function registerReply(program) {
|
|
5
|
+
program
|
|
6
|
+
.command('reply')
|
|
7
|
+
.description('Reply to an existing CRM conversation')
|
|
8
|
+
.argument('<conversationId>')
|
|
9
|
+
.option('-m, --message <text>', 'Reply body (or pipe via stdin)')
|
|
10
|
+
.option('--subject <text>', 'Email subject (email channels only)')
|
|
11
|
+
.option('--cc <emails>', 'Comma-separated CC list (email only)')
|
|
12
|
+
.option('--bcc <emails>', 'Comma-separated BCC list (email only)')
|
|
13
|
+
.option('--json', 'Emit JSON')
|
|
14
|
+
.action(async (conversationId, opts) => {
|
|
15
|
+
const piped = await readStdinIfPiped();
|
|
16
|
+
const content = opts.message ?? piped;
|
|
17
|
+
if (!content)
|
|
18
|
+
fail('Provide --message "..." or pipe text via stdin');
|
|
19
|
+
const rows = await restGet(`crm_conversations?id=eq.${conversationId}&select=id,contact_id,channel,channel_account_id,external_conversation_id,external_participant_id,last_message_subject,mailbox_id`);
|
|
20
|
+
const conv = rows[0];
|
|
21
|
+
if (!conv)
|
|
22
|
+
fail(`Conversation ${conversationId} not found`);
|
|
23
|
+
if (!conv.external_participant_id) {
|
|
24
|
+
fail('Conversation is missing external_participant_id — cannot determine recipient.');
|
|
25
|
+
}
|
|
26
|
+
const body = {
|
|
27
|
+
conversation_id: conv.id,
|
|
28
|
+
contact_id: conv.contact_id,
|
|
29
|
+
channel: conv.channel,
|
|
30
|
+
recipient_external_id: conv.external_participant_id,
|
|
31
|
+
content,
|
|
32
|
+
};
|
|
33
|
+
if (conv.channel_account_id)
|
|
34
|
+
body.channel_account_id = conv.channel_account_id;
|
|
35
|
+
if (conv.external_conversation_id)
|
|
36
|
+
body.external_conversation_id = conv.external_conversation_id;
|
|
37
|
+
if (conv.channel === 'email' || conv.mailbox_id) {
|
|
38
|
+
body.email_subject = opts.subject ?? conv.last_message_subject ?? undefined;
|
|
39
|
+
if (opts.cc)
|
|
40
|
+
body.email_cc = opts.cc.split(',').map((s) => s.trim());
|
|
41
|
+
if (opts.bcc)
|
|
42
|
+
body.email_bcc = opts.bcc.split(',').map((s) => s.trim());
|
|
43
|
+
}
|
|
44
|
+
const result = await invokeFn('crm-send-message', body);
|
|
45
|
+
output(result, { json: opts.json });
|
|
46
|
+
});
|
|
47
|
+
}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync, chmodSync } from 'node:fs';
|
|
2
|
+
import { configPath } from './paths.js';
|
|
3
|
+
const DEFAULTS = {
|
|
4
|
+
update_check_enabled: true,
|
|
5
|
+
};
|
|
6
|
+
export function readConfig() {
|
|
7
|
+
const path = configPath();
|
|
8
|
+
if (!existsSync(path))
|
|
9
|
+
return { ...DEFAULTS };
|
|
10
|
+
try {
|
|
11
|
+
return { ...DEFAULTS, ...JSON.parse(readFileSync(path, 'utf8')) };
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return { ...DEFAULTS };
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function writeConfig(config) {
|
|
18
|
+
const path = configPath();
|
|
19
|
+
writeFileSync(path, JSON.stringify(config, null, 2), { mode: 0o600 });
|
|
20
|
+
chmodSync(path, 0o600);
|
|
21
|
+
}
|
|
22
|
+
export function updateConfig(patch) {
|
|
23
|
+
const next = { ...readConfig(), ...patch };
|
|
24
|
+
writeConfig(next);
|
|
25
|
+
return next;
|
|
26
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const SUPABASE_URL = 'https://neqyerjlcrxjsgkhxalx.supabase.co';
|
|
2
|
+
export const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im5lcXllcmpsY3J4anNna2h4YWx4Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTk5OTk3NTMsImV4cCI6MjA3NTU3NTc1M30.bfJLB9ce_XLdzhJC8TLdAeIG-5beY118sg8f5MvbzZs';
|
|
3
|
+
export const CLI_NAME = 'aca';
|
|
4
|
+
export const CONFIG_DIR_NAME = '.aca';
|
|
5
|
+
export const SESSION_FILE = 'session.json';
|
|
6
|
+
export const CONFIG_FILE = 'config.json';
|
|
7
|
+
export const UPDATE_CHECK_FILE = 'update-check.json';
|
|
8
|
+
export const NPM_REGISTRY_URL = 'https://registry.npmjs.org/@seguelac/aca-cli/latest';
|
|
9
|
+
export const UPDATE_CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import { registerAuth } from './commands/auth.js';
|
|
4
|
+
import { registerOrg } from './commands/org.js';
|
|
5
|
+
import { registerEnroll } from './commands/enroll.js';
|
|
6
|
+
import { registerLists } from './commands/lists.js';
|
|
7
|
+
import { registerLeads } from './commands/leads.js';
|
|
8
|
+
import { registerInbox } from './commands/inbox.js';
|
|
9
|
+
import { registerReply } from './commands/reply.js';
|
|
10
|
+
import { registerCampaign } from './commands/campaign.js';
|
|
11
|
+
import { registerContact } from './commands/contact.js';
|
|
12
|
+
import { registerAutopilot } from './commands/autopilot.js';
|
|
13
|
+
import { registerConfig } from './commands/config.js';
|
|
14
|
+
import { registerCompletion } from './commands/completion.js';
|
|
15
|
+
import { maybeCheckForUpdate } from './update-check.js';
|
|
16
|
+
const VERSION = '0.1.0';
|
|
17
|
+
const program = new Command();
|
|
18
|
+
program
|
|
19
|
+
.name('aca')
|
|
20
|
+
.description('Command-line interface for ACA — outreach, inbox, campaigns from your terminal.')
|
|
21
|
+
.version(VERSION);
|
|
22
|
+
registerAuth(program);
|
|
23
|
+
registerOrg(program);
|
|
24
|
+
registerEnroll(program);
|
|
25
|
+
registerLists(program);
|
|
26
|
+
registerLeads(program);
|
|
27
|
+
registerInbox(program);
|
|
28
|
+
registerReply(program);
|
|
29
|
+
registerCampaign(program);
|
|
30
|
+
registerContact(program);
|
|
31
|
+
registerAutopilot(program);
|
|
32
|
+
registerConfig(program);
|
|
33
|
+
registerCompletion(program);
|
|
34
|
+
async function main() {
|
|
35
|
+
void maybeCheckForUpdate(VERSION);
|
|
36
|
+
try {
|
|
37
|
+
await program.parseAsync(process.argv);
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
const e = err;
|
|
41
|
+
process.stderr.write(`error: ${e?.message ?? String(err)}\n`);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
void main();
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export function output(data, { json = false } = {}) {
|
|
2
|
+
if (json) {
|
|
3
|
+
process.stdout.write(JSON.stringify(data, null, 2) + '\n');
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
if (data == null) {
|
|
7
|
+
console.log('(no data)');
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (Array.isArray(data)) {
|
|
11
|
+
if (data.length === 0) {
|
|
12
|
+
console.log('(empty)');
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
printTable(data);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (typeof data === 'object') {
|
|
19
|
+
printKV(data);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
console.log(String(data));
|
|
23
|
+
}
|
|
24
|
+
function printKV(obj) {
|
|
25
|
+
const keys = Object.keys(obj);
|
|
26
|
+
if (keys.length === 0) {
|
|
27
|
+
console.log('(empty)');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const w = Math.max(...keys.map((k) => k.length));
|
|
31
|
+
for (const k of keys) {
|
|
32
|
+
const v = obj[k];
|
|
33
|
+
const s = typeof v === 'object' ? JSON.stringify(v) : String(v);
|
|
34
|
+
console.log(`${k.padEnd(w)} ${s}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function printTable(rows) {
|
|
38
|
+
const keys = Array.from(rows.reduce((set, r) => {
|
|
39
|
+
Object.keys(r ?? {}).forEach((k) => set.add(k));
|
|
40
|
+
return set;
|
|
41
|
+
}, new Set()));
|
|
42
|
+
const widths = {};
|
|
43
|
+
for (const k of keys) {
|
|
44
|
+
widths[k] = Math.max(k.length, ...rows.map((r) => {
|
|
45
|
+
const v = r?.[k];
|
|
46
|
+
const s = v == null ? '' : typeof v === 'object' ? JSON.stringify(v) : String(v);
|
|
47
|
+
return Math.min(s.length, 40);
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
console.log(keys.map((k) => k.padEnd(widths[k])).join(' '));
|
|
51
|
+
console.log(keys.map((k) => '-'.repeat(widths[k])).join(' '));
|
|
52
|
+
for (const r of rows) {
|
|
53
|
+
console.log(keys
|
|
54
|
+
.map((k) => {
|
|
55
|
+
const v = r?.[k];
|
|
56
|
+
let s = v == null ? '' : typeof v === 'object' ? JSON.stringify(v) : String(v);
|
|
57
|
+
if (s.length > 40)
|
|
58
|
+
s = s.slice(0, 37) + '...';
|
|
59
|
+
return s.padEnd(widths[k]);
|
|
60
|
+
})
|
|
61
|
+
.join(' '));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export function fail(message) {
|
|
65
|
+
console.error(`error: ${message}`);
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
package/dist/paths.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { existsSync, mkdirSync } from 'node:fs';
|
|
4
|
+
import { CONFIG_DIR_NAME, SESSION_FILE, CONFIG_FILE, UPDATE_CHECK_FILE } from './constants.js';
|
|
5
|
+
export function configDir() {
|
|
6
|
+
const dir = join(homedir(), CONFIG_DIR_NAME);
|
|
7
|
+
if (!existsSync(dir))
|
|
8
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
9
|
+
return dir;
|
|
10
|
+
}
|
|
11
|
+
export function sessionPath() {
|
|
12
|
+
return join(configDir(), SESSION_FILE);
|
|
13
|
+
}
|
|
14
|
+
export function configPath() {
|
|
15
|
+
return join(configDir(), CONFIG_FILE);
|
|
16
|
+
}
|
|
17
|
+
export function updateCheckPath() {
|
|
18
|
+
return join(configDir(), UPDATE_CHECK_FILE);
|
|
19
|
+
}
|
package/dist/prompt.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline/promises';
|
|
2
|
+
import { stdin as input, stdout as out } from 'node:process';
|
|
3
|
+
export function silentPrompt(question) {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
process.stdout.write(question);
|
|
6
|
+
const stdin = input;
|
|
7
|
+
if (!stdin.isTTY) {
|
|
8
|
+
let buf = '';
|
|
9
|
+
stdin.setEncoding('utf8');
|
|
10
|
+
stdin.on('data', (chunk) => (buf += chunk));
|
|
11
|
+
stdin.once('end', () => resolve(buf.replace(/\r?\n$/, '')));
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
stdin.setRawMode(true);
|
|
15
|
+
stdin.resume();
|
|
16
|
+
stdin.setEncoding('utf8');
|
|
17
|
+
let value = '';
|
|
18
|
+
const onData = (chunk) => {
|
|
19
|
+
for (const ch of chunk) {
|
|
20
|
+
const code = ch.charCodeAt(0);
|
|
21
|
+
if (ch === '\n' || ch === '\r') {
|
|
22
|
+
stdin.setRawMode(false);
|
|
23
|
+
stdin.pause();
|
|
24
|
+
stdin.removeListener('data', onData);
|
|
25
|
+
process.stdout.write('\n');
|
|
26
|
+
return resolve(value);
|
|
27
|
+
}
|
|
28
|
+
if (code === 3) {
|
|
29
|
+
stdin.setRawMode(false);
|
|
30
|
+
process.stdout.write('\n');
|
|
31
|
+
return reject(new Error('cancelled'));
|
|
32
|
+
}
|
|
33
|
+
if (code === 127 || code === 8) {
|
|
34
|
+
value = value.slice(0, -1);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
value += ch;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
stdin.on('data', onData);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
export async function visiblePrompt(question) {
|
|
45
|
+
const rl = createInterface({ input, output: out });
|
|
46
|
+
try {
|
|
47
|
+
return await rl.question(question);
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
rl.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync, chmodSync } from 'node:fs';
|
|
2
|
+
import { sessionPath } from './paths.js';
|
|
3
|
+
import { SUPABASE_URL, SUPABASE_ANON_KEY } from './constants.js';
|
|
4
|
+
export function readSession() {
|
|
5
|
+
const path = sessionPath();
|
|
6
|
+
if (!existsSync(path))
|
|
7
|
+
return null;
|
|
8
|
+
try {
|
|
9
|
+
const data = JSON.parse(readFileSync(path, 'utf8'));
|
|
10
|
+
if (!data?.access_token)
|
|
11
|
+
return null;
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function writeSession(session) {
|
|
19
|
+
const path = sessionPath();
|
|
20
|
+
writeFileSync(path, JSON.stringify(session, null, 2), { mode: 0o600 });
|
|
21
|
+
chmodSync(path, 0o600);
|
|
22
|
+
}
|
|
23
|
+
export function clearSession() {
|
|
24
|
+
const path = sessionPath();
|
|
25
|
+
if (existsSync(path))
|
|
26
|
+
writeFileSync(path, '{}', { mode: 0o600 });
|
|
27
|
+
}
|
|
28
|
+
export async function login(email, password) {
|
|
29
|
+
const res = await fetch(`${SUPABASE_URL}/auth/v1/token?grant_type=password`, {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: {
|
|
32
|
+
apikey: SUPABASE_ANON_KEY,
|
|
33
|
+
'Content-Type': 'application/json',
|
|
34
|
+
},
|
|
35
|
+
body: JSON.stringify({ email, password }),
|
|
36
|
+
});
|
|
37
|
+
if (!res.ok) {
|
|
38
|
+
const body = await res.text();
|
|
39
|
+
throw new Error(`Login failed (${res.status}): ${body}`);
|
|
40
|
+
}
|
|
41
|
+
const data = (await res.json());
|
|
42
|
+
const session = {
|
|
43
|
+
access_token: data.access_token,
|
|
44
|
+
refresh_token: data.refresh_token,
|
|
45
|
+
expires_at: Math.floor(Date.now() / 1000) + (data.expires_in ?? 3600),
|
|
46
|
+
user_id: data.user?.id,
|
|
47
|
+
email: data.user?.email,
|
|
48
|
+
};
|
|
49
|
+
writeSession(session);
|
|
50
|
+
return session;
|
|
51
|
+
}
|
|
52
|
+
async function refresh(session) {
|
|
53
|
+
const res = await fetch(`${SUPABASE_URL}/auth/v1/token?grant_type=refresh_token`, {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
headers: { apikey: SUPABASE_ANON_KEY, 'Content-Type': 'application/json' },
|
|
56
|
+
body: JSON.stringify({ refresh_token: session.refresh_token }),
|
|
57
|
+
});
|
|
58
|
+
if (!res.ok)
|
|
59
|
+
throw new Error('Session refresh failed. Run `aca login` again.');
|
|
60
|
+
const data = (await res.json());
|
|
61
|
+
const next = {
|
|
62
|
+
...session,
|
|
63
|
+
access_token: data.access_token,
|
|
64
|
+
refresh_token: data.refresh_token,
|
|
65
|
+
expires_at: Math.floor(Date.now() / 1000) + (data.expires_in ?? 3600),
|
|
66
|
+
};
|
|
67
|
+
writeSession(next);
|
|
68
|
+
return next;
|
|
69
|
+
}
|
|
70
|
+
export async function getAccessToken() {
|
|
71
|
+
const session = readSession();
|
|
72
|
+
if (!session?.access_token) {
|
|
73
|
+
throw new Error('Not logged in. Run `aca login` first.');
|
|
74
|
+
}
|
|
75
|
+
const now = Math.floor(Date.now() / 1000);
|
|
76
|
+
if (session.expires_at && session.expires_at - now < 60) {
|
|
77
|
+
const refreshed = await refresh(session);
|
|
78
|
+
return refreshed.access_token;
|
|
79
|
+
}
|
|
80
|
+
return session.access_token;
|
|
81
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { updateCheckPath } from './paths.js';
|
|
3
|
+
import { NPM_REGISTRY_URL, UPDATE_CHECK_INTERVAL_MS } from './constants.js';
|
|
4
|
+
import { readConfig } from './config.js';
|
|
5
|
+
function readCache() {
|
|
6
|
+
const path = updateCheckPath();
|
|
7
|
+
if (!existsSync(path))
|
|
8
|
+
return null;
|
|
9
|
+
try {
|
|
10
|
+
return JSON.parse(readFileSync(path, 'utf8'));
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function writeCache(cache) {
|
|
17
|
+
writeFileSync(updateCheckPath(), JSON.stringify(cache, null, 2), { mode: 0o600 });
|
|
18
|
+
}
|
|
19
|
+
function isNewer(current, latest) {
|
|
20
|
+
const c = current.split('.').map((n) => parseInt(n, 10));
|
|
21
|
+
const l = latest.split('.').map((n) => parseInt(n, 10));
|
|
22
|
+
for (let i = 0; i < 3; i++) {
|
|
23
|
+
const ci = c[i] ?? 0;
|
|
24
|
+
const li = l[i] ?? 0;
|
|
25
|
+
if (li > ci)
|
|
26
|
+
return true;
|
|
27
|
+
if (li < ci)
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
export async function maybeCheckForUpdate(currentVersion) {
|
|
33
|
+
const config = readConfig();
|
|
34
|
+
if (config.update_check_enabled === false)
|
|
35
|
+
return;
|
|
36
|
+
const cache = readCache();
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
if (cache && now - cache.last_check_at < UPDATE_CHECK_INTERVAL_MS) {
|
|
39
|
+
if (cache.latest_version && isNewer(currentVersion, cache.latest_version)) {
|
|
40
|
+
notify(currentVersion, cache.latest_version);
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const controller = new AbortController();
|
|
46
|
+
const timeout = setTimeout(() => controller.abort(), 1500);
|
|
47
|
+
const res = await fetch(NPM_REGISTRY_URL, { signal: controller.signal });
|
|
48
|
+
clearTimeout(timeout);
|
|
49
|
+
if (!res.ok)
|
|
50
|
+
return;
|
|
51
|
+
const data = (await res.json());
|
|
52
|
+
const latest = data.version;
|
|
53
|
+
if (!latest)
|
|
54
|
+
return;
|
|
55
|
+
writeCache({ last_check_at: now, latest_version: latest });
|
|
56
|
+
if (isNewer(currentVersion, latest))
|
|
57
|
+
notify(currentVersion, latest);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function notify(current, latest) {
|
|
63
|
+
process.stderr.write(`\n→ aca-cli ${latest} is available (you have ${current}). Run \`npm i -g @seguelac/aca-cli\` to update.\n Disable with \`aca config set update_check_enabled false\`.\n\n`);
|
|
64
|
+
}
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function parseList(value) {
|
|
2
|
+
if (!value)
|
|
3
|
+
return [];
|
|
4
|
+
return value
|
|
5
|
+
.split(',')
|
|
6
|
+
.map((s) => s.trim())
|
|
7
|
+
.filter(Boolean);
|
|
8
|
+
}
|
|
9
|
+
export async function readStdinIfPiped() {
|
|
10
|
+
if (process.stdin.isTTY)
|
|
11
|
+
return null;
|
|
12
|
+
let data = '';
|
|
13
|
+
for await (const chunk of process.stdin) {
|
|
14
|
+
data += chunk;
|
|
15
|
+
}
|
|
16
|
+
return data.trim() || null;
|
|
17
|
+
}
|