Skip to content

Commit

Permalink
Merge pull request #696 from cosmology-tech/fix-transpile-default-file
Browse files Browse the repository at this point in the history
Fix telescope transpile use .telescope.json as default instead override
  • Loading branch information
NorOldBurden authored Nov 13, 2024
2 parents e57772b + c79f6b1 commit fdb7370
Showing 1 changed file with 144 additions and 132 deletions.
276 changes: 144 additions & 132 deletions packages/telescope/src/commands/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,162 +12,174 @@ export default async (argv: {
let options: {
[key: string]: unknown
} = {}
if (argv.useDefaults) {
const defaultOptions = { ...defaultTelescopeOptions };

dotty.remove(defaultOptions, "aminoEncoding");
dotty.remove(defaultOptions, "packages");

options = defaultOptions;
let protoDirs
let outPath
let data: any
try{
//check if there's valid .telescope.json file to use
data = JSON.parse(readFileSync('./.telescope.json', {
encoding: 'utf8'
}))
} catch(e){}

if (data?.protoDirs && data?.outPath && data?.options) {
protoDirs = data.protoDirs
outPath = data.outPath
options = data.options
} else {
options = {
// global options (can be overridden through plugins)

if (argv.useDefaults) {
const defaultOptions = { ...defaultTelescopeOptions };

dotty.remove(defaultOptions, "aminoEncoding");
dotty.remove(defaultOptions, "packages");

options = defaultOptions;
} else {
options = {
// global options (can be overridden through plugins)
interfaces: {
enabled: false,
useByDefault: false,
useUnionTypes: false,
},

interfaces: {
enabled: false,
useByDefault: false,
useUnionTypes: false,
},
prototypes: {
enabled: true,
parser: {
keepCase: false
},
methods: {
fromJSON: false,
toJSON: false,
encode: true,
decode: true,
fromPartial: true,
toAmino: true,
fromAmino: true,
fromProto: true,
toProto: true
},
addTypeUrlToObjects: true,
addTypeUrlToDecoders: true,

typingsFormat: {
duration: 'duration',
timestamp: 'date',
useExact: false,
useDeepPartial: false,
num64: 'bigint',
customTypes: {
useCosmosSDKDec: true
}
},
},

prototypes: {
enabled: true,
parser: {
keepCase: false
bundle: {
enabled: true
},
methods: {
fromJSON: false,
toJSON: false,
encode: true,
decode: true,
fromPartial: true,
toAmino: true,
fromAmino: true,
fromProto: true,
toProto: true

stargateClients: {
enabled: true,
includeCosmosDefaultTypes: true
},
addTypeUrlToObjects: true,
addTypeUrlToDecoders: true,

typingsFormat: {
duration: 'duration',
timestamp: 'date',
useExact: false,
useDeepPartial: false,
num64: 'bigint',
customTypes: {
useCosmosSDKDec: true
}

aminoEncoding: {
enabled: true,
},
},

bundle: {
enabled: true
},
lcdClients: {
enabled: true
},

stargateClients: {
enabled: true,
includeCosmosDefaultTypes: true
},
rpcClients: {
enabled: true,
camelCase: true
}
}

aminoEncoding: {
enabled: true,
},
}

lcdClients: {
enabled: true
const questions = [
{
_: true,
type: 'path',
name: 'protoDirs',
message: 'where is the proto directory?',
default: './proto'
},

rpcClients: {
enabled: true,
camelCase: true
{
_: true,
type: 'path',
name: 'outPath',
message: 'where is the output directory?',
default: './src/codegen'
}
}

}
];

const questions = [
{
_: true,
type: 'path',
name: 'protoDirs',
message: 'where is the proto directory?',
default: './proto'
},
{
_: true,
type: 'path',
name: 'outPath',
message: 'where is the output directory?',
default: './src/codegen'
}
];

if (argv.config) {
const { config } = argv;
const configs = Array.isArray(config) ? config : [config];
const inputConfigFullPaths = configs.map(c=>path.resolve(c));
let configJson;

for (const inputConfigPath of inputConfigFullPaths) {
try {
const configText = readFileSync(inputConfigPath, {
encoding: 'utf8'
})
if(configJson){
configJson = deepmerge(configJson, JSON.parse(configText));
} else {
configJson = JSON.parse(configText);
if (argv.config) {
const { config } = argv;
const configs = Array.isArray(config) ? config : [config];
const inputConfigFullPaths = configs.map(c => path.resolve(c));
let configJson;

for (const inputConfigPath of inputConfigFullPaths) {
try {
const configText = readFileSync(inputConfigPath, {
encoding: 'utf8'
})
if (configJson) {
configJson = deepmerge(configJson, JSON.parse(configText));
} else {
configJson = JSON.parse(configText);
}
} catch (ex) {
console.log(ex);
throw new Error("Must provide a .json file for --config.");
}
} catch (ex) {
console.log(ex);
throw new Error("Must provide a .json file for --config.");
}
}

// append protoDirs in config to argv.protoDirs
argv.protoDirs = [
...(argv.protoDirs
? Array.isArray(argv.protoDirs)
? argv.protoDirs
: [argv.protoDirs]
: []),
...(configJson.protoDirs ?? []),
];
// append protoDirs in config to argv.protoDirs
argv.protoDirs = [
...(argv.protoDirs
? Array.isArray(argv.protoDirs)
? argv.protoDirs
: [argv.protoDirs]
: []),
...(configJson.protoDirs ?? []),
];

if (configJson.outPath) {
argv.outPath = configJson.outPath;
}

if (configJson.outPath) {
argv.outPath = configJson.outPath;
// For now, useDefaults will be override by --config
if (configJson.options) {
options = configJson.options;
}
}

// For now, useDefaults will be override by --config
if (configJson.options) {
options = configJson.options;
{
({ protoDirs, outPath } = await prompt(questions, argv));
}
}

if (!Array.isArray(protoDirs)) {
protoDirs = [protoDirs];
}

let {
protoDirs,
outPath,
} = await prompt(questions, argv);

if (!Array.isArray(protoDirs)) {
protoDirs = [protoDirs];
// remove any duplicate protodirs
protoDirs = [...new Set(protoDirs)];

writeFileSync(
process.cwd() + '/.telescope.json',
JSON.stringify(
{
protoDirs,
outPath,
options
}, null, 2)
);
}

// remove any duplicate protodirs
protoDirs = [...new Set(protoDirs)];

writeFileSync(
process.cwd() + '/.telescope.json',
JSON.stringify(
{
protoDirs,
outPath,
options
}, null, 2)
);

await telescope({
protoDirs,
outPath,
Expand Down

0 comments on commit fdb7370

Please sign in to comment.