@cosmwasm/ts-codegen 0.7.3 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +238 -59
- package/main/builder/builder.js +456 -0
- package/main/builder/index.js +18 -0
- package/main/bundler/bundler.js +74 -0
- package/main/bundler/index.js +18 -0
- package/main/cmds.js +3 -9
- package/main/commands/create-boilerplate.js +195 -0
- package/main/commands/generate.js +91 -16
- package/main/generators/{ts-client.js → client.js} +29 -23
- package/main/generators/{from-partial.js → message-composer.js} +29 -21
- package/main/generators/react-query.js +27 -21
- package/main/generators/recoil.js +31 -15
- package/main/generators/types.js +95 -0
- package/main/index.js +71 -17
- package/main/types.js +1 -0
- package/main/utils/schemas.js +5 -3
- package/module/builder/builder.js +178 -0
- package/module/builder/index.js +1 -0
- package/module/bundler/bundler.js +36 -0
- package/module/bundler/index.js +1 -0
- package/module/cmds.js +2 -6
- package/module/commands/create-boilerplate.js +116 -0
- package/module/commands/generate.js +82 -8
- package/module/generators/{ts-client.js → client.js} +21 -15
- package/module/generators/{from-partial.js → message-composer.js} +23 -17
- package/module/generators/react-query.js +20 -15
- package/module/generators/recoil.js +23 -9
- package/module/generators/types.js +40 -0
- package/module/index.js +12 -5
- package/module/types.js +1 -0
- package/module/utils/schemas.js +3 -2
- package/package.json +5 -3
- package/types/builder/builder.d.ts +39 -0
- package/types/builder/index.d.ts +1 -0
- package/types/bundler/bundler.d.ts +4 -0
- package/types/bundler/index.d.ts +1 -0
- package/types/commands/{from-partial.d.ts → client.d.ts} +0 -0
- package/types/commands/message-composer.d.ts +2 -0
- package/types/commands/types.d.ts +2 -0
- package/types/generators/client.d.ts +3 -0
- package/types/generators/message-composer.d.ts +3 -0
- package/types/generators/react-query.d.ts +2 -1
- package/types/generators/recoil.d.ts +2 -1
- package/types/generators/types.d.ts +3 -0
- package/types/index.d.ts +10 -5
- package/types/types.d.ts +1 -0
- package/types/utils/schemas.d.ts +8 -4
- package/main/commands/from-partial.js +0 -78
- package/main/commands/react-query.js +0 -98
- package/main/commands/recoil.js +0 -78
- package/main/utils/imports.js +0 -26
- package/module/commands/from-partial.js +0 -33
- package/module/commands/react-query.js +0 -54
- package/module/commands/recoil.js +0 -33
- package/module/utils/imports.js +0 -10
- package/types/clean.d.ts +0 -1
- package/types/cleanse.d.ts +0 -1
- package/types/commands/boilerplate.d.ts +0 -2
- package/types/cosmwasm-typescript-gen.d.ts +0 -2
- package/types/from-partial.d.ts +0 -2
- package/types/generate.d.ts +0 -2
- package/types/generators/from-partial.d.ts +0 -2
- package/types/generators/ts-client.d.ts +0 -2
- package/types/header.d.ts +0 -1
- package/types/imports.d.ts +0 -1
- package/types/parse.d.ts +0 -1
- package/types/prompt.d.ts +0 -3
- package/types/react-query.d.ts +0 -2
- package/types/recoil.d.ts +0 -2
- package/types/utils/imports.d.ts +0 -1
- package/types/utils.d.ts +0 -10
@@ -0,0 +1,116 @@
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2
|
+
|
3
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
4
|
+
|
5
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
6
|
+
|
7
|
+
import * as shell from 'shelljs';
|
8
|
+
import { prompt } from '../utils/prompt';
|
9
|
+
import dargs from 'dargs';
|
10
|
+
|
11
|
+
const glob = require('glob').sync;
|
12
|
+
|
13
|
+
const fs = require('fs');
|
14
|
+
|
15
|
+
const path = require('path');
|
16
|
+
|
17
|
+
const repo = 'git@github.com:pyramation/tmpl-cosmwasm-module.git';
|
18
|
+
export default (async argv => {
|
19
|
+
if (!shell.which('git')) {
|
20
|
+
shell.echo('Sorry, this script requires git');
|
21
|
+
return shell.exit(1);
|
22
|
+
}
|
23
|
+
|
24
|
+
const {
|
25
|
+
name
|
26
|
+
} = await prompt([{
|
27
|
+
type: 'string',
|
28
|
+
name: 'name',
|
29
|
+
message: 'Enter your new module name'
|
30
|
+
}], argv);
|
31
|
+
shell.exec(`git clone ${repo} ${name}`);
|
32
|
+
shell.cd(name);
|
33
|
+
const questions = JSON.parse(fs.readFileSync(`.questions.json`));
|
34
|
+
const fullname = shell.exec('git config --global user.name', {
|
35
|
+
silent: true
|
36
|
+
}).trim();
|
37
|
+
const email = shell.exec('git config --global user.email', {
|
38
|
+
silent: true
|
39
|
+
}).trim(); // @__USERNAME__/__MODULENAME__
|
40
|
+
// __PACKAGE_IDENTIFIER__
|
41
|
+
|
42
|
+
const args = dargs(_objectSpread(_objectSpread({
|
43
|
+
_: []
|
44
|
+
}, argv), {}, {
|
45
|
+
__MODULENAME__: name,
|
46
|
+
__USERFULLNAME__: fullname,
|
47
|
+
__USEREMAIL__: email
|
48
|
+
}), {
|
49
|
+
allowCamelCase: true
|
50
|
+
});
|
51
|
+
const results = await prompt(questions, args);
|
52
|
+
let scopedResults;
|
53
|
+
const license = await prompt([{
|
54
|
+
name: '__LICENSE__',
|
55
|
+
message: 'Which license?',
|
56
|
+
choices: ['MIT', 'closed'],
|
57
|
+
type: 'list',
|
58
|
+
required: true
|
59
|
+
}], []);
|
60
|
+
|
61
|
+
if (results.__ACCESS__ === 'public') {
|
62
|
+
scopedResults = await prompt([{
|
63
|
+
type: 'confirm',
|
64
|
+
name: 'scoped',
|
65
|
+
message: 'use npm scopes?',
|
66
|
+
required: true
|
67
|
+
}], []);
|
68
|
+
}
|
69
|
+
|
70
|
+
const files = [].concat(glob(process.cwd() + '/**/.*')).concat(glob(process.cwd() + '/**/*'));
|
71
|
+
|
72
|
+
for (let i = 0; i < files.length; i++) {
|
73
|
+
const templateFile = files[i];
|
74
|
+
if (fs.lstatSync(templateFile).isDirectory()) continue;
|
75
|
+
let content = fs.readFileSync(templateFile).toString();
|
76
|
+
|
77
|
+
if (path.basename(templateFile) === 'LICENSE' && license.__LICENSE__ === 'closed') {
|
78
|
+
content = `Copyright (c) 2022 __USERFULLNAME__ <__USEREMAIL__> - All Rights Reserved
|
79
|
+
Unauthorized copying via any medium is strictly prohibited
|
80
|
+
Proprietary and confidential`;
|
81
|
+
}
|
82
|
+
|
83
|
+
Object.keys(results).forEach(key => {
|
84
|
+
if (/^__/.test(key)) {
|
85
|
+
content = content.replace(new RegExp(key, 'g'), results[key]);
|
86
|
+
}
|
87
|
+
});
|
88
|
+
|
89
|
+
if (results.__ACCESS__ === 'public') {
|
90
|
+
if (scopedResults.scoped) {
|
91
|
+
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `@${results.__USERNAME__}/${results.__MODULENAME__}`);
|
92
|
+
} else {
|
93
|
+
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `${results.__MODULENAME__}`);
|
94
|
+
}
|
95
|
+
} else {
|
96
|
+
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `@${results.__USERNAME__}/${results.__MODULENAME__}`);
|
97
|
+
}
|
98
|
+
|
99
|
+
if (path.basename(templateFile) === 'README.md') {
|
100
|
+
content = `# ${results.__MODULENAME__}`;
|
101
|
+
}
|
102
|
+
|
103
|
+
fs.writeFileSync(templateFile, content);
|
104
|
+
}
|
105
|
+
|
106
|
+
shell.rm('-rf', '.git');
|
107
|
+
shell.rm('-rf', '.questions.json');
|
108
|
+
console.log(`
|
109
|
+
|
110
|
+
|||
|
111
|
+
(o o)
|
112
|
+
ooO--(_)--Ooo-
|
113
|
+
|
114
|
+
✨ Great work!
|
115
|
+
`);
|
116
|
+
});
|
@@ -1,6 +1,5 @@
|
|
1
1
|
import { prompt } from '../utils/prompt';
|
2
|
-
import
|
3
|
-
import { readSchemas } from '../utils';
|
2
|
+
import codegen from '../index';
|
4
3
|
export default (async argv => {
|
5
4
|
const questions = [{
|
6
5
|
_: true,
|
@@ -19,15 +18,90 @@ export default (async argv => {
|
|
19
18
|
type: 'string',
|
20
19
|
name: 'name',
|
21
20
|
message: 'contract name?'
|
21
|
+
}, {
|
22
|
+
type: 'checkbox',
|
23
|
+
name: 'plugin',
|
24
|
+
message: 'which plugins?',
|
25
|
+
choices: ['client', 'recoil', 'react-query', 'message-composer']
|
22
26
|
}];
|
23
|
-
|
27
|
+
|
28
|
+
if (argv.typesOnly) {
|
29
|
+
argv.plugin = 'types';
|
30
|
+
}
|
31
|
+
|
32
|
+
let {
|
24
33
|
schema,
|
25
34
|
out,
|
26
|
-
name
|
35
|
+
name,
|
36
|
+
plugin
|
27
37
|
} = await prompt(questions, argv);
|
28
|
-
|
29
|
-
|
30
|
-
|
38
|
+
if (!Array.isArray(plugin)) plugin = [plugin]; ///////// REACT QUERY
|
39
|
+
|
40
|
+
const questions2 = [];
|
41
|
+
|
42
|
+
if (plugin.includes('react-query')) {
|
43
|
+
[].push.apply(questions2, [{
|
44
|
+
type: 'confirm',
|
45
|
+
name: 'optionalClient',
|
46
|
+
message: 'optionalClient?',
|
47
|
+
default: false
|
48
|
+
}, {
|
49
|
+
type: 'list',
|
50
|
+
name: 'version',
|
51
|
+
message: 'which react-query version?',
|
52
|
+
default: 'v3',
|
53
|
+
choices: ['v3', 'v4']
|
54
|
+
}]);
|
55
|
+
}
|
56
|
+
|
57
|
+
;
|
58
|
+
const {
|
59
|
+
optionalClient,
|
60
|
+
version
|
61
|
+
} = await prompt(questions2, argv);
|
62
|
+
const questions3 = [];
|
63
|
+
|
64
|
+
if (version === 'v4') {
|
65
|
+
[].push.apply(questions3, [// currently we only support v4 for useMutation
|
66
|
+
{
|
67
|
+
type: 'confirm',
|
68
|
+
name: 'mutations',
|
69
|
+
message: 'Generate useMutation hooks?',
|
70
|
+
default: false
|
71
|
+
}]);
|
72
|
+
}
|
73
|
+
|
74
|
+
;
|
75
|
+
const {
|
76
|
+
mutations
|
77
|
+
} = await prompt(questions3, argv); ///////// END REACT QUERY
|
78
|
+
|
79
|
+
const options = {
|
80
|
+
types: {
|
81
|
+
enabled: true
|
82
|
+
},
|
83
|
+
client: {
|
84
|
+
enabled: plugin.includes('client') || plugin.includes('recoil') || plugin.includes('react-query')
|
85
|
+
},
|
86
|
+
reactQuery: {
|
87
|
+
enabled: plugin.includes('react-query'),
|
88
|
+
optionalClient,
|
89
|
+
version,
|
90
|
+
mutations
|
91
|
+
},
|
92
|
+
recoil: {
|
93
|
+
enabled: plugin.includes('recoil')
|
94
|
+
},
|
95
|
+
messageComposer: {
|
96
|
+
enabled: plugin.includes('message-composer')
|
97
|
+
}
|
98
|
+
};
|
99
|
+
await codegen({
|
100
|
+
contracts: [{
|
101
|
+
name,
|
102
|
+
dir: schema
|
103
|
+
}],
|
104
|
+
outPath: out,
|
105
|
+
options
|
31
106
|
});
|
32
|
-
await tsClient(name, schemas, out);
|
33
107
|
});
|
@@ -6,13 +6,16 @@ import * as w from 'wasm-ast-types';
|
|
6
6
|
import * as t from '@babel/types';
|
7
7
|
import { writeFileSync } from 'fs';
|
8
8
|
import generate from "@babel/generator";
|
9
|
-
import { clean } from "../utils/clean";
|
10
9
|
import { getMessageProperties } from "wasm-ast-types";
|
11
10
|
import { findAndParseTypes, findExecuteMsg, findQueryMsg, getDefinitionSchema } from '../utils';
|
12
|
-
import { cosmjsAminoImportStatements } from '../utils/imports';
|
13
11
|
import { RenderContext } from "wasm-ast-types";
|
14
|
-
export default (async (name, schemas, outPath) => {
|
15
|
-
const
|
12
|
+
export default (async (name, schemas, outPath, tsClientOptions) => {
|
13
|
+
const context = new RenderContext(getDefinitionSchema(schemas), {
|
14
|
+
tsClient: tsClientOptions ?? {}
|
15
|
+
});
|
16
|
+
const options = context.options.tsClient;
|
17
|
+
const localname = pascal(name) + '.client.ts';
|
18
|
+
const TypesFile = pascal(name) + '.types';
|
16
19
|
const QueryMsg = findQueryMsg(schemas);
|
17
20
|
const ExecuteMsg = findExecuteMsg(schemas);
|
18
21
|
const typeHash = await findAndParseTypes(schemas);
|
@@ -21,15 +24,7 @@ export default (async (name, schemas, outPath) => {
|
|
21
24
|
let QueryClient = null;
|
22
25
|
let ReadOnlyInstance = null;
|
23
26
|
const body = [];
|
24
|
-
body.push(w.importStmt(
|
25
|
-
body.push(cosmjsAminoImportStatements(typeHash)); // TYPES
|
26
|
-
|
27
|
-
Object.values(typeHash).forEach(type => {
|
28
|
-
body.push(clean(type));
|
29
|
-
}); // alias the ExecuteMsg
|
30
|
-
|
31
|
-
ExecuteMsg && body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${name}ExecuteMsg`), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
|
32
|
-
const context = new RenderContext(getDefinitionSchema(schemas)); // query messages
|
27
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`)); // query messages
|
33
28
|
|
34
29
|
if (QueryMsg) {
|
35
30
|
QueryClient = pascal(`${name}QueryClient`);
|
@@ -50,7 +45,18 @@ export default (async (name, schemas, outPath) => {
|
|
50
45
|
}
|
51
46
|
}
|
52
47
|
|
53
|
-
|
48
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
49
|
+
delete context.utils.Coin;
|
50
|
+
}
|
51
|
+
|
52
|
+
const imports = context.getImports();
|
53
|
+
const code = header + generate(t.program([...imports, ...body])).code;
|
54
54
|
mkdirp(outPath);
|
55
|
-
writeFileSync(join(outPath,
|
55
|
+
writeFileSync(join(outPath, localname), code);
|
56
|
+
return [{
|
57
|
+
type: 'client',
|
58
|
+
contract: name,
|
59
|
+
localname,
|
60
|
+
filename: join(outPath, localname)
|
61
|
+
}];
|
56
62
|
});
|
@@ -9,22 +9,17 @@ import generate from "@babel/generator";
|
|
9
9
|
import { getMessageProperties } from "wasm-ast-types";
|
10
10
|
import { findAndParseTypes, findExecuteMsg, getDefinitionSchema } from "../utils";
|
11
11
|
import { RenderContext } from "wasm-ast-types";
|
12
|
-
export default (async (name, schemas, outPath) => {
|
13
|
-
const
|
14
|
-
|
12
|
+
export default (async (name, schemas, outPath, messageComposerOptions) => {
|
13
|
+
const context = new RenderContext(getDefinitionSchema(schemas), {
|
14
|
+
messageComposer: messageComposerOptions ?? {}
|
15
|
+
});
|
16
|
+
const options = context.options.messageComposer;
|
17
|
+
const localname = pascal(name) + '.message-composer.ts';
|
18
|
+
const TypesFile = pascal(name) + '.types';
|
15
19
|
const ExecuteMsg = findExecuteMsg(schemas);
|
16
20
|
const typeHash = await findAndParseTypes(schemas);
|
17
21
|
const body = [];
|
18
|
-
body.push(w.importStmt(
|
19
|
-
body.push(w.importStmt(['MsgExecuteContract'], 'cosmjs-types/cosmwasm/wasm/v1/tx'));
|
20
|
-
body.push(w.importStmt(['toUtf8'], '@cosmjs/encoding'));
|
21
|
-
|
22
|
-
if (!typeHash.hasOwnProperty('Coin')) {
|
23
|
-
body.push(w.importStmt(['Coin'], '@cosmjs/amino'));
|
24
|
-
}
|
25
|
-
|
26
|
-
body.push(w.importStmt(Object.keys(typeHash), `./${Contract}`.replace(/\.ts$/, '')));
|
27
|
-
const context = new RenderContext(getDefinitionSchema(schemas)); // execute messages
|
22
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`)); // execute messages
|
28
23
|
|
29
24
|
if (ExecuteMsg) {
|
30
25
|
const children = getMessageProperties(ExecuteMsg);
|
@@ -32,12 +27,23 @@ export default (async (name, schemas, outPath) => {
|
|
32
27
|
if (children.length > 0) {
|
33
28
|
const TheClass = pascal(`${name}MessageComposer`);
|
34
29
|
const Interface = pascal(`${name}Message`);
|
35
|
-
body.push(w.
|
36
|
-
body.push(w.
|
30
|
+
body.push(w.createMessageComposerInterface(context, Interface, ExecuteMsg));
|
31
|
+
body.push(w.createMessageComposerClass(context, TheClass, Interface, ExecuteMsg));
|
37
32
|
}
|
38
33
|
}
|
39
34
|
|
40
|
-
|
35
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
36
|
+
delete context.utils.Coin;
|
37
|
+
}
|
38
|
+
|
39
|
+
const imports = context.getImports();
|
40
|
+
const code = header + generate(t.program([...imports, ...body])).code;
|
41
41
|
mkdirp(outPath);
|
42
|
-
writeFileSync(join(outPath,
|
42
|
+
writeFileSync(join(outPath, localname), code);
|
43
|
+
return [{
|
44
|
+
type: 'message-composer',
|
45
|
+
contract: name,
|
46
|
+
localname,
|
47
|
+
filename: join(outPath, localname)
|
48
|
+
}];
|
43
49
|
});
|
@@ -9,39 +9,33 @@ import { writeFileSync } from 'fs';
|
|
9
9
|
import generate from "@babel/generator";
|
10
10
|
import { findAndParseTypes, findExecuteMsg, findQueryMsg, getDefinitionSchema } from '../utils';
|
11
11
|
import { getMessageProperties } from "wasm-ast-types";
|
12
|
-
import { cosmjsAminoImportStatements } from '../utils/imports';
|
13
12
|
export default (async (contractName, schemas, outPath, reactQueryOptions) => {
|
14
13
|
const context = new RenderContext(getDefinitionSchema(schemas), {
|
15
14
|
reactQuery: reactQueryOptions ?? {}
|
16
15
|
});
|
17
16
|
const options = context.options.reactQuery;
|
18
|
-
const
|
19
|
-
const
|
17
|
+
const localname = pascal(`${contractName}`) + '.react-query.ts';
|
18
|
+
const ContractFile = pascal(`${contractName}`) + '.client';
|
19
|
+
const TypesFile = pascal(`${contractName}`) + '.types';
|
20
20
|
const QueryMsg = findQueryMsg(schemas);
|
21
21
|
const ExecuteMsg = findExecuteMsg(schemas);
|
22
22
|
const typeHash = await findAndParseTypes(schemas);
|
23
23
|
const ExecuteClient = pascal(`${contractName}Client`);
|
24
24
|
const QueryClient = pascal(`${contractName}QueryClient`);
|
25
25
|
const body = [];
|
26
|
-
const reactQueryImports = ['useQuery', 'UseQueryOptions'];
|
27
26
|
const clientImports = [];
|
28
27
|
QueryMsg && clientImports.push(QueryClient); // check that there are commands within the exec msg
|
29
28
|
|
30
|
-
const shouldGenerateMutationHooks = ExecuteMsg && options?.v4 && options?.mutations && getMessageProperties(ExecuteMsg).length > 0;
|
29
|
+
const shouldGenerateMutationHooks = ExecuteMsg && options?.version === 'v4' && options?.mutations && getMessageProperties(ExecuteMsg).length > 0;
|
31
30
|
|
32
31
|
if (shouldGenerateMutationHooks) {
|
33
|
-
body.push(w.importStmt(['ExecuteResult'], '@cosmjs/cosmwasm-stargate'));
|
34
|
-
body.push(cosmjsAminoImportStatements(typeHash));
|
35
|
-
reactQueryImports.push('useMutation', 'UseMutationOptions');
|
36
32
|
clientImports.push(ExecuteClient);
|
37
|
-
} //
|
33
|
+
} // general contract imports
|
38
34
|
|
39
35
|
|
40
|
-
body.push(w.importStmt(
|
36
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`)); // client imports
|
41
37
|
|
42
|
-
body.push(w.importStmt(
|
43
|
-
|
44
|
-
body.push(w.importStmt(clientImports, `./${Contract}`)); // query messages
|
38
|
+
body.push(w.importStmt(clientImports, `./${ContractFile}`)); // query messages
|
45
39
|
|
46
40
|
if (QueryMsg) {
|
47
41
|
[].push.apply(body, w.createReactQueryHooks({
|
@@ -61,7 +55,18 @@ export default (async (contractName, schemas, outPath, reactQueryOptions) => {
|
|
61
55
|
}));
|
62
56
|
}
|
63
57
|
|
64
|
-
|
58
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
59
|
+
delete context.utils.Coin;
|
60
|
+
}
|
61
|
+
|
62
|
+
const imports = context.getImports();
|
63
|
+
const code = header + generate(t.program([...imports, ...body])).code;
|
65
64
|
mkdirp(outPath);
|
66
|
-
writeFileSync(join(outPath,
|
65
|
+
writeFileSync(join(outPath, localname), code);
|
66
|
+
return [{
|
67
|
+
type: 'react-query',
|
68
|
+
contract: contractName,
|
69
|
+
localname,
|
70
|
+
filename: join(outPath, localname)
|
71
|
+
}];
|
67
72
|
});
|
@@ -8,29 +8,43 @@ import { writeFileSync } from 'fs';
|
|
8
8
|
import generate from "@babel/generator";
|
9
9
|
import { findAndParseTypes, findQueryMsg, getDefinitionSchema } from "../utils";
|
10
10
|
import { RenderContext } from "wasm-ast-types";
|
11
|
-
export default (async (name, schemas, outPath) => {
|
12
|
-
const
|
13
|
-
|
11
|
+
export default (async (name, schemas, outPath, recoilOptions) => {
|
12
|
+
const context = new RenderContext(getDefinitionSchema(schemas), {
|
13
|
+
recoil: recoilOptions ?? {}
|
14
|
+
});
|
15
|
+
const options = context.options.recoil;
|
16
|
+
const localname = pascal(name) + '.recoil.ts';
|
17
|
+
const ContractFile = pascal(name) + '.client';
|
18
|
+
const TypesFile = pascal(name) + '.types';
|
14
19
|
const QueryMsg = findQueryMsg(schemas);
|
15
20
|
const typeHash = await findAndParseTypes(schemas);
|
16
21
|
let QueryClient = null;
|
17
22
|
let ReadOnlyInstance = null;
|
18
23
|
const body = [];
|
19
|
-
body.push(w.importStmt(['selectorFamily'], 'recoil'));
|
20
24
|
body.push(w.importStmt(['cosmWasmClient'], './chain'));
|
21
|
-
body.push(w.importStmt(Object.keys(typeHash), `./${
|
22
|
-
const context = new RenderContext(getDefinitionSchema(schemas)); // query messages
|
25
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`)); // query messages
|
23
26
|
|
24
27
|
if (QueryMsg) {
|
25
28
|
QueryClient = pascal(`${name}QueryClient`);
|
26
29
|
ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
|
27
|
-
body.push(w.importStmt([QueryClient], `./${
|
30
|
+
body.push(w.importStmt([QueryClient], `./${ContractFile}`));
|
28
31
|
body.push(w.createRecoilQueryClientType());
|
29
32
|
body.push(w.createRecoilQueryClient(context, name, QueryClient));
|
30
33
|
[].push.apply(body, w.createRecoilSelectors(context, name, QueryClient, QueryMsg));
|
31
34
|
}
|
32
35
|
|
33
|
-
|
36
|
+
if (typeHash.hasOwnProperty('Coin')) {
|
37
|
+
delete context.utils.Coin;
|
38
|
+
}
|
39
|
+
|
40
|
+
const imports = context.getImports();
|
41
|
+
const code = header + generate(t.program([...imports, ...body])).code;
|
34
42
|
mkdirp(outPath);
|
35
|
-
writeFileSync(join(outPath,
|
43
|
+
writeFileSync(join(outPath, localname), code);
|
44
|
+
return [{
|
45
|
+
type: 'recoil',
|
46
|
+
contract: name,
|
47
|
+
localname,
|
48
|
+
filename: join(outPath, localname)
|
49
|
+
}];
|
36
50
|
});
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { pascal } from "case";
|
2
|
+
import { header } from '../utils/header';
|
3
|
+
import { join } from "path";
|
4
|
+
import { sync as mkdirp } from "mkdirp";
|
5
|
+
import * as t from '@babel/types';
|
6
|
+
import { writeFileSync } from 'fs';
|
7
|
+
import generate from "@babel/generator";
|
8
|
+
import { clean } from "../utils/clean";
|
9
|
+
import { findAndParseTypes, findExecuteMsg, getDefinitionSchema } from '../utils';
|
10
|
+
import { RenderContext } from "wasm-ast-types";
|
11
|
+
export default (async (name, schemas, outPath, tsTypesOptions) => {
|
12
|
+
const context = new RenderContext(getDefinitionSchema(schemas), {
|
13
|
+
tsClient: tsTypesOptions ?? {}
|
14
|
+
});
|
15
|
+
const options = context.options.types;
|
16
|
+
const localname = pascal(name) + '.types.ts';
|
17
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
18
|
+
const typeHash = await findAndParseTypes(schemas);
|
19
|
+
const body = []; // TYPES
|
20
|
+
|
21
|
+
Object.values(typeHash).forEach(type => {
|
22
|
+
body.push(clean(type));
|
23
|
+
}); // alias the ExecuteMsg
|
24
|
+
|
25
|
+
if (options.aliasExecuteMsg && ExecuteMsg) {
|
26
|
+
body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${name}ExecuteMsg`), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
|
27
|
+
}
|
28
|
+
|
29
|
+
const imports = context.getImports();
|
30
|
+
const code = header + generate(t.program([...imports, ...body])).code;
|
31
|
+
mkdirp(outPath);
|
32
|
+
const filename = join(outPath, localname);
|
33
|
+
writeFileSync(filename, code);
|
34
|
+
return [{
|
35
|
+
type: 'type',
|
36
|
+
contract: name,
|
37
|
+
localname,
|
38
|
+
filename
|
39
|
+
}];
|
40
|
+
});
|
package/module/index.js
CHANGED
@@ -1,6 +1,13 @@
|
|
1
|
-
|
2
|
-
export { default as
|
3
|
-
export { default as
|
4
|
-
export { default as
|
1
|
+
import { TSBuilder } from './builder';
|
2
|
+
export { default as generateTypes } from './generators/types';
|
3
|
+
export { default as generateClient } from './generators/client';
|
4
|
+
export { default as generateMessageComposer } from './generators/message-composer';
|
5
|
+
export { default as generateReactQuery } from './generators/react-query';
|
6
|
+
export { default as generateRecoil } from './generators/recoil';
|
5
7
|
export * from './utils';
|
6
|
-
export * from './
|
8
|
+
export * from './builder';
|
9
|
+
export * from './bundler';
|
10
|
+
export default (async input => {
|
11
|
+
const builder = new TSBuilder(input);
|
12
|
+
await builder.build();
|
13
|
+
});
|
package/module/types.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/module/utils/schemas.js
CHANGED
@@ -9,16 +9,17 @@ import { readFileSync } from 'fs';
|
|
9
9
|
import { cleanse } from './cleanse';
|
10
10
|
import { compile } from 'json-schema-to-typescript';
|
11
11
|
import { parser } from './parse';
|
12
|
+
;
|
12
13
|
export const readSchemas = async ({
|
13
14
|
schemaDir,
|
14
|
-
|
15
|
+
schemaOptions,
|
15
16
|
clean = true
|
16
17
|
}) => {
|
17
18
|
const fn = clean ? cleanse : str => str;
|
18
19
|
const files = glob(schemaDir + '/**/*.json');
|
19
20
|
const schemas = files.map(file => JSON.parse(readFileSync(file, 'utf-8')));
|
20
21
|
|
21
|
-
if (
|
22
|
+
if (schemaOptions?.packed) {
|
22
23
|
if (schemas.length !== 1) {
|
23
24
|
throw new Error('packed option only supports one file');
|
24
25
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@cosmwasm/ts-codegen",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.8.1",
|
4
4
|
"description": "@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.",
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
6
6
|
"homepage": "https://github.com/cosmwasm/ts-codegen",
|
@@ -82,6 +82,7 @@
|
|
82
82
|
"@babel/types": "7.18.10",
|
83
83
|
"case": "1.6.3",
|
84
84
|
"dargs": "7.0.0",
|
85
|
+
"dotty": "0.1.2",
|
85
86
|
"fuzzy": "0.1.3",
|
86
87
|
"glob": "8.0.3",
|
87
88
|
"inquirerer": "0.1.3",
|
@@ -89,8 +90,9 @@
|
|
89
90
|
"long": "^5.2.0",
|
90
91
|
"minimist": "1.2.6",
|
91
92
|
"mkdirp": "1.0.4",
|
93
|
+
"parse-package-name": "1.0.0",
|
92
94
|
"shelljs": "0.8.5",
|
93
|
-
"wasm-ast-types": "^0.
|
95
|
+
"wasm-ast-types": "^0.7.0"
|
94
96
|
},
|
95
|
-
"gitHead": "
|
97
|
+
"gitHead": "a926a306b0faccb7f4c6d258d53fad9b689ecdfd"
|
96
98
|
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { RenderOptions } from "wasm-ast-types";
|
2
|
+
export interface TSBuilderInput {
|
3
|
+
contracts: Array<ContractFile | string>;
|
4
|
+
outPath: string;
|
5
|
+
options?: TSBuilderOptions;
|
6
|
+
}
|
7
|
+
export interface BundleOptions {
|
8
|
+
enabled?: boolean;
|
9
|
+
scope?: string;
|
10
|
+
bundleFile?: string;
|
11
|
+
}
|
12
|
+
export declare type TSBuilderOptions = {
|
13
|
+
bundle?: BundleOptions;
|
14
|
+
} & RenderOptions;
|
15
|
+
export interface BuilderFile {
|
16
|
+
type: 'type' | 'client' | 'recoil' | 'react-query' | 'message-composer';
|
17
|
+
contract: string;
|
18
|
+
localname: string;
|
19
|
+
filename: string;
|
20
|
+
}
|
21
|
+
export interface ContractFile {
|
22
|
+
name: string;
|
23
|
+
dir: string;
|
24
|
+
}
|
25
|
+
export declare class TSBuilder {
|
26
|
+
contracts: Array<ContractFile | string>;
|
27
|
+
outPath: string;
|
28
|
+
options?: TSBuilderOptions;
|
29
|
+
protected files: BuilderFile[];
|
30
|
+
constructor({ contracts, outPath, options }: TSBuilderInput);
|
31
|
+
getContracts(): ContractFile[];
|
32
|
+
renderTypes(contract: ContractFile): Promise<void>;
|
33
|
+
renderClient(contract: ContractFile): Promise<void>;
|
34
|
+
renderRecoil(contract: ContractFile): Promise<void>;
|
35
|
+
renderReactQuery(contract: ContractFile): Promise<void>;
|
36
|
+
renderMessageComposer(contract: ContractFile): Promise<void>;
|
37
|
+
build(): Promise<void>;
|
38
|
+
bundle(): Promise<void>;
|
39
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './builder';
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
export declare const recursiveModuleBundle: (obj: any) => any;
|
3
|
+
export declare const importNamespace: (ident: string, path: string) => t.ImportDeclaration;
|
4
|
+
export declare const createFileBundle: (pkg: any, filename: any, bundleFile: any, importPaths: any, bundleVariables: any) => void;
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './bundler';
|
File without changes
|
@@ -1,2 +1,3 @@
|
|
1
|
-
|
1
|
+
import { BuilderFile } from "../builder";
|
2
|
+
declare const _default: (contractName: string, schemas: any[], outPath: string, reactQueryOptions?: ReactQueryOptions) => Promise<BuilderFile[]>;
|
2
3
|
export default _default;
|
@@ -1,2 +1,3 @@
|
|
1
|
-
|
1
|
+
import { BuilderFile } from "../builder";
|
2
|
+
declare const _default: (name: string, schemas: any[], outPath: string, recoilOptions?: RecoilOptions) => Promise<BuilderFile[]>;
|
2
3
|
export default _default;
|