Skip to content

Commit

Permalink
CR Comments 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ShriekinNinja committed Mar 5, 2024
2 parents 5f1ac24 + 57f0631 commit be6ff48
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 18 deletions.
57 changes: 48 additions & 9 deletions ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions ts/packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@
"pg": "^8.11.3",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.6",
"typeorm": "^0.3.17"
"typeorm": "^0.3.17",
"xml2js": "^0.6.2"
},
"devDependencies": {
"@types/ini": "^1.3.34",
"@types/lodash": "^4.14.202",
"@types/rox-node": "^5.0.3"
"@types/rox-node": "^5.0.3",
"@types/xml2js": "^0.4.14"
}
}
1 change: 1 addition & 0 deletions ts/packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {

export * from './stores/JsonFile';
export * from './stores/IniFile';
export * from './stores/XmlFile';

export * from './stores/AWSParameterStore';
export * from './stores/AWSSecretsManager';
Expand Down
95 changes: 95 additions & 0 deletions ts/packages/node/src/stores/XmlFile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { promises as fs } from 'fs';
import path from 'path';
import { XmlFileConfigStore } from './XmlFile';

describe('XmlFileConfigStore', () => {
const testXmlFilePath = path.join(__dirname, 'test.xml');

beforeEach(async () => {
await fs.writeFile(
testXmlFilePath,
`
<?xml version="1.1" encoding="UTF-8"?>
<root>
<config>
<set>dev</set>
<key>GREETING</key>
<value>hey</value>
</config>
<config>
<set>prod</set>
<key>SUBJECT</key>
<value>world</value>
</config>
</root>
`,
);
});

afterEach(async () => {
await fs.unlink(testXmlFilePath);
});

test('should read configurations from XML file', async () => {
const store = new XmlFileConfigStore({ path: testXmlFilePath });
const configs = await store.get([
{ set: 'dev', key: 'GREETING' },
{ set: 'prod', key: 'SUBJECT' },
]);

expect(configs).toEqual([
{ set: 'dev', key: 'GREETING', value: 'hey' },
{ set: 'prod', key: 'SUBJECT', value: 'world' },
]);
});

const writeConfigs = async (store: XmlFileConfigStore) => {
const configsToWrite = [
{ set: 'dev', key: 'GREETING', value: 'hey' },
{ set: 'prod', key: 'SUBJECT', value: 'world' },
];
await store.set(configsToWrite);

// * Read the file again to verify the write operation
const newConfigs = await store.get([
{ set: 'dev', key: 'GREETING' },
{ set: 'prod', key: 'SUBJECT' },
]);
expect(newConfigs).toEqual(configsToWrite);
};

test('should write configurations to new XML file', async () => {
await fs.unlink(testXmlFilePath);
const store = new XmlFileConfigStore({ path: testXmlFilePath });
await store.init();
await writeConfigs(store);
});

test('should write configurations to an existing XML file', async () => {
const store = new XmlFileConfigStore({ path: testXmlFilePath });
await writeConfigs(store);
});

test('should query new XML file dev configurations', async () => {
await fs.unlink(testXmlFilePath);
const store = new XmlFileConfigStore({ path: testXmlFilePath });
await store.init();
const queries = [{ set: 'dev', key: 'GREETING' }];
const results = await store.get(queries);
expect(results).toEqual([]);
});

test('should query global configurations', async () => {
const store = new XmlFileConfigStore({ path: testXmlFilePath });
const queries = [{ set: 'dev', key: 'GREETING' }];
const results = await store.get(queries);
expect(results).toEqual([{ set: 'dev', key: 'GREETING', value: 'hey' }]);
});

test('should query configurations', async () => {
const store = new XmlFileConfigStore({ path: testXmlFilePath });
const queries = [{ set: 'prod', key: 'SUBJECT' }];
const results = await store.get(queries);
expect(results).toEqual([{ set: 'prod', key: 'SUBJECT', value: 'world' }]);
});
});
33 changes: 33 additions & 0 deletions ts/packages/node/src/stores/XmlFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import xml2js from 'xml2js';
import { type Config } from '@configu/ts';
import { FileConfigStore } from './File';

export type XmlFileConfigStoreConfiguration = { path: string; builderOptions?: xml2js.BuilderOptions };

export class XmlFileConfigStore extends FileConfigStore {
private builder: xml2js.Builder;
constructor({ path, builderOptions }: XmlFileConfigStoreConfiguration) {
const builder = new xml2js.Builder(builderOptions);
const initialFileState = builder.buildObject({ root: { config: [] } });
super('xml-file', { path, initialFileState });
this.builder = builder;
}

parse(fileContent: string): Config[] {
let output: Config[] = [];
const parser = new xml2js.Parser({ explicitArray: false });
parser.parseString(fileContent, (error, result) => {
if (error) {
throw error;
}
output = result.root.config ?? [];
});

return output;
}

stringify(nextConfigs: Config[]): string {
const xmlObject = { root: { config: nextConfigs } };
return this.builder.buildObject(xmlObject);
}
}
10 changes: 3 additions & 7 deletions ts/packages/ts/src/commands/ExportCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,11 @@ export class ExportCommand extends Command<ExportCommandReturn> {
return _.pickBy(pipe, filter);
}

private mapPipe(pipe: EvalCommandReturn) {
return _.mapValues(pipe, (current) => current.result.value);
}

async run() {
const { pipe } = this.parameters;
const filteredPipe = this.filterPipe(pipe);
const mappedPipe = this.mapPipe(filteredPipe);
const keyMutatedMappedPipe = this.mutateKeys(mappedPipe);
return keyMutatedMappedPipe;
const configDict = _.mapValues(filteredPipe, (current) => current.result.value);
const keyMutatedConfigDict = this.mutateKeys(configDict);
return keyMutatedConfigDict;
}
}

0 comments on commit be6ff48

Please sign in to comment.