Skip to content

Commit

Permalink
refactor: add method to get default storage paths for more flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Nov 16, 2023
1 parent 69fb68a commit 107f9b1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
6 changes: 6 additions & 0 deletions packages/synchronizer/src/handlers/storageHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import envPaths from 'env-paths';
import {Document, parse} from 'yaml';
import {mkdirp} from 'mkdirp';
import {existsSync, readFileSync} from 'fs';
import {readFile, writeFile} from 'fs/promises';
import {dirname, join, normalize} from 'path';
import {DEFAULT_STORAGE_CONFIG_FOLDER} from '../constants';

export abstract class StorageHandler<TData> {
constructor(private _storageFolderPath: string) {}
Expand Down Expand Up @@ -71,3 +73,7 @@ export abstract class StorageHandler<TData> {
}
}
}

export function getDefaultStorageConfigPaths(suffix = '') {
return envPaths(DEFAULT_STORAGE_CONFIG_FOLDER, {suffix});
}
7 changes: 3 additions & 4 deletions packages/synchronizer/src/handlers/storageHandlerAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import envPaths from 'env-paths';
import {StorageHandler} from './storageHandler.js';
import {DEFAULT_STORAGE_CONFIG_FILE_AUTH, DEFAULT_STORAGE_CONFIG_FOLDER} from '../constants.js';
import {StorageHandler, getDefaultStorageConfigPaths} from './storageHandler.js';
import {DEFAULT_STORAGE_CONFIG_FILE_AUTH} from '../constants.js';
import type {TokenSet} from './deviceFlowHandler.js';

export type TokenType = 'Bearer' | 'ApiKey';
Expand Down Expand Up @@ -28,7 +27,7 @@ export class StorageHandlerAuth extends StorageHandler<StorageAuthFormat> {
private _defaultFileName: string;

constructor(
storageFolderPath: string = envPaths(DEFAULT_STORAGE_CONFIG_FOLDER, {suffix: ''}).config,
storageFolderPath: string = getDefaultStorageConfigPaths().config,
defaultFileName: string = DEFAULT_STORAGE_CONFIG_FILE_AUTH
) {
super(storageFolderPath);
Expand Down
6 changes: 2 additions & 4 deletions packages/synchronizer/src/handlers/storageHandlerPolicy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import envPaths from 'env-paths';
import {Document} from 'yaml';
import {StorageHandler} from './storageHandler.js';
import {DEFAULT_STORAGE_CONFIG_FOLDER} from '../constants.js';
import {StorageHandler, getDefaultStorageConfigPaths} from './storageHandler.js';
import type {ValidationConfig} from '@monokle/types';

export type StoragePolicyFormat = ValidationConfig;

export class StorageHandlerPolicy extends StorageHandler<StoragePolicyFormat> {
constructor(storageFolderPath: string = envPaths(DEFAULT_STORAGE_CONFIG_FOLDER, {suffix: ''}).cache) {
constructor(storageFolderPath: string = getDefaultStorageConfigPaths().cache) {
super(storageFolderPath);
}

Expand Down

0 comments on commit 107f9b1

Please sign in to comment.