UNPKG

2.52 kBJavaScriptView Raw
1/***************************************************************************************
2 * (c) 2017 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 ****************************************************************************************/
12
13const fs = require('fs');
14const chalk = require('chalk');
15const request = require('request-promise-native');
16const getReactorHeaders = require('./getReactorHeaders');
17const handleResponseError = require('./handleResponseError');
18const logVerboseHeader = require('./logVerboseHeader');
19
20module.exports = async(
21 envConfig,
22 accessToken,
23 extensionPackageManifest,
24 extensionPackageFromServer,
25 zipPath,
26 argv
27) => {
28 const shouldPost = !extensionPackageFromServer;
29
30 if (extensionPackageFromServer) {
31 console.log(`An existing development extension package with the name ` +
32 `${chalk.bold(extensionPackageManifest.name)} was found on the server and will be updated. ` +
33 `The extension package ID is ${chalk.bold(extensionPackageFromServer.id)}.`);
34 } else {
35 console.log(`No development extension package was found on the server with the ` +
36 `name ${chalk.bold(extensionPackageManifest.name)}. A new extension package will be created.`);
37 }
38
39 if (argv.verbose) {
40 logVerboseHeader('Uploading zip');
41 }
42
43 const options = {
44 method: shouldPost ? 'POST' : 'PATCH',
45 url: shouldPost ?
46 envConfig.extensionPackages : `${envConfig.extensionPackages}/${extensionPackageFromServer.id}`,
47 headers: getReactorHeaders(accessToken),
48 formData: {
49 package: fs.createReadStream(zipPath)
50 },
51 transform: JSON.parse
52 };
53
54 try {
55 const body = await request(options);
56 const extensionPackageId = body.data.id;
57
58 if (shouldPost) {
59 console.log(`The extension package has been assigned the ID ${chalk.bold(extensionPackageId)}.`);
60 }
61
62 return extensionPackageId;
63 } catch (error) {
64 handleResponseError(error, 'Error uploading extension package.');
65 }
66};