Skip to content

Commit

Permalink
Merge pull request #26 from dojyorin/dev
Browse files Browse the repository at this point in the history
feat: homepath and fix: actions workflow
  • Loading branch information
dojyorin authored Dec 2, 2022
2 parents 261ad8a + f351e82 commit 92bfe67
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Release
"on":
push:
tags: v[0-9].[0-9].[0-9]
tags: v[0-9]+.[0-9]+.[0-9]+
jobs:
release:
name: 'Release: ${{github.ref_name}}'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows_json/release.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Release",
"on": {
"push": {
"tags": "v[0-9].[0-9].[0-9]"
"tags": "v[0-9]+.[0-9]+.[0-9]+"
}
},
"jobs": {
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ 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 tmp = tmpPath(); // "C:/Windows/Temp" if running on Windows, or "/tmp" if running on Linux or Mac.
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`.
```

Expand Down
4 changes: 2 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {} from "https://deno.land/std@0.166.0/bytes/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.166.0/path/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.167.0/path/mod.ts";
export {} from "https://deno.land/std@0.167.0/bytes/mod.ts";
28 changes: 26 additions & 2 deletions src/platform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import {dirname, fromFileUrl} from "../deps.ts";

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

/**
* Check if it's running on Windows.
* @return `true` if running on Windows.
Expand All @@ -10,13 +19,28 @@ export function isWin(){

/**
* Returns the system wide temporary directory path for each platform.
* @return `"C:/Windows/Temp"` if running on Windows, or `"/tmp"` if running on Linux or Mac.
* @return `/tmp` if running on Linux or Mac, `C:/Windows/Temp` if running on Windows.
*/
export function tmpPath(){
switch(Deno.build.os){
case "windows": return "C:/Windows/Temp";
case "linux": return "/tmp";
case "darwin": return "/tmp";
case "windows": return "C:/Windows/Temp";
default: throw new Error();
}
}

/**
* Returns the system wide user directory path for each platform.
* @return `$HOME` if running on Linux or Mac, `%USERPROFILE%` if running on Windows.
*/
export function homePath(){
const {HOME, USERPROFILE} = Deno.env.toObject();

switch(Deno.build.os){
case "linux": return `${HOME}`;
case "darwin": return `${HOME}`;
case "windows": return posixSep(`${USERPROFILE}`);
default: throw new Error();
}
}
Expand Down
8 changes: 5 additions & 3 deletions test/platform.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import {assertEquals, dirname, fromFileUrl} from "../deps.test.ts";
import {isWin, tmpPath, cwdMain} from "../src/platform.ts";
import {posixSep, isWin, tmpPath, homePath, cwdMain} from "../src/platform.ts";

Deno.test({
ignore: Deno.build.os !== "windows",
name: "Platform: Temporary (Windows)",
name: "Platform: Directory (Windows)",
async fn(){
assertEquals(isWin(), true);
assertEquals(tmpPath(), "C:/Windows/Temp");
assertEquals(homePath(), posixSep(Deno.env.toObject().USERPROFILE));
}
});

Deno.test({
ignore: Deno.build.os === "windows",
name: "Platform: Temporary (Linux & Mac)",
name: "Platform: Directory (Linux & Mac)",
async fn(){
assertEquals(isWin(), false);
assertEquals(tmpPath(), "/tmp");
assertEquals(homePath(), Deno.env.toObject().HOME);
}
});

Expand Down

0 comments on commit 92bfe67

Please sign in to comment.