@atlassian-dc-mcp/common 0.16.0 → 0.17.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/CHANGELOG.md +23 -0
- package/build/__tests__/runtime-config.test.js +65 -14
- package/build/__tests__/runtime-config.test.js.map +1 -1
- package/build/__tests__/setup-cli.test.d.ts +2 -0
- package/build/__tests__/setup-cli.test.d.ts.map +1 -0
- package/build/__tests__/setup-cli.test.js +166 -0
- package/build/__tests__/setup-cli.test.js.map +1 -0
- package/build/config/__tests__/registry.test.d.ts +2 -0
- package/build/config/__tests__/registry.test.d.ts.map +1 -0
- package/build/config/__tests__/registry.test.js +105 -0
- package/build/config/__tests__/registry.test.js.map +1 -0
- package/build/config/__tests__/resolve-base.test.d.ts +2 -0
- package/build/config/__tests__/resolve-base.test.d.ts.map +1 -0
- package/build/config/__tests__/resolve-base.test.js +89 -0
- package/build/config/__tests__/resolve-base.test.js.map +1 -0
- package/build/config/__tests__/sources/env-file.test.d.ts +2 -0
- package/build/config/__tests__/sources/env-file.test.d.ts.map +1 -0
- package/build/config/__tests__/sources/env-file.test.js +93 -0
- package/build/config/__tests__/sources/env-file.test.js.map +1 -0
- package/build/config/__tests__/sources/home-file.test.d.ts +2 -0
- package/build/config/__tests__/sources/home-file.test.d.ts.map +1 -0
- package/build/config/__tests__/sources/home-file.test.js +114 -0
- package/build/config/__tests__/sources/home-file.test.js.map +1 -0
- package/build/config/__tests__/sources/macos-keychain.test.d.ts +2 -0
- package/build/config/__tests__/sources/macos-keychain.test.d.ts.map +1 -0
- package/build/config/__tests__/sources/macos-keychain.test.js +109 -0
- package/build/config/__tests__/sources/macos-keychain.test.js.map +1 -0
- package/build/config/__tests__/sources/process-env.test.d.ts +2 -0
- package/build/config/__tests__/sources/process-env.test.d.ts.map +1 -0
- package/build/config/__tests__/sources/process-env.test.js +51 -0
- package/build/config/__tests__/sources/process-env.test.js.map +1 -0
- package/build/config/index.d.ts +9 -0
- package/build/config/index.d.ts.map +1 -0
- package/build/config/index.js +9 -0
- package/build/config/index.js.map +1 -0
- package/build/config/registry.d.ts +26 -0
- package/build/config/registry.d.ts.map +1 -0
- package/build/config/registry.js +51 -0
- package/build/config/registry.js.map +1 -0
- package/build/config/resolve-base.d.ts +8 -0
- package/build/config/resolve-base.d.ts.map +1 -0
- package/build/config/resolve-base.js +31 -0
- package/build/config/resolve-base.js.map +1 -0
- package/build/config/runtime-config.d.ts +14 -0
- package/build/config/runtime-config.d.ts.map +1 -0
- package/build/config/runtime-config.js +41 -0
- package/build/config/runtime-config.js.map +1 -0
- package/build/config/source.d.ts +28 -0
- package/build/config/source.d.ts.map +1 -0
- package/build/config/source.js +11 -0
- package/build/config/source.js.map +1 -0
- package/build/config/sources/dotenv-file-cache.d.ts +12 -0
- package/build/config/sources/dotenv-file-cache.d.ts.map +1 -0
- package/build/config/sources/dotenv-file-cache.js +41 -0
- package/build/config/sources/dotenv-file-cache.js.map +1 -0
- package/build/config/sources/env-file.d.ts +16 -0
- package/build/config/sources/env-file.d.ts.map +1 -0
- package/build/config/sources/env-file.js +48 -0
- package/build/config/sources/env-file.js.map +1 -0
- package/build/config/sources/home-file.d.ts +19 -0
- package/build/config/sources/home-file.d.ts.map +1 -0
- package/build/config/sources/home-file.js +102 -0
- package/build/config/sources/home-file.js.map +1 -0
- package/build/config/sources/macos-keychain.d.ts +26 -0
- package/build/config/sources/macos-keychain.d.ts.map +1 -0
- package/build/config/sources/macos-keychain.js +88 -0
- package/build/config/sources/macos-keychain.js.map +1 -0
- package/build/config/sources/process-env.d.ts +8 -0
- package/build/config/sources/process-env.d.ts.map +1 -0
- package/build/config/sources/process-env.js +12 -0
- package/build/config/sources/process-env.js.map +1 -0
- package/build/index.d.ts +2 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -1
- package/build/index.js.map +1 -1
- package/build/setup-cli.d.ts +25 -0
- package/build/setup-cli.d.ts.map +1 -0
- package/build/setup-cli.js +215 -0
- package/build/setup-cli.js.map +1 -0
- package/package.json +5 -4
- package/src/__tests__/runtime-config.test.ts +75 -27
- package/src/__tests__/setup-cli.test.ts +175 -0
- package/src/config/__tests__/registry.test.ts +118 -0
- package/src/config/__tests__/resolve-base.test.ts +101 -0
- package/src/config/__tests__/sources/env-file.test.ts +113 -0
- package/src/config/__tests__/sources/home-file.test.ts +129 -0
- package/src/config/__tests__/sources/macos-keychain.test.ts +136 -0
- package/src/config/__tests__/sources/process-env.test.ts +59 -0
- package/src/config/index.ts +8 -0
- package/src/config/registry.ts +79 -0
- package/src/config/resolve-base.ts +43 -0
- package/src/config/runtime-config.ts +60 -0
- package/src/config/source.ts +37 -0
- package/src/config/sources/dotenv-file-cache.ts +54 -0
- package/src/config/sources/env-file.ts +61 -0
- package/src/config/sources/home-file.ts +121 -0
- package/src/config/sources/macos-keychain.ts +117 -0
- package/src/config/sources/process-env.ts +15 -0
- package/src/index.ts +2 -1
- package/src/setup-cli.ts +308 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/runtime-config.d.ts +0 -14
- package/build/runtime-config.d.ts.map +0 -1
- package/build/runtime-config.js +0 -123
- package/build/runtime-config.js.map +0 -1
- package/src/runtime-config.ts +0 -176
package/src/setup-cli.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { confirm as inquirerConfirm, input as inquirerInput, password as inquirerPassword } from '@inquirer/prompts';
|
|
2
|
+
import { buildDefaultRegistry, type ConfigRegistry } from './config/registry.js';
|
|
3
|
+
import { getProductRuntimeConfig } from './config/runtime-config.js';
|
|
4
|
+
import type {
|
|
5
|
+
ConfigKey,
|
|
6
|
+
ProductDefinition,
|
|
7
|
+
WritableSource,
|
|
8
|
+
} from './config/source.js';
|
|
9
|
+
import { HomeFileSource, getHomeFilePath } from './config/sources/home-file.js';
|
|
10
|
+
import { MacosKeychainSource } from './config/sources/macos-keychain.js';
|
|
11
|
+
|
|
12
|
+
const FALLBACK_PAGE_SIZE = 25;
|
|
13
|
+
|
|
14
|
+
type PromptResult = {
|
|
15
|
+
host: string;
|
|
16
|
+
apiBasePath: string;
|
|
17
|
+
defaultPageSize: string;
|
|
18
|
+
token: string | undefined;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type SetupPrompts = {
|
|
22
|
+
input: (opts: { message: string; default?: string; validate?: (raw: string) => true | string }) => Promise<string>;
|
|
23
|
+
password: (opts: { message: string; mask?: string }) => Promise<string>;
|
|
24
|
+
confirm: (opts: { message: string; default?: boolean }) => Promise<boolean>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type SetupDeps = {
|
|
28
|
+
registry?: ConfigRegistry;
|
|
29
|
+
log?: (message: string) => void;
|
|
30
|
+
exit?: (code: number) => void;
|
|
31
|
+
prompts?: SetupPrompts;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const DEFAULT_PROMPTS: SetupPrompts = {
|
|
35
|
+
input: (opts) => inquirerInput(opts as any),
|
|
36
|
+
password: (opts) => inquirerPassword(opts as any),
|
|
37
|
+
confirm: (opts) => inquirerConfirm(opts as any),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export async function runSetup(product: ProductDefinition, deps: SetupDeps = {}): Promise<void> {
|
|
41
|
+
const registry = deps.registry ?? buildDefaultRegistry();
|
|
42
|
+
const log = deps.log ?? ((message: string) => process.stdout.write(`${message}\n`));
|
|
43
|
+
const exit = deps.exit ?? ((code: number) => { process.exit(code); });
|
|
44
|
+
const prompts = deps.prompts ?? DEFAULT_PROMPTS;
|
|
45
|
+
|
|
46
|
+
registry.initialize();
|
|
47
|
+
|
|
48
|
+
log(`Atlassian DC MCP setup — ${product.id}`);
|
|
49
|
+
log('');
|
|
50
|
+
|
|
51
|
+
const current = getProductRuntimeConfig(product);
|
|
52
|
+
printCurrent(log, registry, product, current);
|
|
53
|
+
|
|
54
|
+
let answers: PromptResult;
|
|
55
|
+
try {
|
|
56
|
+
answers = await promptForValues(prompts, product, current);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (isUserCancel(error)) {
|
|
59
|
+
exit(130);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const homeFile = requireHomeFile(registry);
|
|
66
|
+
writeNonSecretFields(registry, product, answers, homeFile, log);
|
|
67
|
+
const tokenWriter = await writeToken(registry, product, answers.token, homeFile, log, prompts);
|
|
68
|
+
printSummary(log, product, answers, tokenWriter);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function requireHomeFile(registry: ConfigRegistry): HomeFileSource {
|
|
72
|
+
const homeFile = registry.getWritableSource(
|
|
73
|
+
(s): s is HomeFileSource => s instanceof HomeFileSource,
|
|
74
|
+
);
|
|
75
|
+
if (!homeFile) {
|
|
76
|
+
throw new Error('No writable source available for non-secret fields');
|
|
77
|
+
}
|
|
78
|
+
return homeFile;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function printCurrent(
|
|
82
|
+
log: (message: string) => void,
|
|
83
|
+
registry: ConfigRegistry,
|
|
84
|
+
product: ProductDefinition,
|
|
85
|
+
current: ReturnType<typeof getProductRuntimeConfig>,
|
|
86
|
+
): void {
|
|
87
|
+
const keys: ConfigKey[] = ['host', 'apiBasePath', 'token', 'defaultPageSize'];
|
|
88
|
+
for (const key of keys) {
|
|
89
|
+
const primary = registry.locate(product, key)[0];
|
|
90
|
+
const label = displayLabel(key);
|
|
91
|
+
const displayValue = key === 'token' ? maskToken(current.token) : readable(current[key]);
|
|
92
|
+
const origin = primary ? `from ${primary.detail ?? primary.sourceId}` : 'not set';
|
|
93
|
+
log(`Current ${label}: ${displayValue} (${origin})`);
|
|
94
|
+
}
|
|
95
|
+
log('');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function promptForValues(
|
|
99
|
+
prompts: SetupPrompts,
|
|
100
|
+
product: ProductDefinition,
|
|
101
|
+
current: ReturnType<typeof getProductRuntimeConfig>,
|
|
102
|
+
): Promise<PromptResult> {
|
|
103
|
+
const host = await prompts.input({
|
|
104
|
+
message: 'Host (e.g. jira.example.com):',
|
|
105
|
+
default: current.host ?? '',
|
|
106
|
+
});
|
|
107
|
+
const apiBasePath = await prompts.input({
|
|
108
|
+
message: 'API base path:',
|
|
109
|
+
default: current.apiBasePath ?? product.defaultApiBasePath ?? '',
|
|
110
|
+
});
|
|
111
|
+
const defaultPageSize = await prompts.input({
|
|
112
|
+
message: 'Default page size:',
|
|
113
|
+
default: String(current.defaultPageSize ?? FALLBACK_PAGE_SIZE),
|
|
114
|
+
validate: (raw) =>
|
|
115
|
+
/^\d+$/.test(raw.trim()) && Number.parseInt(raw.trim(), 10) > 0
|
|
116
|
+
? true
|
|
117
|
+
: 'Enter a positive integer',
|
|
118
|
+
});
|
|
119
|
+
const token = await promptForToken(prompts, current.token);
|
|
120
|
+
return { host: host.trim(), apiBasePath: apiBasePath.trim(), defaultPageSize: defaultPageSize.trim(), token };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function promptForToken(
|
|
124
|
+
prompts: SetupPrompts,
|
|
125
|
+
existing: string | undefined,
|
|
126
|
+
): Promise<string | undefined> {
|
|
127
|
+
const entered = await prompts.password({ message: 'API token:', mask: '*' });
|
|
128
|
+
const trimmed = entered.trim();
|
|
129
|
+
if (trimmed.length > 0) {
|
|
130
|
+
return trimmed;
|
|
131
|
+
}
|
|
132
|
+
if (!existing) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
await prompts.confirm({ message: 'Keep existing token?', default: true });
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function writeNonSecretFields(
|
|
140
|
+
registry: ConfigRegistry,
|
|
141
|
+
product: ProductDefinition,
|
|
142
|
+
answers: PromptResult,
|
|
143
|
+
homeFile: HomeFileSource,
|
|
144
|
+
log: (message: string) => void,
|
|
145
|
+
): void {
|
|
146
|
+
for (const key of ['host', 'apiBasePath', 'defaultPageSize'] as const) {
|
|
147
|
+
warnIfShadowed(registry, product, key, homeFile, log);
|
|
148
|
+
const value = answers[key];
|
|
149
|
+
if (value.length > 0) {
|
|
150
|
+
homeFile.write(product, key, value);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function writeToken(
|
|
156
|
+
registry: ConfigRegistry,
|
|
157
|
+
product: ProductDefinition,
|
|
158
|
+
token: string | undefined,
|
|
159
|
+
homeFile: HomeFileSource,
|
|
160
|
+
log: (message: string) => void,
|
|
161
|
+
prompts: SetupPrompts,
|
|
162
|
+
): Promise<WritableSource | undefined> {
|
|
163
|
+
if (!token) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const keychain = registry.getWritableSource(
|
|
168
|
+
(s): s is MacosKeychainSource => s instanceof MacosKeychainSource,
|
|
169
|
+
);
|
|
170
|
+
const candidates: WritableSource[] = [];
|
|
171
|
+
if (keychain?.isAvailable()) {
|
|
172
|
+
candidates.push(keychain);
|
|
173
|
+
}
|
|
174
|
+
candidates.push(homeFile);
|
|
175
|
+
|
|
176
|
+
warnIfShadowed(registry, product, 'token', candidates[0], log);
|
|
177
|
+
|
|
178
|
+
for (const writer of candidates) {
|
|
179
|
+
const written = await tryWrite(writer, product, token, log, prompts);
|
|
180
|
+
if (written) {
|
|
181
|
+
if (writer instanceof MacosKeychainSource) {
|
|
182
|
+
homeFile.clear(product, 'token');
|
|
183
|
+
}
|
|
184
|
+
return writer;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
throw new Error('No writable source succeeded for token');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function tryWrite(
|
|
191
|
+
writer: WritableSource,
|
|
192
|
+
product: ProductDefinition,
|
|
193
|
+
token: string,
|
|
194
|
+
log: (message: string) => void,
|
|
195
|
+
prompts: SetupPrompts,
|
|
196
|
+
): Promise<boolean> {
|
|
197
|
+
try {
|
|
198
|
+
writer.write(product, 'token', token);
|
|
199
|
+
return true;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
const stderr = (error as { stderr?: string | Buffer }).stderr?.toString() ?? '';
|
|
202
|
+
log(`Failed to write token to ${writer.describe()}: ${(error as Error).message}`);
|
|
203
|
+
if (stderr) {
|
|
204
|
+
log(stderr.trim());
|
|
205
|
+
}
|
|
206
|
+
if (writer instanceof MacosKeychainSource) {
|
|
207
|
+
const fallback = await prompts.confirm({
|
|
208
|
+
message: 'Fall back to plaintext home file with mode 0600?',
|
|
209
|
+
default: false,
|
|
210
|
+
});
|
|
211
|
+
return !fallback;
|
|
212
|
+
}
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function warnIfShadowed(
|
|
218
|
+
registry: ConfigRegistry,
|
|
219
|
+
product: ProductDefinition,
|
|
220
|
+
key: ConfigKey,
|
|
221
|
+
target: WritableSource,
|
|
222
|
+
log: (message: string) => void,
|
|
223
|
+
): void {
|
|
224
|
+
const shadowing = registry
|
|
225
|
+
.locate(product, key)
|
|
226
|
+
.filter((loc) => priorityOf(registry, loc.sourceId) > target.priority);
|
|
227
|
+
if (shadowing.length === 0) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const envVar = product.envVars[key];
|
|
231
|
+
for (const loc of shadowing) {
|
|
232
|
+
if (loc.sourceId === 'process-env') {
|
|
233
|
+
log(`Warning: process.env[${envVar}] is set — runtime will prefer it over the value you're saving to ${target.describe()}. Unset it to use the new value.`);
|
|
234
|
+
} else {
|
|
235
|
+
log(`Warning: ${loc.detail ?? loc.sourceId} provides ${envVar} — runtime will prefer it over the value you're saving to ${target.describe()}.`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function priorityOf(registry: ConfigRegistry, sourceId: string): number {
|
|
241
|
+
const match = registry.sources.find((s) => s.id === sourceId);
|
|
242
|
+
return match?.priority ?? 0;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function printSummary(
|
|
246
|
+
log: (message: string) => void,
|
|
247
|
+
product: ProductDefinition,
|
|
248
|
+
answers: PromptResult,
|
|
249
|
+
tokenWriter: WritableSource | undefined,
|
|
250
|
+
): void {
|
|
251
|
+
log('');
|
|
252
|
+
log('Saved configuration:');
|
|
253
|
+
log(` host: ${answers.host || '(unchanged)'}`);
|
|
254
|
+
log(` apiBasePath: ${answers.apiBasePath || '(unchanged)'}`);
|
|
255
|
+
log(` defaultPageSize: ${answers.defaultPageSize || '(unchanged)'}`);
|
|
256
|
+
if (tokenWriter) {
|
|
257
|
+
log(` token: ${maskToken(answers.token)} (stored in ${describeWriter(tokenWriter, product)})`);
|
|
258
|
+
} else {
|
|
259
|
+
log(' token: (unchanged)');
|
|
260
|
+
}
|
|
261
|
+
log(`Home file: ${getHomeFilePath(product)}`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function describeWriter(writer: WritableSource, product: ProductDefinition): string {
|
|
265
|
+
if (writer instanceof MacosKeychainSource) {
|
|
266
|
+
return `macOS Keychain (service atlassian-dc-mcp, account ${product.id}-token)`;
|
|
267
|
+
}
|
|
268
|
+
if (writer instanceof HomeFileSource) {
|
|
269
|
+
return writer.describeForProduct(product);
|
|
270
|
+
}
|
|
271
|
+
return writer.describe();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function displayLabel(key: ConfigKey): string {
|
|
275
|
+
switch (key) {
|
|
276
|
+
case 'host':
|
|
277
|
+
return 'host';
|
|
278
|
+
case 'apiBasePath':
|
|
279
|
+
return 'API base path';
|
|
280
|
+
case 'token':
|
|
281
|
+
return 'token';
|
|
282
|
+
case 'defaultPageSize':
|
|
283
|
+
return 'page size';
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function readable(value: string | number | undefined): string {
|
|
288
|
+
if (value === undefined || value === '') {
|
|
289
|
+
return '(not set)';
|
|
290
|
+
}
|
|
291
|
+
return String(value);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function maskToken(token: string | undefined): string {
|
|
295
|
+
if (!token) {
|
|
296
|
+
return '(not set)';
|
|
297
|
+
}
|
|
298
|
+
const last4 = token.slice(-4);
|
|
299
|
+
return `••••${last4}`;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function isUserCancel(error: unknown): boolean {
|
|
303
|
+
if (!error || typeof error !== 'object') {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
const err = error as { name?: string; message?: string };
|
|
307
|
+
return err.name === 'ExitPromptError' || /force closed/i.test(err.message ?? '');
|
|
308
|
+
}
|