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 logics related to default config file for transpile #705

Merged
merged 1 commit into from
Dec 9, 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: 142 additions & 134 deletions packages/telescope/src/commands/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,163 +12,171 @@ export default async (argv: {
let options: {
[key: string]: unknown
} = {}
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 {
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,
},
let protoDirs;
let outPath;
let data: any;
let overrideConfig = true;

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
}
},
},
if (argv.useDefaults) {
const defaultOptions = { ...defaultTelescopeOptions };

bundle: {
enabled: true
},
dotty.remove(defaultOptions, "aminoEncoding");
dotty.remove(defaultOptions, "packages");

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

aminoEncoding: {
enabled: true,
prototypes: {
enabled: true,
parser: {
keepCase: false
},

lcdClients: {
enabled: true
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
}
},
},

rpcClients: {
enabled: true,
camelCase: true
}
}
bundle: {
enabled: true
},

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

aminoEncoding: {
enabled: true,
},

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

rpcClients: {
enabled: true,
camelCase: true
}
];
}

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.");
}

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) {
overrideConfig = false;
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.");
}
}

// 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;
}
// append protoDirs in config to argv.protoDirs
argv.protoDirs = [
...(argv.protoDirs
? Array.isArray(argv.protoDirs)
? argv.protoDirs
: [argv.protoDirs]
: []),
...(configJson.protoDirs ?? []),
];

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

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

if (!Array.isArray(protoDirs)) {
protoDirs = [protoDirs];
if (!argv.config && !argv.useDefaults) {
try {
//check if there's valid .telescope.json file to use
data = JSON.parse(readFileSync('./.telescope.json', {
encoding: 'utf8'
}))
} catch (e) { }

//if there's existing valid .telescope.json, use and keep the file
if (data?.protoDirs && data?.outPath && data?.options) {
overrideConfig = false
protoDirs = data.protoDirs
outPath = data.outPath
options = data.options
}
}
if (!protoDirs || !outPath) {
({ protoDirs, outPath } = await prompt(questions, argv));
}

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

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

if (overrideConfig) {
writeFileSync(
process.cwd() + '/.telescope.json',
JSON.stringify(
Expand Down
Loading