Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit 26e7fcf

Browse files
committed
feat: inject service
1 parent e754556 commit 26e7fcf

14 files changed

+3659
-18
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"indent": ["error", 4],
1919
"linebreak-style": ["error", "unix"],
2020
"quotes": ["error", "single"],
21-
"semi": ["error", "always"]
21+
"semi": ["error", "always"],
22+
"@typescript-eslint/explicit-function-return-type": ["error"]
2223
}
2324
}

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# node-packagekit
2+
23
node-packagekit is a library to integrate [packageKit](https://www.freedesktop.org/software/PackageKit/) with node applications.
4+
35
## Installation
6+
47
### Using NPM
8+
59
```shell
610
$ npm install packagekit
711
```
12+
813
### Using yarn
9-
```shell
14+
15+
```shell
1016
$ yarn add packagekit
1117
```
18+
1219
## How to use it
13-
(work in progress)
20+
21+
(work in progress)

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
"main": "dist/index.js",
1010
"types": "dist/index.d.ts",
1111
"scripts": {
12+
"prebuild": "rimraf dist",
1213
"build": "tsc",
1314
"lint": "eslint src/**/*.ts --fix",
1415
"format": "prettier --write .",
1516
"test": "jest",
1617
"test:coverage": "jest --coverage",
17-
"test:watch": "jest --watch"
18+
"test:watch": "jest --watch",
19+
"start": "ts-node src/"
1820
},
1921
"dependencies": {
20-
"dbus-native": "^0.4.0"
22+
"dbus-native": "^0.4.0",
23+
"reflect-metadata": "^0.1.13",
24+
"tsyringe": "^4.6.0"
2125
},
2226
"devDependencies": {
2327
"@types/jest": "^27.5.0",
@@ -29,7 +33,9 @@
2933
"eslint-plugin-import": "^2.25.2",
3034
"jest": "^28.1.0",
3135
"prettier": "^2.6.2",
36+
"rimraf": "^3.0.2",
3237
"ts-jest": "^28.0.2",
38+
"ts-node": "^10.7.0",
3339
"typescript": "^4.6.4"
3440
}
3541
}

src/di/service.registerer.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { container } from 'tsyringe';
2+
const dbus = require('dbus-native');
3+
import { Service } from '../types/service';
4+
5+
export const DI_TOKEN_SERVICE = 'service';
6+
7+
container.register<Service>(DI_TOKEN_SERVICE, {
8+
useFactory: () => {
9+
const bus = dbus.systemBus();
10+
return bus.getService('org.freedesktop.PackageKit');
11+
},
12+
});

src/index.spec.ts

-7
This file was deleted.

src/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
export const a: any = '1';
1+
import 'reflect-metadata';
2+
import { container } from 'tsyringe';
3+
import { InterfaceProvider } from './interface.provider';
4+
5+
const interfaceProvider = container.resolve(InterfaceProvider);
6+
7+
interfaceProvider
8+
.getPackageKitInterface()
9+
.then((packageKit) => {
10+
console.log('Success', packageKit);
11+
})
12+
.catch((err) => {
13+
console.error(err);
14+
});

src/interface.provider.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { inject, injectable } from 'tsyringe';
2+
import { DI_TOKEN_SERVICE } from './di/service.registerer';
3+
import { PackageKit } from './interface/packagekit.interface';
4+
import { Service } from './types/service';
5+
6+
@injectable()
7+
export class InterfaceProvider {
8+
constructor(@inject(DI_TOKEN_SERVICE) private service: Service) {}
9+
10+
getPackageKitInterface(): Promise<PackageKit> {
11+
return new Promise<PackageKit>((resolve, reject) => {
12+
const callback = (err: unknown, packageKit: PackageKit) => {
13+
if (!err) {
14+
resolve(packageKit);
15+
} else {
16+
reject(err);
17+
}
18+
};
19+
20+
this.service.getInterface<PackageKit>(
21+
'org/freedesktop/PackageKit',
22+
'org.freedesktop.PackageKit',
23+
callback
24+
);
25+
});
26+
}
27+
}

src/interface/packagekit.interface.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Transaction } from './transaction.interface';
2+
3+
export interface PackageKit {
4+
createTransaction(): Transaction;
5+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export interface Transaction {}

src/types/bus.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Service } from "./service";
2+
3+
export interface Bus {
4+
getService(id: string): Service;
5+
}

src/types/service.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface Service {
2+
getInterface<DbusInterface>(
3+
path: string,
4+
name: string,
5+
callback: (err: unknown, dbusInterface: DbusInterface) => void
6+
): void;
7+
}

tsconfig.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
22
"compilerOptions": {
33
"target": "es2016",
4-
"module": "ES2022",
4+
"module": "CommonJS",
55
"esModuleInterop": true,
66
"forceConsistentCasingInFileNames": true,
77
"strict": true,
88
"skipLibCheck": true,
99
"outDir": "dist",
10-
"declaration": true
10+
"declaration": true,
11+
"experimentalDecorators": true,
12+
"emitDecoratorMetadata": true,
13+
"moduleResolution": "Node"
1114
},
1215
"include": ["src"],
1316
"exclude": ["node_modules", "dist"]

0 commit comments

Comments
 (0)