Skip to content

Commit

Permalink
feat(node): Drop support of node 14 and add node 18 and 20
Browse files Browse the repository at this point in the history
  • Loading branch information
SC committed Sep 8, 2023
1 parent 143bf90 commit d2d023a
Show file tree
Hide file tree
Showing 10 changed files with 3,960 additions and 2,279 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extends:
- './node_modules/@neo9/n9-coding-style/.eslintrc.yaml'
4 changes: 2 additions & 2 deletions .github/workflows/node.js.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [16.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand All @@ -28,7 +28,7 @@ jobs:
- name: Setup Nodejs with yarn caching
uses: actions/setup-node@v2
with:
node-version: '14'
node-version: ${{ matrix.node-version }}
cache: yarn

- name: Install dependencies
Expand Down
57 changes: 27 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
"conf dir"
],
"author": "Benjamin DANIEL <benjamin.daniel@neo9.fr>",
"license": "GNU",
"license": "MIT",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"files": [
"dist/src"
],
"scripts": {
"build": "rimraf dist/ && tsc",
"lint": "tslint --project tsconfig.json --format verbose '{src,test}/**/*.ts'",
"lint:apply": "npm run format && tslint --fix --project tsconfig.json '{src,test}/**/*.ts'",
"lint": "eslint --config .eslintrc.yaml '{src,test}/**/*.ts'",
"lint:apply": "npm run format && npx eslint --fix '{src,test}/**/*.ts'",
"format": "prettier --write '{,@(src|test)/**/}*.*'",
"test:dev": "export NODE_ENV=test && TS_NODE_FILES=true ava --verbose --color --serial --watch",
"test": "npm run lint && npm run build && export NODE_ENV=test && nyc ava --verbose --color --serial && nyc report --reporter=html",
Expand All @@ -35,39 +35,42 @@
"url": "git+https://github.com/neo9/n9-node-conf"
},
"engines": {
"node": ">=12.0.0"
"node": ">=16.0.0"
},
"dependencies": {
"app-root-dir": "^1.0.2",
"debug": "^4.3.1",
"fs-extra": "^10.0.0",
"fs-extra": "^11.1.1",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@commitlint/cli": "^12.1.4",
"@neo9/n9-coding-style": "^2.0.0",
"@release-it/conventional-changelog": "^2.0.1",
"@ava/typescript": "^4.1.0",
"@commitlint/cli": "^17.7.1",
"@neo9/n9-coding-style": "^5.1.2",
"@release-it/conventional-changelog": "^7.0.1",
"@types/app-root-dir": "^0.1.0",
"@types/debug": "^4.1.5",
"@types/fs-extra": "^9.0.11",
"@types/debug": "^4.1.8",
"@types/fs-extra": "^11.0.1",
"@types/js-yaml": "^4.0.1",
"@types/lodash": "^4.14.169",
"@types/node": "^15.3.0",
"ava": "^3.15.0",
"codecov": "^3.8.2",
"conventional-changelog-cli": "^2.1.1",
"husky": "^4.2.3",
"@types/node": "^20.5.9",
"ava": "^5.3.1",
"codecov": "^3.8.3",
"conventional-changelog-cli": "4.0.0",
"husky": "4.3.8",
"nyc": "^15.1.0",
"prettier": "^2.3.0",
"release-it": "^14.6.2",
"rimraf": "^3.0.2",
"ts-node": "^9.1.1",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.3.0",
"typescript": "^4.2.4"
"prettier": "^3.0.3",
"release-it": "^16.1.5",
"rimraf": "^5.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "npm run format && npm run lint"
}
},
"ava": {
"files": [
Expand All @@ -80,11 +83,5 @@
"require": [
"ts-node/register"
]
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "npm run format && npm run lint"
}
}
}
60 changes: 40 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ import * as fs from 'fs-extra';
import * as JsYaml from 'js-yaml';
import * as _ from 'lodash';
import * as Path from 'path';

const log = debug('n9-node-conf');

export enum N9ConfMergeStrategy {
/**
* Merge with lodash merge (_.merge)
*/
V1 = 'v1',
/**
* Merge with n9Conf merge customizer
*/
V2 = 'v2',
}

export interface N9ConfOptions {
path?: string;
extendConfig?: {
Expand Down Expand Up @@ -45,17 +57,6 @@ export interface N9ConfBaseConf {
version?: string;
}

export enum N9ConfMergeStrategy {
/**
* Merge with lodash merge (_.merge)
*/
V1 = 'v1',
/**
* Merge with n9Conf merge customizer
*/
V2 = 'v2',
}

function getExtendConfigPath(options: N9ConfOptions, confPath: string): string {
if (process.env.NODE_CONF_EXTEND_ABSOLUTE_PATH) return process.env.NODE_CONF_EXTEND_ABSOLUTE_PATH;
if (options.extendConfig?.path?.absolute) return options.extendConfig.path.absolute;
Expand All @@ -66,9 +67,9 @@ function getExtendConfigPath(options: N9ConfOptions, confPath: string): string {
}

function mergeWithStrategy(
strategy: N9ConfMergeStrategy = N9ConfMergeStrategy.V2,
source: object,
override1: object,
strategy: N9ConfMergeStrategy = N9ConfMergeStrategy.V2,
): object {
switch (strategy) {
case N9ConfMergeStrategy.V1:
Expand All @@ -86,11 +87,14 @@ function mergeWithStrategy(

function readConfigFile(path: string, type: 'json' | 'yaml'): any {
log(`Load extension ${path}`);

switch (type) {
case 'json':
return fs.readJSONSync(path);
case 'yaml':
return JsYaml.load(fs.readFileSync(path, 'utf8'));
default:
throw new Error(`Invalid type "${type}", can't read file ${path}`);
}
}

Expand Down Expand Up @@ -136,7 +140,7 @@ function loadExtendConfig(
}
}

export default (options: N9ConfOptions = {}) => {
export default (options: N9ConfOptions = {}): object | any => {
const rootDir = appRootDir.get();
const confPath: string = process.env.NODE_CONF_PATH || options.path || Path.join(rootDir, 'conf');
const packageJsonDirPath = Path.join(
Expand All @@ -146,7 +150,11 @@ export default (options: N9ConfOptions = {}) => {
const extendConfigPath: string = getExtendConfigPath(options, confPath);
const defaultMergeStrategy: N9ConfMergeStrategy = options.extendConfig?.mergeStrategy;
const currentEnvironment: string = process.env.NODE_ENV || 'development';
const app: { name: string; version: string } = require(packageJsonDirPath); // Fetch package.json of the app

// Fetch package.json of the app
/* eslint-disable-next-line import/no-dynamic-require, @typescript-eslint/no-var-requires, global-require */
const app: { name: string; version: string } = require(packageJsonDirPath);

const extendConfigKey: string = options.extendConfig?.key ?? app.name;
const environments = ['application', `${currentEnvironment}`, 'local']; // Files to load
const sources: N9ConfBaseConf[] = []; // Sources of each config file
Expand All @@ -155,23 +163,26 @@ export default (options: N9ConfOptions = {}) => {
if (extendConfigPath) {
try {
extendConfig = loadExtendConfig(extendConfigPath);
} catch (e) {
} catch (err) {
throw new Error(
`Error while loading extendable config (${
e.message
}) : ${extendConfigPath} ${JSON.stringify(e)}`,
err.message
}) : ${extendConfigPath} ${JSON.stringify(err)}`,
);
}
}

// Load each file
for (const environment of environments) {
const filePath = Path.join(confPath, environment);
const filePath: string = Path.join(confPath, environment);
let fileLoadingError: Error;

try {
/* eslint-disable-next-line import/no-dynamic-require, @typescript-eslint/no-var-requires, global-require */
require(filePath);
} catch (err) {
fileLoadingError = err;

try {
if (!(environment === 'local' && err.code === 'MODULE_NOT_FOUND')) {
log(`Error while loading config file '${filePath}' : ${JSON.stringify(err)}`);
Expand All @@ -193,30 +204,39 @@ export default (options: N9ConfOptions = {}) => {
}
// Load its source
log(`Loading ${environment} configuration`);

/* eslint-disable-next-line import/no-dynamic-require, @typescript-eslint/no-var-requires, global-require */
const loadedSource = require(filePath);

let source = loadedSource.default || loadedSource;

if (extendConfig) {
const strategy: N9ConfMergeStrategy =
extendConfig.metadata?.mergeStrategy ?? defaultMergeStrategy ?? N9ConfMergeStrategy.V2;

const extendConfigForThisEnv: object = extendConfig[environment]?.[extendConfigKey];

if (extendConfigForThisEnv) {
source = mergeWithStrategy(strategy, source, extendConfigForThisEnv);
source = mergeWithStrategy(source, extendConfigForThisEnv, strategy);
}
}

sources.push(source);
}

// Add env, name & version to the returned config
sources.push({
env: currentEnvironment,
name: app.name,
version: app.version,
});

// Return merged sources
let result = _.mergeWith.apply(null, [...sources, customizer]);

if (options.override?.value) {
result = mergeWithStrategy(options.override.mergeStrategy, result, options.override.value);
result = mergeWithStrategy(result, options.override.value, options.override.mergeStrategy);
}

return result;
};
4 changes: 2 additions & 2 deletions test/extend-conf-yaml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ava("Can't load unknown file type", (t: ExecutionContext) => {
overridePackageJsonDirPath: join(__dirname, './fixtures/extend-conf-yaml-1'),
});
},
null,
undefined,
'should not throw error due to wrong conf file',
);

Expand Down Expand Up @@ -139,7 +139,7 @@ ava('Extendable conf error due to invalid yaml', (t: ExecutionContext) => {
overridePackageJsonDirPath: join(__dirname, './fixtures/extend-conf-yaml-1'),
});
},
null,
undefined,
'should not throw error due to wrong conf file',
);

Expand Down
6 changes: 3 additions & 3 deletions test/extend-conf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ava('Extendable conf error due to invalid json', (t: ExecutionContext) => {
overridePackageJsonDirPath: join(__dirname, './fixtures/extend-conf-1'),
});
},
null,
undefined,
'should not throw error due to wrong conf file',
);

Expand All @@ -77,7 +77,7 @@ ava('Extendable conf error unknown mergeStrategy', (t: ExecutionContext) => {
overridePackageJsonDirPath: join(__dirname, './fixtures/extend-conf-3'),
});
},
null,
undefined,
'should throw error due to wrong merge strategy',
);

Expand All @@ -102,7 +102,7 @@ ava('Extendable conf error unknown mergeStrategy forced', (t: ExecutionContext)
overridePackageJsonDirPath: join(__dirname, './fixtures/extend-conf-3'),
});
},
null,
undefined,
'should throw error due to wrong merge strategy',
);

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/conf-invalid-local/local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const conf = {
other: 'yes',
// eslint-disable-next-line
regexp: new RegExp('tes(t-2'),
};

// forget to export the conf
3 changes: 2 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import ava from 'ava';
import { join } from 'path';

import src from '../src';
// tslint:disable-next-line:no-var-requires

/* eslint-disable-next-line import/no-dynamic-require, @typescript-eslint/no-var-requires, global-require */
const app = require(join(appRootDir.get(), 'package.json'));

ava('Simple use case', (t) => {
Expand Down
46 changes: 0 additions & 46 deletions tslint.yaml

This file was deleted.

Loading

0 comments on commit d2d023a

Please sign in to comment.