This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
/
index.js
144 lines (129 loc) · 3.74 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { fs } from 'mz';
import { Client as DefaultAMOClient } from './amo-client';
/** @typedef {import("request").OptionsWithUrl} RequestConfig */
/** @typedef {import("./amo-client").ClientParams} ClientParams */
/** @typedef {import("./amo-client").ReleaseChannel} ReleaseChannel */
/**
* @typedef {object} SignAddonParams
* @property {string} xpiPath
* @property {string} id
* @property {string} version
* @property {ClientParams['apiKey']} apiKey
* @property {ClientParams['apiSecret']} apiSecret
* @property {ClientParams['apiUrlPrefix']=} apiUrlPrefix
* @property {ClientParams['apiJwtExpiresIn']=} apiJwtExpiresIn
* @property {ClientParams['debugLogging']=} verbose
* @property {ReleaseChannel=} channel
* @property {ClientParams['statusCheckTimeout']=} timeout
* @property {ClientParams['downloadDir']=} downloadDir
* @property {ClientParams['proxyServer']=} apiProxy
* @property {ClientParams['requestConfig']=} apiRequestConfig
* @property {typeof DefaultAMOClient=} AMOClient
*
* @param {SignAddonParams} params
*/
const signAddon = async ({
// Absolute path to add-on XPI file.
xpiPath,
// The add-on ID as recognized by AMO. Example: my-addon@jetpack
id,
// The add-on version number for AMO.
version,
// Your API key (JWT issuer) from AMO Devhub.
apiKey,
// Your API secret (JWT secret) from AMO Devhub.
apiSecret,
// Optional arguments:
apiUrlPrefix = 'https://addons.mozilla.org/api/v4',
// Number of seconds until the JWT token for the API request expires.
// This must match the expiration time that the API server accepts.
apiJwtExpiresIn,
verbose = false,
// The release channel (listed or unlisted).
// Ignored for new add-ons, which are always unlisted.
// Defaults to most recently used channel.
channel,
// Number of milliseconds to wait before giving up on a
// response from Mozilla's web service.
timeout,
// Absolute directory to save downloaded files in.
downloadDir,
// Optional proxy to use for all API requests,
// such as "http://yourproxy:6000"
apiProxy,
// Optional object to pass into request() for additional configuration.
// Not all properties are guaranteed to be applied.
apiRequestConfig,
AMOClient = DefaultAMOClient,
}) => {
/**
* @param {string} name
*/
function reportEmpty(name) {
throw new Error(`required argument was empty: ${name}`);
}
if (!xpiPath) {
reportEmpty('xpiPath');
}
if (!version) {
reportEmpty('version');
}
if (!apiSecret) {
reportEmpty('apiSecret');
}
if (!apiKey) {
reportEmpty('apiKey');
}
try {
const stats = await fs.stat(xpiPath);
if (!stats.isFile) {
throw new Error(`not a file: ${xpiPath}`);
}
} catch (statError) {
throw new Error(`error with ${xpiPath}: ${statError}`);
}
const client = new AMOClient({
apiKey,
apiSecret,
apiUrlPrefix,
apiJwtExpiresIn,
downloadDir,
debugLogging: verbose,
statusCheckTimeout: timeout,
proxyServer: apiProxy,
requestConfig: apiRequestConfig,
});
return client.sign({
xpiPath,
guid: id,
version,
channel,
});
};
/**
* @param {SignAddonParams} options
* @param {{
* systemProcess?: typeof process,
* throwError?: boolean,
* logger?: typeof console
* }} extras
* @returns {Promise<void>}
*/
export const signAddonAndExit = async (
options,
{ systemProcess = process, throwError = false, logger = console },
) => {
try {
const result = await signAddon(options);
logger.log(result.success ? 'SUCCESS' : 'FAIL');
systemProcess.exit(result.success ? 0 : 1);
} catch (err) {
logger.error('FAIL');
if (throwError) {
throw err;
}
logger.error(err.stack);
systemProcess.exit(1);
}
};
export default { signAddon, signAddonAndExit };