rebuild-cloud-sdk.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. module.exports = {
  2. friendlyName: 'Rebuild Cloud SDK',
  3. description: 'Regenerate the configuration for the "Cloud SDK" -- the JavaScript module used for AJAX and WebSockets.',
  4. fn: async function(inputs, exits){
  5. var path = require('path');
  6. var endpointsByMethodName = {};
  7. var extraEndpointsOnlyForTestsByMethodName = {};
  8. _.each(sails.config.routes, (target)=>{
  9. // If the route target is an array, then only consider
  10. // the very last sub-target in the array.
  11. if (_.isArray(target)) {
  12. target = _.last(target);
  13. }//fi
  14. // Skip redirects
  15. // (Note that, by doing this, we also skip traditional shorthand
  16. // -- that's ok though.)
  17. if (_.isString(target)) {
  18. return;
  19. }
  20. // Skip routes whose target doesn't contain `action` for any
  21. // other miscellaneous reason.
  22. if (!target.action) {
  23. return;
  24. }
  25. // Just about everything else gets a Cloud SDK method.
  26. // We determine its name using the bare action name.
  27. var bareActionName = _.last(target.action.split(/\//));
  28. var methodName = _.camelCase(bareActionName);
  29. var expandedAddress = sails.getRouteFor(target);
  30. // Skip routes that just serve views.
  31. // (but still generate them for use in tests, for convenience)
  32. if (target.view || (bareActionName.match(/^view-/))) {
  33. extraEndpointsOnlyForTestsByMethodName[methodName] = {
  34. verb: (expandedAddress.method||'get').toUpperCase(),
  35. url: expandedAddress.url
  36. };
  37. return;
  38. }//•
  39. endpointsByMethodName[methodName] = {
  40. verb: (expandedAddress.method||'get').toUpperCase(),
  41. url: expandedAddress.url,
  42. };
  43. // If this is an actions2 action, then determine appropriate serial usage.
  44. // (deduced the same way as helpers)
  45. // > If there is no such action for some reason, then don't compile a
  46. // > method for this one.
  47. var requestable = sails.getActions()[target.action];
  48. if (!requestable) {
  49. sails.log.warn('Skipping unrecognized action: `'+target.action+'`');
  50. return;
  51. }
  52. var def = requestable.toJSON && requestable.toJSON();
  53. if (def && def.fn) {
  54. if (def.args !== undefined) {
  55. endpointsByMethodName[methodName].args = def.args;
  56. } else {
  57. endpointsByMethodName[methodName].args = _.reduce(def.inputs, (args, inputDef, inputCodeName)=>{
  58. args.push(inputCodeName);
  59. return args;
  60. }, []);
  61. }
  62. }
  63. // And we determine whether it needs to communicate over WebSockets
  64. // by checking for an additional property in the route target.
  65. if (target.isSocket) {
  66. endpointsByMethodName[methodName].protocol = 'io.socket';
  67. }
  68. });//∞
  69. var jsCode =
  70. `/**
  71. * cloud.setup.js
  72. *
  73. * Configuration for this Sails app's generated browser SDK ("Cloud").
  74. *
  75. * Above all, the purpose of this file is to provide endpoint definitions,
  76. * each of which corresponds with one particular route+action on the server.
  77. *
  78. `+//* > This file was automatically generated. `+new Date()+`
  79. `* > This file was automatically generated.
  80. * > (To regenerate, run \`sails run rebuild-cloud-sdk\`)
  81. */
  82. Cloud.setup({
  83. /* eslint-disable */
  84. methods: `+JSON.stringify(endpointsByMethodName)+`
  85. /* eslint-enable */
  86. });\n`;
  87. jsCode = _.template(jsCode)(endpointsByMethodName);
  88. // Smash and rewrite the `cloud.setup.js` file in the assets folder to
  89. // reflect the latest set of available cloud actions exposed by this Sails
  90. // app (as determined by its routes above)
  91. await sails.helpers.fs.write.with({
  92. destination: path.resolve(sails.config.appPath, 'assets/js/cloud.setup.js'),
  93. string: jsCode,
  94. force: true
  95. });
  96. // Also, if a `test/` folder exists, set up a barebones bounce of this data
  97. // as a JSON file inside of it, for testing purposes:
  98. var hasTestFolder = await sails.helpers.fs.exists(path.resolve(sails.config.appPath, 'tests/'));
  99. if (hasTestFolder) {
  100. await sails.helpers.fs.write.with({
  101. destination: path.resolve(sails.config.appPath, 'test/private/CLOUD_SDK_METHODS.json'),
  102. string: JSON.stringify(_.extend(endpointsByMethodName, extraEndpointsOnlyForTestsByMethodName)),
  103. force: true
  104. });
  105. }
  106. sails.log.info('--');
  107. sails.log.info('Successfully rebuilt Cloud SDK for use in the browser.');
  108. sails.log.info('(and CLOUD_SDK_METHODS.json for use in automated tests)');
  109. return exits.success();
  110. }
  111. };