Skip to content

Commit fe1dfb6

Browse files
committed
fix auth issues and simplify build.
1 parent 86205b1 commit fe1dfb6

File tree

13 files changed

+455
-743
lines changed

13 files changed

+455
-743
lines changed

dist/cli.js

Lines changed: 0 additions & 142 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
"description": "Tools for interacting with Slack's local data",
55
"type": "module",
66
"bin": {
7-
"slack-tools": "./dist/cli.js"
7+
"slack-tools": "./src/cli.ts"
88
},
99
"scripts": {
10-
"build": "tsup",
1110
"start": "tsx src/cli.ts",
1211
"test": "vitest run",
1312
"test:watch": "vitest",

src/auth.ts

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env node
1+
#!/usr/bin/env -S npx tsx
22

33
import { Command } from 'commander';
44
import { readFileSync } from 'fs';

src/commands/mcp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function registerMcpCommand(program: Command, context: CommandContext): v
1818

1919
// Import package.json version from the process
2020
const packageVersion = process.env.npm_package_version || '1.0.2';
21-
21+
2222
const server = new McpServer({
2323
name: 'slack-tools-server',
2424
version: packageVersion,

src/commands/print.ts

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Command } from 'commander';
2-
import { getSlackAuth } from '../auth';
2+
import { findWorkspaceToken } from '../slack-api';
33
import { CommandContext } from '../context';
4+
import { getStoredAuth } from '../keychain.js';
5+
import { getCookie } from '../cookies.js';
6+
import { getTokens } from '../tokens.js';
47

58
export function registerPrintCommand(program: Command, context: CommandContext): void {
69
program
@@ -13,45 +16,80 @@ export function registerPrintCommand(program: Command, context: CommandContext):
1316
console.log('Getting Slack credentials...');
1417
}
1518

16-
// For print command, we want to work even without a workspace selected
17-
// But if a workspace is set, we'll use it to filter results
18-
const auth = await getSlackAuth({
19-
workspace: context.hasWorkspace ? context.workspace : undefined,
20-
quiet: cmdOptions.quiet,
21-
});
22-
23-
if (Object.keys(auth.tokens).length === 0) {
24-
console.error('Error: No tokens found.');
25-
if (context.hasWorkspace) {
26-
console.error(`No workspace matching "${context.workspace}" was found.`);
27-
}
28-
process.exit(1);
29-
}
19+
// If no workspace is specified, we'll default to one to get credentials
20+
const defaultWorkspace = context.hasWorkspace ? context.workspace : 'default';
3021

31-
if (!cmdOptions.quiet) {
32-
console.log('\nFound tokens for workspaces:\n');
22+
// Get the auth object first
23+
const storedAuth = await getStoredAuth();
24+
if (!cmdOptions.quiet && !storedAuth) {
25+
console.log('No stored auth found, fetching fresh credentials...');
3326
}
3427

35-
for (const [url, details] of Object.entries(auth.tokens)) {
36-
if (cmdOptions.quiet) {
37-
console.log(`${details.token}`);
28+
const auth = storedAuth || {
29+
cookie: await getCookie(),
30+
tokens: await getTokens(context),
31+
};
32+
33+
try {
34+
const { token, cookie, workspaceUrl } = findWorkspaceToken(
35+
auth,
36+
defaultWorkspace,
37+
context,
38+
);
39+
40+
if (!cmdOptions.quiet) {
41+
console.log('\nFound token for workspace:\n');
42+
console.log(`Workspace URL: ${workspaceUrl}`);
43+
console.log(`Token: ${token}\n`);
44+
45+
console.log('Found cookie:');
46+
console.log(`${cookie.name}: ${cookie.value}\n`);
3847
} else {
39-
console.log(`${details.name} (${url})`);
40-
console.log(`Token: ${details.token}\n`);
48+
console.log(token);
49+
console.log(cookie.value);
4150
}
42-
}
51+
} catch (error) {
52+
// If the specified workspace isn't found and we're using a default,
53+
// we'll just use any available token
54+
if (!context.hasWorkspace) {
55+
try {
56+
// Try with an empty string to get any available workspace
57+
const firstWorkspaceKey = Object.keys(auth.tokens)[0];
58+
59+
if (!firstWorkspaceKey) {
60+
throw new Error('No workspaces available');
61+
}
62+
63+
const { token, cookie, workspaceUrl } = findWorkspaceToken(
64+
auth,
65+
firstWorkspaceKey, // Use the first workspace key instead of empty string
66+
context,
67+
);
4368

44-
if (cmdOptions.quiet) {
45-
console.log(`${auth.cookie.value}`);
46-
} else {
47-
console.log('Found cookie:');
48-
console.log(`${auth.cookie.name}: ${auth.cookie.value}\n`);
49-
50-
// Print guidance about workspace selection if we showed multiple workspaces
51-
if (Object.keys(auth.tokens).length > 1 && !context.hasWorkspace) {
52-
console.log('\nTip: To filter results for a specific workspace, use one of:');
53-
console.log(' - Use -w, --workspace <workspace> to specify a workspace directly');
54-
console.log(' - Use -l, --last-workspace to use your most recently used workspace');
69+
if (!cmdOptions.quiet) {
70+
console.log('\nFound token for workspace:\n');
71+
console.log(`Workspace URL: ${workspaceUrl}`);
72+
console.log(`Token: ${token}\n`);
73+
74+
console.log('Found cookie:');
75+
console.log(`${cookie.name}: ${cookie.value}\n`);
76+
77+
console.log('\nTip: To specify a workspace directly, use:');
78+
console.log(' - Use -w, --workspace <workspace> to specify a workspace');
79+
console.log(
80+
' - Use -l, --last-workspace to use your most recently used workspace',
81+
);
82+
} else {
83+
console.log(token);
84+
console.log(cookie.value);
85+
}
86+
} catch (innerError) {
87+
console.error('Error getting any workspace token:', innerError);
88+
process.exit(1);
89+
}
90+
} else {
91+
console.error(`Error getting workspace "${context.workspace}":`, error);
92+
process.exit(1);
5593
}
5694
}
5795
} catch (error) {

src/keychain.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export async function getStoredAuth(): Promise<SlackAuth | null> {
4848
return null;
4949
}
5050

51+
if (!cookie) {
52+
return null;
53+
}
54+
5155
return { tokens, cookie };
5256
} catch (error) {
5357
console.error('Failed to read auth from keychain:', error);

0 commit comments

Comments
 (0)