Skip to content

Commit

Permalink
change ~ experimental inclusion of meta.mainFilename (+ test mods)
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
rivy committed Jan 11, 2021
1 parent 6ee064e commit b7c05a9
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/esm-wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import _ from '../index.js';
// note: not usable by `deno`;
// ...`deno` is unable to load (the CJS module) '../index.js' via import => `'../index.js' does not provide an export named 'default'`
import { adapter } from '../platform-adapters/node.js';

// re-define `meta.mainFilename` adapter for ESM scripts
// HACK: `process._eval` is undocumented; used here as evidence of `node -e ...` differentiating between immediate eval vs file-bound scripts
// eslint-disable-next-line functional/immutable-data
adapter.meta.mainFilename = typeof process._eval === 'undefined' ? process.argv[1] : '';

const default_ = _;
export default default_;
2 changes: 1 addition & 1 deletion src/lib/OSPaths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type moduleInternals_ = typeof module_ & {
};

test('api', (t) => {
const api = ['home', 'temp'];
const api = ['home', 'temp', 'main'];

t.is(typeof module_, 'function');
t.deepEqual(Object.keys(module_).sort(), api.sort());
Expand Down
3 changes: 3 additions & 0 deletions src/lib/OSPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type OSPaths = {
(): OSPaths;
readonly home: () => string | undefined;
readonly temp: () => string;
readonly main: () => string;
};

const { env } = process;
Expand Down Expand Up @@ -85,6 +86,8 @@ class _OSPaths {
OSPaths.home = extension.home;
OSPaths.temp = extension.temp;

OSPaths.main = () => adapter.meta.mainFilename;

return OSPaths;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/platform-adapters/_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export namespace Platform {
export type Adapter = {
readonly env: { readonly get: (_: string) => string | undefined };
readonly meta: { readonly mainFilename: string };
readonly os: { readonly homedir?: () => string; readonly tmpdir?: () => string };
readonly path: {
readonly join: (..._: readonly string[]) => string;
Expand Down
1 change: 1 addition & 0 deletions src/platform-adapters/deno.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const adapter: Platform.Adapter = {
env: { get: Deno.env.get },
// Deno (as of v1.6) has no built-in implementation for homedir() or tmpdir()
os: {}, // * module is tolerant of missing homedir()/tmpdir() functions
meta: { mainFilename: Deno.mainModule },
path,
process: { platform: Deno.build.os },
};
3 changes: 3 additions & 0 deletions src/platform-adapters/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const adapter: Platform.Adapter = {
return process.env[s];
},
},
meta: {
mainFilename: process.mainModule?.filename || '',
},
os,
path,
process,
Expand Down
2 changes: 1 addition & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const settledSupportForESMs =
// Integration tests

test('api', (t) => {
const api = ['home', 'temp'];
const api = ['home', 'temp', 'main'];

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

0 comments on commit b7c05a9

Please sign in to comment.