-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcli.js
executable file
·175 lines (146 loc) · 4.9 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env node
import path from 'node:path';
import process from 'node:process';
import meow from 'meow';
import createConfig from './config.js';
import { upload, publish, fetchToken } from './wrapper.js';
import {
isUploadSuccess,
handlePublishStatus,
} from './util.js';
const cli = meow(`
Usage
$ chrome-webstore-upload [command]
where [command] can be one of
upload, publish
if the command is missing, it will both upload and publish the extension.
Options
--source Path to either a zip file or a directory to be zipped. Defaults to the value of webExt.sourceDir in package.json or the current directory if not specified
--extension-id The ID of the Chrome Extension (environment variable EXTENSION_ID)
--client-id OAuth2 Client ID (environment variable CLIENT_ID)
--client-secret OAuth2 Client Secret (environment variable CLIENT_SECRET)
--refresh-token OAuth2 Refresh Token (environment variable REFRESH_TOKEN)
--auto-publish Can be used with the "upload" command (deprecated, use \`chrome-webstore-upload\` without a command instead)
--trusted-testers Can be used with the "publish" command
--deploy-percentage Can be used with the "publish" command. Defaults to 100
Examples
Upload and publish a new version, using existing environment variables and the default value for --source
$ chrome-webstore-upload
Upload new extension archive to the Chrome Web Store
$ chrome-webstore-upload upload --source my-custom-zip.zip
Publish the last uploaded version (whether it was uploaded via web UI or via CLI)
$ chrome-webstore-upload publish --extension-id elomekmlfonmdhmpmdfldcjgdoacjcba
`, {
importMeta: import.meta,
flags: {
source: {
type: 'string',
},
},
});
if (cli.input.length > 1) {
console.error('Too many parameters');
cli.showHelp(1);
}
const {
apiConfig,
zipPath,
isUpload,
isPublish,
autoPublish,
trustedTesters,
deployPercentage,
} = await createConfig(cli.input[0], cli.flags);
async function doAutoPublish() {
console.log('Fetching token...');
const token = await fetchToken(apiConfig);
console.log(`Uploading ${path.basename(zipPath)}...`);
const uploadResponse = await upload({
apiConfig,
token,
zipPath,
});
if (!isUploadSuccess(uploadResponse)) {
throw uploadResponse;
}
console.log('Publishing...');
const publishResponse = await publish(
{ apiConfig, token },
trustedTesters && 'trustedTesters',
deployPercentage,
);
handlePublishStatus(publishResponse);
}
async function doUpload() {
console.log(`Uploading ${path.basename(zipPath)}`);
const response = await upload({
apiConfig,
zipPath,
});
if (!isUploadSuccess(response)) {
throw response;
}
console.log('Upload completed');
}
async function doPublish() {
console.log('Publishing');
const response = await publish(
{ apiConfig },
trustedTesters && 'trustedTesters',
deployPercentage,
);
handlePublishStatus(response);
}
function errorHandler(error) {
console.log(error?.response?.body ?? error);
process.exitCode = 1;
if (error?.name === 'HTTPError') {
const response = JSON.parse(error?.response?.body ?? '{}');
const { clientId, refreshToken } = apiConfig;
if (response.error_description === 'The OAuth client was not found.') {
console.error(
'Probably the provided client ID is not valid. Try following the guide again',
);
console.error(
'https://github.com/fregante/chrome-webstore-upload-keys',
);
console.error({ clientId });
return;
}
if (response.error_description === 'Bad Request') {
const { clientId } = apiConfig;
console.error(
'Probably the provided refresh token is not valid. Try following the guide again',
);
console.error(
'https://github.com/fregante/chrome-webstore-upload-keys',
);
console.error({ clientId, refreshToken });
return;
}
if (error?.message === 'Response code 400 (Bad Request)') {
// Nothing else to add
return;
}
}
if (error?.itemError?.length > 0) {
for (const itemError of error.itemError) {
console.error('Error: ' + itemError.error_code);
console.error(itemError.error_detail);
}
}
}
async function init() {
if (isUpload && autoPublish) {
await doAutoPublish();
} else if (isUpload) {
await doUpload();
} else if (isPublish) {
await doPublish();
}
}
try {
await init();
} catch (error) {
errorHandler(error);
}