Skip to content

Commit

Permalink
Opt-in to run powershell diectory lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinta365 committed Mar 23, 2024
1 parent da38499 commit 09e7557
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 33 deletions.
48 changes: 28 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import relevant functions.
```javascript
import { dir } from "@cross/dir";
```

Usage
```javascript
const userHome = await dir("home");
// or
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/dir",
"version": "1.0.1",
"version": "1.1.0",
"exports": "./mod.ts",

"tasks": {
Expand Down
34 changes: 22 additions & 12 deletions src/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export { DirectoryTypes } from "./config.ts";
* @param {string} type - The type of directory to retrieve as a string.
* @returns {Promise<string>} A promise that resolves to the full path of the directory.
*/
export async function dir(type: keyof typeof DirectoryTypes): Promise<string>;
export async function dir(type: keyof typeof DirectoryTypes, parseWindowsSpecialDirectories?: boolean): Promise<string>;

/**
* Retrieves the path to a standard user directory based on the provided type and the current operating system.
*
* @param {DirectoryTypes} type - The type of directory to retrieve.
* @returns {Promise<string>} A promise that resolves to the full path of the directory.
*/
export async function dir(type: DirectoryTypes): Promise<string>;
export async function dir(type: DirectoryTypes, parseWindowsSpecialDirectories?: boolean): Promise<string>;

/**
* Retrieves the path to a standard user directory based on the provided type and the current operating system.
Expand All @@ -28,7 +28,7 @@ export async function dir(type: DirectoryTypes): Promise<string>;
* @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<string> {
export async function dir(type: string, parseWindowsSpecialDirectories?: boolean): Promise<string> {
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];
Expand All @@ -37,15 +37,21 @@ export async function dir(type: string): Promise<string> {
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);
}
Expand All @@ -58,5 +64,9 @@ export async function dir(type: string): Promise<string> {
}
}

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}`);
}
}

0 comments on commit 09e7557

Please sign in to comment.