Skip to content

Commit

Permalink
change hash
Browse files Browse the repository at this point in the history
  • Loading branch information
toandq2009 committed Oct 31, 2023
1 parent 15d0f1b commit c3d4266
Showing 1 changed file with 43 additions and 52 deletions.
95 changes: 43 additions & 52 deletions packages/proto-types/proto-types-gen/scripts/proto-gen.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
/* eslint-disable import/no-extraneous-dependencies, @typescript-eslint/no-var-requires */

import "zx/globals";
import fs from "fs";
import FolderHash from "folder-hash";
import 'zx/globals';
import fs from 'fs';
import FolderHash from 'folder-hash';

async function calculateOutputHash(root) {
const dirCandidates = fs.readdirSync(root, {
withFileTypes: true,
withFileTypes: true
});

let dirs = [];

for (const candidate of dirCandidates) {
if (candidate.isDirectory()) {
if (
candidate.name !== "node_modules" &&
candidate.name !== "proto-types-gen"
) {
if (candidate.name !== 'node_modules' && candidate.name !== 'proto-types-gen') {
dirs.push(candidate);
}
}
Expand All @@ -31,29 +28,29 @@ async function calculateOutputHash(root) {
let hash = Buffer.alloc(0);
for (const dir of dirs) {
const p = path.join(root, dir.name);
const buf = Buffer.from((await FolderHash.hashElement(p)).hash, "base64");
const buf = Buffer.from((await FolderHash.hashElement(p)).hash, 'base64');

console.log(p, buf.toString("base64"));
console.log(p, buf.toString('base64'));

hash = Buffer.concat([hash, buf]);
}

return hash.toString("base64");
return hash.toString('base64');
}

function getOutputHash(root) {
return fs.readFileSync(path.join(root, "outputHash")).toString();
return fs.readFileSync(path.join(root, 'outputHash')).toString();
}

function setOutputHash(root, hash) {
return fs.writeFileSync(path.join(root, "outputHash"), hash, { mode: 0o600 });
return fs.writeFileSync(path.join(root, 'outputHash'), hash, { mode: 0o600 });
}

(async () => {
try {
const packageRoot = path.join(__dirname, "../..");
const packageRoot = path.join(__dirname, '../..');

const outDir = path.join(__dirname, "../src");
const outDir = path.join(__dirname, '../src');
$.verbose = false;

if (fs.existsSync(outDir)) {
Expand All @@ -65,59 +62,53 @@ function setOutputHash(root, hash) {

// When executed in CI, the proto output should not be different with ones built locally.
let lastOutputHash = undefined;
if (process.env.CI === "true") {
console.log("You are ci runner");
if (process.env.CI === 'true') {
console.log('You are ci runner');
lastOutputHash = getOutputHash(packageRoot);
console.log("Expected output hash is", lastOutputHash);
console.log('Expected output hash is', lastOutputHash);
}

const protoTsBinPath = (() => {
try {
const binPath = path.join(
__dirname,
"../../node_modules/.bin/protoc-gen-ts_proto"
);
const binPath = path.join(__dirname, '../../node_modules/.bin/protoc-gen-ts_proto');
fs.readFileSync(binPath);
return binPath;
} catch {
const binPath = path.join(
__dirname,
"../../../../node_modules/.bin/protoc-gen-ts_proto"
);
const binPath = path.join(__dirname, '../../../../node_modules/.bin/protoc-gen-ts_proto');
fs.readFileSync(binPath);
return binPath;
}
})();

const baseDirPath = path.join(__dirname, "..");
const baseDirPath = path.join(__dirname, '..');

const baseProtoPath = path.join(baseDirPath, "proto");
const thirdPartyProtoPath = path.join(baseDirPath, "third_party/proto");
const baseProtoPath = path.join(baseDirPath, 'proto');
const thirdPartyProtoPath = path.join(baseDirPath, 'third_party/proto');

const inputs = [
"agoric/swingset/msgs.proto",
"cosmos/authz/v1beta1/tx.proto",
"cosmos/base/v1beta1/coin.proto",
"cosmos/bank/v1beta1/bank.proto",
"cosmos/bank/v1beta1/tx.proto",
"cosmos/bank/v1beta1/authz.proto",
"cosmos/staking/v1beta1/tx.proto",
"cosmos/staking/v1beta1/authz.proto",
"cosmos/gov/v1beta1/gov.proto",
"cosmos/gov/v1beta1/tx.proto",
"cosmos/distribution/v1beta1/tx.proto",
"cosmos/crypto/multisig/v1beta1/multisig.proto",
"cosmos/crypto/secp256k1/keys.proto",
"cosmos/tx/v1beta1/tx.proto",
"cosmos/tx/signing/v1beta1/signing.proto",
"cosmos/base/abci/v1beta1/abci.proto",
"cosmwasm/wasm/v1/tx.proto",
"ibc/applications/transfer/v1/tx.proto",
"secret/compute/v1beta1/msg.proto",
"ethermint/types/v1/web3.proto",
'agoric/swingset/msgs.proto',
'cosmos/authz/v1beta1/tx.proto',
'cosmos/base/v1beta1/coin.proto',
'cosmos/bank/v1beta1/bank.proto',
'cosmos/bank/v1beta1/tx.proto',
'cosmos/bank/v1beta1/authz.proto',
'cosmos/staking/v1beta1/tx.proto',
'cosmos/staking/v1beta1/authz.proto',
'cosmos/gov/v1beta1/gov.proto',
'cosmos/gov/v1beta1/tx.proto',
'cosmos/distribution/v1beta1/tx.proto',
'cosmos/crypto/multisig/v1beta1/multisig.proto',
'cosmos/crypto/secp256k1/keys.proto',
'cosmos/tx/v1beta1/tx.proto',
'cosmos/tx/signing/v1beta1/signing.proto',
'cosmos/base/abci/v1beta1/abci.proto',
'cosmwasm/wasm/v1/tx.proto',
'ibc/applications/transfer/v1/tx.proto',
'secret/compute/v1beta1/msg.proto',
'ethermint/types/v1/web3.proto'
];

const thirdPartyInputs = ["tendermint/crypto/keys.proto"];
const thirdPartyInputs = ['tendermint/crypto/keys.proto'];

await $`protoc \
--plugin=${protoTsBinPath} \
Expand Down Expand Up @@ -145,9 +136,9 @@ function setOutputHash(root, hash) {
$.verbose = true;

const outputHash = await calculateOutputHash(outDir);
console.log("Output hash is", outputHash);
console.log('Output hash is', outputHash);
if (lastOutputHash && lastOutputHash !== outputHash) {
throw new Error("Output is different");
throw new Error('Output is different');
}

setOutputHash(packageRoot, outputHash);
Expand Down

0 comments on commit c3d4266

Please sign in to comment.