Skip to content

Commit

Permalink
Make home directory lookup lazy (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
neezer authored Dec 20, 2022
1 parent fcc8b3a commit 2b03532
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions home-directory-browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const homeDirectory = '';
const getHomeDirectory = () => '';

export default homeDirectory;
export default getHomeDirectory;
4 changes: 2 additions & 2 deletions home-directory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os from 'node:os';

const homeDirectory = os.homedir().replace(/\\/g, '/');
const getHomeDirectory = () => os.homedir().replace(/\\/g, '/');

export default homeDirectory;
export default getHomeDirectory;
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import escapeStringRegexp from 'escape-string-regexp';
import homeDirectory from '#home-directory';
import getHomeDirectory from '#home-directory';

const extractPathRegex = /\s+at.*[(\s](.*)\)?/;
const pathRegex = /^(?:(?:(?:node|node:[\w/]+|(?:(?:node:)?internal\/[\w/]*|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.js)?:\d+:\d+)|native)/;

export default function cleanStack(stack, {pretty = false, basePath} = {}) {
const basePathRegex = basePath && new RegExp(`(file://)?${escapeStringRegexp(basePath.replace(/\\/g, '/'))}/?`, 'g');
const homeDirectory = pretty ? getHomeDirectory() : '';

if (typeof stack !== 'string') {
return undefined;
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os from 'node:os';
import test from 'ava';
import getHomeDirectoryNode from './home-directory.js';
import getHomeDirectoryBrowser from './home-directory-browser.js';
import cleanStack from './index.js';

test('default', t => {
Expand Down Expand Up @@ -270,3 +272,8 @@ test('handle undefined', t => {
const expected = undefined;
t.is(cleanStack(stack, {pretty: true}), expected);
});

test('exports for home-directory files match', t => {
t.is(typeof getHomeDirectoryNode, 'function');
t.is(typeof getHomeDirectoryBrowser, 'function');
});

0 comments on commit 2b03532

Please sign in to comment.