Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Apr 24, 2024
1 parent 3b22ba8 commit 827c02e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 60 deletions.
9 changes: 5 additions & 4 deletions mod.full.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from "./mod.ts";
export * from "./mod.pure.full.ts";

export * from "./src/pure_ext/csv.ts";
export * from "./src/pure_ext/excel.ts";
export * from "./src/pure_ext/zip.ts";
export * from "./src/deno/json.ts";
export * from "./src/deno/os.ts";
export * from "./src/deno/path.ts";
export * from "./src/deno/process.ts";

export * from "./src/deno_ext/dom.ts";
export * from "./src/deno_ext/log.ts";
Expand Down
2 changes: 1 addition & 1 deletion mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./test/pure/deep.test.ts";
import "./test/pure/deflate.test.ts";
import "./test/pure/fetch.test.ts";
import "./test/pure/minipack.test.ts";
import "./test/deno/os.test.ts";
import "./test/pure/primitive.test.ts";
import "./test/pure/stream.test.ts";
import "./test/pure/text.test.ts";
Expand All @@ -16,7 +17,6 @@ import "./test/pure_ext/excel.test.ts";
import "./test/pure_ext/zip.test.ts";

import "./test/deno/json.test.ts";
import "./test/deno/os.test.ts";
import "./test/deno/path.test.ts";
import "./test/deno/process.test.ts";

Expand Down
13 changes: 1 addition & 12 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
export * from "./src/pure/base64.ts";
export * from "./src/pure/byte.ts";
export * from "./src/pure/crypto.ts";
export * from "./src/pure/deep.ts";
export * from "./src/pure/deflate.ts";
export * from "./src/pure/fetch.ts";
export * from "./src/pure/minipack.ts";
export * from "./src/pure/primitive.ts";
export * from "./src/pure/stream.ts";
export * from "./src/pure/text.ts";
export * from "./src/pure/time.ts";
export * from "./src/pure/worker.ts";
export * from "./mod.pure.ts";

export * from "./src/deno/json.ts";
export * from "./src/deno/os.ts";
Expand Down
20 changes: 19 additions & 1 deletion src/deno/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,22 @@
* const isWin = osWin;
* ```
*/
export const osWin:boolean = Deno.build.os === "windows";
export const osWindows:boolean = Deno.build.os === "windows";

/**
* Are you running on Mac?
* @example
* ```ts
* const isWin = osMac;
* ```
*/
export const osMac:boolean = Deno.build.os === "darwin";

/**
* Are you running on Linux?
* @example
* ```ts
* const isWin = osLinux;
* ```
*/
export const osLinux:boolean = Deno.build.os === "linux";
43 changes: 21 additions & 22 deletions src/deno/path.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {osWin} from "./os.ts";
import {osWindows} from "./os.ts";

/**
* Convert from backslash to slash.
* Useful for converting from Windows path to UNIX path.
* @example
* ```ts
* const path = slashU("C:\\file");
* const path = pathSepL("C:\\file");
* ```
*/
export function slashU(path:string):string{
export function pathSepL(path:string):string{
return path.replace(/\\/g, "/");
}

Expand All @@ -17,10 +17,10 @@ export function slashU(path:string):string{
* Useful for converting from UNIX path to Windows path.
* @example
* ```ts
* const path = slashW("C:/file");
* const path = pathSepW("C:/file");
* ```
*/
export function slashW(path:string):string{
export function pathSepW(path:string):string{
return path.replace(/\//g, "\\");
}

Expand All @@ -29,61 +29,60 @@ export function slashW(path:string):string{
* `/tmp` for UNIX and `C:/Windows/Temp` for Windows.
* @example
* ```ts
* const path = tmpPath();
* const path = pathTmp();
* ```
*/
export function tmpPath():string{
return osWin ? "C:/Windows/Temp" : "/tmp";
export function pathTmp():string{
return osWindows ? "C:/Windows/Temp" : "/tmp";
}

/**
* System-wide application data directory path for each OS.
* `/var` for UNIX and `C:/ProgramData` for Windows.
* @example
* ```ts
* const path = dataPath();
* const path = pathVar();
* ```
*/
export function dataPath():string{
return osWin ? "C:/ProgramData" : "/var";
export function pathVar():string{
return osWindows ? "C:/ProgramData" : "/var";
}

/**
* System-wide user config directory path for each OS.
* `~/.config` for UNIX and `~/AppData/Roaming` for Windows.
* @example
* ```ts
* const path = configPath();
* const path = pathConfig();
* ```
*/
export function configPath():string{
return `${homePath()}/${osWin ? "AppData/Roaming" : ".config"}`;
export function pathConfig():string{
return `${pathHome()}/${osWindows ? "AppData/Roaming" : ".config"}`;
}

/**
* System-wide home directory path for each OS.
* `${HOME}` for UNIX and `%USERPROFILE%` for Windows.
* @example
* ```ts
* const path = homePath();
* const path = pathHome();
* ```
*/
export function homePath():string{
export function pathHome():string{
const {HOME, USERPROFILE} = Deno.env.toObject();

return osWin ? slashU(USERPROFILE) : HOME;
return osWindows ? pathSepL(USERPROFILE) : HOME;
}

/**
* Directory of `Deno.mainModule`.
* @example
* ```ts
* const path = mainPath();
* const path = pathMain();
* ```
*/
export function mainPath():string{
const {protocol, origin, pathname} = new URL(Deno.mainModule);
const path = pathname.replace(/[^/]*$/, "");
export function pathMain():string{
const directory = new URL(Deno.mainModule).pathname.replace(/[^/]*$/, "");

return protocol === "file:" ? osWin ? path.replace(/^\//, "") : path : `${origin}${path}`;
return osWindows ? directory.replace(/^\//, "") : directory;
}
28 changes: 21 additions & 7 deletions test/deno/os.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import {assertEquals} from "../../deps.test.ts";
import {osWin} from "../../src/deno/os.ts";
import {osWindows, osMac, osLinux} from "../../src/deno/os.ts";

Deno.test({
ignore: Deno.build.os !== "windows",
name: "Platform: Windows",
ignore: Deno.build.os === "windows",
name: "OS: Windows",
fn(){
assertEquals(osWin, true);
assertEquals(osWindows, true);
assertEquals(osMac, false);
assertEquals(osLinux, false);
}
});

Deno.test({
ignore: Deno.build.os === "windows",
name: "Platform: Unix",
ignore: Deno.build.os === "darwin",
name: "OS: Mac",
fn(){
assertEquals(osWindows, false);
assertEquals(osMac, true);
assertEquals(osLinux, false);
}
});

Deno.test({
ignore: Deno.build.os === "linux",
name: "OS: Linux",
fn(){
assertEquals(osWin, false);
assertEquals(osWindows, false);
assertEquals(osMac, false);
assertEquals(osLinux, true);
}
});
26 changes: 13 additions & 13 deletions test/deno/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals, dirname, fromFileUrl} from "../../deps.test.ts";
import {slashU, slashW, tmpPath, dataPath, configPath, homePath, mainPath} from "../../src/deno/path.ts";
import {pathSepL, pathSepW, pathTmp, pathVar, pathConfig, pathHome, pathMain} from "../../src/deno/path.ts";

const {HOME, USERPROFILE} = Deno.env.toObject();
const ep = `${fromFileUrl(dirname(Deno.mainModule))}/`;
Expand All @@ -10,31 +10,31 @@ Deno.test({
const sampleUnix = "C:/Windows/System32/cmd.exe";
const sampleWindows = "C:\\Windows\\System32\\cmd.exe";

assertEquals(slashU(sampleWindows), sampleUnix);
assertEquals(slashW(sampleUnix), sampleWindows);
assertEquals(pathSepL(sampleWindows), sampleUnix);
assertEquals(pathSepW(sampleUnix), sampleWindows);
}
});

Deno.test({
ignore: Deno.build.os !== "windows",
name: "Path: Windows",
fn(){
assertEquals(tmpPath(), "C:/Windows/Temp");
assertEquals(dataPath(), "C:/ProgramData");
assertEquals(configPath(), `${slashU(USERPROFILE)}/AppData/Roaming`);
assertEquals(homePath(), slashU(USERPROFILE));
assertEquals(mainPath(), slashU(ep));
assertEquals(pathTmp(), "C:/Windows/Temp");
assertEquals(pathVar(), "C:/ProgramData");
assertEquals(pathConfig(), `${pathSepL(USERPROFILE)}/AppData/Roaming`);
assertEquals(pathHome(), pathSepL(USERPROFILE));
assertEquals(pathMain(), pathSepL(ep));
}
});

Deno.test({
ignore: Deno.build.os === "windows",
name: "Path: Unix",
fn(){
assertEquals(tmpPath(), "/tmp");
assertEquals(dataPath(), "/var");
assertEquals(configPath(), `${HOME}/.config`);
assertEquals(homePath(), HOME);
assertEquals(mainPath(), ep);
assertEquals(pathTmp(), "/tmp");
assertEquals(pathVar(), "/var");
assertEquals(pathConfig(), `${HOME}/.config`);
assertEquals(pathHome(), HOME);
assertEquals(pathMain(), ep);
}
});

0 comments on commit 827c02e

Please sign in to comment.