purecloud-sdk-upgrade
Advanced tools
Comparing version 0.1.1 to 0.2.0
83
main.js
@@ -24,2 +24,3 @@ #!/usr/bin/env node | ||
var backupPathOrig = ''; | ||
var simulateUpgrade = false; | ||
@@ -54,3 +55,3 @@ process.on('uncaughtException', function (error) { | ||
writeBoxSeparator(); | ||
writeBoxedLine('Example:'.dim + ' purecloud-sdk-upgrade ' + GENERATE_OPERATION + ' old.json https://site.com/new.json') | ||
writeBoxedLine('Example:'.dim + ' purecloud-sdk-upgrade ' + GENERATE_OPERATION + ' old.json https://site.com/new.json'); | ||
writeBoxSeparator(); | ||
@@ -69,5 +70,5 @@ writeBoxedLine('Parameters'.dim); | ||
writeBoxSeparator(); | ||
writeBoxedLine('Command:'.dim + ' purecloud-sdk-upgrade ' + UPGRADE_OPERATION + ' <source_path> [<path_regex>] [<backup>] [<old_swagger> <new_swagger>]'); | ||
writeBoxedLine('Command:'.dim + ' purecloud-sdk-upgrade ' + UPGRADE_OPERATION + ' <source_path> [<path_regex>] [<backup>] [<old_swagger> <new_swagger>] [<simulate>]'); | ||
writeBoxSeparator(); | ||
writeBoxedLine('Example:'.dim + ' purecloud-sdk-upgrade ' + UPGRADE_OPERATION + ' /git/project/src .*\\\\.java /backups/project old.json https://site.com/new.json') | ||
writeBoxedLine('Example:'.dim + ' purecloud-sdk-upgrade ' + UPGRADE_OPERATION + ' /git/project/src .*\\\\.java /backups/project old.json https://site.com/new.json true'); | ||
writeBoxSeparator(); | ||
@@ -78,5 +79,6 @@ writeBoxedLine('Parameters'.dim); | ||
writeBoxedLine('backup'.bold + ' Optional'.dim + ', relative to source_path, Default=backup/, If FALSE, does not create a backup. Otherwise, value should be a path to where the backup files should be placed.'); | ||
writeBoxedLine('If both of the following parameters are specified, the GENOPS command will be executed prior to upgrading files.') | ||
writeBoxedLine('If both of the following parameters are specified, the GENOPS command will be executed prior to upgrading files.'); | ||
writeBoxedLine(' old_swagger'.bold + ' a path or URL to the old swagger definition'); | ||
writeBoxedLine(' new_swagger'.bold + ' a path or URL to the new swagger definition'); | ||
writeBoxedLine('simulate'.bold + ' Optional'.dim + ', Default=false, true to process everything without writing any changes or backups'); | ||
writeBoxBottom(); | ||
@@ -126,3 +128,3 @@ } | ||
writeBoxedLine(string, width); | ||
writeBoxBottom(width) | ||
writeBoxBottom(width); | ||
} | ||
@@ -193,2 +195,4 @@ | ||
try { | ||
duplicateOperations = []; | ||
_.forEach(oldSwagger.paths, function(oldPath, pathKey) { | ||
@@ -199,15 +203,25 @@ _.forEach(oldPath, function(oldOperation, methodKey) { | ||
if(newSwagger.paths[pathKey] && newSwagger.paths[pathKey][methodKey]){ | ||
var newOperationName = newSwagger.paths[pathKey][methodKey]['x-inin-method-name']; | ||
var newOperationName = newSwagger.paths[pathKey][methodKey]['x-purecloud-method-name']; | ||
if(newOperationName !== oldOperationName){ | ||
if(operationMap[oldOperationName] && operationMap[oldOperationName] !== newOperationName){ | ||
throw oldOperationName + " already is mapped to " + operationMap[oldOperationName] + " now " + newOperationName; | ||
} | ||
operationMap[oldOperationName] = newOperationName; | ||
console.log(`ERROR: ${oldOperationName} already is mapped to ${operationMap[oldOperationName]}! Unable to upgrade ${oldOperationName}.`.red); | ||
duplicateOperations.push(oldOperationName); | ||
} else { | ||
operationMap[oldOperationName] = newOperationName; | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
console.log('Operation count: ' + Object.keys(operationMap).length); | ||
// Remove duplicate operations. They're ambigious, so we can't determine what to change them to. | ||
_.forEach(duplicateOperations, function(duplicateOperationName) { | ||
if (_.includes(_.keys(operationMap), duplicateOperationName)) { | ||
console.log(`Removing duplicate operation ${duplicateOperationName}...`); | ||
_.unset(operationMap, duplicateOperationName); | ||
console.log(`Count is now ${_.keys(operationMap).length}`); | ||
} | ||
}); | ||
console.log('Operation count: ' + _.keys(operationMap).length); | ||
console.log('Writing operationMap.json'); | ||
@@ -250,2 +264,3 @@ fs.writeFileSync('operationMap.json', JSON.stringify(operationMap, null, 2)); | ||
backupPathOrig = process.argv[5] ? process.argv[5] : 'backup'; | ||
simulateUpgrade = process.argv[8] ? process.argv[8] === 'true' : false; | ||
@@ -262,6 +277,6 @@ if (backupPathOrig.toLowerCase() == 'false') { | ||
if (backupPathOrig != '' && !path.isAbsolute(backupPathOrig)) { | ||
if (backupPathOrig !== '' && !path.isAbsolute(backupPathOrig)) { | ||
backupPathOrig = path.join(sourcePath, backupPathOrig); | ||
} | ||
var backupPath = backupPathOrig == '' ? '' : path.join(backupPathOrig, dateFormat(new Date(), 'yyyymmddhMMmss')); | ||
var backupPath = backupPathOrig === '' ? '' : path.join(backupPathOrig, dateFormat(new Date(), 'yyyymmddhMMmss')); | ||
@@ -275,2 +290,3 @@ tryAutoGenOperationMap() | ||
console.log('backupPath=' + backupPath); | ||
console.log('simulateUpgrade=' + simulateUpgrade); | ||
@@ -282,3 +298,3 @@ // Get operation map | ||
var fileList = findFiles(sourcePath, pathRegex); | ||
console.log('Processing ' + fileList.length + ' files...') | ||
console.log('Processing ' + fileList.length + ' files...'); | ||
@@ -294,4 +310,4 @@ // Process files | ||
// Determine backup file and dir | ||
var backupFile = backupPath == '' ? '' : path.join(backupPath, file.substring(sourcePath.length)); | ||
var backupFileDir = backupPath == '' ? '' : path.parse(backupFile).dir; | ||
var backupFile = backupPath === '' ? '' : path.join(backupPath, file.substring(sourcePath.length)); | ||
var backupFileDir = backupPath === '' ? '' : path.parse(backupFile).dir; | ||
@@ -306,8 +322,3 @@ // Check for each operation | ||
// Make regex expression | ||
// Add '(' to the end so we don't get partial matches | ||
// e.g. don't want 'patchUserId' to match 'patchUserIdCallforwarding' | ||
var regexp = new RegExp( | ||
'(\\b' + oldOperation + '\\()|' + | ||
'(\\b' + oldOperationCap + '\\()', | ||
'g'); | ||
var regexp = new RegExp(`(\\b${oldOperation}|${oldOperationCap})(?=(?:WithHttpInfo)?\\()`, 'g'); | ||
@@ -343,3 +354,3 @@ // Iterate through all matches and log each matched line | ||
// This else shouldn't be able to be reached, but you know what they say about assumptions... | ||
console.log('NO MATCH!'.yellow) | ||
console.log('NO MATCH!'.yellow); | ||
return match; | ||
@@ -351,12 +362,14 @@ } | ||
if (content != originalContent) { | ||
// Write backup file | ||
if (backupFileDir != '') { | ||
mkdirp.sync(backupFileDir); | ||
console.log(' Writing backup file: ' + backupFile); | ||
fs.writeFileSync(backupFile, originalContent); | ||
if (simulateUpgrade !== true) { | ||
// Write backup file | ||
if (backupFileDir !== '') { | ||
mkdirp.sync(backupFileDir); | ||
console.log(' Writing backup file: ' + backupFile); | ||
fs.writeFileSync(backupFile, originalContent); | ||
} | ||
// Write file | ||
console.log(' Writing updated file: ' + file); | ||
fs.writeFileSync(file, content); | ||
} | ||
// Write file | ||
console.log(' Writing updated file: ' + file); | ||
fs.writeFileSync(file, content); | ||
} else { | ||
@@ -409,3 +422,3 @@ console.log(' No changes found'); | ||
} | ||
}; | ||
} | ||
@@ -419,3 +432,3 @@ // Return list | ||
console.log('getFile: ' + uri) | ||
console.log(`getFile: ${uri}`); | ||
@@ -422,0 +435,0 @@ var h = null; |
@@ -23,3 +23,3 @@ { | ||
], | ||
"version":"0.1.1", | ||
"version":"0.2.0", | ||
"files": [ | ||
@@ -26,0 +26,0 @@ "main.js" |
16492
397