Skip to content

Commit 465ed9d

Browse files
committed
refactor
1 parent fe04914 commit 465ed9d

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

bin/cli.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
#!/usr/bin/env node
22

3-
const { getUnityChangeset, scrapeArchivedChangesets, scrapeBetaChangesets } = require("../dist/index");
3+
const { getUnityChangeset, scrapeArchivedChangesets, scrapeBetaChangesets, toNumber } = require("../dist/index");
44
const cli = require('cac')();
55
const pkgJson = require('../package.json');
66

7-
const toNumber = function (version, max = false) {
8-
const match = version.toString().match(/^(\d+)\.*(\d*)\.*(\d*)(\w*)(\d*)$/);
9-
if (match === null) return 0;
10-
11-
return parseInt(match[1] || (max ? '9999' : '0')) * 100 * 100 * 100 * 100
12-
+ parseInt(match[2] || (max ? '99' : '0')) * 100 * 100 * 100
13-
+ parseInt(match[3] || (max ? '99' : '0')) * 100 * 100
14-
+ ((match[4] || (max ? 'z' : 'a')).toUpperCase().charCodeAt(0) - 65) * 100
15-
+ parseInt(match[5] || (max ? '99' : '0'));
16-
};
17-
18-
const toMinor = function (version) {
19-
const match = version.toString().match(/^(\d+)\.*(\d*)\.*(\d*)(\w*)(\d*)$/);
20-
if (match === null) return '';
21-
22-
return `${match[1]}.${match[2]}`;
23-
};
24-
257
const groupBy = (array, getKey) =>
268
array.reduce((obj, cur, idx, src) => {
279
const key = getKey(cur, idx, src);
@@ -64,23 +46,20 @@ cli.command('list', 'List changesets')
6446
var max = options.max ? toNumber(options.max, true) : Number.MAX_VALUE;
6547
results = results
6648
.filter(r => options.grep ? r.version.includes(options.grep) : true)
67-
.filter(r => {
68-
const n = toNumber(r.version);
69-
return min <= n && n <= max;
70-
});
49+
.filter(r => min <= r.versionNumber && r.versionNumber <= max);
7150

7251
// Group by minor version
7352
if (options.minorVersions) {
74-
results.forEach(r => r.version = toMinor(r.version))
53+
results.forEach(r => r.version = r.minor)
7554
results = Object.values(groupBy(results, r => r.version)).map(g => g[0]);
7655
}
7756
// Group by minor version and get latest patch
7857
else if (options.latestPatch) {
79-
results = Object.values(groupBy(results, r => toMinor(r.version))).map(g => g[0]);
58+
results = Object.values(groupBy(results, r => r.minor)).map(g => g[0]);
8059
}
8160
// Group by minor version and get oldest patch
8261
else if (options.oldestPatch) {
83-
results = Object.values(groupBy(results, r => toMinor(r.version))).map(g => g[g.length - 1]);
62+
results = Object.values(groupBy(results, r => r.minor)).map(g => g[g.length - 1]);
8463
}
8564

8665
// Output versions
@@ -110,6 +89,8 @@ cli
11089
.example('unity-changeset list')
11190
.example('unity-changeset list --beta')
11291
.example('unity-changeset list --versions')
92+
.example('unity-changeset list --versions --all')
93+
.example('unity-changeset list --versions --all --latest-patch')
11394
.help()
11495
.version(pkgJson.version);
11596

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const getUnityChangeset = async (version: string): Promise<UnityChangeset
2222
case 'b':
2323
return await getBetaChangeset(version, 'beta');
2424
default:
25-
throw Error('The given version was not supported')
25+
throw Error(`The given life-cycle '${match?.[1]}' (in ${version}) was not supported`)
2626
}
2727
};
2828

@@ -85,4 +85,8 @@ export const scrapeBetaChangesets = async (onlyLatestPatch: boolean = false): Pr
8585

8686
return Array.from(hrefs)
8787
.map(href => UnityChangeset.createFromHref(href));
88+
};
89+
90+
export const toNumber = (version: string, max: boolean): number => {
91+
return UnityChangeset.toNumber(version, max);
8892
};

src/unityChangeset.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
const REGEXP_HUB_LINK = /^unityhub:\/\/(\d{4}\.\d+\.\d+(a|b|f)\d+)\/(\w{12})$/
2+
const REGEXP_UNITY = /^(\d+)\.(\d+)\.(\d+)([a-zA-Z]+)(\d+)/;
3+
const REGEXP_UNITY_NUM = /^(\d+)\.?(\d+)?\.?(\d+)?([a-zA-Z]+)?(\d+)?/;
24

35
export class UnityChangeset {
46
version: string = '';
57
changeset: string = '';
8+
versionNumber: number = 0;
9+
minor: string = '';
10+
lifecycle: string = '';
11+
612

713
constructor(version: string, changeset: string) {
814
Object.assign(this, { version, changeset });
15+
16+
const match = this.version.match(REGEXP_UNITY);
17+
if (match) {
18+
this.versionNumber = UnityChangeset.toNumber(this.version, false);
19+
this.minor = `${match[1]}.${match[2]}`;
20+
this.lifecycle = `${match[4]}`;
21+
}
922
}
1023

11-
toString() {
24+
toString = (): string => {
1225
return `${this.version}\t${this.changeset}`;
1326
}
1427

@@ -20,4 +33,15 @@ export class UnityChangeset {
2033
const match = href.match(REGEXP_HUB_LINK);
2134
return new UnityChangeset(match?.[1] as string, match?.[3] as string);
2235
};
36+
37+
static toNumber = (version: string, max: boolean): number => {
38+
const match = version.toString().match(REGEXP_UNITY_NUM);
39+
if (match === null) return 0;
40+
41+
return parseInt(match[1] || (max ? '9999' : '0')) * 100 * 100 * 100 * 100
42+
+ parseInt(match[2] || (max ? '99' : '0')) * 100 * 100 * 100
43+
+ parseInt(match[3] || (max ? '99' : '0')) * 100 * 100
44+
+ ((match[4] || (max ? 'z' : 'a')).toUpperCase().charCodeAt(0) - 65) * 100
45+
+ parseInt(match[5] || (max ? '99' : '0'));
46+
};
2347
}

0 commit comments

Comments
 (0)