-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.js
138 lines (114 loc) · 4.62 KB
/
publish.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
const fs = require('fs');
const requestp = require('request-promise-native');
const readline = require('readline-sync');
const { spawn } = require('child_process');
const { google } = require('googleapis');
if (require.main === module) {
const argv = process.argv.slice(2);
const releaseTag = argv.shift();
let pkgPath = 'out/beta/machete-beta.zip';
let appIds = ["ekcgmjhleflmfemjpeomblomcbhfnfcj"];
if (releaseTag == 'release') {
pkgPath = 'out/release/machete-release.zip';
appIds = ["linbfabhpielmegmeckbhfadhnnjoack"];
}
(async function() {
try {
console.log(`Publishing ${pkgPath} to apps ${appIds}`);
const client = await authClient();
for (const appId of appIds) {
console.log('Requesting access token...');
const header = await authorizationHeader(client);
console.log(`Uploading to ${appId}...`);
const uploadResult = await uploadPackage(appId, header, pkgPath);
console.log('Upload result:', uploadResult);
console.log(`Publishing ${appId}...`);
const publishResult = await publishPackage(appId, header);
console.log('Publish result:', publishResult);
}
console.log('Pushing and tagging current changes in git');
await gitTag(releaseTag);
await gitPush(releaseTag);
process.exit(0);
}
catch (ex) {
console.error(ex);
process.exit(1);
}
}());
}
function asyncSpawn(cmd, args) {
return new Promise((resolve, reject) => {
console.log(cmd, args.join(' '));
const child = spawn(cmd, args, { stdio: 'inherit' });
child.on('close', code => {
if (code == 0)
return resolve();
return reject('Error code: ' + code);
});
});
}
function gitTag(releaseTag) {
const manifest = require('./manifest.json');
return asyncSpawn('git', ['tag', '-f', `${releaseTag}-${manifest.version}`]);
}
async function gitPush(releaseTag) {
await asyncSpawn('git', ['push', 'origin']);
await asyncSpawn('git', ['push', 'origin', '--tags']);
if (releaseTag == 'release') {
await asyncSpawn('git', ['push', 'github']);
await asyncSpawn('git', ['push', 'github', '--tags']);
}
}
function readAsync(fname) {
return new Promise((resolve, reject) => fs.readFile(fname, 'utf8', (err, data) => (err && reject(err)) || resolve(data)));
}
async function authClient() {
const data = await readAsync('../uploader-keys.json');
const codes = JSON.parse(data);
const oauthClient = new google.auth.OAuth2(codes.client_id, codes.client_secret, 'urn:ietf:wg:oauth:2.0:oob');
return oauthClient;
}
async function authorizationHeader(oauthClient) {
try {
const cached = JSON.parse(await readAsync('../uploader-tokens'));
await oauthClient.setCredentials(cached);
return oauthClient.getRequestHeaders();
}
catch (ex) {
console.log(ex);
console.warn(`Couldn't generate headers from cached tokens`);
}
const url = oauthClient.generateAuthUrl({ access_type: 'offline', scope: ['https://www.googleapis.com/auth/chromewebstore'] });
console.log('Please visit the following URL to authorize this upload:');
console.log(` ${url}`);
const accessCode = readline.question('Enter the authorization code here: ');
const { tokens } = await oauthClient.getToken(accessCode);
oauthClient.setCredentials(tokens);
await new Promise((resolve, reject) => fs.writeFile('../new-tokens', JSON.stringify(tokens), err => (err && reject(err)) || resolve()));
return oauthClient.getRequestHeaders();
}
async function uploadPackage(appId, headers, pkgPath) {
const pkgBuffer = await new Promise((resolve, reject) => fs.readFile(pkgPath, (err, data) => (err && reject(err)) || resolve(data)));
const response = await requestp({
uri: 'https://www.googleapis.com/upload/chromewebstore/v1.1/items/' + appId,
method: 'PUT',
headers,
body: pkgBuffer
});
const status = JSON.parse(response);
if (status.uploadState != 'SUCCESS')
throw new Error(JSON.stringify(status));
return status;
}
async function publishPackage(appId, headers) {
const response = await requestp({
uri: `https://www.googleapis.com/chromewebstore/v1.1/items/${appId}/publish`,
method: 'POST',
headers
});
const status = JSON.parse(response);
if (status.status[0] != 'OK')
throw new Error(status.statusDetail.join('. '));
return status;
}