Skip to content

Commit

Permalink
finished loading config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Jul 20, 2023
1 parent b7277e0 commit d1c24b8
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 54 deletions.
36 changes: 36 additions & 0 deletions packages/telescope/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,42 @@ telescope transpile

You should now seem some `.ts` files generated in `./src`. These are the real source files used in your application.

Examples:

```sh
# Telescope takes chain1 folder as input,
# and generate files in 'gen/src' folder.
telescope transpile --protoDirs ../../__fixtures__/chain1 --outPath gen/src
```

```sh
# Telescope takes chain1 folder as input,
# and generate files in 'gen/src' folder using default telescope options.
telescope transpile --protoDirs ../../__fixtures__/chain1 --outPath gen/src --useDefaults
```

```sh
# Telescope takes chain1 folder(from args) and chain2 folder(from config) as input,
# and generate files in 'gen/src'(defined in the config file, will override outPath in args) folder using a config file.
# Note: --config will override --useDefaults.
telescope transpile --protoDirs ../../__fixtures__/chain1 --config .telescope.json
```

```json
//.telescope.json
{
"protoDirs": [
"../../fixtures/chain2"
],
"outPath": "gen/src",
"options": {
// telescope options
...
}
}
```


### Build

Finally, run `install` and `buidl` to generate the JS and types for publishing your module to npm.
Expand Down
167 changes: 113 additions & 54 deletions packages/telescope/src/commands/transpile.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { prompt } from '../prompt';
import telescope from '../index';
import { writeFileSync } from 'fs';
import { writeFileSync, readFileSync } from 'fs';
import { defaultTelescopeOptions } from '@osmonauts/types';
import * as path from 'path'
import * as dotty from 'dotty';

const OPTIONS_FIELD_MAPPINGS = {
build: "prototypes.includes.protos",
nobuild: "prototypes.excluded.protos",
includeAminos: "aminoEncoding.enabled",
includeLCDClients: "lcdClients.enabled",
includeRPCClients: "rpcClients.enabled",
};

export default async (argv: {
[key: string]: string | string[]
}) => {
let options: {
[key: string]: unknown
} = {}
if (argv.useDefaults) {
const defaultOptions = { ...defaultTelescopeOptions };

export default async (argv) => {
dotty.remove(defaultOptions, "aminoEncoding");
dotty.remove(defaultOptions, "packages");

if (argv.useDefaults) {
const SKIP = ['aminoEncoding', 'packages'];
Object.keys(defaultTelescopeOptions)
.forEach(key => {
if (SKIP.includes(key)) return;
argv[key] = defaultTelescopeOptions[key]
})
options = defaultOptions;
}

const questions = [
Expand Down Expand Up @@ -61,62 +75,59 @@ export default async (argv) => {
}
];

let {
protoDirs,
outPath,
build,
nobuild,
includeAminos,
includeLCDClients,
includeRPCClients,
} = await prompt(questions, argv);
if (argv.config) {
const { config } = argv;
const inputConfigFullPath = path.resolve(Array.isArray(config) ? config[0] : config);
let configJson = null;

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

const options: any = {
aminoEncoding: {
enabled: includeAminos
},
lcdClients: {
enabled: includeLCDClients
},
rpcClients: {
enabled: includeRPCClients,
try {
const configText = readFileSync(inputConfigFullPath, {
encoding: 'utf8'
})
configJson = JSON.parse(configText);
} catch (ex) {
console.log(ex);
throw new Error("Must provide a .json file for --config.");
}
};

if(build || nobuild){
if(!options.prototypes){
options.prototypes = {}
// 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;
}

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

if(build){
if (!Array.isArray(build)) {
build = [build];
}
// map options to argv
mapOptionsToArgv(options, argv, OPTIONS_FIELD_MAPPINGS);

if(!options.prototypes.includes){
options.prototypes.includes = {}
}
let {
protoDirs,
outPath,
...answers
} = await prompt(questions, argv);

options.prototypes.includes.protos = build;
if (!Array.isArray(protoDirs)) {
protoDirs = [protoDirs];
}

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

if(!options.prototypes.excluded){
options.prototypes.excluded = {}
}

options.prototypes.excluded.protos = nobuild;
}
// map argv back to configs
mapArgvToOptions(answers, options, OPTIONS_FIELD_MAPPINGS);

writeFileSync(
process.cwd() + '/.telescope.json',
Expand All @@ -135,4 +146,52 @@ export default async (argv) => {
});

console.log(`✨ transpilation successful!`);
};
};

function mapOptionsToArgv(
options: {
[key: string]: unknown;
},
argv: {
[key: string]: string | string[];
},
mappings: {
[key: string]: string;
}
) {
for (const argvKey in mappings) {
if (Object.prototype.hasOwnProperty.call(mappings, argvKey)) {
const optionKey = mappings[argvKey];

const value = dotty.get(options, optionKey);

if(value){
dotty.put(argv, argvKey, value)
}
}
}
}

function mapArgvToOptions(
argv: {
[key: string]: string | string[];
},
options: {
[key: string]: unknown;
},
mappings: {
[key: string]: string;
}
) {
for (const argvKey in mappings) {
if (Object.prototype.hasOwnProperty.call(mappings, argvKey)) {
const optionKey = mappings[argvKey];

const value = dotty.get(argv, argvKey);

if(value){
dotty.put(options, optionKey, value)
}
}
}
}

0 comments on commit d1c24b8

Please sign in to comment.