github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/fish_client/scripts/generate_proto_json.js (about)

     1  /**
     2   * Copyright 2016 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License")
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ------------------------------------------------------------------------------
    16   */
    17  'use strict'
    18  
    19  const fs = require('fs')
    20  const path = require('path')
    21  const protobuf = require('protobufjs')
    22  const jsonTarget = require('../node_modules/protobufjs/cli/targets/json')
    23  
    24  let root = new protobuf.Root()
    25  
    26  let files = fs
    27    .readdirSync(path.resolve(__dirname, '../../protos'))
    28    .map(f => path.resolve(__dirname, '../../protos', f))
    29    .filter(f => f.endsWith('.proto'))
    30  
    31  try {
    32    root = root.loadSync(files)
    33  } catch (e) {
    34    console.log('Unable to load protobuf files!')
    35    throw e
    36  }
    37  
    38  jsonTarget(root, {}, (err, output) => {
    39    if (err) {
    40      throw err
    41    }
    42  
    43    if (output !== '') {
    44      process.stdout.write(output, 'utf8')
    45    }
    46  })