Skip to content

Commit

Permalink
refactor Adapt as a function (with OSPaths property)
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Jan 31, 2021
1 parent faab2ac commit 04509bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// spell-checker:ignore maint rivy
import { OSPathsAdaptionBuilder_ } from './lib/OSPaths';
import { Adapt, OSPaths } from './lib/OSPaths';
import { adapter } from './platform-adapters/node';

const default_ = OSPathsAdaptionBuilder_(adapter);
export default default_;
const default_ = Adapt(adapter).OSPaths as OSPaths;
export { OSPaths, default_ as default };

const haveModuleExports_ = typeof module === 'object' && module.exports;
// ## maint ~ [2020-12-23; rivy] `else` clause *is* tested, but coverage is not visible via `nyc` (currently unable to instrument ESM/.mjs correctly)
Expand Down
67 changes: 29 additions & 38 deletions src/lib/OSPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,16 @@ function isEmpty(s: string | null | undefined): boolean {
return !s; // reminder: JS "falsey" == [undefined, null, NaN, 0, '', false]
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Adapt {
export function isWinOS(adapter_: Platform.Adapter) {
return /^win/i.test(adapter_.process.platform);
}
export function Adapt(adapter_: Platform.Adapter) {
const { env, os, path } = adapter_;

export function normalizePath(adapter_: Platform.Adapter) {
return (path_: string | undefined): string | undefined => {
return path_ ? adapter_.path.normalize(adapter_.path.join(path_, '.')) : void 0;
};
}
const isWinOS = /^win/i.test(adapter_.process.platform);

export function home(adapter_: Platform.Adapter) {
const { env, os, path } = adapter_;

const normalizePath = Adapt.normalizePath(adapter_);
function normalizePath(path_: string | undefined): string | undefined {
return path_ ? adapter_.path.normalize(adapter_.path.join(path_, '.')) : void 0;
}

function home() {
const posix = () =>
normalizePath((typeof os.homedir === 'function' ? os.homedir() : void 0) || env.get('HOME'));

Expand All @@ -52,19 +45,15 @@ namespace Adapt {
return normalizePath(priorityList.find((v) => !isEmpty(v)));
};

return Adapt.isWinOS(adapter_) ? windows : posix;
return isWinOS ? windows() : posix();
}

export function temp(adapter_: Platform.Adapter) {
const { env, os, path } = adapter_;

const normalizePath = Adapt.normalizePath(adapter_);

function temp() {
function joinPathToBase(base: string | undefined, segments: readonly string[]) {
return base ? path.join(base, ...segments) : void 0;
}

const posix = () => {
function posix() {
const fallback = '/tmp';
const priorityList = [
typeof os.tmpdir === 'function' ? os.tmpdir() : void 0,
Expand All @@ -73,39 +62,41 @@ namespace Adapt {
env.get('TMP'),
];
return normalizePath(priorityList.find((v) => !isEmpty(v))) || fallback;
};
}

const windows = () => {
function windows() {
const fallback = 'C:\\Temp';
const priorityListLazy = [
os.tmpdir,
() => env.get('TEMP'),
() => env.get('TMP'),
() => joinPathToBase(env.get('LOCALAPPDATA'), ['Temp']),
() => joinPathToBase(Adapt.home(adapter_)(), ['AppData', 'Local', 'Temp']),
() => joinPathToBase(home(), ['AppData', 'Local', 'Temp']),
() => joinPathToBase(env.get('ALLUSERSPROFILE'), ['Temp']),
() => joinPathToBase(env.get('SystemRoot'), ['Temp']),
() => joinPathToBase(env.get('windir'), ['Temp']),
() => joinPathToBase(env.get('SystemDrive'), ['\\', 'Temp']),
];
const v = priorityListLazy.find((v) => v && !isEmpty(v()));
return (v && normalizePath(v())) || fallback;
};
}

return Adapt.isWinOS(adapter_) ? windows : posix;
return isWinOS ? windows() : posix();
}
}

export function OSPathsAdaptionBuilder_(adapter_: Platform.Adapter): OSPaths {
function OSPaths(): OSPaths {
return obj as OSPaths;
// eslint-disable-next-line functional/no-class
class OSPaths_ {
constructor() {
function OSPaths(): OSPaths {
return new OSPaths_() as OSPaths;
}

OSPaths.home = home;
OSPaths.temp = temp;

return OSPaths;
}
}
const home = Adapt.home(adapter_);
const temp = Adapt.temp(adapter_);

const obj = Object.assign(OSPaths, {
home,
temp,
}) as OSPaths;
return obj as OSPaths;

return { OSPaths: new OSPaths_() as OSPaths };
}
4 changes: 2 additions & 2 deletions src/mod.deno.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// spell-checker:ignore Deno
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { OSPathsAdaptionBuilder_ } from '../dist/esm/lib/OSPaths.mjs';
import { Adapt } from '../dist/esm/lib/OSPaths.mjs';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { adapter } from './platform-adapters/deno.deno.ts';

const default_ = OSPathsAdaptionBuilder_(adapter);
const default_ = Adapt(adapter).OSPaths;
export default default_;

0 comments on commit 04509bb

Please sign in to comment.