diff --git a/.changeset/healthy-poets-compare.md b/.changeset/healthy-poets-compare.md new file mode 100644 index 000000000..c6555b2ea --- /dev/null +++ b/.changeset/healthy-poets-compare.md @@ -0,0 +1,11 @@ +--- +'@verdaccio/config': patch +--- + +Feature + +- add option to set storage from environment variable VERDACCIO_STORAGE_PATH + +#### Related tickets + +https://github.com/verdaccio/verdaccio/issues/1681 diff --git a/docs/env.variables.md b/docs/env.variables.md index 943618176..fa915ed80 100644 --- a/docs/env.variables.md +++ b/docs/env.variables.md @@ -44,3 +44,7 @@ The default header to identify the protocol is `X-Forwarded-Proto`, but there ar ``` $ VERDACCIO_FORWARDED_PROTO=CloudFront-Forwarded-Proto verdaccio --listen 5000 ``` + +#### VERDACCIO_STORAGE_PATH + +By default, the storage is taken from config file, but using this variable allows to set it from environment variable. diff --git a/packages/config/src/config.ts b/packages/config/src/config.ts index 8fd6ec0fc..a6ff40225 100644 --- a/packages/config/src/config.ts +++ b/packages/config/src/config.ts @@ -50,7 +50,7 @@ class Config implements AppConfig { public constructor(config: ConfigRuntime) { const self = this; - this.storage = config.storage; + this.storage = process.env.VERDACCIO_STORAGE_PATH || config.storage; this.config_path = config.config_path; this.plugins = config.plugins; this.security = _.merge(defaultSecurity, config.security); diff --git a/packages/config/test/config-parsing.spec.ts b/packages/config/test/config-parsing.spec.ts index e317c8a51..550bc0cf7 100644 --- a/packages/config/test/config-parsing.spec.ts +++ b/packages/config/test/config-parsing.spec.ts @@ -1,14 +1,7 @@ -import path from 'path'; import { parseConfigFile } from '../src'; +import { parseConfigurationFile } from './utils'; describe('Package access utilities', () => { - 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('JSON format', () => { test('parse default.json', () => { const config = parseConfigFile(parseConfigurationFile('default.json')); diff --git a/packages/config/test/config.spec.ts b/packages/config/test/config.spec.ts index 677896e87..09b50e3e9 100644 --- a/packages/config/test/config.spec.ts +++ b/packages/config/test/config.spec.ts @@ -10,6 +10,7 @@ import { ROLES, WEB_TITLE, } from '../src'; +import { parseConfigurationFile } from './utils'; const resolveConf = (conf) => { const { name, ext } = path.parse(conf); @@ -80,6 +81,32 @@ describe('check basic content parsed file', () => { checkDefaultConfPackages(config); }); + test('should set storage to value set in VERDACCIO_STORAGE_PATH environment variable', () => { + const storageLocation = '/tmp/verdaccio'; + process.env.VERDACCIO_STORAGE_PATH = storageLocation; + const config = new Config(parseConfigFile(resolveConf('default'))); + expect(config.storage).toBe(storageLocation); + delete process.env.VERDACCIO_STORAGE_PATH; + }); + + test('should set storage path to VERDACCIO_STORAGE_PATH if both config and env are set', () => { + const storageLocation = '/tmp/verdaccio'; + process.env.VERDACCIO_STORAGE_PATH = storageLocation; + const config = new Config(parseConfigFile(parseConfigurationFile('storage'))); + expect(config.storage).toBe(storageLocation); + delete process.env.VERDACCIO_STORAGE_PATH; + }); + + test('should take storage from environment variable if not exists in configs', () => { + const storageLocation = '/tmp/verdaccio'; + process.env.VERDACCIO_STORAGE_PATH = storageLocation; + const defaultConfig = parseConfigFile(resolveConf('default')); + delete defaultConfig.storage; + const config = new Config(defaultConfig); + expect(config.storage).toBe(storageLocation); + delete process.env.VERDACCIO_STORAGE_PATH; + }); + test('parse docker.yaml', () => { const config = new Config(parseConfigFile(resolveConf('docker'))); checkDefaultUplink(config); diff --git a/packages/config/test/package-access.spec.ts b/packages/config/test/package-access.spec.ts index 92ae386b5..a78bbb613 100644 --- a/packages/config/test/package-access.spec.ts +++ b/packages/config/test/package-access.spec.ts @@ -1,4 +1,3 @@ -import path from 'path'; import _ from 'lodash'; import { @@ -7,15 +6,9 @@ import { PACKAGE_ACCESS, } from '../src/package-access'; import { parseConfigFile } from '../src'; +import { parseConfigurationFile } from './utils'; describe('Package access utilities', () => { - 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('normalisePackageAccess', () => { test('should test basic conversion', () => { const { packages } = parseConfigFile(parseConfigurationFile('pkgs-basic')); diff --git a/packages/config/test/partials/config/yaml/storage.yaml b/packages/config/test/partials/config/yaml/storage.yaml new file mode 100644 index 000000000..a04462260 --- /dev/null +++ b/packages/config/test/partials/config/yaml/storage.yaml @@ -0,0 +1,7 @@ +--- +storage: './storage_default_storage' + +logs: + - type: stdout + format: pretty + level: warn diff --git a/packages/config/test/uplinks.spec.ts b/packages/config/test/uplinks.spec.ts index 44525d034..b1b43d278 100644 --- a/packages/config/test/uplinks.spec.ts +++ b/packages/config/test/uplinks.spec.ts @@ -1,16 +1,8 @@ -import path from 'path'; - import { hasProxyTo, sanityCheckUplinksProps, uplinkSanityCheck } from '../src/uplinks'; import { normalisePackageAccess, parseConfigFile } from '../src'; +import { parseConfigurationFile } from './utils'; describe('Uplinks Utilities', () => { - 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', () => { test('should test basic conversion', () => { const uplinks = uplinkSanityCheck( diff --git a/packages/config/test/utils/index.ts b/packages/config/test/utils/index.ts new file mode 100644 index 000000000..ca6664513 --- /dev/null +++ b/packages/config/test/utils/index.ts @@ -0,0 +1 @@ +export { parseConfigurationFile } from './parse-configuration-file'; diff --git a/packages/config/test/utils/parse-configuration-file.ts b/packages/config/test/utils/parse-configuration-file.ts new file mode 100644 index 000000000..a466024cb --- /dev/null +++ b/packages/config/test/utils/parse-configuration-file.ts @@ -0,0 +1,8 @@ +import path from 'path'; + +export const parseConfigurationFile = (conf: string) => { + const { name, ext } = path.parse(conf); + const format = ext.startsWith('.') ? ext.substring(1) : 'yaml'; + + return path.join(__dirname, `../partials/config/${format}/${name}.${format}`); +};