Skip to content

Commit e8001de

Browse files
Added properties for the current CWD and process path
1 parent e335412 commit e8001de

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ API
102102
### `host.global`
103103
When running in a web browser, `host.global` is a reference to the `window` object. When running in Node.js, it's a reference to the `global` object.
104104

105+
### `host.path`
106+
The path of the host process, as a string. When running in Node.js, this is set to [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath). When running in a web browser, it is the URL of the web page (`window.location.href`).
107+
108+
### `host.url`
109+
The same as `host.path`, but as a parsed [URL object](https://developer.mozilla.org/en-US/docs/Web/API/URL). When running in Node.js, this will be a `file://` URL.
110+
111+
### `host.cwd`
112+
The current working directory, as a string. When running in Node.js, this is set to [`process.cwd`](https://nodejs.org/api/process.html#process_process_cwd). When running in a web browser, it is the parent directory of the current web page
113+
114+
### `host.cwdURL`
115+
The same as `host.cwd`, but as a parsed [URL object](https://developer.mozilla.org/en-US/docs/Web/API/URL). When running in Node.js, this will be a `file://` URL.
116+
105117

106118
### `host.os`
107119
This property is an object with the following structure:

karma.conf.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ module.exports = karmaConfig({
1212
browsers: {
1313
chrome: host.ci ? host.os.linux : true,
1414
firefox: host.ci ? host.os.linux : true,
15-
safari: host.ci ? host.os.linux : host.os.mac,
16-
edge: host.ci ? host.os.linux : host.os.windows,
17-
ie: host.os.windows,
15+
safari: host.ci ? host.os.linux : host.os.mac, // SauceLabs in CI
16+
edge: host.ci ? host.os.linux : host.os.windows, // SauceLabs in CI
17+
ie: host.ci ? host.os.windows : false, // IE needs to run by itself, due to Babel transforms
1818
},
1919
config: {
2020
// Prevent Karma-Config from using the "karma-host-environment" framework,

src/host.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ export interface Host {
1010
*/
1111
global: Global;
1212

13+
/**
14+
* The path of the host process.
15+
* In Node, this is `process.execPath`. In web browsers, it is the URL of the current page.
16+
*/
17+
path: string;
18+
19+
/**
20+
* The URL of the host process.
21+
* In Node, this will be a "file://" URL. In web browsers it will be an "http://" or "https://" URL.
22+
*/
23+
url: URL;
24+
25+
/**
26+
* The current working directory.
27+
* In Node, this is `process.cwd()`. In web browsers it's the parent directory of the current page URL.
28+
*/
29+
cwd: string;
30+
31+
/**
32+
* The current working directory as a URL.
33+
* In Node, this will be a "file://" URL. In web browsers it will be an "http://" or "https://" URL.
34+
*/
35+
cwdURL: URL;
36+
1337
/**
1438
* Information about the host operating system.
1539
* In web browsers the OS info is inferred from the user agent.

src/isomorphic.browser.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ import { Browsers, BrowsersRecord, Global, Host, OSInfo, OSInfoRecord } from "./
33
import { merge, mergeGlobalHost } from "./merge";
44
import { toJSON } from "./to-json";
55

6+
let url = new URL(window.location.href);
7+
let cwdURL = new URL(".", url);
8+
69
/**
710
* Information about the host environment that the code is running in.
811
*/
912
export const host: Host = {
1013
global: window as unknown as Global,
14+
url,
15+
path: url.href,
16+
cwdURL,
17+
cwd: cwdURL.href,
1118
os: getOSInfo(),
1219
node: false,
1320
browser: getBrowserInfo(),

src/isomorphic.node.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
/* eslint-env node */
22
import * as ci from "@qawolf/ci-info";
3+
import { pathToFileURL } from "url";
34
import { CIInfo, Global, Host, NodeInfo, OSInfo } from "./host";
45
import { merge, mergeGlobalHost } from "./merge";
56
import { toJSON } from "./to-json";
67

8+
const cwd = process.cwd();
9+
const execPath = process.execPath;
10+
711
/**
812
* Information about the host environment that the code is running in.
913
*/
1014
export const host: Host = {
1115
global: global as unknown as Global,
16+
path: execPath,
17+
url: pathToFileURL(execPath),
18+
cwd,
19+
cwdURL: pathToFileURL(cwd),
1220
os: getOSInfo(),
1321
node: getNodeInfo(),
1422
browser: false,

test/specs/exports.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ describe("package exports", () => {
2020

2121
expect(keys).to.have.same.members([
2222
"global",
23+
"path",
24+
"url",
25+
"cwd",
26+
"cwdURL",
2327
"os",
2428
"env",
2529
"ci",

0 commit comments

Comments
 (0)