From 34556c6c9dd1ae4535d161b7e73a88e669f3ff44 Mon Sep 17 00:00:00 2001 From: Martin Heidegger Date: Wed, 27 Apr 2022 02:37:23 +0900 Subject: [PATCH] feat: supporting prerelease and build modifiers as well as .x notation for versions --- index.ts | 5 +++-- test/index.test.ts | 23 ++++++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/index.ts b/index.ts index 5f40fdb..9398144 100644 --- a/index.ts +++ b/index.ts @@ -306,9 +306,10 @@ function validatePluginDefinition (key: string, value: string, index: number, st if (value === '*' || value === '') { throw new Error(`${e} "${key}" can not be marked as "${value}" as a vague version is not cachable or secure!`) } - const parts = /^(\^|~|>=|<|>|<=|==)?((\d+)(\.\d+)?(\.\d+)?)?$/.exec(value) + const parts = /^(\^|~|>=|<|>|<=|==)?((\d+)(\.([0-9]+|x))?(\.([0-9]+|x))?(-[a-z0-9.]+)?(\+[a-z0-9.]+)?)?$/i.exec(value) if (parts !== null) { - const [range, version, major, minor, patch] = parts.slice(1) + /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ + const [range, version, major, minor, _minorNum, patch, _patchNum] = parts.slice(1) if (strict) { if (range !== undefined || range === '') { throw new Error(`${e} "${key}" can not specify a version range "${range}" and needs to be just the version: ${version ?? '1.2.3'}`) diff --git a/test/index.test.ts b/test/index.test.ts index 7b2595a..781d24d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -3,11 +3,12 @@ import { PluginDefinitions, validatePluginDefinitions } from '../index' const valids: Array<{ input: PluginDefinitions, test?: PluginDefinitions, strict?: boolean }> = [ { input: {} }, - { - input: { - a: '1.2.3' - } - }, + { input: { a: '1.2.3' } }, + { input: { a: '1.2' }, strict: false }, + { input: { a: '1.2.x' }, strict: false }, + { input: { a: '1.2.3-prerelease.build1' } }, + { input: { a: '1.2.3-prerelease.build1+build' } }, + { input: { a: '1.2.3+build' } }, { input: { // Keys unsorted! @@ -144,6 +145,18 @@ const invalids: Array<{ def: PluginDefinitions, error: string, strict?: boolean ' a': '1.2.3' }, error: 'Entry #0 has a name with a space in it, this is not acceptable. Use names without spaces!' + }, + { + def: { a: '1.2x.3' }, + error: 'Entry #0 "a" needs to be either a (semver-)version like 1.2.3 or a valid URL: 1.2x.3' + }, + { + def: { a: '1.2.3x' }, + error: 'Entry #0 "a" needs to be either a (semver-)version like 1.2.3 or a valid URL: 1.2.3x' + }, + { + def: { a: '1.x2.3' }, + error: 'Entry #0 "a" needs to be either a (semver-)version like 1.2.3 or a valid URL: 1.x2.3' } ]