Skip to content

Commit

Permalink
feat: parse YAML/JSON/JS config file (#1258)
Browse files Browse the repository at this point in the history
* Parse JSON/YAML config file.

* fix missing export

* fix: typos

* test(config): remove JSON test

* feat: better config error handling, tests

* fix: detect YAML config file via extension

* docs: verdaccio/website#99
  • Loading branch information
honzahommer authored and juanpicado committed May 22, 2019
1 parent 197095e commit 95d134b
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 6 deletions.
29 changes: 26 additions & 3 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ import createError from 'http-errors';
// $FlowFixMe
import sanitizyReadme from '@verdaccio/readme';

import { HTTP_STATUS, API_ERROR, DEFAULT_PORT, DEFAULT_DOMAIN, DEFAULT_PROTOCOL, CHARACTER_ENCODING, HEADERS, DIST_TAGS, DEFAULT_USER } from './constants';
import {
HTTP_STATUS,
API_ERROR,
APP_ERROR,
DEFAULT_PORT,
DEFAULT_DOMAIN,
DEFAULT_PROTOCOL,
CHARACTER_ENCODING,
HEADERS,
DIST_TAGS,
DEFAULT_USER,
} from './constants';
import { generateGravatarUrl, GENERIC_AVATAR } from '../utils/user';

import type { Package } from '@verdaccio/types';
Expand Down Expand Up @@ -381,8 +392,20 @@ export const ErrorCode = {
},
};

export function parseConfigFile(configPath: string): Object {
return YAML.safeLoad(fs.readFileSync(configPath, CHARACTER_ENCODING.UTF8));
export function parseConfigFile(configPath: string) {
try {
if (/\.ya?ml$/i.test(configPath)) {
return YAML.safeLoad(fs.readFileSync(configPath, CHARACTER_ENCODING.UTF8));
} else {
return require(configPath);
}
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
e.message = APP_ERROR.CONFIG_NOT_VALID;
}

throw new Error(e);
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion test/unit/modules/config/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import Config from '../../../../src/lib/config';
import {parseConfigFile} from '../../../../src/lib/utils';
import {DEFAULT_REGISTRY, DEFAULT_UPLINK, ROLES, WEB_TITLE} from '../../../../src/lib/constants';

const resolveConf = (conf) => path.join(__dirname, `../../../../conf/${conf}.yaml`);
const resolveConf = (conf) => {
const { name, ext } = path.parse(conf);

return path.join(__dirname, `../../../../conf/${name}${ext.startsWith('.') ? ext : '.yaml'}`);
};

require('../../../../src/lib/logger').setup([]);

const checkDefaultUplink = (config) => {
Expand Down
47 changes: 45 additions & 2 deletions test/unit/modules/utils/config-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {PACKAGE_ACCESS, ROLES} from '../../../../src/lib/constants';

describe('Config Utilities', () => {

const parseConfigurationFile = (name) => {
return path.join(__dirname, `../../partials/config/yaml/${name}.yaml`);
const parseConfigurationFile = (conf) => {
const { name, ext } = path.parse(conf);
const format = ext.startsWith('.') ? ext.substring(1) : 'yaml';

return path.join(__dirname, `../../partials/config/${format}/${name}.${format}`);
};

describe('uplinkSanityCheck', () => {
Expand Down Expand Up @@ -265,4 +268,44 @@ describe('Config Utilities', () => {
expect(url).toMatch('/-/static/logo.png');
});
});

describe('JSON', () => {
test('parse default.json', () => {
const config = parseConfigFile(parseConfigurationFile('default.json'));

expect(config.storage).toBeDefined();
});

test('parse invalid.json', () => {
expect(function ( ) {
parseConfigFile(parseConfigurationFile('invalid.json'));
}).toThrow(/Error/);
});

test('parse not-exists.json', () => {
expect(function ( ) {
parseConfigFile(parseConfigurationFile('not-exists.json'));
}).toThrow(/Error/);
});
});

describe('JavaScript', () => {
test('parse default.js', () => {
const config = parseConfigFile(parseConfigurationFile('default.js'));

expect(config.storage).toBeDefined();
});

test('parse invalid.js', () => {
expect(function ( ) {
parseConfigFile(parseConfigurationFile('invalid.js'));
}).toThrow(/Error/);
});

test('parse not-exists.js', () => {
expect(function ( ) {
parseConfigFile(parseConfigurationFile('not-exists.js'));
}).toThrow(/Error/);
});
});
});
15 changes: 15 additions & 0 deletions test/unit/partials/config/js/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = { storage: './storage_default_storage',
uplinks: { npmjs: { url: 'http://localhost:4873/' } },
packages:
{ '@*/*': { access: '$all', publish: '$all', proxy: 'npmjs' },
'forbidden-place': { access: 'nobody', publish: '$all' },
react: { access: '$all', publish: '$all', proxy: 'npmjs' },
'corrupted-package': { access: '$all', publish: '$all', proxy: 'npmjs' },
jquery: { access: '$all', publish: '$all', proxy: 'npmjs' },
'auth-package': { access: '$authenticated', publish: '$authenticated' },
vue:
{ access: '$authenticated',
publish: '$authenticated',
proxy: 'npmjs' },
'*': { access: '$all', publish: '$all', proxy: 'npmjs' } },
logs: [ { type: 'stdout', format: 'pretty', level: 'warn' } ] };
1 change: 1 addition & 0 deletions test/unit/partials/config/js/invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {;
55 changes: 55 additions & 0 deletions test/unit/partials/config/json/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"storage": "./storage_default_storage",
"uplinks": {
"npmjs": {
"url": "http://localhost:4873/"
}
},
"packages": {
"@*/*": {
"access": "$all",
"publish": "$all",
"proxy": "npmjs"
},
"forbidden-place": {
"access": "nobody",
"publish": "$all"
},
"react": {
"access": "$all",
"publish": "$all",
"proxy": "npmjs"
},
"corrupted-package": {
"access": "$all",
"publish": "$all",
"proxy": "npmjs"
},
"jquery": {
"access": "$all",
"publish": "$all",
"proxy": "npmjs"
},
"auth-package": {
"access": "$authenticated",
"publish": "$authenticated"
},
"vue": {
"access": "$authenticated",
"publish": "$authenticated",
"proxy": "npmjs"
},
"*": {
"access": "$all",
"publish": "$all",
"proxy": "npmjs"
}
},
"logs": [
{
"type": "stdout",
"format": "pretty",
"level": "warn"
}
]
}
1 change: 1 addition & 0 deletions test/unit/partials/config/json/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{

0 comments on commit 95d134b

Please sign in to comment.