forked from fregante/chrome-webstore-upload-cli
/
index.js
executable file
·122 lines (103 loc) · 3.13 KB
/
index.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
#!/usr/bin/env node
const ora = require('ora');
const path = require('path');
const meow = require('meow');
const { green, red, yellow } = require('chalk');
const createConfig = require('./config');
const { upload, publish, fetchToken } = require('./wrapper');
const {
isUploadSuccess,
exitWithUploadFailure,
exitWithPublishStatus,
validateInput
} = require('./util');
const cli = meow(`
Usage
$ webstore <command>
where <command> is one of
upload, publish
Options
--source Path to either a zip file, or a directory to be zipped
--extension-id The ID of the Chrome Extension
--client-id OAuth2 Client ID
--client-secret OAuth2 Client Secret
--refresh-token OAuth2 Refresh Token
--auto-publish Can be used with the "upload" command
Examples
Upload new extension archive to the Chrome Web Store
$ webstore upload extension.zip --extension-id $EXTENSION_ID --client-id $CLIENT_ID --client-secret $CLIENT_SECRET --refresh-token $REFRESH_TOKEN
Publish extension (with CLIENT_ID, CLIENT_SECRET, and REFRESH_TOKEN set as env variables)
$ webstore publish --client-id elomekmlfonmdhmpmdfldcjgdoacjcba
`, {
string: ['_'],
default: {
source: process.cwd()
}
});
const preliminaryValidation = validateInput(cli.input, cli.flags);
if (preliminaryValidation.error) {
console.error(red(preliminaryValidation.error));
cli.showHelp(1);
}
const {
apiConfig,
zipPath,
isUpload,
isPublish,
autoPublish
} = createConfig(cli.input[0], cli.flags);
const spinner = ora();
const spinnerStart = (text) => {
spinner.text = text;
return spinner.start();
};
if (isUpload && autoPublish) {
spinnerStart('Fetching token');
fetchToken(apiConfig).then(token => {
spinnerStart(`Uploading ${path.basename(zipPath)}`);
return upload({
apiConfig,
token,
zipPath
}).then(uploadRes => {
if (!isUploadSuccess(uploadRes)) {
spinner.stop();
return exitWithUploadFailure(uploadRes);
}
spinnerStart('Publishing');
return publish({ apiConfig, token }).then(publishRes => {
spinner.stop();
exitWithPublishStatus(publishRes);
});
})
}).catch(errorHandler);
return;
}
if (isUpload) {
spinnerStart(`Uploading ${path.basename(zipPath)}`);
upload({
apiConfig,
zipPath
}).then(res => {
spinner.stop();
if (!isUploadSuccess(res)) return exitWithUploadFailure(res);
console.log(green('Upload Completed'));
}).catch(errorHandler);
}
if (isPublish) {
spinnerStart('Publishing');
publish({ apiConfig }).then(res => {
spinner.stop();
exitWithPublishStatus(res);
}).catch(errorHandler);
}
function errorHandler(err) {
spinner.stop();
console.error(red(err.message));
if (err.response && err.response.body) {
try {
console.error(yellow(JSON.stringify(err.response.body, null, 4)));
} catch (err) {}
}
process.exit(1);
}