Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Update for config validation #36

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 61 additions & 49 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,73 @@ var config = require('./config');
var log = require('./lib/util/log');

config = util.buildAppConfig(config)
util.validateConfig(config)

let validate = util.validateConfig(config)
validate.then(() => {
intialProcess().then(() => {
return
}).catch((error) => {
log.error(error);
return
})
}).catch((error) => {
log.error(error)
return
})
exports.getConfig = function () {
return config;
};

async function intialProcess() {
return new Promise(function (resolve, reject) {
login(config).then(function () {
var types = config.modules.types;
if (process.argv.length === 3) {
var val = process.argv[2];

login(config).then(function () {
var types = config.modules.types;
if (val && types.indexOf(val) > -1) {
var exportedModule = require('./lib/export/' + val);
return exportedModule.start().then(function () {
log.success(val + ' was exported successfully!');
return resolve()
}).catch(function (error) {
log.error('Failed to migrate ' + val);
log.error(error);
return reject()
})
} else {
log.error('Please provide valid module name.');
return 0;
}
} else if (process.argv.length === 2) {
var counter = 0;
return Bluebird.map(types, function (type) {
if (config.preserveStackVersion) {
log.success('Exporting: ' + types[counter])
var exportedModule = require('./lib/export/' + types[counter]);
counter++
return exportedModule.start()
} else if (!config.preserveStackVersion && type !== 'stack') {
log.success('Exporting: ' + types[counter])
var exportedModule = require('./lib/export/' + types[counter]);
counter++
return exportedModule.start()
} else {
counter++
}
}, {
concurrency: 1
}).then(function () {
log.success('Stack: ' + config.source_stack + ' has been exported succesfully!');
return resolve()
}).catch(function (error) {
log.error('Failed to migrate stack: ' + config.source_stack + '. Please check error logs for more info');
return reject(error)
});

if (process.argv.length === 3) {
var val = process.argv[2];

if (val && types.indexOf(val) > -1) {
var exportedModule = require('./lib/export/' + val);

return exportedModule.start().then(function () {
log.success(val + ' was exported successfully!');
return;
}).catch(function (error) {
log.error('Failed to migrate ' + val);
log.error(error);
return;
})
} else {
log.error('Please provide valid module name.');
return 0;
}
} else if (process.argv.length === 2) {
var counter = 0;
return Bluebird.map(types, function (type) {
if(config.preserveStackVersion) {
log.success('Exporting: ' + types[counter])
var exportedModule = require('./lib/export/' + types[counter]);
counter++
return exportedModule.start()
} else if(!config.preserveStackVersion && type !== 'stack') {
log.success('Exporting: ' + types[counter])
var exportedModule = require('./lib/export/' + types[counter]);
counter++
return exportedModule.start()
} else {
counter++
log.error('Only one module can be exported at a time.');
return 0;
}
}, {
concurrency: 1
}).then(function () {
log.success('Stack: ' + config.source_stack + ' has been exported succesfully!');
}).catch(function (error) {
log.error('Failed to migrate stack: ' + config.source_stack + '. Please check error logs for more info');
log.error(error);
});

} else {
log.error('Only one module can be exported at a time.');
return 0;
}
});
})
}
44 changes: 30 additions & 14 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,45 @@
var _ = require('lodash')
var pkg = require('../../package')
var defaultConfig = require('../../config/default')
var log = require('./log');

exports.validateConfig = function(config) {
if (!config.host || !config.cdn) {
throw new Error('Host/CDN end point is missing from config');
}
exports.validateConfig = async function (config) {
return new Promise(async function (resolve, reject) {
if (!config.host || !config.cdn) {
return reject('Host/CDN end point is missing from config')
}
if (config.email) {
if (await validateEmail(config.email) && config.password) {
} else {
return reject('Please Provide Valid credential')
}
} else if(config.email && !config.password || config.password && !config.email) {
return reject('Please provide valid credential')
}

if(config.email && config.password && !config.access_token || !config.source_stack) {
throw new Error('Kindly provide access_token or api_token');
} else if(!config.email && !config.password && !config.management_token && !config.source_stack && !config.access_token) {
throw new Error('Kindly provide management_token or email and password');
} else if(config.email && config.password && !config.access_token && config.source_stack) {
throw new Error('Kindly provide access_token');
} else if(!config.email && !config.password && config.preserveStackVersion) {
throw new Error('Kindly provide Email and password for stack details');
}
if (!config.email && !config.password && !config.management_token && !config.source_stack && !config.access_token) {
return reject('Kindly provide source_stack and management_token, email and password or access_token')
} else if (config.email && config.password && !config.access_token && !config.source_stack && !config.management_token) {
return reject('Kindly provide source_stack and management_token or access_token')
} else if (!config.email && !config.password && config.preserveStackVersion) {
return reject('Kindly provide Email and password for stack details')
} else if (!config.email && !config.password && config.source_stack && !config.access_token && !config.management_token || !config.source_stack && config.management_token) {
return reject('Kindly provide Email and password or management_token or access_token')
}
return resolve()
})
};

async function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}

exports.buildAppConfig = function (config) {
config = _.merge(defaultConfig, config)
config.headers = {
api_key: config.source_stack,
authorization: config.management_token,
// access_token: config.access_token,
'X-User-Agent': 'contentstack-export/v' + pkg.version
};
return config;
Expand Down