Skip to content

Commit 767e634

Browse files
committed
Exporting getBkperLocalConfig as library
1 parent 871be66 commit 767e634

File tree

6 files changed

+35
-16
lines changed

6 files changed

+35
-16
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
"author": "mael <[email protected]>",
1111
"license": "Apache-2.0",
1212
"private": false,
13+
"main": "./lib/index.js",
14+
"module": "./lib/index.js",
15+
"types": "./lib/index.d.ts",
16+
"type": "module",
1317
"scripts": {
1418
"clean": "rm -rf ./lib & rm -rf ./node_modules & wait",
19+
"prebuild": "bun install",
1520
"build": "run-s build:*",
1621
"build:clean": "gts clean",
1722
"build:compile": "tsc",
18-
"build:cleanup": "rimraf lib/**/*.map lib/*.map lib/**/*.d.ts lib/*.d.ts",
19-
"build:clean-dist": "rimraf dist",
23+
"prewatch": "bun run build",
2024
"watch": "tsc -w",
2125
"patch": "yarn version --patch",
2226
"minor": "yarn version --minor",
@@ -26,7 +30,7 @@
2630
},
2731
"dependencies": {
2832
"@google-cloud/local-auth": "^3.0.1",
29-
"bkper-js": "^1.0.0",
33+
"bkper-js": "^1.1.0",
3034
"commander": "^6.2.1",
3135
"dotenv": "^8.2.0",
3236
"google-auth-library": "^9.14.0"

src/auth/local-auth-service.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import {authenticate} from '@google-cloud/local-auth';
22
import fs from 'fs';
33
import { Credentials, OAuth2Client } from "google-auth-library";
44
import os from 'os';
5+
import { createRequire } from "module";
6+
import { fileURLToPath } from 'url';
7+
import path from 'path';
8+
9+
const require = createRequire(import.meta.url);
10+
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
11+
const __dirname = path.dirname(__filename); // get the name of the directory
512

613
const keys = require(`${__dirname}/keys.json`);
714

@@ -56,14 +63,13 @@ export async function getOAuthToken(): Promise<string> {
5663
localAuth.on('tokens', (tokens) => {
5764
if (tokens.refresh_token) {
5865
// store the refresh_token in my database!
59-
console.log(tokens.refresh_token);
6066
storeCredentials(tokens)
6167
}
6268
});
6369

6470
let token = await localAuth.getAccessToken();
6571

66-
return token.token;
72+
return token.token || '';
6773

6874
}
6975

src/cli.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env node
22

3-
import program from 'commander';
4-
import { getOAuthToken, login, logout } from './auth/local-auth-service';
53
import { App, Bkper } from 'bkper-js';
6-
var fs = require('fs');
7-
require('dotenv').config();
8-
4+
import program from 'commander';
5+
import fs from 'fs';
6+
import { login, logout } from './auth/local-auth-service.js';
7+
import { getBkperLocalConfig } from './index.js';
98

109
program
1110
.command('login')
@@ -29,10 +28,7 @@ program
2928
.action(async (options) => {
3029

3130
try {
32-
Bkper.setConfig({
33-
apiKeyProvider: async () => process.env.BKPER_API_KEY,
34-
oauthTokenProvider: () => getOAuthToken()
35-
})
31+
Bkper.setConfig(getBkperLocalConfig())
3632
const json: bkper.App = JSON.parse(fs.readFileSync('./bkperapp.json', 'utf8'));
3733
let app = new App(json)
3834
.setReadme(fs.readFileSync('./README.md', 'utf8'))

src/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Config } from 'bkper-js';
2+
import { getOAuthToken } from './auth/local-auth-service.js';
3+
import dotenv from 'dotenv';
4+
dotenv.config()
5+
6+
export function getBkperLocalConfig(): Config {
7+
return {
8+
apiKeyProvider: async () => process.env.BKPER_API_KEY || '',
9+
oauthTokenProvider: () => getOAuthToken()
10+
}
11+
}

tsconfig.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"compilerOptions": {
44
"rootDir": "src",
55
"outDir": "lib",
6+
"module": "ESNext",
7+
"moduleResolution": "Node",
8+
"resolveJsonModule": true,
69
"typeRoots" : ["node_modules/@bkper", "node_modules/@types" ],
7-
"strict": false,
10+
"strict": true,
811
"esModuleInterop": true,
912
"target": "es2015",
10-
"resolveJsonModule": true,
1113
"declaration": true,
1214
"sourceMap": true,
1315
"declarationMap": true

0 commit comments

Comments
 (0)