Skip to content

Commit 2b51743

Browse files
committed
feat: single config point — VS Code extension syncs credentials to CLI store
- configuration-webview: call ConfigService.saveApiKey() on Save so the CLI global store (~/.config/n8nac/credentials.json) is populated automatically - lib.ts: export ConfigService so the extension can consume it via n8nac - base.ts: fallback to N8N_HOST / N8N_API_KEY env vars when Conf store is empty (supports env-based usage without `n8nac init`) Users now only need to configure credentials once in VS Code; `npx n8nac <cmd>` works without a separate `n8nac init`.
1 parent 3d849f3 commit 2b51743

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

packages/cli/src/commands/base.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ export class BaseCommand {
1111
constructor() {
1212
this.configService = new ConfigService();
1313
const localConfig = this.configService.getLocalConfig();
14-
const apiKey = localConfig.host ? this.configService.getApiKey(localConfig.host) : undefined;
1514

16-
if (!localConfig.host || !apiKey) {
15+
// Resolve host: local config → env var
16+
const host = localConfig.host || process.env.N8N_HOST?.replace(/^'|'$/g, '') || '';
17+
18+
// Resolve API key: global Conf store → env var
19+
const apiKey = (host ? this.configService.getApiKey(host) : undefined)
20+
|| process.env.N8N_API_KEY
21+
|| '';
22+
23+
if (!host || !apiKey) {
1724
console.error(chalk.red('❌ CLI not configured.'));
18-
console.error(chalk.yellow('Please run `n8nac init` to set up your environment.'));
25+
console.error(chalk.yellow('Please run `n8nac init` to set up your environment, or set N8N_HOST and N8N_API_KEY environment variables.'));
1926
process.exit(1);
2027
}
2128

2229
const credentials: IN8nCredentials = {
23-
host: localConfig.host,
24-
apiKey: apiKey
30+
host,
31+
apiKey
2532
};
2633

2734
this.client = new N8nApiClient(credentials);
@@ -31,7 +38,7 @@ export class BaseCommand {
3138
directory: localConfig.syncFolder || './workflows',
3239
syncInactive: true,
3340
ignoredTags: [],
34-
host: localConfig.host
41+
host
3542
};
3643
}
3744

packages/cli/src/lib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
* so consumers can simply change their import path without touching business logic.
55
*/
66
export * from './core/index.js';
7+
export { ConfigService } from './services/config-service.js';

packages/vscode-extension/src/ui/configuration-webview.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import { N8nApiClient, type IN8nCredentials } from 'n8nac';
2+
import { N8nApiClient, ConfigService, type IN8nCredentials } from 'n8nac';
33
import { getWorkspaceRoot, isFolderPreviouslyInitialized, getExistingInstanceIdentifier } from '../utils/state-detection.js';
44
import fs from 'fs';
55
import path from 'path';
@@ -129,6 +129,12 @@ export class ConfigurationWebview {
129129
await config.update('projectName', projectName, vscode.ConfigurationTarget.Workspace);
130130
}
131131

132+
// Sync API key to CLI global store so the CLI works without `n8nac init`
133+
if (host && apiKey) {
134+
const configService = new ConfigService();
135+
configService.saveApiKey(host, apiKey);
136+
}
137+
132138
// Write unified config file for CLI alignment
133139
if (workspaceRoot) {
134140
const unifiedPath = path.join(workspaceRoot, 'n8nac-config.json');

0 commit comments

Comments
 (0)