Skip to content

Commit

Permalink
feat: cliamrc.js (#26)
Browse files Browse the repository at this point in the history
* feat: .cliamrc as js to allow variables in config file

* fix: create .cliamrc.js on the fly before to compile

* fix: copyFileSync reference

* fix: copyFileSync file path
  • Loading branch information
steve-lebleu committed Feb 22, 2024
1 parent 4b36739 commit 7971286
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 40 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ jobs:
node-version: '18.19.0'
- name: Install NPM dependencies
run: npm i [email protected] -g && npm i
- name: Compile Typescript files
run: tsc
- name: Create .env file
run: |
touch .env
Expand All @@ -40,6 +38,10 @@ jobs:
echo SMTP_USERNAME = "${{ secrets.SMTP_USERNAME }}" >> .env
echo SMTP_PASSWORD = "${{ secrets.SMTP_PASSWORD }}" >> .env
echo SPARKPOST_API_KEY = "${{ secrets.SPARKPOST_API_KEY }}" >> .env
- name: Create .cliamrc.js file
run: cp ./test/fixtures/cliamrc.js ./.cliamrc.js
- name: Compile Typescript files
run: tsc
- name: Execute units tests
run: npm run ci:test
- name: Upload reports artifact
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ jobs:
node-version: '18.19.0'
- name: Install NPM dependencies
run: npm i [email protected] -g && npm i
- name: Compile Typescript files
run: tsc
- name: Create .env file
run: |
touch .env
Expand All @@ -45,6 +43,10 @@ jobs:
echo SMTP_USERNAME = "${{ secrets.SMTP_USERNAME }}" >> .env
echo SMTP_PASSWORD = "${{ secrets.SMTP_PASSWORD }}" >> .env
echo SPARKPOST_API_KEY = "${{ secrets.SPARKPOST_API_KEY }}" >> .env
- name: Create .cliamrc.js file
run: cp ./test/fixtures/cliamrc.js ./.cliamrc.js
- name: Compile Typescript files
run: tsc
- name: Execute units tests
run: npm run ci:test
- name: Publish to coveralls.io
Expand Down
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'

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: 2 additions & 4 deletions test/00-bootstrap.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require('dotenv').config();

const { writeFileSync } = require('fs');
const { copyFileSync } = 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 7971286

Please sign in to comment.