Skip to content

Commit 8e23bd0

Browse files
author
Ovidiu Barabula
committed
feat(config): add ConfigManager
1 parent d9ae151 commit 8e23bd0

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

src/config-manager/index.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { assert, expect } from 'chai';
2+
import 'mocha';
3+
import * as path from 'path';
4+
import ConfigManagerFactory, { IConfigManager } from './index';
5+
import { Config, ConfigReaderFactory, IConfigReader } from './package-json-config-reader';
6+
7+
describe('ConfigManager', () => {
8+
const testDir = '/tmp/tests/';
9+
const configFile = 'package.json';
10+
let configManager: IConfigManager;
11+
12+
beforeEach(async () => configManager = await ConfigManagerFactory('frontvue'));
13+
14+
15+
it('instantiates', async () => {
16+
expect(configManager).to.be.an('object').to.have.all.keys('has', 'set', 'get');
17+
});
18+
19+
20+
it('instantiates with custom config reader', async () => {
21+
const customReader: ConfigReaderFactory = (namespace: string) => {
22+
let config: Config = {};
23+
return {
24+
fetch(): Promise<Config> {
25+
return Promise.resolve(config);
26+
},
27+
update(object: object): Promise<boolean> {
28+
config = {...config, ...object};
29+
return Promise.resolve(true);
30+
},
31+
};
32+
};
33+
34+
const customConfigManager = await ConfigManagerFactory('frontvue', customReader);
35+
expect(customConfigManager).to.be.an('object').to.have.all.keys('has', 'set', 'get');
36+
});
37+
38+
39+
it('gets all options', async () => {
40+
expect(configManager.get()).to.be.an('object');
41+
});
42+
43+
44+
it('sets an option', async () => {
45+
configManager.set('key', 'value');
46+
expect(configManager.get('key')).to.equal('value');
47+
});
48+
49+
it('gets one option', async () => {
50+
expect(configManager.get('key')).to.equal('value');
51+
});
52+
53+
54+
it('checks if option exists', async () => {
55+
expect(configManager.has('key')).to.be.true;
56+
});
57+
});

src/config-manager/index.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Name: config-manager.ts
3+
* Description: Config Manager Factory
4+
* Author: Ovidiu Barabula <lectii2008@gmail.com>
5+
* @since 0.1.0
6+
*/
7+
8+
import PackageJsonConfigReader, {
9+
Config,
10+
ConfigReaderFactory,
11+
IConfigReader,
12+
} from './package-json-config-reader';
13+
14+
15+
export interface IConfigManager {
16+
has(key: string): boolean;
17+
get(key?: string): any;
18+
set(key: string, value: any): Promise<boolean>;
19+
}
20+
21+
/**
22+
* Create Configuration manager
23+
* @param namespace Configuration namespace, usually app name (used in package.json { config: { <namespace>: {} } })
24+
* @param customReader Custom ConfigReader
25+
*/
26+
async function ConfigManagerFactory(
27+
namespace: string,
28+
customReader?: ConfigReaderFactory,
29+
): Promise<IConfigManager> {
30+
let configReader: IConfigReader;
31+
32+
// Check for custom config reader
33+
if (customReader && typeof customReader === 'function') {
34+
// Initialize custom config reader
35+
configReader = customReader(namespace);
36+
} else {
37+
// If not, stick with the default config reader
38+
configReader = await PackageJsonConfigReader(namespace);
39+
}
40+
41+
// Get the configuration contents
42+
const config: Config = await configReader.fetch();
43+
44+
45+
/**
46+
* Check if key exists in config
47+
* @param key Configuration option key
48+
*/
49+
function has(key: string): boolean {
50+
return config.hasOwnProperty(key);
51+
}
52+
53+
54+
/**
55+
* Retrieve value from configuration
56+
* @param key Configuration option key
57+
*/
58+
function get(key?: string): Config | any {
59+
if (typeof key === 'undefined') {
60+
return config;
61+
}
62+
63+
return config[key];
64+
}
65+
66+
67+
/**
68+
* Set new value for configuration option
69+
* @param key Configuration option key
70+
* @param value New value to be set
71+
*/
72+
async function set(key: string, value: any): Promise<boolean> {
73+
config[key] = value;
74+
const saved = await configReader.update(config);
75+
return saved;
76+
}
77+
78+
// Returning config manager public API
79+
return Object.freeze({
80+
get,
81+
has,
82+
set,
83+
});
84+
}
85+
86+
export default ConfigManagerFactory;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy