mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-03 23:09:17 -05:00
feat: parse YAML/JSON/JS config file (#1258)
* 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: https://github.com/verdaccio/website/pull/99
This commit is contained in:
parent
197095efe3
commit
95d134bdfd
7 changed files with 149 additions and 6 deletions
|
@ -13,7 +13,18 @@ import createError from 'http-errors';
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
import sanitizyReadme from '@verdaccio/readme';
|
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 { generateGravatarUrl, GENERIC_AVATAR } from '../utils/user';
|
||||||
|
|
||||||
import type { Package } from '@verdaccio/types';
|
import type { Package } from '@verdaccio/types';
|
||||||
|
@ -381,8 +392,20 @@ export const ErrorCode = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export function parseConfigFile(configPath: string): Object {
|
export function parseConfigFile(configPath: string) {
|
||||||
return YAML.safeLoad(fs.readFileSync(configPath, CHARACTER_ENCODING.UTF8));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -5,7 +5,12 @@ import Config from '../../../../src/lib/config';
|
||||||
import {parseConfigFile} from '../../../../src/lib/utils';
|
import {parseConfigFile} from '../../../../src/lib/utils';
|
||||||
import {DEFAULT_REGISTRY, DEFAULT_UPLINK, ROLES, WEB_TITLE} from '../../../../src/lib/constants';
|
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([]);
|
require('../../../../src/lib/logger').setup([]);
|
||||||
|
|
||||||
const checkDefaultUplink = (config) => {
|
const checkDefaultUplink = (config) => {
|
||||||
|
|
|
@ -14,8 +14,11 @@ import {PACKAGE_ACCESS, ROLES} from '../../../../src/lib/constants';
|
||||||
|
|
||||||
describe('Config Utilities', () => {
|
describe('Config Utilities', () => {
|
||||||
|
|
||||||
const parseConfigurationFile = (name) => {
|
const parseConfigurationFile = (conf) => {
|
||||||
return path.join(__dirname, `../../partials/config/yaml/${name}.yaml`);
|
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', () => {
|
describe('uplinkSanityCheck', () => {
|
||||||
|
@ -265,4 +268,44 @@ describe('Config Utilities', () => {
|
||||||
expect(url).toMatch('/-/static/logo.png');
|
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
test/unit/partials/config/js/default.js
Normal file
15
test/unit/partials/config/js/default.js
Normal file
|
@ -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
test/unit/partials/config/js/invalid.js
Normal file
1
test/unit/partials/config/js/invalid.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
module.exports = {;
|
55
test/unit/partials/config/json/default.json
Normal file
55
test/unit/partials/config/json/default.json
Normal file
|
@ -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
test/unit/partials/config/json/invalid.json
Normal file
1
test/unit/partials/config/json/invalid.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{
|
Loading…
Add table
Reference in a new issue