From 55afd5fb04f90b209edd3aa2ec386dcb13cd7ceb Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Mon, 29 Jul 2019 20:53:26 -0400 Subject: [PATCH] Add utility to parse maven identifiers. --- src/index.ts | 14 +++++++++----- src/model/type.ts | 3 +++ src/util/maven.ts | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/util/maven.ts diff --git a/src/index.ts b/src/index.ts index beac586..491745f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,13 @@ import { resolve } from 'path' import yargs from 'yargs' +function positionalRoot(yargs: yargs.Argv) { + return yargs.positional('root', { + describe: 'File structure root', + type: 'string' + }) +} + // Registering yargs configuration. // tslint:disable-next-line:no-unused-expression yargs @@ -10,14 +17,11 @@ yargs root: resolve }) .command({ - command: 'generate ', + command: 'generate server ', aliases: ['g'], describe: 'Generate a distribution.json', builder: (yargs) => { - return yargs.positional('root', { - describe: 'File structure root', - type: 'string' - }) + return positionalRoot(yargs) }, handler: (argv) => { console.log(`got generate with root=${argv.root}`) diff --git a/src/model/type.ts b/src/model/type.ts index c009bf6..91bac11 100644 --- a/src/model/type.ts +++ b/src/model/type.ts @@ -4,6 +4,9 @@ export const Types: {[property: string]: Type} = { id: 'Library', defaultExtension: 'jar' }, + /** + * @deprecated Will be replaced by Types.Forge. + */ ForgeHosted: { id: 'ForgeHosted', defaultExtension: 'jar' diff --git a/src/util/maven.ts b/src/util/maven.ts new file mode 100644 index 0000000..c2444fc --- /dev/null +++ b/src/util/maven.ts @@ -0,0 +1,39 @@ +import { normalize } from 'path' +import { URL } from 'url' + +export class MavenUtil { + + public static readonly ID_REGEX = /(.+):(.+):([^@]+)(?:@{1}(.+)$)?/ + + public static isMavenIdentifier(id: string) { + return this.ID_REGEX.test(id) + } + + public static mavenToString(id: string, extension = 'jar') { + if (!this.isMavenIdentifier(id)) { + return null + } + + const result = this.ID_REGEX.exec(id) + if (result != null) { + const group = result[1] + const artifact = result[2] + const version = result[3] + const ext = result[4] || extension + + return `${group.replace(/\./g, '/')}/${artifact}/${version}/${artifact}-${version}.${ext}` + } + return null + } + + public static mavenToUrl(id: string, extension = 'jar') { + const res = this.mavenToString(id, extension) + return res == null ? null : new URL(res) + } + + public static mavenToPath(id: string, extension = 'jar') { + const res = this.mavenToString(id, extension) + return res == null ? null : normalize(res) + } + +}