Skip to content

Commit

Permalink
fix(cli): make download-connector-list more robust
Browse files Browse the repository at this point in the history
Signed-off-by: Taranveer Virk <taranveer@virk.cc>
  • Loading branch information
virkt25 committed Jun 20, 2018
1 parent 0cba673 commit a4c2ce0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
42 changes: 14 additions & 28 deletions packages/cli/bin/download-connector-list.js
Expand Up @@ -2,8 +2,8 @@ const https = require('https');
const fs = require('fs');
const path = require('path');
const util = require('util');
const request = require('request-promise-native');

const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);

const DEST = path.resolve('generators/datasource/connectors.json');
Expand All @@ -15,36 +15,20 @@ const URL =
* so the list only has to be maintained in one place.
*/
async function download() {
var file = fs.createWriteStream(DEST);
var request = https
.get(URL, function(response) {
response.pipe(file);
file.on('finish', async function() {
file.close();
await transformConnectorJSON();
});
})
.on('error', function(err) {
fs.unlink(DEST);
return err;
});
}

/**
* This function transforms the array of Connector objects from
* loopback-workspace as follows:
*
* - Transforms the array into an object / map
* - Transforms display:password to type:password so it can be used by CLI directly
* - Transforms description to message so it can be used by CLI directly
*/
async function transformConnectorJSON() {
let data = await readFileAsync(DEST, 'utf-8');
data = JSON.parse(data);
const data = await request(URL, {json: true});
const out = {};

/**
* This transforms the array of Connector objects from
* loopback-workspace as follows:
*
* - Transforms the array into an object / map
* - Transforms display:password to type:password so it can be used by CLI directly
* - Transforms description to message so it can be used by CLI directly
*/
data.forEach(item => {
if (item.settings) {
Object.entries(item.settings).forEach(([key, value]) => {
Object.values(item.settings).forEach(value => {
if (value.display === 'password') {
value.type = 'password';
delete value.display;
Expand All @@ -58,6 +42,8 @@ async function transformConnectorJSON() {
}
out[item.name] = item;
});

// Write data to file
await writeFileAsync(DEST, JSON.stringify(out, null, 2));
}

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/package.json
Expand Up @@ -31,6 +31,8 @@
"mem-fs": "^1.1.3",
"mem-fs-editor": "^4.0.0",
"nsp": "^3.2.1",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
"rimraf": "^2.6.2",
"sinon": "^4.5.0",
"yeoman-assert": "^3.1.1",
Expand Down

0 comments on commit a4c2ce0

Please sign in to comment.