appstash 0.4.0 → 0.6.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/config-store.d.ts CHANGED
@@ -1,6 +1,10 @@
1
+ export interface ContextTargetEndpoint {
2
+ endpoint: string;
3
+ }
1
4
  export interface ContextConfig {
2
5
  name: string;
3
6
  endpoint: string;
7
+ targets?: Record<string, ContextTargetEndpoint>;
4
8
  createdAt: string;
5
9
  updatedAt: string;
6
10
  }
@@ -18,20 +22,31 @@ export interface GlobalSettings {
18
22
  export interface ConfigStoreOptions {
19
23
  baseDir?: string;
20
24
  }
25
+ export interface ClientConfig {
26
+ endpoint: string;
27
+ headers: Record<string, string>;
28
+ }
21
29
  export interface ConfigStore {
22
30
  loadSettings(): GlobalSettings;
23
31
  saveSettings(settings: GlobalSettings): void;
24
32
  createContext(name: string, options: {
25
33
  endpoint: string;
34
+ targets?: Record<string, ContextTargetEndpoint>;
26
35
  }): ContextConfig;
27
36
  loadContext(name: string): ContextConfig | null;
28
37
  listContexts(): ContextConfig[];
29
38
  deleteContext(name: string): boolean;
30
39
  getCurrentContext(): ContextConfig | null;
31
40
  setCurrentContext(name: string): boolean;
41
+ getTargetEndpoint(targetName: string, contextName?: string): string | null;
32
42
  setCredentials(contextName: string, creds: ContextCredentials): void;
33
43
  getCredentials(contextName: string): ContextCredentials | null;
34
44
  removeCredentials(contextName: string): boolean;
35
45
  hasValidCredentials(contextName: string): boolean;
46
+ setVar(key: string, value: string, contextName?: string): void;
47
+ getVar(key: string, contextName?: string): string | null;
48
+ deleteVar(key: string, contextName?: string): boolean;
49
+ listVars(contextName?: string): Record<string, string>;
50
+ getClientConfig(targetName: string, contextName?: string): ClientConfig;
36
51
  }
37
52
  export declare function createConfigStore(toolName: string, options?: ConfigStoreOptions): ConfigStore;
package/config-store.js CHANGED
@@ -37,17 +37,16 @@ exports.createConfigStore = createConfigStore;
37
37
  const fs = __importStar(require("fs"));
38
38
  const path = __importStar(require("path"));
39
39
  const index_1 = require("./index");
40
- const DEFAULT_SETTINGS = {};
41
40
  function readJson(filePath, fallback) {
42
41
  if (fs.existsSync(filePath)) {
43
42
  try {
44
43
  return JSON.parse(fs.readFileSync(filePath, 'utf8'));
45
44
  }
46
45
  catch {
47
- return fallback;
46
+ return JSON.parse(JSON.stringify(fallback));
48
47
  }
49
48
  }
50
- return fallback;
49
+ return JSON.parse(JSON.stringify(fallback));
51
50
  }
52
51
  function writeJson(filePath, data, mode) {
53
52
  const dir = path.dirname(filePath);
@@ -73,7 +72,7 @@ function createConfigStore(toolName, options) {
73
72
  return path.join(contextsDir, `${name}.json`);
74
73
  }
75
74
  function loadSettings() {
76
- return readJson(settingsPath(), DEFAULT_SETTINGS);
75
+ return readJson(settingsPath(), {});
77
76
  }
78
77
  function saveSettings(settings) {
79
78
  writeJson(settingsPath(), settings);
@@ -89,6 +88,9 @@ function createConfigStore(toolName, options) {
89
88
  createdAt: now,
90
89
  updatedAt: now,
91
90
  };
91
+ if (options.targets) {
92
+ context.targets = options.targets;
93
+ }
92
94
  writeJson(contextPath(name), context);
93
95
  return context;
94
96
  }
@@ -177,6 +179,108 @@ function createConfigStore(toolName, options) {
177
179
  }
178
180
  return true;
179
181
  }
182
+ function getTargetEndpoint(targetName, contextName) {
183
+ let ctx;
184
+ if (contextName) {
185
+ ctx = loadContext(contextName);
186
+ }
187
+ else {
188
+ ctx = getCurrentContext();
189
+ }
190
+ if (!ctx)
191
+ return null;
192
+ if (ctx.targets && ctx.targets[targetName]) {
193
+ return ctx.targets[targetName].endpoint;
194
+ }
195
+ return ctx.endpoint;
196
+ }
197
+ function varsPath(ctxName) {
198
+ const varsDir = (0, index_1.resolve)(dirs, 'config', 'vars');
199
+ if (!fs.existsSync(varsDir)) {
200
+ fs.mkdirSync(varsDir, { recursive: true });
201
+ }
202
+ return path.join(varsDir, `${ctxName}.json`);
203
+ }
204
+ function loadVars(ctxName) {
205
+ return readJson(varsPath(ctxName), {});
206
+ }
207
+ function saveVars(ctxName, vars) {
208
+ writeJson(varsPath(ctxName), vars);
209
+ }
210
+ function resolveContextName(contextName) {
211
+ if (contextName)
212
+ return contextName;
213
+ const settings = loadSettings();
214
+ return settings.currentContext || null;
215
+ }
216
+ function setVar(key, value, contextName) {
217
+ const ctxName = resolveContextName(contextName);
218
+ if (!ctxName) {
219
+ throw new Error('No active context. Run "context create" or "context use" first.');
220
+ }
221
+ const vars = loadVars(ctxName);
222
+ vars[key] = value;
223
+ saveVars(ctxName, vars);
224
+ }
225
+ function getVar(key, contextName) {
226
+ const ctxName = resolveContextName(contextName);
227
+ if (!ctxName)
228
+ return null;
229
+ const vars = loadVars(ctxName);
230
+ return vars[key] ?? null;
231
+ }
232
+ function deleteVar(key, contextName) {
233
+ const ctxName = resolveContextName(contextName);
234
+ if (!ctxName)
235
+ return false;
236
+ const vars = loadVars(ctxName);
237
+ if (key in vars) {
238
+ delete vars[key];
239
+ saveVars(ctxName, vars);
240
+ return true;
241
+ }
242
+ return false;
243
+ }
244
+ function listVars(contextName) {
245
+ const ctxName = resolveContextName(contextName);
246
+ if (!ctxName)
247
+ return {};
248
+ return loadVars(ctxName);
249
+ }
250
+ function getClientConfig(targetName, contextName) {
251
+ const envPrefix = toolName.toUpperCase().replace(/[^A-Z0-9]/g, '_');
252
+ const targetSuffix = targetName.toUpperCase().replace(/[^A-Z0-9]/g, '_');
253
+ // Tier 1: Try appstash store
254
+ const ctx = contextName ? loadContext(contextName) : getCurrentContext();
255
+ if (ctx) {
256
+ const endpoint = getTargetEndpoint(targetName, ctx.name);
257
+ const headers = {};
258
+ if (hasValidCredentials(ctx.name)) {
259
+ const creds = getCredentials(ctx.name);
260
+ if (creds?.token) {
261
+ headers['Authorization'] = `Bearer ${creds.token}`;
262
+ }
263
+ }
264
+ if (endpoint) {
265
+ return { endpoint, headers };
266
+ }
267
+ }
268
+ // Tier 2: Try env vars
269
+ const envToken = process.env[`${envPrefix}_TOKEN`];
270
+ const envEndpoint = process.env[`${envPrefix}_${targetSuffix}_ENDPOINT`] ||
271
+ process.env[`${envPrefix}_ENDPOINT`];
272
+ if (envEndpoint) {
273
+ const headers = {};
274
+ if (envToken) {
275
+ headers['Authorization'] = `Bearer ${envToken}`;
276
+ }
277
+ return { endpoint: envEndpoint, headers };
278
+ }
279
+ // Tier 3: Throw with actionable error
280
+ throw new Error(`No configuration found for target "${targetName}". ` +
281
+ `Set up a context with "${toolName} context create" and authenticate with "${toolName} auth", ` +
282
+ `or set ${envPrefix}_${targetSuffix}_ENDPOINT and ${envPrefix}_TOKEN environment variables.`);
283
+ }
180
284
  return {
181
285
  loadSettings,
182
286
  saveSettings,
@@ -186,9 +290,15 @@ function createConfigStore(toolName, options) {
186
290
  deleteContext,
187
291
  getCurrentContext,
188
292
  setCurrentContext,
293
+ getTargetEndpoint,
189
294
  setCredentials,
190
295
  getCredentials,
191
296
  removeCredentials,
192
297
  hasValidCredentials,
298
+ setVar,
299
+ getVar,
300
+ deleteVar,
301
+ listVars,
302
+ getClientConfig,
193
303
  };
194
304
  }
@@ -1,6 +1,10 @@
1
+ export interface ContextTargetEndpoint {
2
+ endpoint: string;
3
+ }
1
4
  export interface ContextConfig {
2
5
  name: string;
3
6
  endpoint: string;
7
+ targets?: Record<string, ContextTargetEndpoint>;
4
8
  createdAt: string;
5
9
  updatedAt: string;
6
10
  }
@@ -18,20 +22,31 @@ export interface GlobalSettings {
18
22
  export interface ConfigStoreOptions {
19
23
  baseDir?: string;
20
24
  }
25
+ export interface ClientConfig {
26
+ endpoint: string;
27
+ headers: Record<string, string>;
28
+ }
21
29
  export interface ConfigStore {
22
30
  loadSettings(): GlobalSettings;
23
31
  saveSettings(settings: GlobalSettings): void;
24
32
  createContext(name: string, options: {
25
33
  endpoint: string;
34
+ targets?: Record<string, ContextTargetEndpoint>;
26
35
  }): ContextConfig;
27
36
  loadContext(name: string): ContextConfig | null;
28
37
  listContexts(): ContextConfig[];
29
38
  deleteContext(name: string): boolean;
30
39
  getCurrentContext(): ContextConfig | null;
31
40
  setCurrentContext(name: string): boolean;
41
+ getTargetEndpoint(targetName: string, contextName?: string): string | null;
32
42
  setCredentials(contextName: string, creds: ContextCredentials): void;
33
43
  getCredentials(contextName: string): ContextCredentials | null;
34
44
  removeCredentials(contextName: string): boolean;
35
45
  hasValidCredentials(contextName: string): boolean;
46
+ setVar(key: string, value: string, contextName?: string): void;
47
+ getVar(key: string, contextName?: string): string | null;
48
+ deleteVar(key: string, contextName?: string): boolean;
49
+ listVars(contextName?: string): Record<string, string>;
50
+ getClientConfig(targetName: string, contextName?: string): ClientConfig;
36
51
  }
37
52
  export declare function createConfigStore(toolName: string, options?: ConfigStoreOptions): ConfigStore;
@@ -1,17 +1,16 @@
1
1
  import * as fs from 'fs';
2
2
  import * as path from 'path';
3
3
  import { appstash, resolve } from './index';
4
- const DEFAULT_SETTINGS = {};
5
4
  function readJson(filePath, fallback) {
6
5
  if (fs.existsSync(filePath)) {
7
6
  try {
8
7
  return JSON.parse(fs.readFileSync(filePath, 'utf8'));
9
8
  }
10
9
  catch {
11
- return fallback;
10
+ return JSON.parse(JSON.stringify(fallback));
12
11
  }
13
12
  }
14
- return fallback;
13
+ return JSON.parse(JSON.stringify(fallback));
15
14
  }
16
15
  function writeJson(filePath, data, mode) {
17
16
  const dir = path.dirname(filePath);
@@ -37,7 +36,7 @@ export function createConfigStore(toolName, options) {
37
36
  return path.join(contextsDir, `${name}.json`);
38
37
  }
39
38
  function loadSettings() {
40
- return readJson(settingsPath(), DEFAULT_SETTINGS);
39
+ return readJson(settingsPath(), {});
41
40
  }
42
41
  function saveSettings(settings) {
43
42
  writeJson(settingsPath(), settings);
@@ -53,6 +52,9 @@ export function createConfigStore(toolName, options) {
53
52
  createdAt: now,
54
53
  updatedAt: now,
55
54
  };
55
+ if (options.targets) {
56
+ context.targets = options.targets;
57
+ }
56
58
  writeJson(contextPath(name), context);
57
59
  return context;
58
60
  }
@@ -141,6 +143,108 @@ export function createConfigStore(toolName, options) {
141
143
  }
142
144
  return true;
143
145
  }
146
+ function getTargetEndpoint(targetName, contextName) {
147
+ let ctx;
148
+ if (contextName) {
149
+ ctx = loadContext(contextName);
150
+ }
151
+ else {
152
+ ctx = getCurrentContext();
153
+ }
154
+ if (!ctx)
155
+ return null;
156
+ if (ctx.targets && ctx.targets[targetName]) {
157
+ return ctx.targets[targetName].endpoint;
158
+ }
159
+ return ctx.endpoint;
160
+ }
161
+ function varsPath(ctxName) {
162
+ const varsDir = resolve(dirs, 'config', 'vars');
163
+ if (!fs.existsSync(varsDir)) {
164
+ fs.mkdirSync(varsDir, { recursive: true });
165
+ }
166
+ return path.join(varsDir, `${ctxName}.json`);
167
+ }
168
+ function loadVars(ctxName) {
169
+ return readJson(varsPath(ctxName), {});
170
+ }
171
+ function saveVars(ctxName, vars) {
172
+ writeJson(varsPath(ctxName), vars);
173
+ }
174
+ function resolveContextName(contextName) {
175
+ if (contextName)
176
+ return contextName;
177
+ const settings = loadSettings();
178
+ return settings.currentContext || null;
179
+ }
180
+ function setVar(key, value, contextName) {
181
+ const ctxName = resolveContextName(contextName);
182
+ if (!ctxName) {
183
+ throw new Error('No active context. Run "context create" or "context use" first.');
184
+ }
185
+ const vars = loadVars(ctxName);
186
+ vars[key] = value;
187
+ saveVars(ctxName, vars);
188
+ }
189
+ function getVar(key, contextName) {
190
+ const ctxName = resolveContextName(contextName);
191
+ if (!ctxName)
192
+ return null;
193
+ const vars = loadVars(ctxName);
194
+ return vars[key] ?? null;
195
+ }
196
+ function deleteVar(key, contextName) {
197
+ const ctxName = resolveContextName(contextName);
198
+ if (!ctxName)
199
+ return false;
200
+ const vars = loadVars(ctxName);
201
+ if (key in vars) {
202
+ delete vars[key];
203
+ saveVars(ctxName, vars);
204
+ return true;
205
+ }
206
+ return false;
207
+ }
208
+ function listVars(contextName) {
209
+ const ctxName = resolveContextName(contextName);
210
+ if (!ctxName)
211
+ return {};
212
+ return loadVars(ctxName);
213
+ }
214
+ function getClientConfig(targetName, contextName) {
215
+ const envPrefix = toolName.toUpperCase().replace(/[^A-Z0-9]/g, '_');
216
+ const targetSuffix = targetName.toUpperCase().replace(/[^A-Z0-9]/g, '_');
217
+ // Tier 1: Try appstash store
218
+ const ctx = contextName ? loadContext(contextName) : getCurrentContext();
219
+ if (ctx) {
220
+ const endpoint = getTargetEndpoint(targetName, ctx.name);
221
+ const headers = {};
222
+ if (hasValidCredentials(ctx.name)) {
223
+ const creds = getCredentials(ctx.name);
224
+ if (creds?.token) {
225
+ headers['Authorization'] = `Bearer ${creds.token}`;
226
+ }
227
+ }
228
+ if (endpoint) {
229
+ return { endpoint, headers };
230
+ }
231
+ }
232
+ // Tier 2: Try env vars
233
+ const envToken = process.env[`${envPrefix}_TOKEN`];
234
+ const envEndpoint = process.env[`${envPrefix}_${targetSuffix}_ENDPOINT`] ||
235
+ process.env[`${envPrefix}_ENDPOINT`];
236
+ if (envEndpoint) {
237
+ const headers = {};
238
+ if (envToken) {
239
+ headers['Authorization'] = `Bearer ${envToken}`;
240
+ }
241
+ return { endpoint: envEndpoint, headers };
242
+ }
243
+ // Tier 3: Throw with actionable error
244
+ throw new Error(`No configuration found for target "${targetName}". ` +
245
+ `Set up a context with "${toolName} context create" and authenticate with "${toolName} auth", ` +
246
+ `or set ${envPrefix}_${targetSuffix}_ENDPOINT and ${envPrefix}_TOKEN environment variables.`);
247
+ }
144
248
  return {
145
249
  loadSettings,
146
250
  saveSettings,
@@ -150,9 +254,15 @@ export function createConfigStore(toolName, options) {
150
254
  deleteContext,
151
255
  getCurrentContext,
152
256
  setCurrentContext,
257
+ getTargetEndpoint,
153
258
  setCredentials,
154
259
  getCredentials,
155
260
  removeCredentials,
156
261
  hasValidCredentials,
262
+ setVar,
263
+ getVar,
264
+ deleteVar,
265
+ listVars,
266
+ getClientConfig,
157
267
  };
158
268
  }
package/esm/index.d.ts CHANGED
@@ -98,4 +98,4 @@ export declare function ensure(dirs: AppStashResult): EnsureResult;
98
98
  */
99
99
  export declare function resolve(dirs: AppStashResult, kind: 'config' | 'cache' | 'data' | 'logs' | 'tmp', ...parts: string[]): string;
100
100
  export { createConfigStore } from './config-store';
101
- export type { ConfigStore, ConfigStoreOptions, ContextConfig, ContextCredentials, Credentials, GlobalSettings, } from './config-store';
101
+ export type { ClientConfig, ConfigStore, ConfigStoreOptions, ContextConfig, ContextCredentials, ContextTargetEndpoint, Credentials, GlobalSettings, } from './config-store';
package/index.d.ts CHANGED
@@ -98,4 +98,4 @@ export declare function ensure(dirs: AppStashResult): EnsureResult;
98
98
  */
99
99
  export declare function resolve(dirs: AppStashResult, kind: 'config' | 'cache' | 'data' | 'logs' | 'tmp', ...parts: string[]): string;
100
100
  export { createConfigStore } from './config-store';
101
- export type { ConfigStore, ConfigStoreOptions, ContextConfig, ContextCredentials, Credentials, GlobalSettings, } from './config-store';
101
+ export type { ClientConfig, ConfigStore, ConfigStoreOptions, ContextConfig, ContextCredentials, ContextTargetEndpoint, Credentials, GlobalSettings, } from './config-store';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appstash",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "Simple, clean application directory resolution",
6
6
  "main": "index.js",
@@ -40,5 +40,5 @@
40
40
  "xdg",
41
41
  "appdirs"
42
42
  ],
43
- "gitHead": "de740970766f110cad481ea900880a9b3120f9d5"
43
+ "gitHead": "276d21250b952f110b9d8a509616ffd1cf5871e7"
44
44
  }