|
1 |
| -import { cac, debugLog } from "./deps.ts"; |
| 1 | +import { Command, debugLog } from "./deps.ts"; |
| 2 | +import { version } from "./version.ts"; |
2 | 3 | import { printErrorAndExit } from "./helpers/console.ts";
|
3 | 4 | import { isPublishError, publish, upload } from "./helpers/api.ts";
|
4 | 5 |
|
5 |
| -export const cli = cac("cwc"); |
| 6 | +const cli = new Command() |
| 7 | + .name("cwc") |
| 8 | + .version(version) |
| 9 | + .allowEmpty(false) |
| 10 | + .throwErrors() |
| 11 | + .description("Upload & publish extensions and themes to the Chrome Web Store") |
| 12 | + .example( |
| 13 | + "Upload and auto-publish your extension", |
| 14 | + 'cwc upload --file "<path-to-file-or-folder>" --id "<extension-id>" --client-id "<client-id>" --client-secret "<client-secret>" --refresh-token "<refresh-token>" --auto-publish', |
| 15 | + ) |
| 16 | + .example( |
| 17 | + "Publish the last uploaded version of your extension", |
| 18 | + 'cwc publish --id "<extension-id>" --client-id "<client-id>" --client-secret "<client-secret>" --refresh-token "<refresh-token>"', |
| 19 | + ) |
| 20 | + .example( |
| 21 | + "Publish the last uploaded version of your extension but only to trusted testers", |
| 22 | + 'cwc publish --id "<extension-id>" --client-id "<client-id>" --client-secret "<client-secret>" --refresh-token "<refresh-token>" --trusted-testers', |
| 23 | + ) |
| 24 | + .option( |
| 25 | + "-i, --id <id>", |
| 26 | + "ID of the extension or theme on the Chrome Web Store", |
| 27 | + { |
| 28 | + required: true, |
| 29 | + requiredValue: true, |
| 30 | + global: true, |
| 31 | + }, |
| 32 | + ) |
| 33 | + .option("-c, --client-id <client-id>", "Google API OAuth2 client ID", { |
| 34 | + required: true, |
| 35 | + requiredValue: true, |
| 36 | + global: true, |
| 37 | + }) |
| 38 | + .option( |
| 39 | + "-s, --client-secret <client-secret>", |
| 40 | + "Google API OAuth2 client secret", |
| 41 | + { |
| 42 | + required: true, |
| 43 | + requiredValue: true, |
| 44 | + global: true, |
| 45 | + }, |
| 46 | + ) |
| 47 | + .option( |
| 48 | + "-r, --refresh-token <refresh-token>", |
| 49 | + "Google API OAuth2 refresh token", |
| 50 | + { |
| 51 | + required: true, |
| 52 | + requiredValue: true, |
| 53 | + global: true, |
| 54 | + }, |
| 55 | + ) |
| 56 | + .command( |
| 57 | + "upload", |
| 58 | + new Command() |
| 59 | + .description("Upload directory or ZIP file to Chrome Web Store") |
| 60 | + .option( |
| 61 | + "-f, --file <file>", |
| 62 | + "Path of directory or ZIP file to upload", |
| 63 | + { |
| 64 | + required: true, |
| 65 | + requiredValue: true, |
| 66 | + }, |
| 67 | + ) |
| 68 | + .option( |
| 69 | + "-p, --auto-publish [auto-publish:boolean]", |
| 70 | + "Automatically publish after upload", |
| 71 | + ) |
| 72 | + .action( |
| 73 | + async ( |
| 74 | + entry: { |
| 75 | + file: string; |
| 76 | + id: string; |
| 77 | + clientId: string; |
| 78 | + clientSecret: string; |
| 79 | + refreshToken: string; |
| 80 | + autoPublish?: boolean; |
| 81 | + }, |
| 82 | + ): Promise<void> => { |
| 83 | + const debug = debugLog("cwc:upload"); |
| 84 | + debug("parsed options %j", entry); |
6 | 85 |
|
7 |
| -cli.help(); |
| 86 | + const uploadResult = await upload({ |
| 87 | + source: entry.file, |
| 88 | + extensionId: entry.id, |
| 89 | + clientId: entry.clientId, |
| 90 | + clientSecret: entry.clientSecret, |
| 91 | + refreshToken: entry.refreshToken, |
| 92 | + autoPublish: entry.autoPublish || false, |
| 93 | + }); |
| 94 | + debug("upload result %j", uploadResult); |
8 | 95 |
|
9 |
| -cli.option( |
10 |
| - "--extension-id <id>", |
11 |
| - "ID of the extension on the Chrome Web Store", |
12 |
| -); |
13 |
| -cli.option("--client-id <client-id>", "Google API OAuth2 client ID"); |
14 |
| -cli.option( |
15 |
| - "--client-secret <client-secret>", |
16 |
| - "Google API OAuth2 client secret", |
17 |
| -); |
18 |
| -cli.option( |
19 |
| - "--refresh-token <refresh-token>", |
20 |
| - "Google API OAuth2 refresh token", |
21 |
| -); |
| 96 | + if (uploadResult.upload.uploadState === "SUCCESS") { |
| 97 | + console.log("Upload OK"); |
| 98 | + } else { |
| 99 | + console.warn(`Upload ${uploadResult.upload.uploadState}`); |
| 100 | + if (uploadResult.upload.itemError?.[0]) { |
| 101 | + printErrorAndExit( |
| 102 | + `${uploadResult.upload.itemError[0].error_code} – ${ |
| 103 | + uploadResult.upload.itemError[0].error_detail |
| 104 | + }`, |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
22 | 108 |
|
23 |
| -cli.command("upload", "Upload directory or ZIP file to Chrome Web Store") |
24 |
| - .option("--source <input>", "Path of directory or ZIP file to upload") |
25 |
| - .option("--auto-publish", "Automatically publish after upload") |
26 |
| - .action(async (entry) => { |
27 |
| - const debug = debugLog("cwc:upload"); |
28 |
| - debug("parsed options %j", entry); |
| 109 | + if (entry.autoPublish) { |
| 110 | + if (!isPublishError(uploadResult.publish)) { |
| 111 | + console.log(`Publish ${uploadResult.publish?.status}`); |
| 112 | + } else { |
| 113 | + printErrorAndExit(uploadResult.publish.error.message); |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + ), |
| 118 | + ) |
| 119 | + .command( |
| 120 | + "publish", |
| 121 | + new Command() |
| 122 | + .description("Publish last upload to Chrome Web Store") |
| 123 | + .option( |
| 124 | + "-t, --trusted-testers [trusted-testers:boolean]", |
| 125 | + "Publish only to trusted testers", |
| 126 | + ) |
| 127 | + .action( |
| 128 | + async ( |
| 129 | + entry: { |
| 130 | + id: string; |
| 131 | + clientId: string; |
| 132 | + clientSecret: string; |
| 133 | + refreshToken: string; |
| 134 | + trustedTesters?: boolean; |
| 135 | + }, |
| 136 | + ): Promise<void> => { |
| 137 | + const debug = debugLog("cwc:publish"); |
| 138 | + debug("parsed options %j", entry); |
| 139 | + const publishResult = await publish({ |
| 140 | + extensionId: entry.id, |
| 141 | + clientId: entry.clientId, |
| 142 | + clientSecret: entry.clientSecret, |
| 143 | + refreshToken: entry.refreshToken, |
| 144 | + trustedTesters: entry.trustedTesters || false, |
| 145 | + }); |
| 146 | + debug("publish result %j", publishResult); |
29 | 147 |
|
30 |
| - const uploadResult = await upload({ |
31 |
| - source: entry.source, |
32 |
| - extensionId: entry.extensionId, |
33 |
| - clientId: entry.clientId, |
34 |
| - clientSecret: entry.clientSecret, |
35 |
| - refreshToken: entry.refreshToken, |
36 |
| - autoPublish: entry.autoPublish || false, |
37 |
| - }); |
38 |
| - debug("upload result %j", uploadResult); |
39 |
| - |
40 |
| - if (uploadResult.upload.uploadState === "SUCCESS") { |
41 |
| - console.log("Upload OK"); |
42 |
| - } else { |
43 |
| - console.warn(`Upload ${uploadResult.upload.uploadState}`); |
44 |
| - if (uploadResult.upload.itemError?.[0]) { |
45 |
| - printErrorAndExit( |
46 |
| - `${uploadResult.upload.itemError[0].error_code} – ${ |
47 |
| - uploadResult.upload.itemError[0].error_detail |
48 |
| - }`, |
49 |
| - ); |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - if (entry.autoPublish) { |
54 |
| - if (!isPublishError(uploadResult.publish)) { |
55 |
| - console.log(`Publish ${uploadResult.publish?.status}`); |
56 |
| - } else { |
57 |
| - printErrorAndExit(uploadResult.publish.error.message); |
58 |
| - } |
59 |
| - } |
60 |
| - }); |
61 |
| - |
62 |
| -cli.command("publish", "Publish last upload to Chrome Web Store") |
63 |
| - .option("--trusted-testers", "Publish only to trusted testers") |
64 |
| - .action(async (entry) => { |
65 |
| - const debug = debugLog("cwc:publish"); |
66 |
| - debug("parsed options %j", entry); |
67 |
| - const publishResult = await publish({ |
68 |
| - extensionId: entry.extensionId, |
69 |
| - clientId: entry.clientId, |
70 |
| - clientSecret: entry.clientSecret, |
71 |
| - refreshToken: entry.refreshToken, |
72 |
| - trustedTesters: entry.trustedTesters || false, |
73 |
| - }); |
74 |
| - debug("publish result %j", publishResult); |
75 |
| - |
76 |
| - if (!isPublishError(publishResult)) { |
77 |
| - console.log(`Publish ${publishResult.status[0]}`); |
78 |
| - } else { |
79 |
| - printErrorAndExit(publishResult.error.message); |
80 |
| - } |
81 |
| - }); |
| 148 | + if (!isPublishError(publishResult)) { |
| 149 | + console.log(`Publish ${publishResult.status[0]}`); |
| 150 | + } else { |
| 151 | + printErrorAndExit(publishResult.error.message); |
| 152 | + } |
| 153 | + }, |
| 154 | + ), |
| 155 | + ); |
82 | 156 |
|
83 | 157 | if (import.meta.main) {
|
84 | 158 | try {
|
85 |
| - const parsed = cli.parse([ |
86 |
| - Deno.execPath(), |
87 |
| - import.meta.url, |
88 |
| - ...Deno.args, |
89 |
| - ], { run: false }); |
90 |
| - if (!cli.matchedCommandName && !parsed.options.help) { |
91 |
| - throw new Error("No valid command found"); |
| 159 | + const parsed = await cli.parse(Deno.args); |
| 160 | + if (parsed.args.length < 1 && Object.keys(parsed.options).length < 1) { |
| 161 | + throw new Error("No arguments provided!"); |
92 | 162 | }
|
93 |
| - cli.runMatchedCommand(); |
94 | 163 | } catch (error) {
|
95 |
| - cli.outputHelp(); |
96 |
| - printErrorAndExit(error.message, 1); |
| 164 | + console.error("Error –", error.message); |
| 165 | + cli.showHelp(); |
| 166 | + Deno.exit(1); |
97 | 167 | }
|
98 | 168 | }
|
0 commit comments