mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
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:
parent
dd62e034b7
commit
5101101da2
6
packages/utility-features/app-paths/.eslintrc.json
Normal file
6
packages/utility-features/app-paths/.eslintrc.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "@k8slens/eslint-config/eslint",
|
||||
"parserOptions": {
|
||||
"project": "./tsconfig.json"
|
||||
}
|
||||
}
|
||||
1
packages/utility-features/app-paths/.prettierrc
Normal file
1
packages/utility-features/app-paths/.prettierrc
Normal file
@ -0,0 +1 @@
|
||||
"@k8slens/eslint-config/prettier"
|
||||
12
packages/utility-features/app-paths/index.ts
Normal file
12
packages/utility-features/app-paths/index.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { appPathsFeature } from "./src/feature";
|
||||
|
||||
export type { JoinPaths } from "./src/join-paths/join-paths.injectable";
|
||||
export { joinPathsInjectionToken } from "./src/join-paths/join-paths.injectable";
|
||||
|
||||
export { appPathsInjectionToken, pathNames } from "./src/app-paths-injection-token";
|
||||
|
||||
export type { AppPaths, PathName } from "./src/app-paths-injection-token";
|
||||
|
||||
export { testUtils } from "./src/test-utils";
|
||||
|
||||
export default appPathsFeature;
|
||||
1
packages/utility-features/app-paths/jest.config.js
Normal file
1
packages/utility-features/app-paths/jest.config.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = require("@k8slens/jest").monorepoPackageConfig(__dirname).configForNode;
|
||||
39
packages/utility-features/app-paths/package.json
Normal file
39
packages/utility-features/app-paths/package.json
Normal file
@ -0,0 +1,39 @@
|
||||
{
|
||||
"name": "@k8slens/app-paths",
|
||||
"private": false,
|
||||
"version": "0.1.0",
|
||||
"description": "TBD",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/lensapp/lens.git"
|
||||
},
|
||||
"type": "commonjs",
|
||||
"author": {
|
||||
"name": "OpenLens Authors",
|
||||
"email": "info@k8slens.dev"
|
||||
},
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/lensapp/lens",
|
||||
"scripts": {
|
||||
"build": "lens-webpack-build",
|
||||
"clean": "rimraf dist/",
|
||||
"test:unit": "jest --coverage --runInBand",
|
||||
"lint": "lens-lint",
|
||||
"lint:fix": "lens-lint --fix"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@k8slens/feature-core": "^6.5.0-alpha.5",
|
||||
"@ogre-tools/injectable": "^16.1.0",
|
||||
"@ogre-tools/injectable-extension-for-auto-registration": "^16.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@k8slens/eslint-config": "^6.5.0-alpha.3",
|
||||
"@k8slens/react-testing-library-discovery": "^1.0.0-alpha.4",
|
||||
"@k8slens/webpack": "^6.5.0-alpha.6"
|
||||
}
|
||||
}
|
||||
@ -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",
|
||||
});
|
||||
15
packages/utility-features/app-paths/src/feature.ts
Normal file
15
packages/utility-features/app-paths/src/feature.ts
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
40
packages/utility-features/app-paths/src/test-utils/index.ts
Normal file
40
packages/utility-features/app-paths/src/test-utils/index.ts
Normal file
@ -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 };
|
||||
4
packages/utility-features/app-paths/tsconfig.json
Normal file
4
packages/utility-features/app-paths/tsconfig.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "@k8slens/typescript/config/base.json",
|
||||
"include": ["**/*.ts"]
|
||||
}
|
||||
1
packages/utility-features/app-paths/webpack.config.js
Normal file
1
packages/utility-features/app-paths/webpack.config.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = require("@k8slens/webpack").configForNode;
|
||||
Loading…
Reference in New Issue
Block a user