protoc-gen-tsd
Version:
Protocol Buffers Compiler(protoc) plugin for TypeScript - Generate definition file(d.ts)
131 lines (130 loc) • 3.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isReservedWord = exports.snakeToCamel = exports.isProto2Syntax = exports.replaceProtoSuffix = exports.filePathFromProtoWithoutExt = exports.filePathToPseudoNamespace = exports.getPathToRoot = exports.withinNamespaceFromExportEntry = exports.uppercaseFirst = exports.withAllStdIn = void 0;
const lodash_camelcase_1 = __importDefault(require("lodash.camelcase"));
const PROTO2_SYNTAX = 'proto2';
function withAllStdIn(callback) {
const ret = [];
let len = 0;
const stdin = process.stdin;
stdin.on('readable', function () {
let chunk;
while ((chunk = stdin.read())) {
if (!(chunk instanceof Buffer))
throw new Error('Did not receive buffer');
ret.push(chunk);
len += chunk.length;
}
});
stdin.on('end', function () {
callback(Buffer.concat(ret, len));
});
}
exports.withAllStdIn = withAllStdIn;
function uppercaseFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
exports.uppercaseFirst = uppercaseFirst;
function withinNamespaceFromExportEntry(name, exportEntry) {
return exportEntry.protoPackage ? name.substring(exportEntry.protoPackage.length + 1) : name;
}
exports.withinNamespaceFromExportEntry = withinNamespaceFromExportEntry;
function getPathToRoot(fileName) {
const depth = fileName.split('/').length;
return depth === 1 ? './' : new Array(depth).join('../');
}
exports.getPathToRoot = getPathToRoot;
function filePathToPseudoNamespace(filePath) {
return filePath.replace('.proto', '').replace(/\//g, '_').replace(/\./g, '_').replace(/-/g, '_');
}
exports.filePathToPseudoNamespace = filePathToPseudoNamespace;
function filePathFromProtoWithoutExt(protoFilePath) {
return protoFilePath.replace('.proto', '');
}
exports.filePathFromProtoWithoutExt = filePathFromProtoWithoutExt;
function replaceProtoSuffix(protoFilePath) {
const suffix = '.proto';
const hasProtoSuffix = protoFilePath.slice(protoFilePath.length - suffix.length) === suffix;
return hasProtoSuffix ? protoFilePath.slice(0, -suffix.length) : protoFilePath;
}
exports.replaceProtoSuffix = replaceProtoSuffix;
function isProto2Syntax(fileDescriptor) {
return fileDescriptor.getSyntax() === '' || fileDescriptor.getSyntax() === PROTO2_SYNTAX;
}
exports.isProto2Syntax = isProto2Syntax;
function snakeToCamel(str) {
return lodash_camelcase_1.default(str);
}
exports.snakeToCamel = snakeToCamel;
function isReservedWord(name) {
for (const keyword of reservedKeywords) {
if (name === keyword) {
return true;
}
}
return false;
}
exports.isReservedWord = isReservedWord;
const reservedKeywords = [
'abstract',
'boolean',
'break',
'byte',
'case',
'catch',
'char',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'double',
'else',
'enum',
'export',
'extends',
'false',
'final',
'finally',
'float',
'for',
'function',
'goto',
'if',
'implements',
'import',
'in',
'instanceof',
'int',
'interface',
'long',
'native',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'short',
'static',
'super',
'switch',
'synchronized',
'this',
'throw',
'throws',
'transient',
'try',
'typeof',
'var',
'void',
'volatile',
'while',
'with',
];