service_packager.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. 'use strict';
  34. var fs = require('fs');
  35. var path = require('path');
  36. var _ = require('lodash');
  37. var async = require('async');
  38. var pbjs = require('protobufjs/cli/pbjs');
  39. var parseArgs = require('minimist');
  40. var Mustache = require('mustache');
  41. var package_json = require('../package.json');
  42. var template_path = path.resolve(__dirname, 'service_packager');
  43. var package_tpl_path = path.join(template_path, 'package.json.template');
  44. var arg_format = {
  45. string: ['include', 'out', 'name', 'version'],
  46. alias: {
  47. include: 'i',
  48. out: 'o',
  49. name: 'n',
  50. version: 'v'
  51. }
  52. };
  53. // TODO(mlumish): autogenerate README.md from proto file
  54. /**
  55. * Render package.json file from template using provided parameters.
  56. * @param {Object} params Map of parameter names to values
  57. * @param {function(Error, string)} callback Callback to pass rendered template
  58. * text to
  59. */
  60. function generatePackage(params, callback) {
  61. fs.readFile(package_tpl_path, {encoding: 'utf-8'}, function(err, template) {
  62. if (err) {
  63. callback(err);
  64. } else {
  65. var rendered = Mustache.render(template, params);
  66. callback(null, rendered);
  67. }
  68. });
  69. }
  70. /**
  71. * Copy a file
  72. * @param {string} src_path The filepath to copy from
  73. * @param {string} dest_path The filepath to copy to
  74. */
  75. function copyFile(src_path, dest_path) {
  76. fs.createReadStream(src_path).pipe(fs.createWriteStream(dest_path));
  77. }
  78. /**
  79. * Run the script. Copies the index.js and LICENSE files to the output path,
  80. * renders the package.json template to the output path, and generates a
  81. * service.json file from the input proto files using pbjs. The arguments are
  82. * taken directly from the command line, and handled as follows:
  83. * -i (--include) : An include path for pbjs (can be dpulicated)
  84. * -o (--output): The output path
  85. * -n (--name): The name of the package
  86. * -v (--version): The package version
  87. * @param {Array} argv The argument vector
  88. */
  89. function main(argv) {
  90. var args = parseArgs(argv, arg_format);
  91. var out_path = path.resolve(args.out);
  92. var include_dirs = [];
  93. if (args.include) {
  94. include_dirs = _.map(_.flatten([args.include]), function(p) {
  95. return path.resolve(p);
  96. });
  97. }
  98. args.grpc_version = package_json.version;
  99. generatePackage(args, function(err, rendered) {
  100. if (err) throw err;
  101. fs.writeFile(path.join(out_path, 'package.json'), rendered, function(err) {
  102. if (err) throw err;
  103. });
  104. });
  105. copyFile(path.join(template_path, 'index.js'),
  106. path.join(out_path, 'index.js'));
  107. copyFile(path.join(__dirname, '..', 'LICENSE'),
  108. path.join(out_path, 'LICENSE'));
  109. var service_stream = fs.createWriteStream(path.join(out_path,
  110. 'service.json'));
  111. var pbjs_args = _.flatten(['node', 'pbjs',
  112. args._[0],
  113. '-legacy',
  114. _.map(include_dirs, function(dir) {
  115. return "-path=" + dir;
  116. })]);
  117. var old_stdout = process.stdout;
  118. process.__defineGetter__('stdout', function() {
  119. return service_stream;
  120. });
  121. var pbjs_status = pbjs.main(pbjs_args);
  122. process.__defineGetter__('stdout', function() {
  123. return old_stdout;
  124. });
  125. if (pbjs_status !== pbjs.STATUS_OK) {
  126. throw new Error('pbjs failed with status code ' + pbjs_status);
  127. }
  128. }
  129. exports.main = main;