Guest User

Untitled

a guest
Jan 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #!/usr/bin/node
  2. ////////////////////////////////////////////////////////////////////////////////
  3. //// Create AWS Service Promisified Functions
  4. //// @author Michael Rosata
  5. ////
  6. //// @dependencies ramda, yargs
  7. ////
  8. //// @desc
  9. //// Returns the code that would wrap every method from one AWS Service into
  10. //// functions that return Promises rather than those returning Node style
  11. //// callbacks.
  12. //// The new functions are named similar to their original counterparts.
  13. //// IE:
  14. //// S3.getBucket() --> s3GetBucket()
  15. //// It can also generate an entire module.export = {...} for the service.
  16. //// in which case `s3getBucket` becomes `s3.getBucket`
  17. ////
  18. //// @uses
  19. //// node ./denode-aws-service-code.js --service s3
  20. //// node ./denode-aws-service-code.js --service s3 --as-export
  21. //// node ./denode-aws-service-code.js --service sqs --namespace SQS
  22. //// node ./denode-aws-service-code.js --service sqs --promisify customFnName
  23. ////
  24. const AWS = require('aws-sdk')
  25. const argv = require('yargs').argv
  26. const {
  27. map,
  28. filter,
  29. complement,
  30. pipe,
  31. either,
  32. equals,
  33. apply,
  34. join,
  35. propOr,
  36. toUpper,
  37. } = require('ramda')
  38.  
  39.  
  40. ////
  41. //// Command Line Arguments
  42. const SERVICE = argv.service || argv.s || ''
  43. const NS = argv.namespace || argv.ns || toUpper(SERVICE)
  44. const AS_EXPORT = Boolean(argv.asExport || argv.e)
  45. const PROMISIFY = argv.promisify || argv.p || 'denodify'
  46.  
  47.  
  48. ////
  49. //// Utility Functions
  50. ////
  51.  
  52. // capitalize ::
  53. const capitalize =
  54. s =>
  55. `${s.substr(0,1).toUpperCase()}${s.substr(1)}`
  56.  
  57. // getCode ::
  58. const getCode =
  59. (ns, promisify) =>
  60. methods =>
  61. methods.map(name => `const ${ns}${capitalize(name)} = ${promisify}(${ns}.${name}, ${ns})`)
  62.  
  63. // getExport ::
  64. const getExportCode =
  65. (ns, promisify) =>
  66. (methods = []) => [
  67. `module.exports = {`, // Declare and open object literal
  68. ...methods.map(name => ` ${name}: ${promisify}(${ns}.${name}, ${ns}),`),
  69. '}', // closed object literal
  70. ]
  71.  
  72. // notApiOrConstructor ::
  73. const notApiOrConstructor = complement(
  74. either(
  75. equals('serviceIdentifier'),
  76. either(equals('api'), equals('constructor'))
  77. ))
  78.  
  79.  
  80. ////
  81. //// Main Code
  82. ////
  83.  
  84. /**
  85. * Create Code which would promisify (denodify) an AWS service
  86. * into either a series of declared functions or as one namespace
  87. * that essentially has the same collection of methods as the
  88. * service being denodified
  89. *
  90. * @param {string} serviceName - name of service, ie: 's3', 'sqs'.
  91. * @param {string} [serviceNS] - namespace of service on AWS object. By
  92. * default it assumes the `serviceName` capitalized.
  93. * @param {string} [promisify] - name of the promisify function.
  94. * @param {boolean} [asExport] - default false, create as object instead
  95. * of having all individual functions.
  96. */
  97. function denodeAwsServiceCode(serviceName, serviceNS = toUpper(serviceName), promisify = 'denodify', asExport = false) {
  98. // Create the service so we can get the method names from its __proto__
  99. const service = new AWS[serviceNS]()
  100.  
  101. // Depending on whether the code will be one namespace or many functions, we need different builder
  102. const codeBuilder = asExport ? getExportCode : getCode
  103.  
  104. // Get the keys, filter unwanted properties, build the code
  105. return pipe(
  106. x => Reflect.ownKeys(x.__proto__),
  107. filter(notApiOrConstructor),
  108. codeBuilder(serviceName, promisify),
  109. join('\n')
  110. )(service)
  111. }
  112.  
  113.  
  114. ////
  115. //// If Command Line Arguments Passed, then we shall execute the function
  116. //// to to build and output code that promisifies the AWS 'SERVICE' service
  117. if (SERVICE && NS) {
  118. const theCode = denodeAwsServiceCode(SERVICE, NS, PROMISIFY, AS_EXPORT)
  119.  
  120. console.log(
  121. `/**\
  122. \n * AWS SERVICE ${NS} AS PROMISE RETURNING FUNCTIONS\
  123. \n */\
  124. \n\const AWS = require('aws-sdk')
  125. \n\
  126. \n// denodify ::\
  127. \nconst denodify =\
  128. \n (fn, context = null) =>\
  129. \n (...args) =>\
  130. \n new Promise((resolve, reject) => {\
  131. \n fn.call(context, ...args, (err, data) => {\
  132. \n if (err) return reject(err)\
  133. \n return resolve(data)\
  134. \n })\
  135. \n })\
  136. \n\
  137. \n////\
  138. \n//// Actual AWS Service:${NS}:\
  139. \nconst ${SERVICE} = new AWS.${NS}({/*..config..*/})\
  140. \n\
  141. \n////\
  142. \n//// Denodified Versions of ${SERVICE} API methods\
  143. \n${theCode}\
  144. \n`
  145. )
  146. }
  147.  
  148.  
  149. module.exports = denodeAwsServiceCode
Add Comment
Please, Sign In to add comment