@grpc/proto-loader
Advanced tools
Comparing version 0.6.0-pre7 to 0.6.0-pre8
@@ -23,6 +23,17 @@ #!/usr/bin/env node | ||
const path = require("path"); | ||
const mkdirp = require("mkdirp"); | ||
const Protobuf = require("protobufjs"); | ||
const yargs = require("yargs"); | ||
const camelCase = require("lodash.camelcase"); | ||
const util_1 = require("../src/util"); | ||
function compareName(x, y) { | ||
if (x.name < y.name) { | ||
return -1; | ||
} | ||
else if (x.name > y.name) { | ||
return 1; | ||
} | ||
else { | ||
return 0; | ||
} | ||
} | ||
class TextFormatter { | ||
@@ -270,2 +281,3 @@ constructor() { | ||
formatter.writeLine(''); | ||
messageType.fieldsArray.sort((fieldA, fieldB) => fieldA.id - fieldB.id); | ||
for (const field of messageType.fieldsArray) { | ||
@@ -336,3 +348,3 @@ if (field.resolvedType && childTypes.indexOf(field.resolvedType) < 0) { | ||
function generateMessageAndEnumImports(formatter, namespace) { | ||
for (const nested of namespace.nestedArray) { | ||
for (const nested of namespace.nestedArray.sort(compareName)) { | ||
if (nested instanceof Protobuf.Type || nested instanceof Protobuf.Enum) { | ||
@@ -349,3 +361,3 @@ formatter.writeLine(getImportLine(nested)); | ||
formatter.indent(); | ||
for (const nested of namespace.nestedArray) { | ||
for (const nested of namespace.nestedArray.sort(compareName)) { | ||
if (nested instanceof Protobuf.Enum || nested instanceof Protobuf.Type) { | ||
@@ -367,3 +379,3 @@ formatter.writeLine(`export type ${nested.name} = ${getTypeInterfaceName(nested)};`); | ||
formatter.indent(); | ||
for (const methodName of Object.keys(serviceType.methods)) { | ||
for (const methodName of Object.keys(serviceType.methods).sort()) { | ||
const method = serviceType.methods[methodName]; | ||
@@ -415,3 +427,3 @@ for (const name of [methodName, camelCase(methodName)]) { | ||
formatter.indent(); | ||
for (const nested of namespace.nestedArray) { | ||
for (const nested of namespace.nestedArray.sort(compareName)) { | ||
if (nested instanceof Protobuf.Service) { | ||
@@ -444,3 +456,3 @@ generateServiceClientInterface(formatter, nested); | ||
formatter.indent(); | ||
for (const nested of namespace.nestedArray) { | ||
for (const nested of namespace.nestedArray.sort(compareName)) { | ||
generateSingleLoadedDefinitionType(formatter, nested); | ||
@@ -454,3 +466,3 @@ } | ||
formatter.indent(); | ||
for (const methodName of Object.keys(serviceType.methods)) { | ||
for (const methodName of Object.keys(serviceType.methods).sort()) { | ||
const method = serviceType.methods[methodName]; | ||
@@ -487,3 +499,3 @@ const requestType = 'messages.' + stripLeadingPeriod(method.resolvedRequestType.fullName) + '__Output'; | ||
formatter.indent(); | ||
for (const nested of namespace.nestedArray) { | ||
for (const nested of namespace.nestedArray.sort(compareName)) { | ||
if (nested instanceof Protobuf.Service) { | ||
@@ -499,3 +511,3 @@ generateServiceHandlerInterface(formatter, nested); | ||
} | ||
function generateMasterFile(formatter, root, options) { | ||
function generateRootFile(formatter, root, options) { | ||
formatter.writeLine(`import * as grpc from '${options.grpcLib}';`); | ||
@@ -525,4 +537,5 @@ formatter.writeLine("import { ServiceDefinition, EnumTypeDefinition, MessageTypeDefinition } from '@grpc/proto-loader';"); | ||
} | ||
function writeFile(filename, contents) { | ||
return mkdirp(path.dirname(filename)).then(() => fs.promises.writeFile(filename, contents)); | ||
async function writeFile(filename, contents) { | ||
await fs.promises.mkdir(path.dirname(filename), { recursive: true }); | ||
return fs.promises.writeFile(filename, contents); | ||
} | ||
@@ -535,3 +548,5 @@ function generateFilesForNamespace(namespace, options) { | ||
generateMessageInterfaces(fileFormatter, nested, options); | ||
console.log(`Writing ${options.outDir}/${getPath(nested)} from file ${nested.filename}`); | ||
if (options.verbose) { | ||
console.log(`Writing ${options.outDir}/${getPath(nested)} from file ${nested.filename}`); | ||
} | ||
filePromises.push(writeFile(`${options.outDir}/${getPath(nested)}`, fileFormatter.getFullText())); | ||
@@ -541,3 +556,5 @@ } | ||
generateEnumInterface(fileFormatter, nested); | ||
console.log(`Writing ${options.outDir}/${getPath(nested)} from file ${nested.filename}`); | ||
if (options.verbose) { | ||
console.log(`Writing ${options.outDir}/${getPath(nested)} from file ${nested.filename}`); | ||
} | ||
filePromises.push(writeFile(`${options.outDir}/${getPath(nested)}`, fileFormatter.getFullText())); | ||
@@ -554,4 +571,6 @@ } | ||
const masterFileFormatter = new TextFormatter(); | ||
generateMasterFile(masterFileFormatter, root, options); | ||
console.log(`Writing ${options.outDir}/${masterFileName}`); | ||
generateRootFile(masterFileFormatter, root, options); | ||
if (options.verbose) { | ||
console.log(`Writing ${options.outDir}/${masterFileName}`); | ||
} | ||
filePromises.push(writeFile(`${options.outDir}/${masterFileName}`, masterFileFormatter.getFullText())); | ||
@@ -561,36 +580,6 @@ filePromises.push(...generateFilesForNamespace(root, options)); | ||
} | ||
function addIncludePathResolver(root, includePaths) { | ||
const originalResolvePath = root.resolvePath; | ||
root.resolvePath = (origin, target) => { | ||
if (path.isAbsolute(target)) { | ||
return target; | ||
} | ||
for (const directory of includePaths) { | ||
const fullPath = path.join(directory, target); | ||
try { | ||
fs.accessSync(fullPath, fs.constants.R_OK); | ||
return fullPath; | ||
} | ||
catch (err) { | ||
continue; | ||
} | ||
} | ||
process.emitWarning(`${target} not found in any of the include paths ${includePaths}`); | ||
return originalResolvePath(origin, target); | ||
}; | ||
} | ||
async function writeAllFiles(protoFiles, options) { | ||
await mkdirp(options.outDir); | ||
await fs.promises.mkdir(options.outDir, { recursive: true }); | ||
for (const filename of protoFiles) { | ||
console.log(`Processing ${filename}`); | ||
const root = new Protobuf.Root(); | ||
options = options || {}; | ||
if (!!options.includeDirs) { | ||
if (!Array.isArray(options.includeDirs)) { | ||
throw new Error('The includeDirs option must be an array'); | ||
} | ||
addIncludePathResolver(root, options.includeDirs); | ||
} | ||
const loadedRoot = await root.load(filename, options); | ||
root.resolveAll(); | ||
const loadedRoot = await util_1.loadProtosWithOptions(filename, options); | ||
writeFilesForRoot(loadedRoot, path.basename(filename).replace('.proto', '.ts'), options); | ||
@@ -604,3 +593,3 @@ } | ||
.array('includeDirs') | ||
.boolean(['keepCase', 'defaults', 'arrays', 'objects', 'oneofs', 'json']) | ||
.boolean(['keepCase', 'defaults', 'arrays', 'objects', 'oneofs', 'json', 'verbose']) | ||
// .choices('longs', ['String', 'Number']) | ||
@@ -638,3 +627,4 @@ // .choices('enums', ['String']) | ||
includeDirs: 'I', | ||
outDir: 'O' | ||
outDir: 'O', | ||
verbose: 'v' | ||
}).describe({ | ||
@@ -658,5 +648,10 @@ keepCase: 'Preserve the case of field names', | ||
.argv; | ||
console.log(argv); | ||
if (argv.verbose) { | ||
console.log('Parsed arguments:', argv); | ||
} | ||
util_1.addCommonProtos(); | ||
writeAllFiles(argv._, argv).then(() => { | ||
console.log('Success'); | ||
if (argv.verbose) { | ||
console.log('Success'); | ||
} | ||
}, (error) => { | ||
@@ -663,0 +658,0 @@ throw error; |
@@ -0,4 +1,22 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
/// <reference types="node" /> | ||
import * as Protobuf from 'protobufjs'; | ||
import * as descriptor from 'protobufjs/ext/descriptor'; | ||
import { Options } from './util'; | ||
export { Long } from 'long'; | ||
@@ -81,5 +99,3 @@ /** | ||
} | ||
export declare type Options = Protobuf.IParseOptions & Protobuf.IConversionOptions & { | ||
includeDirs?: string[]; | ||
}; | ||
export { Options }; | ||
/** | ||
@@ -86,0 +102,0 @@ * Load a .proto file with the specified options. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -20,6 +19,7 @@ * @license | ||
*/ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const camelCase = require("lodash.camelcase"); | ||
const Protobuf = require("protobufjs"); | ||
const descriptor = require("protobufjs/ext/descriptor"); | ||
const util_1 = require("./util"); | ||
function isAnyExtension(obj) { | ||
@@ -29,3 +29,2 @@ return ('@type' in obj) && (typeof obj['@type'] === 'string'); | ||
exports.isAnyExtension = isAnyExtension; | ||
const camelCase = require("lodash.camelcase"); | ||
const descriptorOptions = { | ||
@@ -155,22 +154,2 @@ longs: String, | ||
} | ||
function addIncludePathResolver(root, includePaths) { | ||
const originalResolvePath = root.resolvePath; | ||
root.resolvePath = (origin, target) => { | ||
if (path.isAbsolute(target)) { | ||
return target; | ||
} | ||
for (const directory of includePaths) { | ||
const fullPath = path.join(directory, target); | ||
try { | ||
fs.accessSync(fullPath, fs.constants.R_OK); | ||
return fullPath; | ||
} | ||
catch (err) { | ||
continue; | ||
} | ||
} | ||
process.emitWarning(`${target} not found in any of the include paths ${includePaths}`); | ||
return originalResolvePath(origin, target); | ||
}; | ||
} | ||
/** | ||
@@ -203,13 +182,4 @@ * Load a .proto file with the specified options. | ||
function load(filename, options) { | ||
const root = new Protobuf.Root(); | ||
options = options || {}; | ||
if (!!options.includeDirs) { | ||
if (!Array.isArray(options.includeDirs)) { | ||
return Promise.reject(new Error('The includeDirs option must be an array')); | ||
} | ||
addIncludePathResolver(root, options.includeDirs); | ||
} | ||
return root.load(filename, options).then(loadedRoot => { | ||
loadedRoot.resolveAll(); | ||
return createPackageDefinition(root, options); | ||
return util_1.loadProtosWithOptions(filename, options).then(loadedRoot => { | ||
return createPackageDefinition(loadedRoot, options); | ||
}); | ||
@@ -219,27 +189,7 @@ } | ||
function loadSync(filename, options) { | ||
const root = new Protobuf.Root(); | ||
options = options || {}; | ||
if (!!options.includeDirs) { | ||
if (!Array.isArray(options.includeDirs)) { | ||
throw new Error('The includeDirs option must be an array'); | ||
} | ||
addIncludePathResolver(root, options.includeDirs); | ||
} | ||
const loadedRoot = root.loadSync(filename, options); | ||
loadedRoot.resolveAll(); | ||
return createPackageDefinition(root, options); | ||
const loadedRoot = util_1.loadProtosWithOptionsSync(filename, options); | ||
return createPackageDefinition(loadedRoot, options); | ||
} | ||
exports.loadSync = loadSync; | ||
// Load Google's well-known proto files that aren't exposed by Protobuf.js. | ||
{ | ||
// Protobuf.js exposes: any, duration, empty, field_mask, struct, timestamp, | ||
// and wrappers. compiler/plugin is excluded in Protobuf.js and here. | ||
const wellKnownProtos = ['api', 'descriptor', 'source_context', 'type']; | ||
const sourceDir = path.join(path.dirname(require.resolve('protobufjs')), 'google', 'protobuf'); | ||
for (const proto of wellKnownProtos) { | ||
const file = path.join(sourceDir, `${proto}.proto`); | ||
const descriptor = Protobuf.loadSync(file).toJSON(); | ||
Protobuf.common(proto, descriptor.nested.google.nested); | ||
} | ||
} | ||
util_1.addCommonProtos(); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@grpc/proto-loader", | ||
"version": "0.6.0-pre7", | ||
"version": "0.6.0-pre8", | ||
"author": "Google Inc.", | ||
@@ -49,3 +49,2 @@ "contributors": [ | ||
"long": "^4.0.0", | ||
"mkdirp": "^1.0.4", | ||
"protobufjs": "^6.9.0", | ||
@@ -52,0 +51,0 @@ "yargs": "^15.3.1" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60211
5
9
1079
- Removedmkdirp@^1.0.4
- Removedmkdirp@1.0.4(transitive)