Skip to content

Commit 289f5a3

Browse files
committed
1.2.0
- Add getVersions function to CLI & Node - Fix data pack version returning undefined
1 parent 695eb76 commit 289f5a3

File tree

10 files changed

+201
-96
lines changed

10 files changed

+201
-96
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
node_modules/
2-
3-
index.js
4-
cli.js
2+
*.js

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 1.2.0
4+
*2021-03-06*
5+
- Added function `getVersions(pack_format: number, type?: string): object` to retrieve a list of versions that have the specified pack_format.
6+
- Added CLI implementation of `getVersions`, `pack-format --list [--data|--version] <pack_format>`.
7+
- Fixed data pack format always returning `undefined`.
8+
39
## 1.1.3
410
*2021-03-04*
511
- Changed output to return `undefined` for future snapshots.

cli.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#!/usr/bin/env node
2-
const VERSION: string = '1.1.3'
32

4-
import getPackFormat from './index'
3+
const VERSION: string = '1.2.0'
4+
5+
import { getPackFormat, getVersions } from './index'
56

67
const arg = (n: number): string => process.argv[n + 1]
8+
const indent = (n: number): string => ' '.repeat(n * 4)
9+
const log = function (arg: string, desc: string[], example: string): void {
10+
console.log(`\n${indent(2)}pack-format ${arg}`)
11+
for (let i in desc)
12+
console.log(indent(3) + desc[i])
13+
console.log(`${indent(3)}Example: ${example}`)
14+
}
715

816
if (arg(1) && !arg(1).includes('h'))
917
if (/^-*v/.test(arg(1)))
@@ -12,17 +20,31 @@ if (arg(1) && !arg(1).includes('h'))
1220
console.log(`Data pack format of ${arg(2)} is ${getPackFormat(arg(2), 'data')}`)
1321
else if (/^-*r/.test(arg(1)))
1422
console.log(`Resource pack format of ${arg(2)} is ${getPackFormat(arg(2), 'resource')}`)
23+
else if (/^-*l/.test(arg(1)))
24+
if (arg(3)) console.log(getVersions(+arg(3), /^-*d/.test(arg(2)) ? 'data' : 'resource'))
25+
else console.log(getVersions(+arg(2)))
1526
else
1627
console.log(`Pack format of ${arg(1)} is ${getPackFormat(arg(1))}`)
1728
else {
18-
const indent = (n: number): string => ' '.repeat(n * 4)
19-
const log = function (arg: string, ...desc: string[]): void {
20-
console.log(`\n${indent(2)}pack-format ${arg}`)
21-
for (let i in desc)
22-
console.log(indent(+i + 3) + desc[i])
23-
}
2429
console.log(`\n${indent(1)}pack-format arguments:`)
25-
log('<version>', 'Retrieve the pack format of any Minecraft version.', 'Defaults to resource pack format when applicable.')
26-
log('(--data|-d) <version>', 'Retrieve the data pack format in particular when applicable.')
27-
log('(--resource|-r) <version>', 'Retrieve the resource pack format in particular when applicable.')
30+
log(
31+
'<version>',
32+
['Retrieve the pack format of any Minecraft version.', 'Defaults to resource pack format when applicable.'],
33+
'pack-format 1.16',
34+
)
35+
log(
36+
'(--data|-d) <version>',
37+
['Retrieve the data pack format in particular when applicable.'],
38+
'pack-format --data 20w45a',
39+
)
40+
log(
41+
'(--resource|-r) <version>',
42+
['Retrieve the resource pack format in particular when applicable.'],
43+
'pack-format -r 20w45a',
44+
)
45+
log(
46+
'(--list|-l) [(--data|-d)|(--resource|-r)] <pack_format>',
47+
['Retrieve a list of versions attached to a specific pack format.'],
48+
'pack-format --list -d 6',
49+
)
2850
}

index.ts

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
1-
#!/usr/bin/env node
1+
import { packType, formatResult, versionsResult } from './types'
22

33
class Snapshot {
44
version: string
55
constructor(version: string) { this.version = version }
6-
get year() { return parseInt(this.version.replace(/^(\d\d).+$/, '$1')) }
7-
get week() { return parseInt(this.version.replace(/^\d\dw(\d\d)\w$/, '$1')) }
8-
get id() { return (this.year - 10) * 52 + this.week }
6+
get year(): number { return parseInt(this.version.replace(/^(\d\d).+$/, '$1')) }
7+
get week(): number { return parseInt(this.version.replace(/^\d\dw(\d\d)\w$/, '$1')) }
8+
get id(): number { return (this.year - 10) * 52 + this.week }
99
}
1010

11-
type packType = 'resource' | 'data'
12-
type result = number | undefined
11+
const RELEASES: Record<number, string[]> = {
12+
1: ['1.6.x', '1.7.x', '1.8.x'],
13+
2: ['1.9.x', '1.10.x'],
14+
3: ['1.11.x', '1.12.x'],
15+
4: ['1.13.x', '1.14.x'],
16+
5: ['1.15.x', '1.16.0', '1.16.1'],
17+
6: ['1.16.x'],
18+
7: ['1.17.x'],
19+
}
20+
21+
const d: Date = new Date()
22+
const fauxCurrentSnapshot: string = (d.getFullYear() - 2000) + 'w' + ((d.getMonth() + 1) * 5) + 'a'
23+
const START_SNAPSHOTS: Record<string, Record<packType, formatResult>> = {
24+
'13w24a': { resource: 1, data: undefined },
25+
'15w31a': { resource: 2, data: undefined },
26+
'16w32a': { resource: 3, data: undefined },
27+
'17w48a': { resource: 4, data: 4 },
28+
'20w06a': { resource: 5, data: 5 },
29+
'20w45a': { resource: 7, data: 6 },
30+
'20w46a': { resource: 7, data: 7 },
31+
[fauxCurrentSnapshot]: { resource: undefined, data: undefined }
32+
}
1333

14-
function getPackFormat(version: string, type?: packType): result {
34+
function getPackFormat(version: string, type: packType = 'resource'): formatResult {
1535
if (!version) return undefined
1636
version = version.toString().toLowerCase().trim()
1737

1838
// Snapshot //
1939

2040
if (/^\d\dw\d\d\w$/.test(version)) {
21-
const d: Date = new Date()
22-
const fauxCurrentSnapshot: string = (d.getFullYear() - 2000) + 'w' + ((d.getMonth() + 1) * 5) + 'a'
23-
const startSnapshots: Record<string, Record<packType, result>> = {
24-
'13w24a': { resource: 1, data: undefined },
25-
'15w31a': { resource: 2, data: undefined },
26-
'16w32a': { resource: 3, data: undefined },
27-
'17w48a': { resource: 4, data: 4 },
28-
'20w06a': { resource: 5, data: 5 },
29-
'20w45a': { resource: 7, data: 6 },
30-
'20w46a': { resource: 7, data: 7 },
31-
[fauxCurrentSnapshot]: { resource: undefined, data: undefined }
32-
}
3341
const snapshot = new Snapshot(version)
3442

35-
let ver: result
36-
for (let testSnap in startSnapshots) {
43+
let ver: formatResult
44+
for (let testSnap in START_SNAPSHOTS) {
3745
if (snapshot.id >= (new Snapshot(testSnap)).id) {
38-
ver = startSnapshots[testSnap][type || 'resource']
46+
ver = START_SNAPSHOTS[testSnap][type]
3947
}
4048
}
4149
return ver
@@ -49,22 +57,11 @@ function getPackFormat(version: string, type?: packType): result {
4957
if (version.includes('1.16.2-pre')) return 5
5058
else version = version.replace(/-.+$/, '')
5159
}
52-
5360
if (version.match(/^\d+\.\d+$/)) version += '.0'
5461

55-
const PACK_FORMATS: Record<number, Array<string>> = {
56-
1: ['1.6.x', '1.7.x', '1.8.x'],
57-
2: ['1.9.x', '1.10.x'],
58-
3: ['1.11.x', '1.12.x'],
59-
4: ['1.13.x', '1.14.x'],
60-
5: ['1.15.x', '1.16.0', '1.16.1'],
61-
6: ['1.16.x'],
62-
7: ['1.17.x'],
63-
}
64-
for (let i in PACK_FORMATS) {
65-
if (+i < 4 && type === 'data')
66-
return undefined
67-
for (let testVer of PACK_FORMATS[i]) {
62+
for (let i in RELEASES) {
63+
if (+i < 4 && type === 'data') continue
64+
for (let testVer of RELEASES[i]) {
6865
const matchExact = testVer === version
6966
const matchMinor = testVer.includes('x') && version.includes(testVer.replace('.x', ''))
7067
if (matchExact || matchMinor) return +i
@@ -73,4 +70,39 @@ function getPackFormat(version: string, type?: packType): result {
7370
return undefined
7471
}
7572

73+
function getVersions(format: number, type: packType = 'resource'): versionsResult {
74+
let output = {
75+
'releases': { 'min': '', 'max': '' },
76+
'snapshots': { 'min': '', 'max': '' },
77+
}
78+
if (!format || (type === 'data' && format < 4)) return output
79+
80+
output.releases.min = RELEASES[format][0]
81+
output.releases.max = RELEASES[format].slice(-1)[0]
82+
83+
const startSnaps = Object.keys(START_SNAPSHOTS)
84+
for (let snap in START_SNAPSHOTS) {
85+
if (START_SNAPSHOTS[snap][type] === format) {
86+
let maxSnap: string = snap
87+
let i = 1
88+
do {
89+
let nextSnap = startSnaps[startSnaps.indexOf(snap) + i++]
90+
if (nextSnap) maxSnap = nextSnap
91+
else break
92+
}
93+
while (getPackFormat(maxSnap, type) === getPackFormat(snap, type))
94+
95+
const prevSnap: string = maxSnap?.replace(/(\d)(?=\w$)/, (_, n) => String(+n - 1))
96+
output.snapshots.min = snap
97+
output.snapshots.max = prevSnap
98+
break
99+
}
100+
}
101+
102+
return output
103+
}
104+
105+
getPackFormat.getPackFormat = getPackFormat
106+
getPackFormat.getVersions = getVersions
107+
76108
export = getPackFormat

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pack-format",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
44
"description": "Returns the pack_format of any Minecraft version, including snapshots",
55
"scripts": {
66
"prepublish": "tsc",
@@ -18,7 +18,8 @@
1818
},
1919
"files": [
2020
"index.js",
21-
"cli.js"
21+
"cli.js",
22+
"test.js"
2223
],
2324
"repository": {
2425
"type": "git",

readme.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,42 @@ It was added in Minecraft version 1.6, and as such using this tool on any versio
1414
## Install
1515

1616
pack-format is available on [npm](https://www.npmjs.com/package/pack-format).
17+
You must have Node.js installed to be able to use this.
1718

1819
To install pack-format, open the command line and type `npm install pack-format` to use for a Node.js project, or `npm install -g pack-format` to use from the command line.
1920

2021
## Usage
2122

2223
### Node
2324

25+
Retrieve the `pack_format` of a given Minecraft version, optionally specifying whether the resource (default) or data pack version should be returned.
2426
```js
2527
const packFormat = require('pack-format')
2628
packFormat('1.14.4') // 4
2729
packFormat('1.16.2-pre1', 'resource') // 5
2830
packFormat('20w45a', 'data') // 6
2931
```
3032

33+
Retrieve a list of versions corresponding to a specific `pack_format`, again optionally specifying resource/data pack version.
34+
```js
35+
const {getVersions} = require('pack-format')
36+
getVersions(3) // { releases: { min: '1.11.x', max: '1.12.x' }, snapshots: { min: '16w32a', max: '17w47a' } }
37+
getVersions(6, 'data') // { releases: { min: '1.15.x', max: '1.16.1' }, snapshots: { min: '20w06a', max: '20w44a' } }
38+
```
39+
3140
### Command line
3241

33-
`pack-format [--data|--resource] <version>`
42+
Retrieve the `pack_format` of a given Minecraft version:
43+
```sh
44+
pack-format [--data|--resource] <version>
45+
```
3446

47+
Retrieve a list of corresponding Minecraft versions:
48+
```sh
49+
pack-format --list [--data|--resource] <pack_format>
50+
```
51+
52+
Examples:
3553
```sh
3654
> pack-format 1.14.4
3755
Pack format of 1.14.4 is 4
@@ -41,4 +59,7 @@ Resource pack format of 1.16.2-pre1 is 5
4159

4260
> pack-format --data 20w45a
4361
Data pack format of 20w45a is 6
62+
63+
> pack-format --list --resource 3
64+
{ releases: { min: '1.11.x', max: '1.12.x' }, snapshots: { min: '16w32a', max: '17w47a' } }
4465
```

test.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)