Skip to content

Commit

Permalink
feat: .cliamrc as js to allow variables in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-lebleu committed Feb 22, 2024
1 parent 4b36739 commit bef5876
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ dist/

# Test configuration
.cliamrc.json
.cliamrc-bk.json
.test.js
.cliamrc.js
.cliam-test.js
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ To improve and facilitate the implementation, flexibility and maintenance of tra
- [Beneficiary use cases](#beneficiary-use-cases)
- [Supported web API providers](#supported-web-api-providers)
- [Licence](#licence)
- [Documentation](https://github.com/steve-lebleu/cliam/wiki)

<h2 id="requirements">> Requirements</h2>

Expand All @@ -46,16 +47,16 @@ To improve and facilitate the implementation, flexibility and maintenance of tra

### Configure

Create a *.cliamrc.json* file on the root of your project.
Create a *.cliamrc.js* module on the root of your project.

```shell
> touch .cliamrc.json
> touch .cliamrc.js
```

Define a minimalist configuration in *.cliamrc.json* newly created:
Define a minimalist configuration in *.cliamrc.js* newly created:

```json
{
```javascript
module.exports = {
"sandbox": true,
"transporters": [
{
Expand Down
4 changes: 2 additions & 2 deletions src/classes/cliam.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class Cliam {
async mail(event: Event|string, payload: IPayload): Promise<SendingResponse|SendingError> {
const key = payload.transporterId || Object.keys(Container.transporters).shift();
if (!this.mailers[key]) {
this.mailers[key] = new Mailer(Container.transporters[key]);
throw new Error('transporterId not found in cliamrc configuration');
}
return this.mailers[key].send(event, payload)
return this.mailers[key].send(event, payload);
}
}

Expand Down
31 changes: 8 additions & 23 deletions src/services/container.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as Chalk from 'chalk';

import { existsSync, readFileSync } from 'fs';

import * as CliamCfg from './../../.cliamrc.js'

Check failure on line 5 in src/services/container.service.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module './../../.cliamrc.js' or its corresponding type declarations.

import { Transporter } from './../transporters/transporter.class';
import { TransporterFactory } from './../transporters/transporter.factory';
import { ClientConfiguration } from './../classes/client-configuration.class';
Expand Down Expand Up @@ -34,7 +36,7 @@ class Container {
/**
* @description Path of the cliamrc configuration. Always at root of the project.
*/
private readonly PATH: string = `${process.cwd()}/.cliamrc.json`;
private readonly PATH: string = `${process.cwd()}/.cliamrc.js`;

/**
* @description Don't come here motherfucker
Expand All @@ -59,8 +61,11 @@ class Container {
* @returns {Container} Container instance
*/
private set(): Container {

this.configuration = new ClientConfiguration( this.validates( this.read(this.PATH) ) );
if (!existsSync(this.PATH)) {
process.stdout.write( Chalk.bold.red('.cliamrc.js file cannot be found\n') );
process.exit(0);
}
this.configuration = new ClientConfiguration( this.validates( CliamCfg ) );

this.transporters = this.configuration.transporters.reduce((result, transporterDefinition) => {
result[transporterDefinition.id] = TransporterFactory.get(this.configuration.variables, transporterDefinition);
Expand All @@ -70,26 +75,6 @@ class Container {
return this;
}

/**
* @description Read the cliamrc configuration file and return it
*
* @param path Path of the cliamrc file
*
* @returns {Record<string,unknown>} Object containing client configuration values
*/
private read(path: string): Record<string,unknown> {
if (!existsSync(path)) {
process.stdout.write( Chalk.bold.red('.cliamrc.json file cannot be found\n') );
process.exit(0);
}
const content = readFileSync(path, { encoding: 'utf-8' });
if (!content) {
process.stdout.write( Chalk.bold.red('.cliamrc.json content not found\n') );
process.exit(0);
}
return JSON.parse(content) as Record<string,unknown>;
}

/**
* @description Validates the client configuration setup
*
Expand Down
6 changes: 1 addition & 5 deletions test/00-bootstrap.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require('dotenv').config();

const { writeFileSync } = require('fs');

const { cliamrc } = require(process.cwd() + '/test/fixtures');

writeFileSync(`${process.cwd()}/.cliamrc.json`, JSON.stringify(cliamrc, null, 2), { encoding: 'utf-8' });
copyFileSync(`${process.cwd()}/test/fixtures/.cliamrc.js`, `${process.cwd()}/.cliamrc.js`);

describe('Units tests', () => {
require('./01-client-configuration.test');
Expand Down

0 comments on commit bef5876

Please sign in to comment.