Skip to content

Commit 08dbfa6

Browse files
committed
Update NodeJS packaging patterns.
- retire use_pbjs flag -- it's not used anymore, and it does not fit with the new pattern of Gapic code. - update the grpc package template: this fits with the proto packages used by gcloud-node. - update gax package template: this will work well with the new pattern of Gapic code, as you can see in googleapis/gapic-generator#392 or googleapis/google-cloud-node#1476
1 parent 845cd0f commit 08dbfa6

14 files changed

Lines changed: 73 additions & 223 deletions

File tree

bin/gen-api-package

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,6 @@ var parseArgs = function parseArgs() {
197197
dest: 'altJava'
198198
}
199199
);
200-
cli.addArgument(
201-
[ '--nodejs_use_pbjs' ],
202-
{
203-
defaultValue: false,
204-
action: 'storeTrue',
205-
help: 'When set, the nodejs grpc client is generated with the json data\n'
206-
+ ' generated from pbjs. Otherwise the client bundles .proto\n'
207-
+ ' files. Note that pbjs-based client does not work currently\n'
208-
+ ' (see https://github.com/googleapis/packman/issues/35).',
209-
dest: 'nodejsUsePbjs'
210-
}
211-
);
212200
cli.addArgument(
213201
[ '--override_plugins' ],
214202
{

lib/api_repo.js

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ var fs = require('fs-extra');
2424
var glob = require('glob');
2525
var packager = require('./packager');
2626
var path = require('path');
27-
var pbjs = require('protobufjs/cli/pbjs');
28-
var pbjsUtil = require('protobufjs/cli/pbjs/util');
2927
var readline = require('readline');
3028
var request = require('request');
3129
var tmp = require('tmp');
3230

3331
var EventEmitter = require('events').EventEmitter;
34-
var ProtoBuf = require('protobufjs');
3532
var StreamZip = require('node-stream-zip');
3633

3734
exports.ApiRepo = ApiRepo;
@@ -214,7 +211,7 @@ ApiRepo.prototype.buildPackages =
214211
packageInfo: that.packageInfo,
215212
generated: generated
216213
}, templateInfo[l]);
217-
if (l === 'nodejs' && !that.opts.nodejsUsePbjs) {
214+
if (l === 'nodejs') {
218215
opts.packageInfo.protoFiles = _.filter(generated, function(file) {
219216
return file.match(/\.proto$/);
220217
});
@@ -594,22 +591,7 @@ ApiRepo.prototype._buildProtos =
594591
var fullPathProtos = [];
595592
function makeModule() {
596593
var includePath = _.union(that.includePath, that.repoDirs);
597-
if (!that.opts.nodejsUsePbjs) {
598-
makeProtoBasedNodeModule(fullPathProtos, includePath);
599-
return;
600-
}
601-
var opts = {
602-
root: that.repoDirs,
603-
source: 'proto',
604-
path: includePath
605-
};
606-
607-
var builder = loadProtos(fullPathProtos, opts);
608-
var outDir = path.join(that.outDir, language);
609-
fs.mkdirsSync(outDir);
610-
var servicePath = path.join(outDir, 'service.js');
611-
var commonJS = pbjs.targets.commonjs(builder, opts);
612-
fs.writeFile(servicePath, commonJS, findOutputs);
594+
makeProtoBasedNodeModule(fullPathProtos, includePath);
613595
}
614596

615597
this._findProtos(
@@ -619,7 +601,6 @@ ApiRepo.prototype._buildProtos =
619601
next();
620602
}
621603
);
622-
makeModule();
623604
}.bind(this);
624605
this._findProtos(name, version, makeNodeModule);
625606
} else {
@@ -1033,41 +1014,3 @@ ApiRepo.prototype._makeProtocFunc = function _makeProtocFunc(opts, language) {
10331014

10341015
return callProtoc;
10351016
};
1036-
1037-
/**
1038-
* Helps construct a JSON representation each proto file for the nodejs build.
1039-
*
1040-
* @param {string[]} filenames - The list of files.
1041-
* @param {object} opts - provides configuration info
1042-
* @param {object} opts.root - a virtual root folder that contains all the protos
1043-
* @param {object} opts.path - an array of folders where other protos reside
1044-
*
1045-
* @return {object} a ProtoBuf.Builder containing loaded representations of the
1046-
* protos
1047-
*/
1048-
function loadProtos(filenames, opts) {
1049-
opts = opts || [];
1050-
var builder = ProtoBuf.newBuilder();
1051-
var loaded = [];
1052-
builder.importRoot = opts.root;
1053-
filenames.forEach(function(filename) {
1054-
var data = pbjs.sources.proto.load(filename, opts, loaded);
1055-
builder.import(data, filename);
1056-
});
1057-
builder.resolveAll();
1058-
return builder;
1059-
}
1060-
1061-
/**
1062-
* Replace isDescriptor with a version that always returns false.
1063-
*
1064-
* pbjs/util.isDescriptor excludes imports that in google/protobuf.
1065-
*
1066-
* However, the nodejs packages need to be self-contained, so we actually want
1067-
* to include these.
1068-
* @param {string} name - The name of the message.
1069-
* @return {Boolean} Whether it is a descriptor or not.
1070-
*/
1071-
pbjsUtil.isDescriptor = function(name) {
1072-
return false;
1073-
};

lib/packager.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,6 @@ function makeNodejsPackage(opts, done) {
366366
opts = _.merge({}, settings.nodejs, opts);
367367
var tasks = [];
368368

369-
if (!opts.packageInfo.nodejsUsePbjs) {
370-
opts.templates.push('service.js.mustache');
371-
}
372369
// Move copyable files to the top-level dir.
373370
opts.copyables.forEach(function(f) {
374371
var src = path.join(opts.templateDir, f);
@@ -384,7 +381,10 @@ function makeNodejsPackage(opts, done) {
384381
var dstBase = removeMustacheExt(f);
385382
var tmpl = path.join(opts.templateDir, f);
386383
var dst = path.join(opts.top, dstBase);
387-
tasks.push(expand(tmpl, dst, opts.packageInfo));
384+
// index.js should reside among other .js files for GAX packages.
385+
if (dstBase !== 'index.js') {
386+
tasks.push(expand(tmpl, dst, opts.packageInfo));
387+
}
388388
});
389389

390390
function runTask(err, jsFiles) {
@@ -397,13 +397,22 @@ function makeNodejsPackage(opts, done) {
397397
return;
398398
}
399399
opts.packageInfo.apiFiles = _.map(jsFiles, function(jsFile) {
400-
var filePath = jsFile.replace(/\.js$/, '');
400+
var filePath = path.basename(jsFile, '.js');
401401
return {
402402
name: underscoreToLowerCamelCase(filePath),
403403
filePath: filePath
404404
};
405405
});
406-
opts.packageInfo.serviceAddressName = opts.packageInfo.apiFiles[0].name;
406+
if (opts.packageInfo.apiFiles.length > 0) {
407+
_.last(opts.packageInfo.apiFiles).last = true;
408+
opts.packageInfo.serviceAddressName = opts.packageInfo.apiFiles[0].name;
409+
// Creating a task to bring index.js into the same directory where
410+
// other generated .js files exist.
411+
tasks.push(expand(path.join(opts.templateDir, 'index.js.mustache'),
412+
path.join(path.dirname(jsFiles[0]), 'index.js'),
413+
opts.packageInfo));
414+
}
415+
opts.packageInfo.singleFile = (opts.packageInfo.apiFiles.length === 1);
407416
async.series(tasks, function(err) {
408417
if (!err && opts.copyables.indexOf('PUBLISHING.md') !== -1) {
409418
console.log('The nodejs package', pkgName(opts.packageInfo),
@@ -418,7 +427,9 @@ function makeNodejsPackage(opts, done) {
418427
// find lib/*.js files for gax API packages to generate index.js properly.
419428
if (opts.mockApiFilesForTest) {
420429
// For tests, mock data is passed instead of scanning the filesystem.
421-
runTask(null, opts.mockApiFilesForTest);
430+
runTask(null, _.map(opts.mockApiFilesForTest, function(file) {
431+
return path.join(opts.top, 'src', file);
432+
}));
422433
} else {
423434
glob.glob('*.js', {cwd: path.join(opts.top, 'src')}, runTask);
424435
}

templates/gax/nodejs/index.js.mustache

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,39 @@
1616
'use strict';
1717

1818
{{#apiFiles}}
19-
var {{{name}}} = require('./src/{{{filePath}}}');
19+
var {{{name}}} = require('./{{{filePath}}}');
2020
{{/apiFiles}}
2121
var gax = require('google-gax');
22+
var extend = require('extend');
23+
{{^singleFile}}
24+
var lodash = require('lodash');
25+
{{/singleFile}}
2226

23-
module.exports = function(options) {
24-
options = options || {};
25-
if (!('scopes' in options)) {
26-
options.scopes = [];
27-
{{#apiFiles}}
28-
options.scopes.push({{{name}}}.ALL_SCOPES);
29-
{{/apiFiles}}
30-
}
27+
function {{{api.version}}}(options) {
28+
options = extend({
29+
scopes: {{{api.version}}}.ALL_SCOPES
30+
}, options);
3131
var gaxGrpc = gax.grpc(options);
32+
{{#singleFile}}
33+
return {{{apiFiles[0].name}}}(gaxGrpc);
34+
{{/singleFile}}
35+
{{^singleFile}}
3236
var result = {};
3337
{{#apiFiles}}
3438
extend(result, {{{name}}}(gaxGrpc));
3539
{{/apiFiles}}
3640
return result;
41+
{{/singleFile}}
3742
}
38-
39-
module.exports.SERVICE_ADDRESS = {{{serviceAddressName}}}.SERVICE_ADDRESS;
43+
{{{api.version}}}.SERVICE_ADDRESS = {{{serviceAddressName}}}.SERVICE_ADDRESS;
44+
{{#singleFile}}
45+
{{{api.version}}}.ALL_SCOPES = {{{serviceAddressName}}}.ALL_SCOPES;
46+
{{/singleFile}}
47+
{{^singleFile}}
48+
{{{api.version}}}.ALL_SCOPES = lodash.union(
49+
{{#apiFiles}}
50+
{{{name}}}.ALL_SCOPES{{^last}},{{/last}}
51+
{{/apiFiles}}
52+
);
53+
{{/singleFile}}
54+
module.exports = {{{api.version}}};

templates/gax/nodejs/package.json.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"grpc": "^{{{dependencies.grpc.nodejs.version}}}",
1212
"{{{api.dependsOn}}}-{{{api.version}}}": "^{{{api.semantic_version}}}"
1313
},
14-
"main": "index.js"
14+
"main": "src/index.js"
1515
}

templates/nodejs/package.json.mustache

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"main": "index.js",
99
"files": [
1010
"LICENSE",
11-
{{#nodejsUseProtos}}
1211
"proto/**/*",
13-
{{/nodejsUseProtos}}
1412
"index.js"
1513
]
1614
}

templates/nodejs/service.js.mustache

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/api_repo.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,8 @@ describe('ApiRepo', function() {
100100
describe('on the test fixture repo with no plugins', function() {
101101
var fakes, repo; // eslint-disable-line
102102
describe('configured for nodejs', function() {
103-
// TODO: add a test case for nodejsUsePbjs: false.
104103
beforeEach(function() {
105104
repo = new ApiRepo({
106-
nodejsUsePbjs: true,
107105
includePath: [path.join(__dirname, 'fixtures', 'include')],
108106
languages: ['nodejs'],
109107
templateRoot: path.join(__dirname, '..', 'templates')
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,27 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
'use strict';
1617

17-
var apiClient = require('./src/foo_api');
18-
for (var key in apiClient) {
19-
exports[key] = apiClient[key];
20-
}
21-
var apiClient = require('./src/bar_api');
22-
for (var key in apiClient) {
23-
exports[key] = apiClient[key];
18+
var fooApi = require('./foo_api');
19+
var barApi = require('./bar_api');
20+
var gax = require('google-gax');
21+
var extend = require('extend');
22+
var lodash = require('lodash');
23+
24+
function v2(options) {
25+
options = extend({
26+
scopes: v2.ALL_SCOPES
27+
}, options);
28+
var gaxGrpc = gax.grpc(options);
29+
var result = {};
30+
extend(result, fooApi(gaxGrpc));
31+
extend(result, barApi(gaxGrpc));
32+
return result;
2433
}
34+
v2.SERVICE_ADDRESS = fooApi.SERVICE_ADDRESS;
35+
v2.ALL_SCOPES = lodash.union(
36+
fooApi.ALL_SCOPES,
37+
barApi.ALL_SCOPES
38+
);
39+
module.exports = v2;

test/fixtures/nodejs/package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
"author": "Google Inc.",
66
"description": "GRPC library for service packager-unittest-v2",
77
"license": "BSD-3-Clause",
8-
"dependencies": {
9-
"grpc": "^0.10.0",
10-
"protobufjs": "^5.0.1",
11-
"google-auth-library": "^0.9.2"
12-
},
138
"main": "index.js",
149
"files": [
1510
"LICENSE",
16-
"index.js",
17-
"service.js"
11+
"proto/**/*",
12+
"index.js"
1813
]
1914
}

0 commit comments

Comments
 (0)