Skip to content

Commit

Permalink
Merge pull request #2 from nfroidure/refactor/app_env
Browse files Browse the repository at this point in the history
refactor(env): refactor attempt for app env
  • Loading branch information
nfroidure committed Aug 16, 2023
2 parents f7dd103 + 683096f commit 164ce92
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 207 deletions.
27 changes: 8 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,26 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [main]
pull_request:
branches: [ master ]
branches: [main]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]

services:
redis:
image: redis
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm run precz
cache: "npm"
- name: Install dependencies
run: npm ci
- name: Run pre-commit tests
run: npm run precz
10 changes: 5 additions & 5 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Per default, we take the process environment as is
projects statically so one can isolate the process
env by using the `ISOLATED_ENV` environment variable.

[See in context](./src/services/ENV.ts#L76-L81)
[See in context](./src/services/ENV.ts#L73-L78)



Expand All @@ -87,7 +87,7 @@ You may want to set some env vars depending on the
`NODE_ENV`. We use `dotenv` to provide your such
ability.

[See in context](./src/services/ENV.ts#L89-L94)
[See in context](./src/services/ENV.ts#L86-L91)



Expand All @@ -96,7 +96,7 @@ You may want to set some env vars depending on the
You may need to keep some secrets out of your Git
history fo each deployment targets too.

[See in context](./src/services/ENV.ts#L97-L100)
[See in context](./src/services/ENV.ts#L94-L97)



Expand All @@ -114,7 +114,7 @@ A service to determine the directory of the NodeJS project
The `ENV` service adds a layer of configuration over just using
node's `process.env` value.

[See in context](./src/services/ENV.ts#L23-L27)
[See in context](./src/services/ENV.ts#L20-L24)



Expand All @@ -124,5 +124,5 @@ The `APP_CONFIG` service allows to manage a typed application
configuration by selectively loading the configuration file
according to the `APP_ENV` environment variable.

[See in context](./src/services/APP_CONFIG.ts#L8-L13)
[See in context](./src/services/APP_CONFIG.ts#L7-L12)

56 changes: 28 additions & 28 deletions package-lock.json

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

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jest",
"jsdocs",
"jsarch",
"ghactions",
"codeclimate"
]
},
Expand Down Expand Up @@ -69,11 +70,11 @@
},
"homepage": "https://github.com/nfroidure/application-services#readme",
"dependencies": {
"common-services": "^13.0.0",
"common-services": "^14.0.0",
"dotenv": "^16.3.1",
"knifecycle": "^15.0.1",
"knifecycle": "^16.0.0",
"pkg-dir": "^7.0.0",
"yerror": "^7.0.0"
"yerror": "^8.0.0"
},
"devDependencies": {
"@swc/cli": "^0.1.62",
Expand All @@ -92,8 +93,8 @@
"jest": "^29.6.2",
"jsarch": "^6.0.1",
"jsdoc-to-markdown": "^8.0.0",
"metapak": "^5.1.3",
"metapak-nfroidure": "15.0.0",
"metapak": "^5.1.4",
"metapak-nfroidure": "15.0.1",
"prettier": "^3.0.1",
"rimraf": "^5.0.1",
"typescript": "^5.1.6"
Expand Down
23 changes: 10 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,22 @@ declare module 'application-services' {

import initProcessEnvService from './services/PROCESS_ENV.js';
import initProjectDirService from './services/PROJECT_DIR.js';
import initEnvService, { NodeEnv } from './services/ENV.js';
import initConfigService from './services/APP_CONFIG.js';
import initEnvService from './services/ENV.js';
import initAppConfigService from './services/APP_CONFIG.js';
import { extractAppEnv } from './libs/env.js';

import type { BaseAppEnv, BaseAppEnvVars } from './services/ENV.js';
import type { BaseAppConfig } from './services/APP_CONFIG.js';
export type {
BaseAppEnvVars,
AppEnvVars,
ENVConfig,
ENVDependencies,
} from './services/ENV.js';
export type { BaseAppConfig, AppConfig } from './services/APP_CONFIG.js';

export {
NodeEnv,
initProcessEnvService,
initProjectDirService,
initEnvService,
initConfigService,
initAppConfigService,
extractAppEnv,
};

export type { BaseAppEnv, BaseAppEnvVars, BaseAppConfig };

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AppEnvVars extends BaseAppEnvVars {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AppConfig extends BaseAppConfig {}
14 changes: 4 additions & 10 deletions src/services/APP_CONFIG.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { describe, it, beforeEach, jest, expect } from '@jest/globals';
import initCONFIGS from './APP_CONFIG.js';
import { YError } from 'yerror';
import { BaseAppEnv } from './ENV.js';
import type { AppConfig } from './APP_CONFIG.js';
import type { ImporterService, LogService } from 'common-services';

type TestAppConfig = {
CONFIG: {
testConfig: string;
};
};

describe('initCONFIGS', () => {
const log = jest.fn<LogService>();
const importer = jest.fn<ImporterService<{ default: TestAppConfig }>>();
const importer = jest.fn<ImporterService<{ default: AppConfig }>>();

beforeEach(() => {
log.mockReset();
Expand All @@ -28,7 +22,7 @@ describe('initCONFIGS', () => {
},
});

const CONFIGS = await initCONFIGS<TestAppConfig, BaseAppEnv>({
const CONFIGS = await initCONFIGS({
APP_ENV: 'local',
PROJECT_SRC: '/home/whoami/my-whook-project/src',
log,
Expand Down Expand Up @@ -71,7 +65,7 @@ describe('initCONFIGS', () => {
});

try {
await initCONFIGS<TestAppConfig, BaseAppEnv>({
await initCONFIGS({
APP_ENV: 'local',
PROJECT_SRC: '/home/whoami/my-whook-project/src',
log,
Expand Down
15 changes: 8 additions & 7 deletions src/services/APP_CONFIG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { autoService, singleton, name } from 'knifecycle';
import { noop } from '../libs/utils.js';
import path from 'path';
import { printStackTrace, YError } from 'yerror';
import type { ImporterService, LogService } from 'common-services';
import type { BaseAppEnv } from './ENV.js';
import type { BaseAppEnv, ImporterService, LogService } from 'common-services';

/* Architecture Note #1.4: `APP_CONFIG`
Expand All @@ -13,10 +12,12 @@ The `APP_CONFIG` service allows to manage a typed application
*/

export type BaseAppConfig = Record<string, unknown>;
export type AppConfigDependencies<T, U extends string = BaseAppEnv> = {
APP_ENV: U;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AppConfig extends BaseAppConfig {}
export type AppConfigDependencies<T extends BaseAppEnv> = {
APP_ENV: T;
PROJECT_SRC: string;
importer: ImporterService<{ default: T }>;
importer: ImporterService<{ default: AppConfig }>;
log?: LogService;
};

Expand All @@ -40,12 +41,12 @@ export default name(
* @return {Promise<Object>}
* A promise of a an object the actual configuration properties.
*/
async function initAppConfig<T, U extends string = BaseAppEnv>({
async function initAppConfig<T extends BaseAppEnv>({
APP_ENV,
PROJECT_SRC,
importer,
log = noop,
}: AppConfigDependencies<T, U>): Promise<T> {
}: AppConfigDependencies<T>): Promise<AppConfig> {
log('debug', `🏭 - Initializing the APP_CONFIG service.`);

const configPath = path.join(PROJECT_SRC, 'config', APP_ENV, 'config.js');
Expand Down

0 comments on commit 164ce92

Please sign in to comment.