|
1 |
| -// deno-lint-ignore-file no-explicit-any |
2 |
| -import { cac } from "https://unpkg.com/cac@6.7.12/mod.ts"; |
| 1 | +// deno-fmt-ignore-file |
| 2 | +import { Command } from "https://deno.land/x/cliffy@v0.25.7/command/command.ts"; |
3 | 3 | import {
|
4 | 4 | getUnityChangeset,
|
5 |
| - scrapeArchivedChangesets, |
6 |
| - scrapeBetaChangesets, |
| 5 | + listChangesets, |
| 6 | + SearchMode, |
| 7 | + FilterOptions, |
| 8 | + GroupMode, |
| 9 | + OutputMode, |
| 10 | + FormatMode, |
7 | 11 | } from "./index.ts";
|
8 |
| -import { UnityChangeset } from "./unityChangeset.ts"; |
9 | 12 |
|
10 |
| -interface CliOptions { |
11 |
| - min: string; |
12 |
| - max: string; |
13 |
| - grep: string; |
14 |
| - json: boolean; |
15 |
| - prettyJson: boolean; |
16 |
| - all: boolean; |
17 |
| - beta: boolean; |
18 |
| - versions: boolean; |
19 |
| - minorVersions: boolean; |
20 |
| - latestPatch: boolean; |
21 |
| - oldestPatch: boolean; |
22 |
| - latestLifecycle: boolean; |
23 |
| -} |
24 |
| - |
25 |
| -const groupBy = <T, K extends keyof any>(arr: T[], key: (i: T) => K) => |
26 |
| - arr.reduce((groups, item) => { |
27 |
| - (groups[key(item)] ||= []).push(item); |
28 |
| - return groups; |
29 |
| - }, {} as Record<K, T[]>); |
30 |
| - |
31 |
| -const cli = cac("unity-changeset"); |
32 |
| -cli.command("<version>", "Get a changeset for specific version") |
33 |
| - .action((version) => |
34 |
| - (async () => { |
35 |
| - try { |
36 |
| - const changeset = await getUnityChangeset(version); |
37 |
| - console.log(changeset.changeset); |
38 |
| - } catch { |
| 13 | +new Command() |
| 14 | + /* |
| 15 | + * Main command |
| 16 | + */ |
| 17 | + .name("unity-changeset") |
| 18 | + .description("Find Unity changesets.") |
| 19 | + .example("unity-changeset 2018.4.36f1", "Get changeset of Unity 2018.4.36f1 ('6cd387d23174' will be output).") |
| 20 | + .arguments("<version>") |
| 21 | + .action((_, version) => { |
| 22 | + getUnityChangeset(version) |
| 23 | + .then((c) => console.log(c.changeset)) |
| 24 | + .catch(() => { |
39 | 25 | console.error("The given version was not found.");
|
40 | 26 | Deno.exit(1);
|
41 |
| - } |
42 |
| - })() |
43 |
| - ); |
44 |
| - |
45 |
| -cli.command("list", "List changesets") |
46 |
| - .option("--min <version>", "Minimum version (included)") |
47 |
| - .option("--max <version>", "Maximum version (included)") |
48 |
| - .option("--grep <version>", "Grep version") |
49 |
| - .option("--json", "Output in json format") |
50 |
| - .option("--pretty-json", "Output in pretty json format") |
51 |
| - .option("--all", "List all changesets (alpha/beta included)") |
52 |
| - .option("--beta", "List alpha/beta changesets") |
53 |
| - .option("--versions", "Output only the available Unity versions") |
54 |
| - .option("--minor-versions", "Output only the available Unity minor versions") |
55 |
| - .option("--latest-patch", "Output only the latest Unity patch versions") |
56 |
| - .option("--oldest-patch", "Output only the oldest Unity patch versions") |
57 |
| - .option( |
58 |
| - "--latest-lifecycle", |
59 |
| - "Output only the latest lifecycle Unity patch versions", |
60 |
| - ) |
61 |
| - .action((options: CliOptions) => |
62 |
| - (async () => { |
63 |
| - let results = options.all |
64 |
| - ? (await scrapeArchivedChangesets()).concat( |
65 |
| - await scrapeBetaChangesets(), |
66 |
| - ) |
67 |
| - : options.beta |
68 |
| - ? await scrapeBetaChangesets() |
69 |
| - : await scrapeArchivedChangesets(); |
70 |
| - |
71 |
| - // Filter by min/max. |
72 |
| - const min = options.min |
73 |
| - ? UnityChangeset.toNumber(options.min, false) |
74 |
| - : Number.MIN_VALUE; |
75 |
| - const max = options.max |
76 |
| - ? UnityChangeset.toNumber(options.max, true) |
77 |
| - : Number.MAX_VALUE; |
78 |
| - results = results |
79 |
| - .filter((r) => options.grep ? r.version.includes(options.grep) : true) |
80 |
| - .filter((r) => min <= r.versionNumber && r.versionNumber <= max); |
| 27 | + }); |
| 28 | + }) |
| 29 | + /* |
| 30 | + * Sub command: list. |
| 31 | + */ |
| 32 | + .command( |
| 33 | + "list", |
| 34 | + new Command() |
| 35 | + .description("List Unity changesets.") |
| 36 | + .example("unity-changeset list", "List changesets of the archived versions.") |
| 37 | + .example("unity-changeset list --all --json", "List changesets of all versions in json format.") |
| 38 | + .example("unity-changeset list --version-only --min 2018.3 --max 2019.4", "List all versions from 2018.3 to 2019.4.") |
| 39 | + .example("unity-changeset list --version-only --grep '(2018.4|2019.4)'", "List all versions in 2018.4 and 2019.4.") |
| 40 | + .example("unity-changeset list --lts --latest-patch", "List changesets of the latest patch versions (LTS only).") |
| 41 | + // Search options. |
| 42 | + .group("Search options") |
| 43 | + .option("--all", "Search all changesets (alpha/beta included)", { conflicts: ["beta"] }) |
| 44 | + .option("--beta", "Search only pre-release (alpha/beta) changesets", { conflicts: ["all"] }) |
| 45 | + // Filter options. |
| 46 | + .group("Filter options") |
| 47 | + .option("--min <version>", "Minimum version (included)") |
| 48 | + .option("--max <version>", "Maximum version (included)") |
| 49 | + .option("--grep <regex>", "Regular expression (e.g. '20(18|19).4.*')") |
| 50 | + .option("--latest-lifecycle", "Only the latest lifecycle (default)") |
| 51 | + .option("--all-lifecycles", "All lifecycles", { conflicts: ["latest-lifecycle"] }) |
| 52 | + // Group options. |
| 53 | + .group("Group options") |
| 54 | + .option("--latest-patch", "The latest patch versions only") |
| 55 | + .option("--oldest-patch", "The oldest patch versions in lateat lifecycle only", { conflicts: ["latest-patch"] }) |
| 56 | + // Output options. |
| 57 | + .group("Output options") |
| 58 | + .option("--versions", "Outputs only the version (no changesets)") |
| 59 | + .option("--minor-versions", "Outputs only the minor version (no changesets)", { conflicts: ["version-only"] }) |
| 60 | + .option("--json", "Output in json format") |
| 61 | + .option("--pretty-json", "Output in pretty json format") |
| 62 | + .action((options) => { |
| 63 | + // Search mode. |
| 64 | + const searchMode = options.all |
| 65 | + ? SearchMode.All |
| 66 | + : options.beta |
| 67 | + ? SearchMode.PreRelease |
| 68 | + : SearchMode.Archived; |
81 | 69 |
|
82 |
| - // Group by minor version |
83 |
| - if (options.minorVersions) { |
84 |
| - results.forEach((r) => r.version = r.minor); |
85 |
| - results = Object.values(groupBy(results, (r) => r.version)).map((g) => |
86 |
| - g[0] |
87 |
| - ); |
88 |
| - } // Group by minor version and get latest lifecycle patch |
89 |
| - else if (options.latestLifecycle) { |
90 |
| - results = Object.values(groupBy(results, (r) => r.minor)) |
91 |
| - .map((g) => g.filter((v) => v.lifecycle == g[0].lifecycle)[0]); |
92 |
| - } // Group by minor version and get latest patch |
93 |
| - else if (options.latestPatch) { |
94 |
| - results = Object.values(groupBy(results, (r) => r.minor)).map((g) => |
95 |
| - g[0] |
96 |
| - ); |
97 |
| - } // Group by minor version and get oldest patch |
98 |
| - else if (options.oldestPatch) { |
99 |
| - results = Object.values(groupBy(results, (r) => r.minor)).map((g) => |
100 |
| - g[g.length - 1] |
101 |
| - ); |
102 |
| - } |
| 70 | + // Group mode. |
| 71 | + const groupMode = options.latestPatch |
| 72 | + ? GroupMode.LatestPatch |
| 73 | + : options.oldestPatch |
| 74 | + ? GroupMode.OldestPatch |
| 75 | + : GroupMode.All; |
103 | 76 |
|
104 |
| - // If the result is empty, do not output anything |
105 |
| - if (results.length == 0) { |
106 |
| - return; |
107 |
| - } |
| 77 | + // Filter options. |
| 78 | + const filterOptions : FilterOptions = { |
| 79 | + min: options.min || "", |
| 80 | + max: options.max || "", |
| 81 | + grep: options.grep || "", |
| 82 | + allLifecycles: (options.allLifecycles && !options.latestLifecycle) |
| 83 | + ? true |
| 84 | + : false, |
| 85 | + lts: false, |
| 86 | + }; |
108 | 87 |
|
109 |
| - const res = options.versions || options.minorVersions |
110 |
| - ? results.map((r) => r.version) |
111 |
| - : results; |
| 88 | + // Output mode. |
| 89 | + const outputMode = options.versions |
| 90 | + ? OutputMode.VersionOnly |
| 91 | + : options.minorVersions |
| 92 | + ? OutputMode.MinorVersionOnly |
| 93 | + : OutputMode.Changeset; |
| 94 | + |
| 95 | + // Format mode. |
| 96 | + const formatMode = options.json |
| 97 | + ? FormatMode.Json |
| 98 | + : options.prettyJson |
| 99 | + ? FormatMode.PrettyJson |
| 100 | + : FormatMode.None; |
112 | 101 |
|
113 |
| - // Output in json format or plain |
114 |
| - if (options.prettyJson) { |
115 |
| - console.log(JSON.stringify(res, null, " ")); |
116 |
| - } else if (options.json) { |
117 |
| - console.log(JSON.stringify(res)); |
118 |
| - } else { |
119 |
| - console.log(res.map((r) => r.toString()).join("\n")); |
120 |
| - } |
121 |
| - })() |
122 |
| - ); |
123 |
| - |
124 |
| -cli |
125 |
| - .usage("unity-changeset <command> [options]") |
126 |
| - .example("unity-changeset 2020.1.15f1") |
127 |
| - .example("unity-changeset 2021.1.0a7") |
128 |
| - .example("unity-changeset list") |
129 |
| - .example("unity-changeset list --beta") |
130 |
| - .example("unity-changeset list --versions") |
131 |
| - .example("unity-changeset list --versions --all") |
132 |
| - .example("unity-changeset list --versions --all --latest-patch") |
133 |
| - .help(); |
134 |
| - |
135 |
| -if (0 == Deno.args.length) { |
136 |
| - cli.outputHelp(); |
137 |
| - Deno.exit(1); |
138 |
| -} |
139 |
| - |
140 |
| -cli.parse(); |
| 102 | + listChangesets(searchMode, filterOptions, groupMode, outputMode, formatMode) |
| 103 | + .then((result) => console.log(result)); |
| 104 | + }), |
| 105 | + ) |
| 106 | + /* |
| 107 | + * Run with arguments. |
| 108 | + */ |
| 109 | + .parse(Deno.args); |
0 commit comments