Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix telescope transpile use .telescope.json as default instead override #696

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading