appstash 0.5.0 → 0.7.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
@@ -22,6 +22,10 @@ export interface GlobalSettings {
22
22
  export interface ConfigStoreOptions {
23
23
  baseDir?: string;
24
24
  }
25
+ export interface ClientConfig {
26
+ endpoint: string;
27
+ headers: Record<string, string>;
28
+ }
25
29
  export interface ConfigStore {
26
30
  loadSettings(): GlobalSettings;
27
31
  saveSettings(settings: GlobalSettings): void;
@@ -39,5 +43,10 @@ export interface ConfigStore {
39
43
  getCredentials(contextName: string): ContextCredentials | null;
40
44
  removeCredentials(contextName: string): boolean;
41
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;
42
51
  }
43
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);
@@ -195,6 +194,93 @@ function createConfigStore(toolName, options) {
195
194
  }
196
195
  return ctx.endpoint;
197
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
+ }
198
284
  return {
199
285
  loadSettings,
200
286
  saveSettings,
@@ -209,5 +295,10 @@ function createConfigStore(toolName, options) {
209
295
  getCredentials,
210
296
  removeCredentials,
211
297
  hasValidCredentials,
298
+ setVar,
299
+ getVar,
300
+ deleteVar,
301
+ listVars,
302
+ getClientConfig,
212
303
  };
213
304
  }
@@ -22,6 +22,10 @@ export interface GlobalSettings {
22
22
  export interface ConfigStoreOptions {
23
23
  baseDir?: string;
24
24
  }
25
+ export interface ClientConfig {
26
+ endpoint: string;
27
+ headers: Record<string, string>;
28
+ }
25
29
  export interface ConfigStore {
26
30
  loadSettings(): GlobalSettings;
27
31
  saveSettings(settings: GlobalSettings): void;
@@ -39,5 +43,10 @@ export interface ConfigStore {
39
43
  getCredentials(contextName: string): ContextCredentials | null;
40
44
  removeCredentials(contextName: string): boolean;
41
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;
42
51
  }
43
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);
@@ -159,6 +158,93 @@ export function createConfigStore(toolName, options) {
159
158
  }
160
159
  return ctx.endpoint;
161
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
+ }
162
248
  return {
163
249
  loadSettings,
164
250
  saveSettings,
@@ -173,5 +259,10 @@ export function createConfigStore(toolName, options) {
173
259
  getCredentials,
174
260
  removeCredentials,
175
261
  hasValidCredentials,
262
+ setVar,
263
+ getVar,
264
+ deleteVar,
265
+ listVars,
266
+ getClientConfig,
176
267
  };
177
268
  }
package/esm/index.d.ts CHANGED
@@ -21,7 +21,7 @@ export interface AppStashResult {
21
21
  * Options for appstash()
22
22
  */
23
23
  export interface AppStashOptions {
24
- /** Base directory (defaults to os.homedir()) */
24
+ /** Base directory (defaults to APPSTASH_BASE_DIR env var, then os.homedir()) */
25
25
  baseDir?: string;
26
26
  /** Use XDG fallback if home fails (default: true) */
27
27
  useXdgFallback?: boolean;
@@ -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, ContextTargetEndpoint, Credentials, GlobalSettings, } from './config-store';
101
+ export type { ClientConfig, ConfigStore, ConfigStoreOptions, ContextConfig, ContextCredentials, ContextTargetEndpoint, Credentials, GlobalSettings, } from './config-store';
package/esm/index.js CHANGED
@@ -62,6 +62,9 @@ export function appstash(tool, options = {}) {
62
62
  if (baseDir) {
63
63
  base = baseDir;
64
64
  }
65
+ else if (process.env.APPSTASH_BASE_DIR) {
66
+ base = process.env.APPSTASH_BASE_DIR;
67
+ }
65
68
  else {
66
69
  const home = getHomeDir();
67
70
  if (!home) {
package/index.d.ts CHANGED
@@ -21,7 +21,7 @@ export interface AppStashResult {
21
21
  * Options for appstash()
22
22
  */
23
23
  export interface AppStashOptions {
24
- /** Base directory (defaults to os.homedir()) */
24
+ /** Base directory (defaults to APPSTASH_BASE_DIR env var, then os.homedir()) */
25
25
  baseDir?: string;
26
26
  /** Use XDG fallback if home fails (default: true) */
27
27
  useXdgFallback?: boolean;
@@ -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, ContextTargetEndpoint, Credentials, GlobalSettings, } from './config-store';
101
+ export type { ClientConfig, ConfigStore, ConfigStoreOptions, ContextConfig, ContextCredentials, ContextTargetEndpoint, Credentials, GlobalSettings, } from './config-store';
package/index.js CHANGED
@@ -101,6 +101,9 @@ function appstash(tool, options = {}) {
101
101
  if (baseDir) {
102
102
  base = baseDir;
103
103
  }
104
+ else if (process.env.APPSTASH_BASE_DIR) {
105
+ base = process.env.APPSTASH_BASE_DIR;
106
+ }
104
107
  else {
105
108
  const home = getHomeDir();
106
109
  if (!home) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appstash",
3
- "version": "0.5.0",
3
+ "version": "0.7.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": "fd9721cb17389c4e2f9847169d5a42de2f9746dd"
43
+ "gitHead": "febc209cbaea97a5976b0554fdb4e1780f6b9618"
44
44
  }