1+ import yargs from 'yargs-parser' ;
2+
13import { getPackFormat , getVersions , LATEST } from './index'
24import { version as VERSION } from '../package.json' ;
35
4- const arg = ( n : number ) : string => process . argv [ n + 1 ]
56const indent = ( n : number ) : string => ' ' . repeat ( n * 4 )
67const log = function ( arg : string , desc : string [ ] , example : string ) : void {
78 console . log ( `\n${ indent ( 2 ) } pack-format ${ arg } ` )
@@ -10,33 +11,47 @@ const log = function (arg: string, desc: string[], example: string): void {
1011 console . log ( `${ indent ( 3 ) } Example: ${ example } ` )
1112}
1213
13- if ( arg ( 1 ) && ! arg ( 1 ) . includes ( 'h' ) ) {
14- const cmd = arg ( 1 )
15- if ( / ^ - * v / . test ( cmd ) ) {
16- console . log ( `The current version of pack-format is ${ VERSION } ` )
14+ const argOpts : yargs . Options = {
15+ alias : {
16+ help : [ 'h' ] ,
17+ version : [ 'v' ] ,
18+ resource : [ 'r' ] ,
19+ data : [ 'd' ] ,
20+ list : [ 'l' ] ,
21+ latest : [ 'L' ] ,
22+ } ,
23+ boolean : [ 'help' , 'version' , 'resource' , 'data' , 'list' , 'latest' ] ,
24+ }
25+ const args = yargs ( process . argv . slice ( 2 ) , argOpts )
26+ const ver = args . _ [ 0 ] ;
27+
28+ if ( ver ) {
29+ if ( args . version ) {
30+ console . log ( `pack-format v${ VERSION } ` )
31+ }
32+ else if ( args . list ) {
33+ console . debug ( ver , args )
34+ if ( Number . isNaN ( ver ) ) throw new Error ( `'${ ver } ' is not a valid pack format` )
35+ if ( Math . round ( + ver ) !== + ver ) throw new Error ( `'${ ver } ' is a version number, not a pack format` )
36+ const type = args . data ? 'data' : 'resource'
37+ console . log ( getVersions ( + ver , type ) )
1738 }
18- else if ( / ^ - * d / . test ( cmd ) ) {
19- const ver = arg ( 2 )
39+ else if ( args . data ) {
2040 console . log ( `Data pack format of ${ ver } is ${ getPackFormat ( ver , 'data' ) } ` )
2141 }
22- else if ( / ^ - * r / . test ( cmd ) ) {
23- const ver = arg ( 2 )
42+ else if ( args . resource ) {
2443 console . log ( `Resource pack format of ${ ver } is ${ getPackFormat ( ver , 'resource' ) } ` )
2544 }
26- else if ( / ^ - * l / . test ( cmd ) ) {
27- if ( arg ( 3 ) ) console . log ( getVersions ( + arg ( 3 ) , / ^ - * d / . test ( arg ( 2 ) ) ? 'data' : 'resource' ) )
28- else console . log ( getVersions ( + arg ( 2 ) ) )
29- }
30- else if ( / ^ - * L / . test ( cmd ) ) {
31- const type = / ^ - * d / . test ( arg ( 2 ) ) ? 'data' : 'resource'
32- if ( arg ( 2 ) ) console . log ( `The latest ${ type } pack version is ${ LATEST [ type ] } .` )
33- else console . log ( `The latest pack version is ${ LATEST . resource } .` )
34- }
3545 else {
36- console . log ( `Pack format of ${ cmd } is ${ getPackFormat ( cmd ) } ` )
46+ console . log ( `Pack format of ${ ver } is ${ getPackFormat ( ver ) } ` )
3747 }
3848}
39- else {
49+ else if ( args . latest ) {
50+ const type = args . data ? 'data' : args . resource ? 'resource' : ''
51+ if ( type ) console . log ( `The latest ${ type } pack version is ${ LATEST [ type ] } .` )
52+ else console . log ( `The latest pack version is ${ LATEST . resource } .` )
53+ }
54+ if ( args . help ) {
4055 console . log ( `\n${ indent ( 1 ) } pack-format arguments:` )
4156 log (
4257 '<version>' ,
0 commit comments