@cosmwasm/ts-codegen 0.34.2 → 0.35.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 +83 -20
- package/main/builder/builder.js +145 -247
- package/main/builder/index.js +0 -2
- package/main/bundler/bundler.js +3 -16
- package/main/bundler/index.js +0 -2
- package/main/cli.js +20 -38
- package/main/cmds.js +0 -5
- package/main/commands/create-boilerplate.js +132 -160
- package/main/commands/generate.js +168 -192
- package/main/commands/install.js +89 -123
- package/main/file.js +13 -25
- package/main/generators/client.js +55 -81
- package/main/generators/create-helpers.js +0 -14
- package/main/generators/message-composer.js +42 -68
- package/main/generators/msg-builder.js +49 -78
- package/main/generators/react-query.js +68 -92
- package/main/generators/recoil.js +47 -71
- package/main/generators/types.js +45 -73
- package/main/helpers/index.js +0 -6
- package/main/index.js +8 -30
- package/main/plugins/client.js +56 -90
- package/main/plugins/index.js +0 -2
- package/main/plugins/{msg-builder.js → message-builder.js} +56 -93
- package/main/plugins/message-composer.js +42 -76
- package/main/plugins/plugin-base.js +40 -70
- package/main/plugins/provider-bundle.js +42 -81
- package/main/plugins/provider.js +58 -103
- package/main/plugins/react-query.js +67 -99
- package/main/plugins/recoil.js +45 -82
- package/main/plugins/types.js +34 -68
- package/main/ts-codegen.js +7 -15
- package/main/utils/clean.js +8 -17
- package/main/utils/cleanse.js +10 -25
- package/main/utils/files.js +2 -22
- package/main/utils/header.js +0 -3
- package/main/utils/index.js +0 -2
- package/main/utils/parse.js +2 -7
- package/main/utils/prompt.js +16 -36
- package/main/utils/schemas.js +86 -133
- package/main/utils/unused.js +4 -15
- package/module/builder/builder.js +12 -33
- package/module/bundler/bundler.js +3 -3
- package/module/cli.js +0 -1
- package/module/commands/create-boilerplate.js +6 -19
- package/module/commands/generate.js +14 -19
- package/module/commands/install.js +14 -17
- package/module/file.js +0 -3
- package/module/generators/client.js +6 -7
- package/module/generators/create-helpers.js +0 -4
- package/module/generators/message-composer.js +4 -5
- package/module/generators/msg-builder.js +10 -14
- package/module/generators/react-query.js +8 -9
- package/module/generators/recoil.js +2 -3
- package/module/generators/types.js +4 -6
- package/module/plugins/client.js +4 -10
- package/module/plugins/{msg-builder.js → message-builder.js} +10 -18
- package/module/plugins/message-composer.js +4 -10
- package/module/plugins/plugin-base.js +10 -10
- package/module/plugins/provider-bundle.js +0 -10
- package/module/plugins/provider.js +0 -14
- package/module/plugins/react-query.js +8 -13
- package/module/plugins/recoil.js +2 -9
- package/module/plugins/types.js +4 -7
- package/module/ts-codegen.js +0 -2
- package/module/utils/clean.js +8 -13
- package/module/utils/cleanse.js +10 -21
- package/module/utils/files.js +2 -6
- package/module/utils/parse.js +2 -4
- package/module/utils/prompt.js +0 -5
- package/module/utils/schemas.js +2 -13
- package/module/utils/unused.js +4 -6
- package/package.json +3 -3
- package/src/builder/builder.ts +7 -6
- package/src/commands/generate.ts +14 -13
- package/src/generators/msg-builder.ts +7 -9
- package/src/plugins/{msg-builder.ts → message-builder.ts} +6 -6
- package/src/plugins/message-composer.ts +2 -2
- package/types/src/builder/builder.d.ts +3 -2
- package/types/src/generators/msg-builder.d.ts +2 -2
- package/types/src/generators/msg-builder.ts +2 -2
- package/types/src/plugins/message-builder.d.ts +12 -0
- package/types/src/plugins/msg-builder.d.ts +1 -1
@@ -8,16 +8,13 @@ export class MessageComposerPlugin extends BuilderPluginBase {
|
|
8
8
|
initContext(contract, options) {
|
9
9
|
return new RenderContext(contract, options, this.builder.builderContext);
|
10
10
|
}
|
11
|
-
|
12
11
|
async doRender(name, context) {
|
13
12
|
const {
|
14
13
|
enabled
|
15
14
|
} = this.option.messageComposer;
|
16
|
-
|
17
15
|
if (!enabled) {
|
18
16
|
return;
|
19
17
|
}
|
20
|
-
|
21
18
|
const {
|
22
19
|
schemas
|
23
20
|
} = context.contract;
|
@@ -26,30 +23,27 @@ export class MessageComposerPlugin extends BuilderPluginBase {
|
|
26
23
|
const ExecuteMsg = findExecuteMsg(schemas);
|
27
24
|
const typeHash = await findAndParseTypes(schemas);
|
28
25
|
const body = [];
|
29
|
-
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
26
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
30
27
|
|
28
|
+
// execute messages
|
31
29
|
if (ExecuteMsg) {
|
32
30
|
const children = getMessageProperties(ExecuteMsg);
|
33
|
-
|
34
31
|
if (children.length > 0) {
|
35
|
-
const TheClass = pascal(`${name}
|
36
|
-
const Interface = pascal(`${name}
|
32
|
+
const TheClass = pascal(`${name}MsgComposer`);
|
33
|
+
const Interface = pascal(`${name}Msg`);
|
37
34
|
body.push(w.createMessageComposerInterface(context, Interface, ExecuteMsg));
|
38
35
|
body.push(w.createMessageComposerClass(context, TheClass, Interface, ExecuteMsg));
|
39
36
|
context.addProviderInfo(name, w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE, TheClass, localname);
|
40
37
|
}
|
41
38
|
}
|
42
|
-
|
43
39
|
if (typeHash.hasOwnProperty("Coin")) {
|
44
40
|
// @ts-ignore
|
45
41
|
delete context.utils.Coin;
|
46
42
|
}
|
47
|
-
|
48
43
|
return [{
|
49
44
|
type: TYPE,
|
50
45
|
localname,
|
51
46
|
body
|
52
47
|
}];
|
53
48
|
}
|
54
|
-
|
55
49
|
}
|
@@ -6,41 +6,36 @@ import { header } from "../utils/header";
|
|
6
6
|
import generate from "@babel/generator";
|
7
7
|
import * as t from "@babel/types";
|
8
8
|
|
9
|
+
/**
|
10
|
+
* IBuilderPlugin is a common plugin that render generated code.
|
11
|
+
*/
|
12
|
+
|
9
13
|
/**
|
10
14
|
* BuilderPluginBase enable ts-codegen users implement their own plugins by only implement a few functions.
|
11
15
|
*/
|
12
16
|
export class BuilderPluginBase {
|
13
17
|
constructor(opt, builder) {
|
14
18
|
_defineProperty(this, "builder", void 0);
|
15
|
-
|
16
19
|
_defineProperty(this, "option", void 0);
|
17
|
-
|
18
20
|
_defineProperty(this, "utils", void 0);
|
19
|
-
|
20
21
|
this.option = opt;
|
21
22
|
this.builder = builder;
|
22
23
|
}
|
23
|
-
|
24
24
|
setBuilder(builder) {
|
25
25
|
this.builder = builder;
|
26
26
|
}
|
27
|
-
|
28
27
|
async render(name, contractInfo, outPath) {
|
29
28
|
const {
|
30
29
|
enabled
|
31
30
|
} = this.option;
|
32
|
-
|
33
31
|
if (!enabled) {
|
34
32
|
return;
|
35
33
|
}
|
36
|
-
|
37
34
|
const context = this.initContext(contractInfo, this.option);
|
38
35
|
const results = await this.doRender(name, context);
|
39
|
-
|
40
36
|
if (!results || !results.length) {
|
41
37
|
return [];
|
42
38
|
}
|
43
|
-
|
44
39
|
return results.map(result => {
|
45
40
|
const imports = context.getImports(this.utils, result.localname);
|
46
41
|
const code = header + generate(t.program([...imports, ...result.body])).code;
|
@@ -56,11 +51,16 @@ export class BuilderPluginBase {
|
|
56
51
|
};
|
57
52
|
});
|
58
53
|
}
|
54
|
+
|
59
55
|
/**
|
60
56
|
* init context here
|
61
57
|
* @param contract
|
62
58
|
* @param options
|
63
59
|
*/
|
64
60
|
|
65
|
-
|
61
|
+
/**
|
62
|
+
* render generated code here.
|
63
|
+
* @param name
|
64
|
+
* @param context
|
65
|
+
*/
|
66
66
|
}
|
@@ -14,22 +14,17 @@ export class ContractsProviderBundlePlugin extends BuilderPluginBase {
|
|
14
14
|
IMessageComposerProvider: "__contractContextBase__"
|
15
15
|
};
|
16
16
|
}
|
17
|
-
|
18
17
|
initContext(contract, options) {
|
19
18
|
return new RenderContext(contract, options, this.builder.builderContext);
|
20
19
|
}
|
21
|
-
|
22
20
|
async doRender(name, context) {
|
23
21
|
if (!this.option?.useContractsHooks?.enabled) {
|
24
22
|
return;
|
25
23
|
}
|
26
|
-
|
27
24
|
const providerInfos = context.getProviderInfos();
|
28
|
-
|
29
25
|
if (!Object.keys(providerInfos)?.length) {
|
30
26
|
return;
|
31
27
|
}
|
32
|
-
|
33
28
|
const localname = "contractContextProviders.ts";
|
34
29
|
const body = [];
|
35
30
|
context.addUtil("CosmWasmClient");
|
@@ -37,22 +32,18 @@ export class ContractsProviderBundlePlugin extends BuilderPluginBase {
|
|
37
32
|
context.addUtil("IQueryClientProvider");
|
38
33
|
context.addUtil("ISigningClientProvider");
|
39
34
|
context.addUtil("IMessageComposerProvider");
|
40
|
-
|
41
35
|
for (const name in providerInfos) {
|
42
36
|
if (Object.prototype.hasOwnProperty.call(providerInfos, name)) {
|
43
37
|
const providerInfo = providerInfos[name];
|
44
|
-
|
45
38
|
for (const key in providerInfo) {
|
46
39
|
if (Object.prototype.hasOwnProperty.call(providerInfo, key)) {
|
47
40
|
const info = providerInfo[key];
|
48
41
|
body.push(w.importStmt([info.classname], `./${info.basename}`));
|
49
42
|
}
|
50
43
|
}
|
51
|
-
|
52
44
|
body.push(w.importStmt([pascal(name)], `./${GetLocalBaseNameByContractName(name)}`));
|
53
45
|
}
|
54
46
|
}
|
55
|
-
|
56
47
|
body.push(w.createIContractsContext(providerInfos));
|
57
48
|
body.push(w.createGettingProviders(providerInfos));
|
58
49
|
return [{
|
@@ -62,5 +53,4 @@ export class ContractsProviderBundlePlugin extends BuilderPluginBase {
|
|
62
53
|
body
|
63
54
|
}];
|
64
55
|
}
|
65
|
-
|
66
56
|
}
|
@@ -13,22 +13,17 @@ export class ContractsContextProviderPlugin extends BuilderPluginBase {
|
|
13
13
|
IEmptyClient: "__contractContextBase__"
|
14
14
|
};
|
15
15
|
}
|
16
|
-
|
17
16
|
initContext(contract, options) {
|
18
17
|
return new RenderContext(contract, options, this.builder.builderContext);
|
19
18
|
}
|
20
|
-
|
21
19
|
async doRender(name, context) {
|
22
20
|
if (!this.option?.useContractsHooks?.enabled) {
|
23
21
|
return;
|
24
22
|
}
|
25
|
-
|
26
23
|
const providerInfo = context.getProviderInfos()[name];
|
27
|
-
|
28
24
|
if (!Object.keys(providerInfo)?.length) {
|
29
25
|
return;
|
30
26
|
}
|
31
|
-
|
32
27
|
context.addUtil("ContractBase");
|
33
28
|
context.addUtil("IContractConstructor");
|
34
29
|
const localname = GetLocalNameByContractName(name);
|
@@ -37,39 +32,31 @@ export class ContractsContextProviderPlugin extends BuilderPluginBase {
|
|
37
32
|
let clientClasses = [];
|
38
33
|
const body = [];
|
39
34
|
const signClientProviderInfo = providerInfo[w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE];
|
40
|
-
|
41
35
|
if (signClientProviderInfo) {
|
42
36
|
clientFile = `./${signClientProviderInfo.basename}`;
|
43
37
|
clientClasses.push(signClientProviderInfo.classname);
|
44
38
|
} else {
|
45
39
|
needEmptyClientType = true;
|
46
40
|
}
|
47
|
-
|
48
41
|
const queryClientProviderInfo = providerInfo[w.PROVIDER_TYPES.QUERY_CLIENT_TYPE];
|
49
|
-
|
50
42
|
if (queryClientProviderInfo) {
|
51
43
|
clientFile = `./${queryClientProviderInfo.basename}`;
|
52
44
|
clientClasses.push(queryClientProviderInfo.classname);
|
53
45
|
} else {
|
54
46
|
needEmptyClientType = true;
|
55
47
|
}
|
56
|
-
|
57
48
|
if (clientFile) {
|
58
49
|
body.push(w.importStmt(clientClasses, clientFile));
|
59
50
|
}
|
60
|
-
|
61
51
|
const messageComposerProviderInfo = providerInfo[w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE];
|
62
|
-
|
63
52
|
if (messageComposerProviderInfo) {
|
64
53
|
body.push(w.importStmt([messageComposerProviderInfo.classname], `./${messageComposerProviderInfo.basename}`));
|
65
54
|
} else {
|
66
55
|
needEmptyClientType = true;
|
67
56
|
}
|
68
|
-
|
69
57
|
if (needEmptyClientType) {
|
70
58
|
context.addUtil("IEmptyClient");
|
71
59
|
}
|
72
|
-
|
73
60
|
body.push(w.createProvider(name, providerInfo));
|
74
61
|
return [{
|
75
62
|
type: "plugin",
|
@@ -78,5 +65,4 @@ export class ContractsContextProviderPlugin extends BuilderPluginBase {
|
|
78
65
|
body
|
79
66
|
}];
|
80
67
|
}
|
81
|
-
|
82
68
|
}
|
@@ -7,17 +7,14 @@ export class ReactQueryPlugin extends BuilderPluginBase {
|
|
7
7
|
initContext(contract, options) {
|
8
8
|
return new RenderContext(contract, options, this.builder.builderContext);
|
9
9
|
}
|
10
|
-
|
11
10
|
async doRender(name, context) {
|
12
11
|
const options = this.option.reactQuery;
|
13
12
|
const {
|
14
13
|
enabled
|
15
14
|
} = options;
|
16
|
-
|
17
15
|
if (!enabled) {
|
18
16
|
return;
|
19
17
|
}
|
20
|
-
|
21
18
|
const {
|
22
19
|
schemas
|
23
20
|
} = context.contract;
|
@@ -31,19 +28,21 @@ export class ReactQueryPlugin extends BuilderPluginBase {
|
|
31
28
|
const QueryClient = pascal(`${name}QueryClient`);
|
32
29
|
const body = [];
|
33
30
|
const clientImports = [];
|
34
|
-
QueryMsg && clientImports.push(QueryClient);
|
31
|
+
QueryMsg && clientImports.push(QueryClient);
|
35
32
|
|
33
|
+
// check that there are commands within the exec msg
|
36
34
|
const shouldGenerateMutationHooks = ExecuteMsg && options?.version === 'v4' && options?.mutations && getMessageProperties(ExecuteMsg).length > 0;
|
37
|
-
|
38
35
|
if (shouldGenerateMutationHooks) {
|
39
36
|
clientImports.push(ExecuteClient);
|
40
|
-
}
|
41
|
-
|
37
|
+
}
|
42
38
|
|
43
|
-
|
39
|
+
// general contract imports
|
40
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
44
41
|
|
45
|
-
|
42
|
+
// client imports
|
43
|
+
body.push(w.importStmt(clientImports, `./${ContractFile}`));
|
46
44
|
|
45
|
+
// query messages
|
47
46
|
if (QueryMsg) {
|
48
47
|
[].push.apply(body, w.createReactQueryHooks({
|
49
48
|
context,
|
@@ -52,7 +51,6 @@ export class ReactQueryPlugin extends BuilderPluginBase {
|
|
52
51
|
QueryClient
|
53
52
|
}));
|
54
53
|
}
|
55
|
-
|
56
54
|
if (shouldGenerateMutationHooks) {
|
57
55
|
[].push.apply(body, w.createReactQueryMutationHooks({
|
58
56
|
context,
|
@@ -61,17 +59,14 @@ export class ReactQueryPlugin extends BuilderPluginBase {
|
|
61
59
|
ExecuteClient
|
62
60
|
}));
|
63
61
|
}
|
64
|
-
|
65
62
|
if (typeHash.hasOwnProperty('Coin')) {
|
66
63
|
// @ts-ignore
|
67
64
|
delete context.utils.Coin;
|
68
65
|
}
|
69
|
-
|
70
66
|
return [{
|
71
67
|
type: 'react-query',
|
72
68
|
localname,
|
73
69
|
body
|
74
70
|
}];
|
75
71
|
}
|
76
|
-
|
77
72
|
}
|
package/module/plugins/recoil.js
CHANGED
@@ -7,25 +7,20 @@ import { BuilderPluginBase } from './plugin-base';
|
|
7
7
|
export class RecoilPlugin extends BuilderPluginBase {
|
8
8
|
constructor(...args) {
|
9
9
|
super(...args);
|
10
|
-
|
11
10
|
_defineProperty(this, "utils", {
|
12
11
|
selectorFamily: 'recoil'
|
13
12
|
});
|
14
13
|
}
|
15
|
-
|
16
14
|
initContext(contract, options) {
|
17
15
|
return new RenderContext(contract, options, this.builder.builderContext);
|
18
16
|
}
|
19
|
-
|
20
17
|
async doRender(name, context) {
|
21
18
|
const {
|
22
19
|
enabled
|
23
20
|
} = this.option.recoil;
|
24
|
-
|
25
21
|
if (!enabled) {
|
26
22
|
return;
|
27
23
|
}
|
28
|
-
|
29
24
|
const {
|
30
25
|
schemas
|
31
26
|
} = context.contract;
|
@@ -38,8 +33,9 @@ export class RecoilPlugin extends BuilderPluginBase {
|
|
38
33
|
let ReadOnlyInstance = null;
|
39
34
|
const body = [];
|
40
35
|
body.push(w.importStmt(['cosmWasmClient'], './chain'));
|
41
|
-
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
36
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
|
42
37
|
|
38
|
+
// query messages
|
43
39
|
if (QueryMsg) {
|
44
40
|
QueryClient = pascal(`${name}QueryClient`);
|
45
41
|
ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
|
@@ -48,17 +44,14 @@ export class RecoilPlugin extends BuilderPluginBase {
|
|
48
44
|
body.push(w.createRecoilQueryClient(context, name, QueryClient));
|
49
45
|
[].push.apply(body, w.createRecoilSelectors(context, name, QueryClient, QueryMsg));
|
50
46
|
}
|
51
|
-
|
52
47
|
if (typeHash.hasOwnProperty('Coin')) {
|
53
48
|
// @ts-ignore
|
54
49
|
delete context.utils.Coin;
|
55
50
|
}
|
56
|
-
|
57
51
|
return [{
|
58
52
|
type: 'recoil',
|
59
53
|
localname,
|
60
54
|
body
|
61
55
|
}];
|
62
56
|
}
|
63
|
-
|
64
57
|
}
|
package/module/plugins/types.js
CHANGED
@@ -8,16 +8,13 @@ export class TypesPlugin extends BuilderPluginBase {
|
|
8
8
|
initContext(contract, options) {
|
9
9
|
return new RenderContext(contract, options, this.builder.builderContext);
|
10
10
|
}
|
11
|
-
|
12
11
|
async doRender(name, context) {
|
13
12
|
const {
|
14
13
|
enabled
|
15
14
|
} = this.option.types;
|
16
|
-
|
17
15
|
if (!enabled) {
|
18
16
|
return;
|
19
17
|
}
|
20
|
-
|
21
18
|
const {
|
22
19
|
schemas
|
23
20
|
} = context.contract;
|
@@ -25,21 +22,21 @@ export class TypesPlugin extends BuilderPluginBase {
|
|
25
22
|
const localname = pascal(name) + '.types.ts';
|
26
23
|
const ExecuteMsg = findExecuteMsg(schemas);
|
27
24
|
const typeHash = await findAndParseTypes(schemas);
|
28
|
-
const body = [];
|
25
|
+
const body = [];
|
29
26
|
|
27
|
+
// TYPES
|
30
28
|
Object.values(typeHash).forEach(type => {
|
31
29
|
body.push(clean(type));
|
32
|
-
});
|
30
|
+
});
|
33
31
|
|
32
|
+
// alias the ExecuteMsg
|
34
33
|
if (options.aliasExecuteMsg && ExecuteMsg) {
|
35
34
|
body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${name}ExecuteMsg`), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
|
36
35
|
}
|
37
|
-
|
38
36
|
return [{
|
39
37
|
type: 'type',
|
40
38
|
localname,
|
41
39
|
body
|
42
40
|
}];
|
43
41
|
}
|
44
|
-
|
45
42
|
}
|
package/module/ts-codegen.js
CHANGED
package/module/utils/clean.js
CHANGED
@@ -1,29 +1,27 @@
|
|
1
1
|
export const clean = obj => {
|
2
|
-
var copy;
|
3
|
-
|
4
|
-
if (null == obj || 'object' != typeof obj) return obj;
|
2
|
+
var copy;
|
3
|
+
// Handle the 3 simple types, and null or undefined
|
4
|
+
if (null == obj || 'object' != typeof obj) return obj;
|
5
5
|
|
6
|
+
// Handle Date
|
6
7
|
if (obj instanceof Date) {
|
7
8
|
copy = new Date();
|
8
9
|
copy.setTime(obj.getTime());
|
9
10
|
return copy;
|
10
|
-
}
|
11
|
-
|
11
|
+
}
|
12
12
|
|
13
|
+
// Handle Array
|
13
14
|
if (obj instanceof Array) {
|
14
15
|
copy = [];
|
15
|
-
|
16
16
|
for (var i = 0, len = obj.length; i < len; i++) {
|
17
17
|
copy[i] = clean(obj[i]);
|
18
18
|
}
|
19
|
-
|
20
19
|
return copy;
|
21
|
-
}
|
22
|
-
|
20
|
+
}
|
23
21
|
|
22
|
+
// Handle Object
|
24
23
|
if (obj instanceof Object || typeof obj === 'object') {
|
25
24
|
copy = {};
|
26
|
-
|
27
25
|
for (var attr in obj) {
|
28
26
|
if (obj.hasOwnProperty(attr)) {
|
29
27
|
switch (attr) {
|
@@ -33,7 +31,6 @@ export const clean = obj => {
|
|
33
31
|
case 'start':
|
34
32
|
case 'end':
|
35
33
|
break;
|
36
|
-
|
37
34
|
default:
|
38
35
|
copy[attr] = clean(obj[attr]);
|
39
36
|
}
|
@@ -41,9 +38,7 @@ export const clean = obj => {
|
|
41
38
|
copy[attr] = clean(obj[attr]);
|
42
39
|
}
|
43
40
|
}
|
44
|
-
|
45
41
|
return copy;
|
46
42
|
}
|
47
|
-
|
48
43
|
throw new Error("Unable to copy obj! Its type isn't supported.");
|
49
44
|
};
|
package/module/utils/cleanse.js
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
import { pascal } from "case";
|
2
|
-
|
3
2
|
const cleanFor = str => {
|
4
3
|
/*
|
5
4
|
1. look at first char after _for_
|
@@ -9,52 +8,46 @@ const cleanFor = str => {
|
|
9
8
|
const m = str.match(/(_[a-z]+_)[A-Z]/);
|
10
9
|
str = str.replace(m[1], pascal(m[1]));
|
11
10
|
}
|
12
|
-
|
13
11
|
return str;
|
14
12
|
};
|
15
|
-
|
16
13
|
const cleanNullable = str => {
|
17
14
|
if (/^Nullable_/.test(str)) {
|
18
15
|
str = str.replace(/^Nullable_/, 'Nullable');
|
19
16
|
}
|
20
|
-
|
21
17
|
return str;
|
22
18
|
};
|
23
|
-
|
24
19
|
export const cleanse = obj => {
|
25
|
-
var copy;
|
26
|
-
|
27
|
-
if (null == obj || 'object' != typeof obj) return obj;
|
20
|
+
var copy;
|
21
|
+
// Handle the 3 simple types, and null or undefined
|
22
|
+
if (null == obj || 'object' != typeof obj) return obj;
|
28
23
|
|
24
|
+
// Handle Date
|
29
25
|
if (obj instanceof Date) {
|
30
26
|
copy = new Date();
|
31
27
|
copy.setTime(obj.getTime());
|
32
28
|
return copy;
|
33
|
-
}
|
34
|
-
|
29
|
+
}
|
35
30
|
|
31
|
+
// Handle Array
|
36
32
|
if (obj instanceof Array) {
|
37
33
|
copy = [];
|
38
|
-
|
39
34
|
for (var i = 0, len = obj.length; i < len; i++) {
|
40
35
|
copy[i] = cleanse(obj[i]);
|
41
36
|
}
|
42
|
-
|
43
37
|
return copy;
|
44
|
-
}
|
45
|
-
|
38
|
+
}
|
46
39
|
|
40
|
+
// Handle Object
|
47
41
|
if (obj instanceof Object || typeof obj === 'object') {
|
48
|
-
copy = {};
|
42
|
+
copy = {};
|
49
43
|
|
44
|
+
// https://github.com/CosmWasm/cosmwasm-typescript-gen/issues/27
|
50
45
|
if (Array.isArray(obj.enum) && obj.enum.length === 0) {
|
51
46
|
delete obj.enum;
|
52
|
-
|
53
47
|
if (!obj.type) {
|
54
48
|
obj.type = 'string';
|
55
49
|
}
|
56
50
|
}
|
57
|
-
|
58
51
|
for (var attr in obj) {
|
59
52
|
if (obj.hasOwnProperty(attr)) {
|
60
53
|
if (/_for_/.test(attr)) {
|
@@ -70,9 +63,7 @@ export const cleanse = obj => {
|
|
70
63
|
} else {
|
71
64
|
copy[attr] = cleanse(obj[attr]);
|
72
65
|
}
|
73
|
-
|
74
66
|
break;
|
75
|
-
|
76
67
|
default:
|
77
68
|
copy[attr] = cleanse(obj[attr]);
|
78
69
|
}
|
@@ -81,9 +72,7 @@ export const cleanse = obj => {
|
|
81
72
|
copy[attr] = cleanse(obj[attr]);
|
82
73
|
}
|
83
74
|
}
|
84
|
-
|
85
75
|
return copy;
|
86
76
|
}
|
87
|
-
|
88
77
|
throw new Error("Unable to copy obj! Its type isn't supported.");
|
89
78
|
};
|
package/module/utils/files.js
CHANGED
@@ -9,7 +9,6 @@ import traverse from "@babel/traverse";
|
|
9
9
|
export const writeAstToFile = (outPath, program, filename, removeUnusedImports = false, isTsDisable = false, isEslintDisable = false) => {
|
10
10
|
const ast = t.program(program);
|
11
11
|
const content = generate(ast).code;
|
12
|
-
|
13
12
|
if (removeUnusedImports) {
|
14
13
|
const plugins = ["typescript"];
|
15
14
|
const newAst = parse(content, {
|
@@ -26,18 +25,15 @@ export const writeAstToFile = (outPath, program, filename, removeUnusedImports =
|
|
26
25
|
export const writeContentToFile = (outPath, content, filename, isTsDisable = false, isEslintDisable = false) => {
|
27
26
|
let esLintPrefix = "";
|
28
27
|
let tsLintPrefix = "";
|
29
|
-
let nameWithoutPath = filename.replace(outPath, "");
|
30
|
-
|
28
|
+
let nameWithoutPath = filename.replace(outPath, "");
|
29
|
+
// strip off leading slash
|
31
30
|
if (nameWithoutPath.startsWith("/")) nameWithoutPath = nameWithoutPath.replace(/^\//, "");
|
32
|
-
|
33
31
|
if (isTsDisable) {
|
34
32
|
tsLintPrefix = `//@ts-nocheck\n`;
|
35
33
|
}
|
36
|
-
|
37
34
|
if (isEslintDisable) {
|
38
35
|
esLintPrefix = `/* eslint-disable */\n`;
|
39
36
|
}
|
40
|
-
|
41
37
|
const text = tsLintPrefix + esLintPrefix + content;
|
42
38
|
mkdirp(dirname(filename));
|
43
39
|
writeFileSync(filename, text);
|
package/module/utils/parse.js
CHANGED
@@ -12,16 +12,15 @@ export const parser = codes => {
|
|
12
12
|
addType(key, node) {
|
13
13
|
hash[key] = node;
|
14
14
|
}
|
15
|
-
|
16
15
|
});
|
17
16
|
babelTraverse(ast, visitor);
|
18
17
|
});
|
19
18
|
return hash;
|
20
19
|
};
|
21
|
-
|
22
20
|
const visitorFn = parser => ({
|
23
21
|
TSTypeAliasDeclaration(path) {
|
24
|
-
parser.addType(path.node.id.name, path.parentPath.node);
|
22
|
+
parser.addType(path.node.id.name, path.parentPath.node);
|
23
|
+
// if (path.node.id.name.endsWith('For_Empty')) {
|
25
24
|
// const newName = path.node.id.name.replace(/For_Empty$/, '_for_Empty');
|
26
25
|
// path.parentPath.node.declaration.id.name = newName;
|
27
26
|
// parser.addType(newName, path.parentPath.node);
|
@@ -33,5 +32,4 @@ const visitorFn = parser => ({
|
|
33
32
|
TSInterfaceDeclaration(path) {
|
34
33
|
parser.addType(path.node.id.name, path.parentPath.node);
|
35
34
|
}
|
36
|
-
|
37
35
|
});
|
package/module/utils/prompt.js
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2
|
-
|
3
2
|
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
3
|
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
4
|
import { filter } from 'fuzzy';
|
8
5
|
import { prompt as inquirerer } from 'inquirerer';
|
9
6
|
export const getFuzzySearch = list => {
|
@@ -39,7 +36,6 @@ export const getFuzzySearchNames = nameValueItemList => {
|
|
39
36
|
});
|
40
37
|
};
|
41
38
|
};
|
42
|
-
|
43
39
|
const transform = questions => {
|
44
40
|
return questions.map(q => {
|
45
41
|
if (q.type === 'fuzzy') {
|
@@ -61,7 +57,6 @@ const transform = questions => {
|
|
61
57
|
}
|
62
58
|
});
|
63
59
|
};
|
64
|
-
|
65
60
|
export const prompt = async (questions = [], argv = {}) => {
|
66
61
|
questions = transform(questions);
|
67
62
|
return await inquirerer(questions, argv);
|