Skip to content

Commit 37fb052

Browse files
committed
feat(appstash): add APPSTASH_BASE_DIR env var support
When no baseDir option is passed programmatically, appstash now checks the APPSTASH_BASE_DIR environment variable before falling back to os.homedir(). This enables test isolation without overriding HOME. Priority: baseDir option > APPSTASH_BASE_DIR env var > os.homedir()
1 parent db30bc2 commit 37fb052

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/appstash/__tests__/appstash.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,36 @@ describe('appstash', () => {
132132
});
133133
});
134134

135+
describe('APPSTASH_BASE_DIR env var', () => {
136+
const originalEnv = process.env.APPSTASH_BASE_DIR;
137+
138+
afterEach(() => {
139+
if (originalEnv === undefined) {
140+
delete process.env.APPSTASH_BASE_DIR;
141+
} else {
142+
process.env.APPSTASH_BASE_DIR = originalEnv;
143+
}
144+
});
145+
146+
it('should use APPSTASH_BASE_DIR when no baseDir option is provided', () => {
147+
process.env.APPSTASH_BASE_DIR = tempBase;
148+
const dirs = appstash('pgpm');
149+
150+
expect(dirs.root).toBe(path.join(tempBase, '.pgpm'));
151+
expect(dirs.config).toBe(path.join(tempBase, '.pgpm', 'config'));
152+
});
153+
154+
it('should prefer baseDir option over APPSTASH_BASE_DIR env var', () => {
155+
const otherBase = fs.mkdtempSync(path.join(os.tmpdir(), 'appstash-other-'));
156+
process.env.APPSTASH_BASE_DIR = otherBase;
157+
158+
const dirs = appstash('pgpm', { baseDir: tempBase });
159+
160+
expect(dirs.root).toBe(path.join(tempBase, '.pgpm'));
161+
fs.rmSync(otherBase, { recursive: true, force: true });
162+
});
163+
});
164+
135165
describe('Edge cases', () => {
136166
it('should handle tool names with special characters', () => {
137167
const dirs = appstash('my-app', { baseDir: tempBase });

packages/appstash/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface AppStashResult {
2626
* Options for appstash()
2727
*/
2828
export interface AppStashOptions {
29-
/** Base directory (defaults to os.homedir()) */
29+
/** Base directory (defaults to APPSTASH_BASE_DIR env var, then os.homedir()) */
3030
baseDir?: string;
3131
/** Use XDG fallback if home fails (default: true) */
3232
useXdgFallback?: boolean;
@@ -112,6 +112,8 @@ export function appstash(tool: string, options: AppStashOptions = {}): AppStashR
112112
let base: string;
113113
if (baseDir) {
114114
base = baseDir;
115+
} else if (process.env.APPSTASH_BASE_DIR) {
116+
base = process.env.APPSTASH_BASE_DIR;
115117
} else {
116118
const home = getHomeDir();
117119
if (!home) {

0 commit comments

Comments
 (0)