Skip to content

Commit 5644732

Browse files
committed
change ~ experimental inclusion of meta.mainFilename (+ test mods)
- add `meta.mainFilename` to Platform.Adapter - note: associated `OSPaths.main()` function is tested but undocumented - primarily for ESM platform testing if ESM script name hack - will be needed for XDGAppPaths (but not for OSPaths)
1 parent 5756b97 commit 5644732

File tree

7 files changed

+16
-2
lines changed

7 files changed

+16
-2
lines changed

src/esm-wrapper/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import _ from '../index.js';
55
// note: not usable by `deno`;
66
// ...`deno` is unable to load (the CJS module) '../index.js' via import => `'../index.js' does not provide an export named 'default'`
7+
import { adapter } from '../platform-adapters/node.js';
8+
9+
// re-define `meta.mainFilename` adapter for ESM scripts
10+
// HACK: `process._eval` is undocumented; used here as evidence of `node -e ...` differentiating between immediate eval vs file-bound scripts
11+
// eslint-disable-next-line functional/immutable-data
12+
adapter.meta.mainFilename = typeof process._eval === 'undefined' ? process.argv[1] : '';
713

814
const default_ = _;
915
export default default_;

src/lib/OSPaths.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type moduleInternals_ = typeof module_ & {
88
};
99

1010
test('api', (t) => {
11-
const api = ['home', 'temp'];
11+
const api = ['home', 'temp', 'main'];
1212

1313
t.is(typeof module_, 'function');
1414
t.deepEqual(Object.keys(module_).sort(), api.sort());

src/lib/OSPaths.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type OSPaths = {
99
(): OSPaths;
1010
readonly home: () => string | undefined;
1111
readonly temp: () => string;
12+
readonly main: () => string;
1213
};
1314

1415
const { env } = process;
@@ -85,6 +86,8 @@ class _OSPaths {
8586
OSPaths.home = extension.home;
8687
OSPaths.temp = extension.temp;
8788

89+
OSPaths.main = () => adapter.meta.mainFilename;
90+
8891
return OSPaths;
8992
}
9093
}

src/platform-adapters/_base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export namespace Platform {
33
export type Adapter = {
44
readonly env: { readonly get: (_: string) => string | undefined };
5+
readonly meta: { readonly mainFilename: string };
56
readonly os: { readonly homedir?: () => string; readonly tmpdir?: () => string };
67
readonly path: {
78
readonly join: (..._: readonly string[]) => string;

src/platform-adapters/deno.deno.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const adapter: Platform.Adapter = {
1515
env: { get: Deno.env.get },
1616
// Deno (as of v1.6) has no built-in implementation for homedir() or tmpdir()
1717
os: {}, // * module is tolerant of missing homedir()/tmpdir() functions
18+
meta: { mainFilename: Deno.mainModule },
1819
path,
1920
process: { platform: Deno.build.os },
2021
};

src/platform-adapters/node.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export const adapter: Platform.Adapter = {
99
return process.env[s];
1010
},
1111
},
12+
meta: {
13+
mainFilename: process.mainModule?.filename || '',
14+
},
1215
os,
1316
path,
1417
process,

test/integration.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const settledSupportForESMs =
2626
// Integration tests
2727

2828
test('api', (t) => {
29-
const api = ['home', 'temp'];
29+
const api = ['home', 'temp', 'main'];
3030

3131
t.is(typeof module_, 'function');
3232
t.deepEqual(Object.keys(module_).sort(), api.sort());

0 commit comments

Comments
 (0)