From 09e7557b691f340028f5338ee1859c86df83e818 Mon Sep 17 00:00:00 2001 From: Pinta365 Date: Sat, 23 Mar 2024 20:23:36 +0100 Subject: [PATCH] Opt-in to run powershell diectory lookups. --- README.md | 48 ++++++++++++++++++++++++++++-------------------- deno.jsonc | 2 +- src/dir.ts | 34 ++++++++++++++++++++++------------ 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 39ed3f1..c752918 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ import relevant functions. ```javascript import { dir } from "@cross/dir"; ``` - +Usage ```javascript const userHome = await dir("home"); // or @@ -49,29 +49,37 @@ console.log(`Home directory: ${await dir("home")}`); const userHome = await dir(DirectoryTypes.home); ``` +**Note concerning Windows special folders** + +If no environment variable is configured for the directory or if it returns empty the program can try to parse the [special folders](https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-8.0) found on windows systems if you supply a true argument for the second parameter of `dir()`, the optional parseWindowsSpecialDirectories parameter. Powershell will be used to resolve the directory path. + +```javascript +const userHome = await dir("home", true); +``` + ## Supported directories -| Directory Type | Description | Windows | Linux | macOS | -| -------------- | -------------------------------------------------------------------------- | ------- | ----- | ----- | -| home | The user's home directory. | X | X | X | -| cache | A directory for storing application-specific cache data. | X | X | X | -| config | A directory for storing application configuration data. | X | X | X | -| data | A directory for storing application-specific data (non-cache). | X | X | X | -| data_local | A directory for storing application-specific local (non-roaming) data. | X | X | X | -| download | The user's default download directory. | X | X | X | -| tmp | A temporary directory for storing short-lived files. | X | X | X | -| executable | A directory for storing executable files (Linux only). | | X | | -| audio | A directory for storing audio files. | X | X | X | -| desktop | The user's desktop directory. | X | X | X | -| document | The user's documents directory. | X | X | X | -| font | A directory for storing font files. | X | X | X | -| picture | A directory for storing picture files. | X | X | X | -| public | A directory for storing shared data accessible to all users (Linux/macOS). | | X | X | -| template | A directory for storing user template files. | X | X | | -| video | A directory for storing video files. | X | X | X | +| Directory Type | Description | Win Env | Win SpecialFolder | Linux | macOS | +| -------------- | ---------------------------------------------------------------------- | ------- | ------- | ----- | ----- | +| home | The user's home directory. | X | X | X | X | +| cache | A directory for storing application-specific cache data. | X | X | X | X | +| config | A directory for storing application configuration data. | X | X | X | X | +| data | A directory for storing application-specific data (non-cache). | X | X | X | X | +| data_local | A directory for storing application-specific local (non-roaming) data. | X | X | X | X | +| download | The user's default download directory. | | X | X | X | +| tmp | A temporary directory for storing short-lived files. | X | | X | X | +| executable | A directory for storing executable files (Linux only). | | | X | | +| audio | A directory for storing audio files. | | X | X | X | +| desktop | The user's desktop directory. | | X | X | X | +| document | The user's documents directory. | | X | X | X | +| font | A directory for storing font files. | | X | X | X | +| picture | A directory for storing picture files. | | X | X | X | +| public | A directory for storing shared data accessible to all users. | | | X | X | +| template | A directory for storing user template files. | | X | X | | +| video | A directory for storing video files. | | X | X | X | > **Note** For some Windows directories where simple environmental variables is not enough `dir`uses powershell to -> retrieve the path. +> retrieve the path if invoked with true as second function argument. `dir("type", true)` ## Issues diff --git a/deno.jsonc b/deno.jsonc index f5f335d..d55c820 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,6 +1,6 @@ { "name": "@cross/dir", - "version": "1.0.1", + "version": "1.1.0", "exports": "./mod.ts", "tasks": { diff --git a/src/dir.ts b/src/dir.ts index 40b20ac..e9ffd80 100644 --- a/src/dir.ts +++ b/src/dir.ts @@ -10,7 +10,7 @@ export { DirectoryTypes } from "./config.ts"; * @param {string} type - The type of directory to retrieve as a string. * @returns {Promise} A promise that resolves to the full path of the directory. */ -export async function dir(type: keyof typeof DirectoryTypes): Promise; +export async function dir(type: keyof typeof DirectoryTypes, parseWindowsSpecialDirectories?: boolean): Promise; /** * Retrieves the path to a standard user directory based on the provided type and the current operating system. @@ -18,7 +18,7 @@ export async function dir(type: keyof typeof DirectoryTypes): Promise; * @param {DirectoryTypes} type - The type of directory to retrieve. * @returns {Promise} A promise that resolves to the full path of the directory. */ -export async function dir(type: DirectoryTypes): Promise; +export async function dir(type: DirectoryTypes, parseWindowsSpecialDirectories?: boolean): Promise; /** * Retrieves the path to a standard user directory based on the provided type and the current operating system. @@ -28,7 +28,7 @@ export async function dir(type: DirectoryTypes): Promise; * @throws {Error} If the directory type is not supported on the current platform. * @throws {Error} If no suitable environment variable is found for the requested directory. */ -export async function dir(type: string): Promise { +export async function dir(type: string, parseWindowsSpecialDirectories?: boolean): Promise { const platform = getCurrentOS(); const dirType = typeof type === "string" ? DirectoryTypes[type.toLowerCase() as keyof typeof DirectoryTypes] : type; const configs = directoryConfig[dirType] && directoryConfig[dirType][platform as keyof DirectoryPathConfig]; @@ -37,15 +37,21 @@ export async function dir(type: string): Promise { throw new Error(`Directory type ${dirType ?? type} not supported on this platform (${platform})`); } + let gotWindowsConfigItem: boolean = false; + let baseEnv: string | undefined; + for (const config of configs) { - let baseEnv; if (platform === "windows" && isWindowsConfigItem(config)) { - const ps = await spawn([ - "powershell", - "-Command", - `[Environment]::GetFolderPath('${config.key}')`, - ]); - baseEnv = ps.stdout.trim(); + if(parseWindowsSpecialDirectories) { + const ps = await spawn([ + "powershell", + "-Command", + `[Environment]::GetFolderPath('${config.key}')`, + ]); + baseEnv = ps.stdout.trim(); + } else { + gotWindowsConfigItem = true; + } } else { baseEnv = getEnv(config.key); } @@ -58,5 +64,9 @@ export async function dir(type: string): Promise { } } - throw new Error(`No environment variable set for ${dirType} on ${platform}`); -} + if(gotWindowsConfigItem) { + throw new Error(`No environment variable set for ${dirType} on ${platform}, run dir() with parseWindowsSpecialDirectories parameter set to true to parse windows special directories.`); + } else { + throw new Error(`No environment variable set for ${dirType} on ${platform}`); + } +} \ No newline at end of file