Skip to content

Commit

Permalink
Merge pull request #22 from entando-k8s/ENG-1287-extract-labels
Browse files Browse the repository at this point in the history
ENG-1287 added support for extracting labels from remote environment
  • Loading branch information
ffleandro authored Sep 21, 2020
2 parents 5b6feba + f915f08 commit ea3f744
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const program = require('commander');

const { generateFromNpm, generateFromGit, generateFromEnv, generateInteractivly } = require('../lib/actions');
const { generateFromNpm, generateFromGit, generateFromEnv, generateInteractively } = require('../lib/actions');
const version = require('../package.json').version;

program.storeOptionsAsProperties(false);
Expand Down Expand Up @@ -46,5 +46,5 @@ program
if (process.argv.length > 2) {
program.parse(process.argv);
} else {
generateInteractivly();
generateInteractively();
}
2 changes: 1 addition & 1 deletion lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ module.exports = {
generateFromNpm,
generateFromGit,
generateFromEnv,
generateInteractivly: generateInteractively,
generateInteractively,
};
38 changes: 28 additions & 10 deletions lib/env-bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const yaml = require('js-yaml');
const objectMapper = require('object-mapper');
const axiosRetry = require('axios-retry');

axiosRetry(axios, { retries: 3 }); // Configure retries when extracting data
// Configure retries when extracting data
axiosRetry(axios, { retries: 5, retryDelay: axiosRetry.exponentialDelay});

var allAnswers = {};
var componentCache = {};
Expand All @@ -22,6 +23,7 @@ const ALL_TYPES = [
'plugin',
'category',
'resource',
'label',
];

var coreBaseApi, k8ssvcApi, clientId, clientSecret, apiUrlTable;
Expand Down Expand Up @@ -78,6 +80,10 @@ const componentDetailExtractors = {
components.map((c) => {
return { value: c.path, name: `${c.path}` };
}),
label: (components) =>
components.map((c) => {
return { value: c.key, name: `${c.titles.en} (${c.key})` };
}),
};

const componentIdExtractors = {
Expand Down Expand Up @@ -114,6 +120,9 @@ const componentIdExtractors = {
resource: (c) => {
return c.path;
},
label: (c) => {
return c.key;
},
};

const componentCodeExtractors = {
Expand Down Expand Up @@ -150,6 +159,9 @@ const componentCodeExtractors = {
resource: (c) => {
return c.path;
},
label: (c) => {
return c.key;
},
};

const componentResponseProcessor = {
Expand All @@ -165,7 +177,7 @@ const componentResponseProcessor = {

return pages;
},
pageModel: async (response) => {
pageModel: async (response, token) => {
return response.data.payload;
},
fragment: async (response, token) => {
Expand All @@ -185,7 +197,7 @@ const componentResponseProcessor = {

return fragments.map(f => objectMapper(f, schema));
},
widget: async (response) => {
widget: async (response, token) => {
return response.data.payload;
},
contentType: async (response, token) => {
Expand All @@ -200,16 +212,16 @@ const componentResponseProcessor = {

return contentTypes;
},
contentModel: async (response) => {
contentModel: async (response, token) => {
return response.data.payload;
},
plugin: async (response) => {
plugin: async (response, token) => {
return response.data._embedded.entandoPlugins;
},
group: async (response) => {
group: async (response, token) => {
return response.data.payload;
},
category: async (response) => {
category: async (response, token) => {
const categories = response.data.payload;
const schema = {
code: 'code',
Expand All @@ -224,6 +236,9 @@ const componentResponseProcessor = {
const files = await recursiveTraverseFileTree(response.data.payload, token);
return files;
},
label: async (response, token) => {
return response.data.payload;
},
};

function canonizeCode (code) {
Expand Down Expand Up @@ -278,7 +293,7 @@ async function getComponents (type) {
async function recursiveTraverseFileTree (root, token) {
const rootTraversed = await Promise.all(root.map(async (resource) => {
if (resource.directory === true) {
const result = await axios.get(apiUrlTable.resource.replace('{path}', resource.path), {
const result = await axios.get(apiUrlTable.resource.replace('currentPath=', `currentPath=${resource.path}`), {
headers: { Authorization: `Bearer ${token}` },
});

Expand Down Expand Up @@ -342,6 +357,7 @@ const collectQuestions = [
{ name: 'Content Types', value: 'contentType' },
{ name: 'Groups', value: 'group' },
{ name: 'Categories', value: 'category' },
{ name: 'Labels', value: 'label' },
{ name: 'File Browser', value: 'resource' },
],
},
Expand All @@ -352,7 +368,8 @@ const collectQuestions = [
message: 'Which components do you want to include in the bundle?',
choices: async (answers) => {
const type = answers.componentType;
return componentDetailExtractors[type](componentCache[type]);
const components = await getComponents(type);
return componentDetailExtractors[type](components);
},
default: async (answers) => {
if (hasProperty(allAnswers, answers.componentType)) {
Expand Down Expand Up @@ -428,7 +445,7 @@ async function saveComponentsDescriptor (type, location, componentIds) {
await fs.promises.mkdir(`${location}/${typePlural}`, { recursive: true });

let descriptors = [];
if (type === 'group' || type === 'category') { // Should group in a single descriptor
if (type === 'group' || type === 'category' || type === 'label') { // Should group in a single descriptor
console.log(`Processing ${typePlural}`);
const descriptorName = `${typePlural}/${typePlural}-descriptor.yaml`;
fs.promises.writeFile(`${location}/${descriptorName}`, yaml.safeDump(components));
Expand Down Expand Up @@ -531,6 +548,7 @@ async function setupEnvironment (options) {
pageConfiguration: `${coreBaseApi}/api/pages/{code}/configuration`,
page: `${coreBaseApi}/api/pages?status=published`,
group: `${coreBaseApi}/api/groups`,
label: `${coreBaseApi}/api/labels`,
category: `${coreBaseApi}/api/categories`,
contentType: `${coreBaseApi}/api/plugins/cms/contentTypes`,
contentTypeDetails: `${coreBaseApi}/api/plugins/cms/contentTypes/{code}`,
Expand Down

0 comments on commit ea3f744

Please sign in to comment.