From 797128660b2837df526249b630e5b3ddb3242449 Mon Sep 17 00:00:00 2001 From: Steve Lebleu Date: Thu, 22 Feb 2024 13:00:47 +0100 Subject: [PATCH] feat: cliamrc.js (#26) * 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 --- .github/workflows/build.yml | 6 ++++-- .github/workflows/release.yml | 6 ++++-- .gitignore | 4 ++-- README.md | 11 ++++++----- src/classes/cliam.class.ts | 4 ++-- src/services/container.service.ts | 31 ++++++++----------------------- test/00-bootstrap.test.js | 6 ++---- 7 files changed, 28 insertions(+), 40 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 06386e3..f2ca0ce 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,8 +19,6 @@ jobs: node-version: '18.19.0' - name: Install NPM dependencies run: npm i typescript@5.3.3 -g && npm i - - name: Compile Typescript files - run: tsc - name: Create .env file run: | touch .env @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e3ca4e4..05a02ce 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -24,8 +24,6 @@ jobs: node-version: '18.19.0' - name: Install NPM dependencies run: npm i typescript@5.3.3 -g && npm i - - name: Compile Typescript files - run: tsc - name: Create .env file run: | touch .env @@ -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 diff --git a/.gitignore b/.gitignore index ddc375f..567b2dc 100644 --- a/.gitignore +++ b/.gitignore @@ -84,5 +84,5 @@ dist/ # Test configuration .cliamrc.json -.cliamrc-bk.json -.test.js +.cliamrc.js +.cliam-test.js diff --git a/README.md b/README.md index 4ddec7c..914ab67 100644 --- a/README.md +++ b/README.md @@ -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)

> Requirements

@@ -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": [ { diff --git a/src/classes/cliam.class.ts b/src/classes/cliam.class.ts index c20dea6..b04e27a 100644 --- a/src/classes/cliam.class.ts +++ b/src/classes/cliam.class.ts @@ -52,9 +52,9 @@ class Cliam { async mail(event: Event|string, payload: IPayload): Promise { 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); } } diff --git a/src/services/container.service.ts b/src/services/container.service.ts index 3ee7593..d44dc6b 100644 --- a/src/services/container.service.ts +++ b/src/services/container.service.ts @@ -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'; @@ -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 @@ -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); @@ -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} Object containing client configuration values - */ - private read(path: string): Record { - 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; - } - /** * @description Validates the client configuration setup * diff --git a/test/00-bootstrap.test.js b/test/00-bootstrap.test.js index 33f83f9..292b812 100644 --- a/test/00-bootstrap.test.js +++ b/test/00-bootstrap.test.js @@ -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');