feat: Introduce minimal Feature for app paths

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>
Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-tursoandJanne Savolainen committed 2023-05-24 14:45:52 +03:00
1 parent dd62e034b7
commit 5101101da2
12 files changed
+187

No files matched your search

@@ -0,0 +1,28 @@
import { getInjectionToken } from "@ogre-tools/injectable";
export const pathNames = [
"home",
"appData",
"userData",
"cache",
"temp",
"exe",
"module",
"desktop",
"documents",
"downloads",
"music",
"pictures",
"videos",
"logs",
"crashDumps",
"recent",
] as const;
export type PathName = (typeof pathNames)[number];
export type AppPaths = Record<PathName, string>;
export const appPathsInjectionToken = getInjectionToken<AppPaths>({
id: "app-paths-token",
});
@@ -0,0 +1,15 @@
import { autoRegister } from "@ogre-tools/injectable-extension-for-auto-registration";
import { getFeature } from "@k8slens/feature-core";
export const appPathsFeature = getFeature({
id: "app-paths",
register: (di) => {
autoRegister({
di,
targetModule: module,
getRequireContexts: () => [require.context("./", true, /\.injectable\.(ts|tsx)$/)],
});
},
});
@@ -0,0 +1,20 @@
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
import path from "path";
export type JoinPaths = (...args: string[]) => string;
export const joinPathsInjectionToken = getInjectionToken<JoinPaths>({
id: "join-paths-injection-token",
});
const joinPathsInjectable = getInjectable({
id: "join-paths",
instantiate: (): JoinPaths => path.join,
// This causes side effect e.g. Windows uses different separator than e.g. linux
causesSideEffects: true,
injectionToken: joinPathsInjectionToken,
});
export default joinPathsInjectable;
@@ -0,0 +1,20 @@
import { createContainer, DiContainer } from "@ogre-tools/injectable";
import { registerFeature } from "@k8slens/feature-core";
import { joinPathsInjectionToken } from "./join-paths.injectable";
import { appPathsFeature } from "../feature";
import path from "path";
describe("join-paths", () => {
let di: DiContainer;
let joinPaths: (...args: string[]) => string;
beforeEach(() => {
di = createContainer("irrelevant");
registerFeature(di, appPathsFeature);
joinPaths = di.inject(joinPathsInjectionToken);
});
it("is the native function", () => {
expect(joinPaths).toBe(path.join);
});
});
@@ -0,0 +1,40 @@
import type { DiContainer } from "@ogre-tools/injectable";
import joinPathsInjectable from "../join-paths/join-paths.injectable";
import path from "path";
import { getInjectable } from "@ogre-tools/injectable";
import { appPathsInjectionToken } from "../app-paths-injection-token";
const doGlobalOverrides = (di: DiContainer) => {
// Note: implementation of app-paths is still in OpenLens.
// Therefore, a stub is registered in its place to serve spirit of unit testing.
const appPathsStubInjectable = getInjectable({
id: "app-paths-stub",
instantiate: () => ({
appData: "some-app-data-directory",
cache: "some-cache-directory",
crashDumps: "some-crash-dumps-directory",
desktop: "some-desktop-directory",
documents: "some-documents-directory",
downloads: "some-downloads-directory",
exe: "some-exe-directory",
home: "some-home-directory",
logs: "some-logs-directory",
module: "some-module-directory",
music: "some-music-directory",
pictures: "some-pictures-directory",
recent: "some-recent-directory",
temp: "some-temp-directory",
videos: "some-videos-directory",
userData: "some-user-data-directory",
}),
injectionToken: appPathsInjectionToken,
});
di.register(appPathsStubInjectable);
di.override(joinPathsInjectable, () => path.posix.join);
};
export const testUtils = { doGlobalOverrides };