This repository has been archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
sign.js
131 lines (119 loc) · 3.62 KB
/
sign.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var _ = require("lodash");
var getID = require("jetpack-id");
var tmp = require("tmp");
var nodefn = require("when/node");
var when = require("when");
var DefaultAMOClient = require("./amo-client").Client;
var cmd = require("./cmd");
var utils = require("./utils");
var logger = utils.console;
var xpi = require("./xpi");
var AMO_API_PREFIX = require("./settings").AMO_API_PREFIX;
function sign(options, config) {
config = _.assign({
createXPI: xpi,
getManifest: utils.getManifest,
AMOClient: DefaultAMOClient,
}, config);
options = _.assign({
addonDir: process.cwd(),
apiUrlPrefix: AMO_API_PREFIX,
}, options);
return when.promise(function(resolve, reject) {
var missingOptions = [];
var toCheck = [
{value: options.apiKey, flag: "--api-key"},
{value: options.apiSecret, flag: "--api-secret"},
];
toCheck.forEach(function(opt) {
if (!opt.value) {
missingOptions.push(opt.flag);
}
});
if (missingOptions.length) {
console.error();
missingOptions.forEach(function(flag) {
console.error(" error: missing required option `%s'", flag);
});
console.error();
return reject();
}
resolve(config.getManifest({
addonDir: options.addonDir,
xpiPath: options.xpi,
}));
}).then(function(manifest) {
var xpiConfig = {
manifest: manifest,
};
if (options.xpi) {
logger.log("Signing XPI: " + options.xpi);
return _.assign(xpiConfig, {xpiPath: options.xpi});
} else {
var createTmpDir = nodefn.lift(tmp.dir);
return createTmpDir({
prefix: "tmp-unsigned-xpi-",
unsafeCleanup: true,
})
.then(function(tmpResult) {
var tmpDir = tmpResult[0];
var removeTmpDir = tmpResult[1];
var xpiOptions = _.assign({}, options, {
xpiPath: tmpDir,
});
return config.createXPI(manifest, xpiOptions)
.then(function(xpiPath) {
logger.log("Created XPI for signing: " + xpiPath);
return _.assign(xpiConfig, {
xpiPath: xpiPath,
cleanUp: removeTmpDir,
});
});
});
}
}).then(function(xpiConfig) {
var client = new config.AMOClient({
apiKey: options.apiKey,
apiSecret: options.apiSecret,
apiUrlPrefix: options.apiUrlPrefix,
debugLogging: options.verbose,
signedStatusCheckTimeout: options.timeout || undefined,
});
return client.sign({
xpiPath: xpiConfig.xpiPath,
guid: getID(xpiConfig.manifest),
version: xpiConfig.manifest.version,
}).then(function(result) {
if (typeof xpiConfig.cleanUp !== "undefined") {
client.debug("cleaning up XPI temp directory");
xpiConfig.cleanUp();
}
return result;
});
});
}
function signCmd(program, options, config) {
config = _.assign({
systemProcess: process,
}, config);
return when.promise(function(resolve, reject) {
resolve(cmd.validateProgram(program));
}).then(function() {
return sign(_.assign({}, options, program), config);
}).then(function(result) {
logger.log(result.success ? "SUCCESS" : "FAIL");
config.systemProcess.exit(result.success ? 0 : 1);
}, function(err) {
logger.error("FAIL");
if (err) {
console.error(err.stack);
}
config.systemProcess.exit(1);
});
}
exports.sign = sign;
exports.signCmd = signCmd;