Skip to content

Commit

Permalink
feat: add support for configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinechalifour committed Aug 13, 2019
1 parent 5371f04 commit f7ea7d5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .mementorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"targetUrl": "https://api.punkapi.com/v2",
"delay": 2,
"port": 3344
}
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ This project was bootstrapped with [TSDX](https://github.com/jaredpalmer/tsdx).
## Getting started

```
# Usage
# Usage with configuration file
# You may use any configuration file supported by https://github.com/davidtheclark/cosmiconfig (e.g. .mementorc, memento.config.js, ...)
npx @antoinechalifour/memento
# Usage with CLI args
npx @antoinechalifour/memento --targetUrl=<YOUR API BASE URL> --port=<THE PORT>
# Example for caching Punk API
Expand All @@ -21,6 +25,18 @@ npx @antoinechalifour/memento --targetUrl=https://api.punkapi.com/v2 --delay=300

### Options

#### Configuration file example
You may use any configuration file supported by [cosmiconfig](https://github.com/davidtheclark/cosmiconfig). For instance, you may create a `.mementorc` in your project root :

```
{
"targetUrl": "https://api.punkapi.com/v2",
"delay": 2,
"port": 3344
}
```

#### Command Line Options
The following options can be used in the command line :

| Option | Description | Example | Default value |
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/cosmiconfig": "^5.0.3",
"@types/jest": "^24.0.17",
"@types/koa": "^2.0.49",
"@types/koa-bodyparser": "^4.3.0",
Expand All @@ -67,6 +68,7 @@
"awilix": "^4.2.2",
"awilix-koa": "^4.0.0",
"axios": "^0.19.0",
"cosmiconfig": "^5.2.1",
"koa": "^2.7.0",
"koa-bodyparser": "^4.2.1",
"koa-logger": "^3.2.1",
Expand Down
51 changes: 43 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
import yargs from 'yargs';
import assert from 'assert';
import { createContainer, asClass, asValue } from 'awilix';
import cosmiconfig from 'cosmiconfig';

import { createApp } from './app';
import { RespondToRequest } from './domain/usecase';
import { ResponseRepositoryMemory } from './infrastructure/repository';
import { NetworkServiceAxios } from './infrastructure/service';

const cliArgs = yargs.argv;
interface Configuration {
targetUrl: string;
delay: number;
port: number;
}

assert(cliArgs.targetUrl, 'targetUrl option is required');
function getDelayFromString(str: string) {
return str ? parseInt(str, 10) : 0;
}

const targetUrl = cliArgs.targetUrl as string;
const delay = cliArgs.delay ? parseInt(cliArgs.delay as string, 10) : 0;
const port = cliArgs.port ? parseInt(cliArgs.port as string, 10) : 3344;
function getPortFromString(port: string) {
return port ? parseInt(port, 10) : 3344;
}

function loadConfigurationFromFile(): Configuration | null {
const configExplorer = cosmiconfig('memento');
const cosmicConfiguration = configExplorer.searchSync();

if (!cosmicConfiguration) {
return null;
}

return {
targetUrl: cosmicConfiguration.config.targetUrl,
delay: getDelayFromString(cosmicConfiguration.config.delay),
port: getPortFromString(cosmicConfiguration.config.port),
};
}

function loadConfigurationFromCli(): Configuration {
const cliArgs = yargs.argv;
const targetUrl = cliArgs.targetUrl as string;
const delay = getDelayFromString(cliArgs.delay as string);
const port = getPortFromString(cliArgs.port as string);

return { targetUrl, delay, port };
}

const configuration = loadConfigurationFromFile() || loadConfigurationFromCli();

assert(configuration.targetUrl, 'targetUrl option is required');

const container = createContainer();

container.register({
// Constants
targetUrl: asValue(targetUrl),
delay: asValue(delay),
targetUrl: asValue(configuration.targetUrl),
delay: asValue(configuration.delay),

// Use cases
respondToRequestUseCase: asClass(RespondToRequest),
Expand All @@ -32,4 +67,4 @@ container.register({
networkService: asClass(NetworkServiceAxios).singleton(),
});

createApp({ port, container }).run();
createApp({ port: configuration.port, container }).run();
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,13 @@
"@types/keygrip" "*"
"@types/node" "*"

"@types/cosmiconfig@^5.0.3":
version "5.0.3"
resolved "https://registry.yarnpkg.com/@types/cosmiconfig/-/cosmiconfig-5.0.3.tgz#880644bb155d4038d3b752159684b777b0a159dd"
integrity sha512-HgTGG7X5y9pLl3pixeo2XtDEFD8rq2EuH+S4mK6teCnAwWMucQl6v1D43hI4Uw1VJh6nu59lxLkqXHRl4uwThA==
dependencies:
"@types/node" "*"

"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
Expand Down

0 comments on commit f7ea7d5

Please sign in to comment.