Skip to content

Commit 0e6fc3c

Browse files
author
Ovidiu Barabula
committed
feat(config): add destroy method to PackageJsonConfigReader
1 parent 0266796 commit 0e6fc3c

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/config-manager/index.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ describe('ConfigManager', () => {
2222
const customReader: ConfigReaderFactory = (namespace: string) => {
2323
let config: Config = {};
2424
return {
25+
destroy(): Promise<Config> {
26+
const oldConfig = config;
27+
delete config[namespace];
28+
return Promise.resolve(oldConfig);
29+
},
2530
fetch(): Promise<Config> {
2631
return Promise.resolve(config);
2732
},

src/config-manager/package-json-config-reader.spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ describe('PackageJsonConfigReader', () => {
4848

4949
it('instantiates with no custom filepath', async () => {
5050
mockfs.restore();
51-
const configReader = await PackageJsonConfigReader('frontvue');
51+
expect(await PackageJsonConfigReader('frontvue'))
52+
.to.be.an('object')
53+
.to.have.all.keys('destroy', 'fetch', 'update');
5254
});
5355

5456

@@ -75,17 +77,23 @@ describe('PackageJsonConfigReader', () => {
7577
it('instantiates with prior Frontvue config object', async () => {
7678
const filepath = path.join(testDir, priorConfiguredFile);
7779
const configReader = await PackageJsonConfigReader('frontvue', filepath);
78-
const config = await configReader.fetch();
79-
expect(config).to.eql({ key: 'value' });
80+
expect(await configReader.fetch()).to.eql({ key: 'value' });
8081
});
8182

8283

8384
it('updates config object', async () => {
8485
const filepath = path.join(testDir, noConfigFile);
8586
const configReader = await PackageJsonConfigReader('frontvue', filepath);
8687
const updated = await configReader.update({ key: 'value' });
87-
const config = await configReader.fetch();
88-
expect(config).to.eql({ key: 'value' });
88+
expect(await configReader.fetch()).to.eql({ key: 'value' });
89+
});
90+
91+
92+
it('removes config from file and returns it', async () => {
93+
const filepath = path.join(testDir, priorConfiguredFile);
94+
const configReader = await PackageJsonConfigReader('frontvue', filepath);
95+
expect(await configReader.destroy()).to.eql({ key: 'value' });
96+
expect(await configReader.fetch()).to.be.undefined;
8997
});
9098

9199

src/config-manager/package-json-config-reader.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Config {
1313
}
1414

1515
export interface IConfigReader {
16+
destroy(): Promise<Config>;
1617
fetch(): Promise<Config>;
1718
update(config: Config): Promise<boolean>;
1819
}
@@ -78,13 +79,29 @@ async function PackageJsonConfigReader(namespace: string, filepath?: string): Pr
7879
return updated;
7980
}
8081

82+
83+
/**
84+
* Remove configuration from file and return the removed config object
85+
*/
86+
async function destroy(): Promise<Config> {
87+
// Save existing config
88+
const fileContent = await configFile.read();
89+
// Delete config object
90+
delete packageJson.config[namespace];
91+
// Update the file
92+
await configFile.write(packageJson);
93+
return fileContent.config[namespace];
94+
}
95+
96+
8197
// If no configuration object is found, initialize with an empty object
82-
// if (!packageJson.hasOwnProperty('config') && !packageJson.config.hasOwnProperty(namespace)) {
8398
if (!hasNested(packageJson, `config.${namespace}`)) {
8499
await initializeEmptyConfig();
85100
}
86101

102+
87103
return Object.freeze({
104+
destroy,
88105
fetch,
89106
update,
90107
});

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