Skip to content

Commit bb8939d

Browse files
authored
Merge pull request #11 from adobe/fetch-plugin-util
extract out fetch plugin util; upgrade node
2 parents b13f7ea + 5fc968d commit bb8939d

6 files changed

+2299
-2047
lines changed

bin/fetch.access.token.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ governing permissions and limitations under the License.
1313

1414
const fetch = require('node-fetch');
1515
const { URLSearchParams } = require('url');
16+
const { ENVIRONMENTS } = require('./constants');
1617

17-
module.exports = async ({
18-
CLIENT_SECRET,
19-
IMS_HOST,
20-
IMS_PASSWORD,
21-
IMS_USER_EMAIL,
22-
IMS_USER_ID
23-
}) => {
18+
const { CLIENT_SECRET, IMS_USER_EMAIL, IMS_USER_ID, IMS_PASSWORD } = process.env;
19+
20+
module.exports = async (env = 'prod') => {
2421
if (!IMS_USER_EMAIL) {
2522
throw new Error('You need to set IMS_USER_EMAIL in your environment');
2623
}
@@ -33,6 +30,8 @@ module.exports = async ({
3330
throw new Error('You need to set CLIENT_SECRET in your environment');
3431
}
3532

33+
const { IMS_HOST } = ENVIRONMENTS[env];
34+
3635
const tokenParams = new URLSearchParams();
3736
tokenParams.append('client_id', 'NovaTestToken');
3837
tokenParams.append('client_secret', CLIENT_SECRET);

bin/fetch.plugin.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env node
2+
3+
/*
4+
Copyright 2020 Adobe. All rights reserved.
5+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License. You may obtain a copy
7+
of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software distributed under
9+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10+
OF ANY KIND, either express or implied. See the License for the specific language
11+
governing permissions and limitations under the License.
12+
*/
13+
14+
const fetch = require('node-fetch');
15+
16+
const { IMS_ORG } = process.env;
17+
18+
const { ENVIRONMENTS } = require('./constants');
19+
const fetchAccessToken = require('./fetch.access.token');
20+
21+
/* eslint-disable no-console */
22+
23+
module.exports = async (namespace, env = 'prod') => {
24+
if (!IMS_ORG) {
25+
throw new Error('You need to set IMS_ORG in your environment');
26+
}
27+
28+
const { GRAFFIAS_SERVER } = ENVIRONMENTS[env];
29+
30+
const tokenResponseJson = await fetchAccessToken(env);
31+
32+
const queryPluginsResponse = await fetch(GRAFFIAS_SERVER, {
33+
method: 'POST',
34+
headers: {
35+
'x-gw-ims-org-id': IMS_ORG,
36+
'x-gw-ims-user-id': tokenResponseJson.userId,
37+
'x-api-key': 'NovaTestToken',
38+
Authorization: `Bearer ${tokenResponseJson.access_token}`,
39+
Accept: 'application/json',
40+
'Content-Type': 'application/json;charset=UTF-8'
41+
},
42+
body: JSON.stringify({
43+
query: `
44+
query queryPlugins($namespace: String){
45+
plugins (namespace:$namespace){
46+
uuid
47+
namespace
48+
version
49+
}
50+
}`,
51+
variables: { namespace }
52+
})
53+
});
54+
55+
const plugins = await queryPluginsResponse.json();
56+
57+
if (!plugins?.data?.plugins) {
58+
throw new Error('There was a problem fetching plugins from the server.');
59+
}
60+
61+
const foundPlugin = plugins.data.plugins.find(plugin => plugin.namespace === namespace);
62+
return foundPlugin;
63+
};

bin/uploader.js

+9-43
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ const fetch = require('node-fetch');
1717
const path = require('path');
1818
const semver = require('semver');
1919

20-
const { CLIENT_SECRET, ENV_NAME, IMS_USER_EMAIL, IMS_USER_ID, IMS_PASSWORD, IMS_ORG } = process.env;
20+
const { ENV_NAME, IMS_ORG } = process.env;
2121

2222
const args = process.argv.slice(2);
2323

2424
const { ENVIRONMENTS } = require('./constants');
2525
const fetchAccessToken = require('./fetch.access.token');
26+
const fetchPlugin = require('./fetch.plugin');
2627
const readPluginJsonFromPackage = require('./read.plugin.json.from.package');
2728

2829
const CREATE_QUERY = `
@@ -48,10 +49,11 @@ const UPDATE_QUERY = `
4849
throw new Error('You need to set IMS_ORG in your environment');
4950
}
5051

51-
const env = ENV_NAME || 'prod';
52-
const { GRAFFIAS_SERVER, IMS_HOST } = ENVIRONMENTS[env];
52+
const environmentIndex = args.find(arg => arg.startsWith('--environment='));
53+
const env = environmentIndex ? environmentIndex.split('=')[1] : ENV_NAME || 'prod';
54+
const { GRAFFIAS_SERVER } = ENVIRONMENTS[env];
5355

54-
const zipFile = args[0];
56+
const zipFile = args[args.length - 1];
5557
if (!zipFile) {
5658
throw new Error('You must provide the zip plugin package as an argument.');
5759
}
@@ -64,44 +66,9 @@ const UPDATE_QUERY = `
6466

6567
const { namespace, version } = descriptor;
6668

67-
const tokenResponseJson = await fetchAccessToken({
68-
CLIENT_SECRET,
69-
IMS_PASSWORD,
70-
IMS_HOST,
71-
IMS_USER_EMAIL,
72-
IMS_USER_ID
73-
});
74-
75-
const queryPluginsResponse = await fetch(GRAFFIAS_SERVER, {
76-
method: 'POST',
77-
headers: {
78-
'x-gw-ims-org-id': IMS_ORG,
79-
'x-gw-ims-user-id': tokenResponseJson.userId,
80-
'x-api-key': 'NovaTestToken',
81-
Authorization: `Bearer ${tokenResponseJson.access_token}`,
82-
Accept: 'application/json',
83-
'Content-Type': 'application/json;charset=UTF-8'
84-
},
85-
body: JSON.stringify({
86-
query: `
87-
query queryPlugins($namespace: String){
88-
plugins (namespace:$namespace){
89-
uuid
90-
namespace
91-
version
92-
}
93-
}`,
94-
variables: { namespace }
95-
})
96-
});
97-
98-
const plugins = await queryPluginsResponse.json();
99-
100-
if (!plugins || !plugins.data || !plugins.data.plugins) {
101-
throw new Error('There was a problem fetching plugins from the server.');
102-
}
69+
const tokenResponseJson = await fetchAccessToken(env);
10370

104-
const foundPlugin = plugins.data.plugins.find(plugin => plugin.namespace === namespace);
71+
const foundPlugin = await fetchPlugin(namespace, env);
10572

10673
if (foundPlugin) {
10774
if (semver.lt(version, foundPlugin.version)) {
@@ -134,8 +101,7 @@ const UPDATE_QUERY = `
134101
});
135102

136103
const uploadedPlugin = await uploadPluginResponse.json();
137-
const uuid = uploadedPlugin.data && uploadedPlugin.data[field]
138-
? uploadedPlugin.data[field].uuid : null;
104+
const uuid = uploadedPlugin.data?.[field]?.uuid || null;
139105

140106
if (uuid) {
141107
console.log(`Uploaded plugin uuid: ${uuid}`);

0 commit comments

Comments
 (0)