|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2019 Google LLC |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are |
| 9 | + * met: |
| 10 | + * |
| 11 | + * * Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * * Redistributions in binary form must reproduce the above |
| 14 | + * copyright notice, this list of conditions and the following disclaimer |
| 15 | + * in the documentation and/or other materials provided with the |
| 16 | + * distribution. |
| 17 | + * * Neither the name of Google Inc. nor the names of its |
| 18 | + * contributors may be used to endorse or promote products derived from |
| 19 | + * this software without specific prior written permission. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +import * as fs from 'fs'; |
| 35 | +import * as path from 'path'; |
| 36 | +import * as util from 'util'; |
| 37 | +import * as pbjs from 'protobufjs/cli/pbjs'; |
| 38 | + |
| 39 | +const readdir = util.promisify(fs.readdir); |
| 40 | +const readFile = util.promisify(fs.readFile); |
| 41 | +const stat = util.promisify(fs.stat); |
| 42 | +const pbjsMain = util.promisify(pbjs.main); |
| 43 | + |
| 44 | +const PROTO_LIST_REGEX = /_proto_list.json$/; |
| 45 | + |
| 46 | +/** |
| 47 | + * Recursively scans directories starting from `directory` and finds all files |
| 48 | + * matching `PROTO_LIST_REGEX`. |
| 49 | + * |
| 50 | + * @param {string} directory Path to start the scan from. |
| 51 | + * @return {Promise<string[]} Resolves to an array of strings, each element is a full path to a matching file. |
| 52 | + */ |
| 53 | +async function findProtoJsonFiles(directory: string): Promise<string[]> { |
| 54 | + const result: string[] = []; |
| 55 | + const files = await readdir(directory); |
| 56 | + for (const file of files) { |
| 57 | + const fullPath = path.join(directory, file); |
| 58 | + const fileStat = await stat(fullPath); |
| 59 | + if (fileStat.isFile() && file.match(PROTO_LIST_REGEX)) { |
| 60 | + result.push(fullPath); |
| 61 | + } else if (fileStat.isDirectory()) { |
| 62 | + const nested = await findProtoJsonFiles(fullPath); |
| 63 | + result.push(...nested); |
| 64 | + } |
| 65 | + } |
| 66 | + return result; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Normalizes the Linux path for the current operating system. |
| 71 | + * |
| 72 | + * @param {string} filePath Linux-style path (with forward slashes) |
| 73 | + * @return {string} Normalized path. |
| 74 | + */ |
| 75 | +function normalizePath(filePath: string): string { |
| 76 | + return path.join(...filePath.split('/')); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Returns a combined list of proto files listed in all JSON files given. |
| 81 | + * |
| 82 | + * @param {string[]} protoJsonFiles List of JSON files to parse |
| 83 | + * @return {Promise<string[]>} Resolves to an array of proto files. |
| 84 | + */ |
| 85 | +async function buildListOfProtos(protoJsonFiles: string[]): Promise<string[]> { |
| 86 | + const result: string[] = []; |
| 87 | + for (const file of protoJsonFiles) { |
| 88 | + const directory = path.dirname(file); |
| 89 | + const content = await readFile(file); |
| 90 | + const list = JSON.parse(content.toString()).map((filePath: string) => |
| 91 | + path.join(directory, normalizePath(filePath)) |
| 92 | + ); |
| 93 | + result.push(...list); |
| 94 | + } |
| 95 | + return result; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Runs `pbjs` to compile the given proto files, placing the result into |
| 100 | + * `./protos/protos.json`. |
| 101 | + * |
| 102 | + * @param {string[]} protos List of proto files to compile. |
| 103 | + */ |
| 104 | +async function compileProtos(protos: string[]): Promise<void> { |
| 105 | + const pbjsArgs = [ |
| 106 | + '--target', |
| 107 | + 'json', |
| 108 | + '-p', |
| 109 | + path.join('node_modules', 'google-gax', 'protos'), |
| 110 | + '-p', |
| 111 | + 'protos', |
| 112 | + '-o', |
| 113 | + path.join('protos', 'protos.json'), |
| 114 | + ]; |
| 115 | + pbjsArgs.push(...protos); |
| 116 | + await pbjsMain(pbjsArgs); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Main function. Takes an array of directories to process. |
| 121 | + * Looks for JSON files matching `PROTO_LIST_REGEX`, parses them to get a list of all |
| 122 | + * proto files used by the client library, and calls `pbjs` to compile them all into |
| 123 | + * JSON (`pbjs -t json`). |
| 124 | + * |
| 125 | + * Exported to be called from a test. |
| 126 | + * |
| 127 | + * @param {string[]} directories List of directories to process. Normally, just the |
| 128 | + * `./src` folder of the given client library. |
| 129 | + */ |
| 130 | +export async function main(directories: string[]): Promise<void> { |
| 131 | + const protoJsonFiles: string[] = []; |
| 132 | + for (const directory of directories) { |
| 133 | + protoJsonFiles.push(...(await findProtoJsonFiles(directory))); |
| 134 | + } |
| 135 | + const protos = await buildListOfProtos(protoJsonFiles); |
| 136 | + await compileProtos(protos); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Shows the usage information. |
| 141 | + */ |
| 142 | +function usage() { |
| 143 | + console.log(`Usage: node ${process.argv[1]} directory ...`); |
| 144 | + console.log( |
| 145 | + `Finds all files matching ${PROTO_LIST_REGEX} in the given directories.` |
| 146 | + ); |
| 147 | + console.log( |
| 148 | + `Each of those files should contain a JSON array of proto files used by the` |
| 149 | + ); |
| 150 | + console.log( |
| 151 | + `client library. Those proto files will be compiled to JSON using pbjs tool` |
| 152 | + ); |
| 153 | + console.log(`from protobufjs.`); |
| 154 | +} |
| 155 | + |
| 156 | +if (require.main === module) { |
| 157 | + if (process.argv.length <= 2) { |
| 158 | + usage(); |
| 159 | + process.exit(1); |
| 160 | + } |
| 161 | + // argv[0] is node.js binary, argv[1] is script path |
| 162 | + main(process.argv.slice(2)); |
| 163 | +} |
0 commit comments