Skip to content

Commit

Permalink
Support for fallback to a defaultDir. Closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinta365 committed Mar 23, 2024
1 parent 1a40625 commit da38499
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const directoryConfig: { [key in DirectoryTypes]: DirectoryPathConfig } =
},
[DirectoryTypes.tmp]: {
windows: [{ key: "TMP" }],
linux: [{ key: "TMPDIR" }],
linux: [{ key: "TMPDIR", defaultDir: "/tmp" }],
macos: [{ key: "TMPDIR" }],
},
[DirectoryTypes.video]: {
Expand All @@ -173,22 +173,26 @@ export const directoryConfig: { [key in DirectoryTypes]: DirectoryPathConfig } =
* Configuration item for Windows directory paths.
* @property {string} key - Environment variable name
* @property {string} [extraFolder] - Optional subfolder to append
* @property {string} [defaultDir] - Optional default directory if "key" is undefined
* @property {boolean} [winSpecialFolder] - Indicates if a Windows special folder
*/
type WindowsDirectoryPathConfigItem = {
key: string;
extraFolder?: string;
defaultDir?: string;
winSpecialFolder?: boolean;
};

/**
* Configuration item for Unix (Linux/macOS) directory paths.
* @property {string} key - Environment variable name
* @property {string} [extraFolder] - Optional subfolder to append
* @property {string} [defaultDir] - Optional default directory if "key" is undefined
*/
type UnixDirectoryPathConfigItem = {
key: string;
extraFolder?: string;
defaultDir?: string;
};

/**
Expand Down
22 changes: 10 additions & 12 deletions src/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,23 @@ export async function dir(type: string): Promise<string> {
}

for (const config of configs) {
let baseEnv;
if (platform === "windows" && isWindowsConfigItem(config)) {
const ps = await spawn([
"powershell",
"-Command",
`[Environment]::GetFolderPath('${config.key}')`,
]);
const baseEnv = ps.stdout.trim();

if (baseEnv) {
const fullPath = config.extraFolder ? `${baseEnv}${config.extraFolder}` : baseEnv;
return fullPath;
}
baseEnv = ps.stdout.trim();
} else {
// Handle non-Windows logic
const baseEnv = getEnv(config.key);
if (baseEnv) {
const fullPath = config.extraFolder ? `${baseEnv}${config.extraFolder}` : baseEnv;
return fullPath;
}
baseEnv = getEnv(config.key);
}

if (baseEnv) {
const fullPath = config.extraFolder ? `${baseEnv}${config.extraFolder}` : baseEnv;
return fullPath;
} else if (config.defaultDir) {
return config.defaultDir;
}
}

Expand Down

0 comments on commit da38499

Please sign in to comment.