Skip to content

Commit

Permalink
Merge pull request #28 from dojyorin/dev
Browse files Browse the repository at this point in the history
add mainpath.
  • Loading branch information
dojyorin authored Dec 5, 2022
2 parents 92bfe67 + 9cd4882 commit bb74d81
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ const formatted = trimExtend(decoded); // formatted string.
**Platform Specific**

```ts
const posix = posixSep("C:\\Users"); // POSIX style (slash) path string.
const win = isWin(); // "true" if running on Windows.
const posix = posixSep("C:\\Users\\Administrator"); // POSIX style (slash) path string.
const win = winSep("C:/Users/Administrator"); // Windows style (backslash) path string.
const iswin = isWin(); // "true" if running on Windows.
const tmp = tmpPath(); // `/tmp` if running on Linux or Mac, `C:/Windows/Temp` if running on Windows.
const home = homePath(); // `$HOME` if running on Linux or Mac, `%USERPROFILE%` if running on Windows.
cwdMain(); // Move current directory to `Deno.mainModule`.
const main = mainPath(); // Returns the directory of `Deno.mainModule`.
```

</p>
Expand Down
6 changes: 3 additions & 3 deletions deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {assertEquals} from "https://deno.land/std@0.166.0/testing/asserts.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.166.0/path/mod.ts";
export {serve} from "https://deno.land/std@0.166.0/http/mod.ts";
export {assertEquals} from "https://deno.land/std@0.167.0/testing/asserts.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.167.0/path/mod.ts";
export {serve} from "https://deno.land/std@0.167.0/http/mod.ts";
22 changes: 19 additions & 3 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export function posixSep(path:string){
return path.replaceAll("\\", "/");
}

/**
* Convert from slash to backslash.
* @param path POSIX style (slash) path string.
* @return Windows style (backslash) path string.
*/
export function winSep(path:string){
return path.replaceAll("/", "\\");
}

/**
* Check if it's running on Windows.
* @return `true` if running on Windows.
Expand Down Expand Up @@ -46,8 +55,15 @@ export function homePath(){
}

/**
* Move current directory to `Deno.mainModule`.
* Returns the directory of `Deno.mainModule`.
*/
export function cwdMain(){
Deno.chdir(fromFileUrl(dirname(Deno.mainModule)));
export function mainPath(){
const path = fromFileUrl(dirname(Deno.mainModule));

switch(Deno.build.os){
case "linux": return path;
case "darwin": return path;
case "windows": return posixSep(path);
default: throw new Error();
}
}
28 changes: 14 additions & 14 deletions test/platform.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import {assertEquals, dirname, fromFileUrl} from "../deps.test.ts";
import {posixSep, isWin, tmpPath, homePath, cwdMain} from "../src/platform.ts";
import {posixSep, winSep, isWin, tmpPath, homePath, mainPath} from "../src/platform.ts";

Deno.test({
name: "Platform: Separator.",
async fn(){
const samplePosix = "C:/Windows/System32/cmd.exe";
const sampleWin = "C:\\Windows\\System32\\cmd.exe";

assertEquals(posixSep(sampleWin), samplePosix);
assertEquals(winSep(samplePosix), sampleWin);
}
});

Deno.test({
ignore: Deno.build.os !== "windows",
Expand All @@ -8,6 +19,7 @@ Deno.test({
assertEquals(isWin(), true);
assertEquals(tmpPath(), "C:/Windows/Temp");
assertEquals(homePath(), posixSep(Deno.env.toObject().USERPROFILE));
assertEquals(mainPath(), posixSep(fromFileUrl(dirname(Deno.mainModule))));
}
});

Expand All @@ -18,18 +30,6 @@ Deno.test({
assertEquals(isWin(), false);
assertEquals(tmpPath(), "/tmp");
assertEquals(homePath(), Deno.env.toObject().HOME);
}
});

Deno.test({
name: "Platform: CWD.",
async fn(){
const backup = Deno.cwd();

cwdMain();

assertEquals(fromFileUrl(dirname(Deno.mainModule)), Deno.cwd());

Deno.chdir(backup);
assertEquals(mainPath(), fromFileUrl(dirname(Deno.mainModule)));
}
});

0 comments on commit bb74d81

Please sign in to comment.