@cosmwasm/ts-codegen 0.35.7 → 1.1.0
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/LICENSE-Apache +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +106 -101
- package/{types/src/builder → builder}/builder.d.ts +2 -2
- package/builder/builder.js +172 -0
- package/builder/index.js +17 -0
- package/bundler/bundler.d.ts +8 -0
- package/bundler/bundler.js +79 -0
- package/bundler/index.js +17 -0
- package/cli.d.ts +2 -0
- package/cli.js +24 -0
- package/cmds.d.ts +2 -0
- package/cmds.js +14 -0
- package/commands/create-boilerplate.d.ts +3 -0
- package/commands/create-boilerplate.js +132 -0
- package/commands/generate.d.ts +3 -0
- package/commands/generate.js +175 -0
- package/commands/install.d.ts +3 -0
- package/commands/install.js +107 -0
- package/{src/builder/builder.ts → esm/builder/builder.js} +39 -139
- package/esm/builder/index.js +1 -0
- package/esm/bundler/bundler.js +47 -0
- package/esm/bundler/index.js +1 -0
- package/esm/cli.js +20 -0
- package/{src → esm}/cmds.js +0 -1
- package/{src/commands/create-boilerplate.ts → esm/commands/create-boilerplate.js} +35 -70
- package/{src/commands/generate.ts → esm/commands/generate.js} +19 -24
- package/{src/commands/install.ts → esm/commands/install.js} +22 -35
- package/esm/file.js +18 -0
- package/esm/generators/client.js +67 -0
- package/esm/generators/create-helpers.js +36 -0
- package/esm/generators/message-builder.js +58 -0
- package/esm/generators/message-composer.js +55 -0
- package/esm/generators/react-query.js +76 -0
- package/esm/generators/recoil.js +58 -0
- package/esm/generators/types.js +58 -0
- package/{src/index.ts → esm/index.js} +4 -6
- package/esm/plugins/client.js +59 -0
- package/esm/plugins/message-builder.js +52 -0
- package/esm/plugins/message-composer.js +46 -0
- package/esm/plugins/plugin-base.js +47 -0
- package/esm/plugins/provider-bundle.js +58 -0
- package/esm/plugins/provider.js +73 -0
- package/esm/plugins/react-query.js +69 -0
- package/esm/plugins/recoil.js +51 -0
- package/esm/plugins/types.js +38 -0
- package/{src → esm}/ts-codegen.js +1 -2
- package/{src/utils/clean.ts → esm/utils/clean.js} +7 -7
- package/{src/utils/cleanse.ts → esm/utils/cleanse.js} +18 -22
- package/esm/utils/files.js +42 -0
- package/{src/utils/parse.ts → esm/utils/parse.js} +4 -13
- package/esm/utils/prompt.js +59 -0
- package/{src/utils/schemas.ts → esm/utils/schemas.js} +16 -44
- package/esm/utils/unused.js +47 -0
- package/file.js +20 -0
- package/generators/client.js +95 -0
- package/generators/create-helpers.js +43 -0
- package/generators/message-builder.js +86 -0
- package/generators/message-composer.js +83 -0
- package/generators/react-query.js +104 -0
- package/generators/recoil.js +86 -0
- package/generators/types.js +86 -0
- package/{module/helpers → helpers}/contractContextBase.js +5 -2
- package/{module/helpers → helpers}/contractContextBaseShortHandCtor.js +5 -2
- package/{module/helpers → helpers}/contractsContextTSX.js +5 -2
- package/{module/helpers/index.js → helpers/index.d.ts} +1 -1
- package/helpers/index.js +19 -0
- package/{types/src/index.d.ts → index.d.ts} +1 -0
- package/index.js +41 -0
- package/package.json +28 -63
- package/plugins/client.js +86 -0
- package/plugins/index.d.ts +1 -0
- package/plugins/index.js +17 -0
- package/plugins/message-builder.js +79 -0
- package/plugins/message-composer.js +73 -0
- package/{types/src/plugins → plugins}/plugin-base.d.ts +1 -1
- package/plugins/plugin-base.js +77 -0
- package/plugins/provider-bundle.js +85 -0
- package/{types/src/plugins → plugins}/provider.d.ts +2 -2
- package/plugins/provider.js +102 -0
- package/plugins/react-query.js +96 -0
- package/plugins/recoil.js +78 -0
- package/plugins/types.js +65 -0
- package/ts-codegen.js +8 -0
- package/utils/clean.js +49 -0
- package/utils/cleanse.js +89 -0
- package/utils/files.js +73 -0
- package/utils/header.js +14 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +17 -0
- package/utils/parse.d.ts +1 -0
- package/utils/parse.js +43 -0
- package/utils/prompt.d.ts +3 -0
- package/utils/prompt.js +65 -0
- package/utils/schemas.d.ts +11 -0
- package/utils/schemas.js +81 -0
- package/utils/unused.js +73 -0
- package/main/builder/builder.js +0 -308
- package/main/builder/index.js +0 -16
- package/main/bundler/bundler.js +0 -61
- package/main/bundler/index.js +0 -16
- package/main/cli.js +0 -51
- package/main/cmds.js +0 -15
- package/main/commands/create-boilerplate.js +0 -166
- package/main/commands/generate.js +0 -196
- package/main/commands/install.js +0 -138
- package/main/file.js +0 -35
- package/main/generators/client.js +0 -90
- package/main/generators/create-helpers.js +0 -38
- package/main/generators/message-composer.js +0 -77
- package/main/generators/msg-builder.js +0 -84
- package/main/generators/react-query.js +0 -102
- package/main/generators/recoil.js +0 -81
- package/main/generators/types.js +0 -82
- package/main/helpers/contractContextBase.js +0 -8
- package/main/helpers/contractContextBaseShortHandCtor.js +0 -8
- package/main/helpers/contractsContextTSX.js +0 -8
- package/main/helpers/index.js +0 -38
- package/main/index.js +0 -119
- package/main/plugins/client.js +0 -112
- package/main/plugins/index.js +0 -16
- package/main/plugins/message-builder.js +0 -102
- package/main/plugins/message-composer.js +0 -98
- package/main/plugins/plugin-base.js +0 -103
- package/main/plugins/provider-bundle.js +0 -108
- package/main/plugins/provider.js +0 -126
- package/main/plugins/react-query.js +0 -120
- package/main/plugins/recoil.js +0 -108
- package/main/plugins/types.js +0 -89
- package/main/ts-codegen.js +0 -20
- package/main/utils/clean.js +0 -53
- package/main/utils/cleanse.js +0 -87
- package/main/utils/files.js +0 -57
- package/main/utils/header.js +0 -11
- package/main/utils/index.js +0 -16
- package/main/utils/parse.js +0 -44
- package/main/utils/prompt.js +0 -98
- package/main/utils/schemas.js +0 -144
- package/main/utils/unused.js +0 -57
- package/module/builder/builder.js +0 -143
- package/module/builder/index.js +0 -1
- package/module/bundler/bundler.js +0 -36
- package/module/bundler/index.js +0 -1
- package/module/cli.js +0 -19
- package/module/cmds.js +0 -8
- package/module/commands/create-boilerplate.js +0 -102
- package/module/commands/generate.js +0 -163
- package/module/commands/install.js +0 -98
- package/module/file.js +0 -18
- package/module/generators/client.js +0 -65
- package/module/generators/create-helpers.js +0 -33
- package/module/generators/message-composer.js +0 -52
- package/module/generators/msg-builder.js +0 -58
- package/module/generators/react-query.js +0 -75
- package/module/generators/recoil.js +0 -53
- package/module/generators/types.js +0 -55
- package/module/index.js +0 -14
- package/module/plugins/client.js +0 -63
- package/module/plugins/index.js +0 -1
- package/module/plugins/message-builder.js +0 -55
- package/module/plugins/message-composer.js +0 -49
- package/module/plugins/plugin-base.js +0 -66
- package/module/plugins/provider-bundle.js +0 -56
- package/module/plugins/provider.js +0 -68
- package/module/plugins/react-query.js +0 -72
- package/module/plugins/recoil.js +0 -57
- package/module/plugins/types.js +0 -42
- package/module/ts-codegen.js +0 -6
- package/module/utils/clean.js +0 -44
- package/module/utils/cleanse.js +0 -78
- package/module/utils/files.js +0 -40
- package/module/utils/header.js +0 -8
- package/module/utils/index.js +0 -1
- package/module/utils/parse.js +0 -35
- package/module/utils/prompt.js +0 -63
- package/module/utils/schemas.js +0 -85
- package/module/utils/unused.js +0 -43
- package/src/builder/index.ts +0 -1
- package/src/bundler/bundler.ts +0 -67
- package/src/bundler/index.ts +0 -1
- package/src/cli.js +0 -21
- package/src/file.js +0 -20
- package/src/generators/client.ts +0 -109
- package/src/generators/create-helpers.ts +0 -71
- package/src/generators/message-composer.ts +0 -86
- package/src/generators/msg-builder.ts +0 -78
- package/src/generators/react-query.ts +0 -102
- package/src/generators/recoil.ts +0 -92
- package/src/generators/types.ts +0 -94
- package/src/helpers/index.ts +0 -3
- package/src/plugins/client.ts +0 -124
- package/src/plugins/index.ts +0 -1
- package/src/plugins/message-builder.ts +0 -85
- package/src/plugins/message-composer.ts +0 -89
- package/src/plugins/plugin-base.ts +0 -122
- package/src/plugins/provider-bundle.ts +0 -98
- package/src/plugins/provider.ts +0 -115
- package/src/plugins/react-query.ts +0 -115
- package/src/plugins/recoil.ts +0 -89
- package/src/plugins/types.ts +0 -74
- package/src/utils/files.ts +0 -73
- package/src/utils/index.ts +0 -1
- package/src/utils/prompt.js +0 -65
- package/src/utils/unused.ts +0 -52
- package/types/src/bundler/bundler.d.ts +0 -4
- package/types/src/cli.d.ts +0 -1
- package/types/src/cmds.d.ts +0 -1
- package/types/src/commands/create-boilerplate.d.ts +0 -2
- package/types/src/commands/generate.d.ts +0 -2
- package/types/src/commands/install.d.ts +0 -2
- package/types/src/generators/msg-builder.ts +0 -5
- package/types/src/plugins/msg-builder.d.ts +0 -12
- package/types/src/plugins/use-contracts.d.ts +0 -12
- package/types/src/types.d.ts +0 -16
- package/types/src/utils/parse.d.ts +0 -1
- package/types/src/utils/prompt.d.ts +0 -3
- package/types/src/utils/schemas.d.ts +0 -10
- /package/{types/src/builder → builder}/index.d.ts +0 -0
- /package/{types/src/bundler → bundler}/index.d.ts +0 -0
- /package/{src/helpers/contractContextBase.ts → esm/helpers/contractContextBase.js} +0 -0
- /package/{src/helpers/contractContextBaseShortHandCtor.ts → esm/helpers/contractContextBaseShortHandCtor.js} +0 -0
- /package/{src/helpers/contractsContextTSX.ts → esm/helpers/contractsContextTSX.js} +0 -0
- /package/{types/src/helpers/index.d.ts → esm/helpers/index.js} +0 -0
- /package/{types/src/plugins/index.d.ts → esm/plugins/index.js} +0 -0
- /package/{src/utils/header.ts → esm/utils/header.js} +0 -0
- /package/{types/src/utils/index.d.ts → esm/utils/index.js} +0 -0
- /package/{types/src/file.d.ts → file.d.ts} +0 -0
- /package/{types/src/generators → generators}/client.d.ts +0 -0
- /package/{types/src/generators → generators}/create-helpers.d.ts +0 -0
- /package/{types/src/generators/msg-builder.d.ts → generators/message-builder.d.ts} +0 -0
- /package/{types/src/generators → generators}/message-composer.d.ts +0 -0
- /package/{types/src/generators → generators}/react-query.d.ts +0 -0
- /package/{types/src/generators → generators}/recoil.d.ts +0 -0
- /package/{types/src/generators → generators}/types.d.ts +0 -0
- /package/{types/src/helpers → helpers}/contractContextBase.d.ts +0 -0
- /package/{types/src/helpers → helpers}/contractContextBaseShortHandCtor.d.ts +0 -0
- /package/{types/src/helpers → helpers}/contractsContextTSX.d.ts +0 -0
- /package/{types/src/plugins → plugins}/client.d.ts +0 -0
- /package/{types/src/plugins → plugins}/message-builder.d.ts +0 -0
- /package/{types/src/plugins → plugins}/message-composer.d.ts +0 -0
- /package/{types/src/plugins → plugins}/provider-bundle.d.ts +0 -0
- /package/{types/src/plugins → plugins}/react-query.d.ts +0 -0
- /package/{types/src/plugins → plugins}/recoil.d.ts +0 -0
- /package/{types/src/plugins → plugins}/types.d.ts +0 -0
- /package/{types/src/ts-codegen.d.ts → ts-codegen.d.ts} +0 -0
- /package/{types/src/utils → utils}/clean.d.ts +0 -0
- /package/{types/src/utils → utils}/cleanse.d.ts +0 -0
- /package/{types/src/utils → utils}/files.d.ts +0 -0
- /package/{types/src/utils → utils}/header.d.ts +0 -0
- /package/{types/src/utils → utils}/unused.d.ts +0 -0
@@ -0,0 +1,107 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const path_1 = require("path");
|
4
|
+
const mkdirp_1 = require("mkdirp");
|
5
|
+
const glob_1 = require("glob");
|
6
|
+
const rimraf_1 = require("rimraf");
|
7
|
+
const shelljs_1 = require("shelljs");
|
8
|
+
const prompt_1 = require("../utils/prompt");
|
9
|
+
const parse_package_name_1 = require("parse-package-name");
|
10
|
+
const os_1 = require("os");
|
11
|
+
const fs_1 = require("fs");
|
12
|
+
const TMPDIR = (0, os_1.tmpdir)();
|
13
|
+
const rnd = () => Math.random().toString(36).substring(2, 15) +
|
14
|
+
Math.random().toString(36).substring(2, 15);
|
15
|
+
const getPackages = (names) => {
|
16
|
+
return names.map(pkg => {
|
17
|
+
const { name, version } = (0, parse_package_name_1.parse)(pkg);
|
18
|
+
return `${name}@${version}`;
|
19
|
+
}).join(' ');
|
20
|
+
};
|
21
|
+
exports.default = async (argv) => {
|
22
|
+
// don't prompt if we got this...
|
23
|
+
if (argv._.length) {
|
24
|
+
argv.pkg = argv._;
|
25
|
+
}
|
26
|
+
// current dir/package
|
27
|
+
const cur = process.cwd();
|
28
|
+
let thisPackage;
|
29
|
+
try {
|
30
|
+
thisPackage = JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(cur, 'package.json'), 'utf-8'));
|
31
|
+
}
|
32
|
+
catch (e) {
|
33
|
+
throw new Error('make sure you are inside of a telescope package!');
|
34
|
+
}
|
35
|
+
// what are we installing?
|
36
|
+
let { pkg } = await (0, prompt_1.prompt)([
|
37
|
+
{
|
38
|
+
type: 'checkbox',
|
39
|
+
name: 'pkg',
|
40
|
+
message: 'which chain contracts do you want to support?',
|
41
|
+
choices: [
|
42
|
+
'stargaze-base-factory',
|
43
|
+
'stargaze-base-minter',
|
44
|
+
'stargaze-sg721-base',
|
45
|
+
'stargaze-sg721-metdata-onchain',
|
46
|
+
'stargaze-sg721-nt',
|
47
|
+
'stargaze-splits',
|
48
|
+
'stargaze-vending-factory',
|
49
|
+
'stargaze-vending-minter',
|
50
|
+
'stargaze-whitelist',
|
51
|
+
'wasmswap'
|
52
|
+
].map(name => {
|
53
|
+
return {
|
54
|
+
name,
|
55
|
+
value: `@cosmjson/${name}`
|
56
|
+
};
|
57
|
+
})
|
58
|
+
}
|
59
|
+
], argv);
|
60
|
+
// install
|
61
|
+
if (!Array.isArray(pkg))
|
62
|
+
pkg = [pkg];
|
63
|
+
const tmp = (0, path_1.join)(TMPDIR, rnd());
|
64
|
+
(0, mkdirp_1.sync)(tmp);
|
65
|
+
process.chdir(tmp);
|
66
|
+
(0, shelljs_1.exec)(`npm install ${getPackages(pkg)} --production --prefix ./smart-contracts`);
|
67
|
+
// protos
|
68
|
+
const pkgs = (0, glob_1.sync)('./smart-contracts/**/package.json');
|
69
|
+
const cmds = pkgs
|
70
|
+
.filter((f) => { return f !== './smart-contracts/package.json'; })
|
71
|
+
.map((f) => (0, path_1.resolve)((0, path_1.join)(tmp, f)))
|
72
|
+
.map((conf) => {
|
73
|
+
const extDir = (0, path_1.dirname)(conf);
|
74
|
+
const dir = extDir.split('node_modules/')[1];
|
75
|
+
const dst = (0, path_1.basename)(dir);
|
76
|
+
const files = (0, glob_1.sync)(`${extDir}/**/*`, { nodir: true });
|
77
|
+
files.forEach(f => {
|
78
|
+
if ((0, path_1.extname)(f) === '.json'
|
79
|
+
|| f === 'package.json'
|
80
|
+
|| /license/i.test(f)
|
81
|
+
|| /readme/i.test(f))
|
82
|
+
return;
|
83
|
+
(0, rimraf_1.sync)(f);
|
84
|
+
});
|
85
|
+
return [extDir, (0, path_1.resolve)((0, path_1.join)(cur, 'contracts', dst)), dir];
|
86
|
+
});
|
87
|
+
// move protos
|
88
|
+
for (const [src, dst, pkg] of cmds) {
|
89
|
+
(0, rimraf_1.sync)(dst);
|
90
|
+
console.log(`installing ${pkg}...`);
|
91
|
+
(0, mkdirp_1.sync)((0, path_1.dirname)(dst));
|
92
|
+
(0, shelljs_1.exec)(`mv ${src} ${dst}`);
|
93
|
+
}
|
94
|
+
// package
|
95
|
+
const packageInfo = JSON.parse((0, fs_1.readFileSync)('./smart-contracts/package.json', 'utf-8'));
|
96
|
+
const deps = packageInfo.dependencies ?? {};
|
97
|
+
thisPackage.devDependencies = thisPackage.devDependencies ?? {};
|
98
|
+
thisPackage.devDependencies = {
|
99
|
+
...thisPackage.devDependencies,
|
100
|
+
...deps
|
101
|
+
};
|
102
|
+
thisPackage.devDependencies = Object.fromEntries(Object.entries(thisPackage.devDependencies).sort());
|
103
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(cur, 'package.json'), JSON.stringify(thisPackage, null, 2));
|
104
|
+
// cleanup
|
105
|
+
(0, rimraf_1.sync)(tmp);
|
106
|
+
process.chdir(cur);
|
107
|
+
};
|
@@ -1,18 +1,13 @@
|
|
1
|
-
import {
|
2
|
-
|
1
|
+
import { defaultOptions, BuilderContext } from "wasm-ast-types";
|
3
2
|
import { header } from '../utils/header';
|
4
3
|
import { join } from "path";
|
5
4
|
import { writeFileSync } from 'fs';
|
6
5
|
import { sync as mkdirp } from "mkdirp";
|
7
|
-
|
8
6
|
import { basename } from 'path';
|
9
7
|
import { readSchemas } from '../utils';
|
10
|
-
import { IBuilderPlugin } from '../plugins';
|
11
|
-
|
12
8
|
import deepmerge from 'deepmerge';
|
13
9
|
import { pascal } from "case";
|
14
10
|
import { createFileBundle, recursiveModuleBundle } from "../bundler";
|
15
|
-
|
16
11
|
import generate from '@babel/generator';
|
17
12
|
import * as t from '@babel/types';
|
18
13
|
import { ReactQueryPlugin } from "../plugins/react-query";
|
@@ -24,219 +19,124 @@ import { TypesPlugin } from "../plugins/types";
|
|
24
19
|
import { ContractsContextProviderPlugin } from "../plugins/provider";
|
25
20
|
import { createHelpers } from "../generators/create-helpers";
|
26
21
|
import { ContractsProviderBundlePlugin } from "../plugins/provider-bundle";
|
27
|
-
|
28
|
-
const defaultOpts: TSBuilderOptions = {
|
22
|
+
const defaultOpts = {
|
29
23
|
bundle: {
|
30
24
|
enabled: true,
|
31
25
|
scope: 'contracts',
|
32
26
|
bundleFile: 'bundle.ts'
|
33
27
|
},
|
34
28
|
useShorthandCtor: true
|
35
|
-
}
|
36
|
-
|
37
|
-
export interface TSBuilderInput {
|
38
|
-
contracts: Array<ContractFile | string>;
|
39
|
-
outPath: string;
|
40
|
-
options?: TSBuilderOptions;
|
41
|
-
plugins?: IBuilderPlugin[];
|
42
|
-
};
|
43
|
-
|
44
|
-
export interface BundleOptions {
|
45
|
-
enabled?: boolean;
|
46
|
-
scope?: string;
|
47
|
-
bundleFile?: string;
|
48
|
-
bundlePath?: string;
|
49
|
-
};
|
50
|
-
|
51
|
-
export interface UseContractsOptions {
|
52
|
-
enabled?: boolean;
|
53
29
|
};
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
* Default: true
|
60
|
-
*/
|
61
|
-
useShorthandCtor?: boolean;
|
62
|
-
useContractsHooks?: UseContractsOptions;
|
63
|
-
} & RenderOptions;
|
64
|
-
|
65
|
-
export type BuilderFileType = 'type' | 'client' | 'recoil' | 'react-query' | 'message-composer' | 'message-builder' | 'plugin';
|
66
|
-
|
67
|
-
export interface BuilderFile {
|
68
|
-
type: BuilderFileType;
|
69
|
-
pluginType?: string;
|
70
|
-
contract: string;
|
71
|
-
//filename only: Factory.client.ts
|
72
|
-
localname: string;
|
73
|
-
//full path: contracts/Factory.client.ts
|
74
|
-
filename: string;
|
75
|
-
};
|
76
|
-
|
77
|
-
export interface ContractFile {
|
78
|
-
name: string;
|
79
|
-
dir: string;
|
80
|
-
}
|
81
|
-
|
82
|
-
function getContract(contractOpt): ContractFile {
|
30
|
+
;
|
31
|
+
;
|
32
|
+
;
|
33
|
+
;
|
34
|
+
function getContract(contractOpt) {
|
83
35
|
if (typeof contractOpt === 'string') {
|
84
36
|
const name = basename(contractOpt);
|
85
37
|
const contractName = pascal(name);
|
86
38
|
return {
|
87
39
|
name: contractName,
|
88
40
|
dir: contractOpt
|
89
|
-
}
|
41
|
+
};
|
90
42
|
}
|
91
43
|
return {
|
92
44
|
name: pascal(contractOpt.name),
|
93
45
|
dir: contractOpt.dir
|
94
46
|
};
|
95
47
|
}
|
96
|
-
|
97
48
|
export class TSBuilder {
|
98
|
-
contracts
|
99
|
-
outPath
|
100
|
-
options
|
101
|
-
plugins
|
102
|
-
builderContext
|
103
|
-
|
104
|
-
protected files: BuilderFile[] = [];
|
105
|
-
|
49
|
+
contracts;
|
50
|
+
outPath;
|
51
|
+
options;
|
52
|
+
plugins = [];
|
53
|
+
builderContext = new BuilderContext();
|
54
|
+
files = [];
|
106
55
|
loadDefaultPlugins() {
|
107
|
-
|
108
|
-
new TypesPlugin(this.options),
|
109
|
-
new ClientPlugin(this.options),
|
110
|
-
new MessageComposerPlugin(this.options),
|
111
|
-
new ReactQueryPlugin(this.options),
|
112
|
-
new RecoilPlugin(this.options),
|
113
|
-
new MessageBuilderPlugin(this.options),
|
114
|
-
new ContractsContextProviderPlugin(this.options),
|
115
|
-
]);
|
56
|
+
this.plugins.push(new TypesPlugin(this.options), new ClientPlugin(this.options), new MessageComposerPlugin(this.options), new ReactQueryPlugin(this.options), new RecoilPlugin(this.options), new MessageBuilderPlugin(this.options), new ContractsContextProviderPlugin(this.options));
|
116
57
|
}
|
117
|
-
|
118
|
-
constructor({ contracts, outPath, options, plugins }: TSBuilderInput) {
|
58
|
+
constructor({ contracts, outPath, options, plugins }) {
|
119
59
|
this.contracts = contracts;
|
120
60
|
this.outPath = outPath;
|
121
|
-
this.options = deepmerge(
|
122
|
-
deepmerge(
|
123
|
-
defaultOptions,
|
124
|
-
defaultOpts
|
125
|
-
),
|
126
|
-
options ?? {}
|
127
|
-
);
|
128
|
-
|
61
|
+
this.options = deepmerge(deepmerge(defaultOptions, defaultOpts), options ?? {});
|
129
62
|
this.loadDefaultPlugins();
|
130
|
-
|
131
63
|
if (plugins && plugins.length) {
|
132
|
-
|
64
|
+
this.plugins.push(...plugins);
|
133
65
|
}
|
134
|
-
|
135
|
-
this.plugins.forEach(plugin => plugin.setBuilder(this))
|
66
|
+
this.plugins.forEach(plugin => plugin.setBuilder(this));
|
136
67
|
}
|
137
|
-
|
138
68
|
async build() {
|
139
69
|
await this.process();
|
140
70
|
await this.after();
|
141
71
|
}
|
142
|
-
|
143
72
|
// lifecycle functions
|
144
|
-
|
73
|
+
async process() {
|
145
74
|
for (const contractOpt of this.contracts) {
|
146
75
|
const contract = getContract(contractOpt);
|
147
76
|
//resolve contract schema.
|
148
77
|
const contractInfo = await readSchemas({
|
149
78
|
schemaDir: contract.dir
|
150
79
|
});
|
151
|
-
|
152
80
|
//lifecycle and plugins.
|
153
81
|
await this.render(contract.name, contractInfo);
|
154
82
|
}
|
155
83
|
}
|
156
|
-
|
157
|
-
private async render(name: string, contractInfo: ContractInfo) {
|
84
|
+
async render(name, contractInfo) {
|
158
85
|
for (const plugin of this.plugins) {
|
159
86
|
let files = await plugin.render(name, contractInfo, this.outPath);
|
160
87
|
if (files && files.length) {
|
161
|
-
|
88
|
+
this.files.push(...files);
|
162
89
|
}
|
163
90
|
}
|
164
91
|
}
|
165
|
-
|
166
|
-
private async after() {
|
167
|
-
|
92
|
+
async after() {
|
168
93
|
//create useContracts bundle file
|
169
94
|
const contractsProviderBundlePlugin = new ContractsProviderBundlePlugin(this.options);
|
170
95
|
contractsProviderBundlePlugin.setBuilder(this);
|
171
|
-
|
172
96
|
//contractContextProviders.ts
|
173
|
-
const files = await contractsProviderBundlePlugin.render(
|
174
|
-
|
175
|
-
|
176
|
-
schemas: [],
|
177
|
-
},
|
178
|
-
this.outPath
|
179
|
-
);
|
180
|
-
|
97
|
+
const files = await contractsProviderBundlePlugin.render("contractContextProviders", {
|
98
|
+
schemas: [],
|
99
|
+
}, this.outPath);
|
181
100
|
if (files && files.length) {
|
182
|
-
|
101
|
+
this.files.push(...files);
|
183
102
|
}
|
184
|
-
|
185
103
|
const helpers = createHelpers({
|
186
104
|
outPath: this.outPath,
|
187
105
|
contracts: this.contracts,
|
188
106
|
options: this.options,
|
189
107
|
plugins: this.plugins,
|
190
108
|
}, this.builderContext);
|
191
|
-
|
192
109
|
if (helpers && helpers.length) {
|
193
|
-
|
110
|
+
this.files.push(...helpers);
|
194
111
|
}
|
195
|
-
|
196
112
|
if (this.options.bundle.enabled) {
|
197
113
|
this.bundle();
|
198
114
|
}
|
199
115
|
}
|
200
|
-
|
201
116
|
async bundle() {
|
202
117
|
const allFiles = this.files;
|
203
|
-
|
204
118
|
const bundleFile = this.options.bundle.bundleFile;
|
205
|
-
const bundlePath = join(
|
206
|
-
this.options?.bundle?.bundlePath ?? this.outPath,
|
207
|
-
bundleFile
|
208
|
-
);
|
119
|
+
const bundlePath = join(this.options?.bundle?.bundlePath ?? this.outPath, bundleFile);
|
209
120
|
const bundleVariables = {};
|
210
121
|
const importPaths = [];
|
211
|
-
|
212
122
|
allFiles.forEach(file => {
|
213
|
-
createFileBundle(
|
214
|
-
`${this.options.bundle.scope}.${file.contract}`,
|
215
|
-
file.filename,
|
216
|
-
bundlePath,
|
217
|
-
importPaths,
|
218
|
-
bundleVariables
|
219
|
-
);
|
220
|
-
|
123
|
+
createFileBundle(`${this.options.bundle.scope}.${file.contract}`, file.filename, bundlePath, importPaths, bundleVariables);
|
221
124
|
});
|
222
|
-
|
223
125
|
const ast = recursiveModuleBundle(bundleVariables);
|
126
|
+
const nodes = [
|
127
|
+
...importPaths,
|
128
|
+
...ast
|
129
|
+
];
|
130
|
+
// @ts-ignore
|
224
131
|
let code = generate(t.program(
|
225
|
-
|
226
|
-
|
227
|
-
...ast
|
228
|
-
]
|
229
|
-
)).code;
|
230
|
-
|
132
|
+
// @ts-ignore
|
133
|
+
nodes)).code;
|
231
134
|
if (this.options?.bundle?.bundlePath) {
|
232
135
|
mkdirp(this.options?.bundle?.bundlePath);
|
233
136
|
}
|
234
|
-
|
235
137
|
mkdirp(this.outPath);
|
236
|
-
|
237
|
-
|
238
|
-
|
138
|
+
if (code.trim() === '')
|
139
|
+
code = 'export {};';
|
239
140
|
writeFileSync(bundlePath, header + code);
|
240
|
-
|
241
141
|
}
|
242
142
|
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './builder';
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import nested from 'nested-obj';
|
3
|
+
import { relative, dirname, extname } from 'path';
|
4
|
+
export const recursiveModuleBundle = (obj) => {
|
5
|
+
return Object.keys(obj).map(key => {
|
6
|
+
const value = obj[key];
|
7
|
+
if (typeof value === 'object' && value && value.__export) {
|
8
|
+
// e.g. abci
|
9
|
+
// 1. create variable for abci
|
10
|
+
// 2. splat ALL _0, parms into abci
|
11
|
+
// 3. export that variable
|
12
|
+
const nmspc = t.variableDeclaration('const', [t.variableDeclarator(t.identifier(key), t.objectExpression(Object.keys(obj[key])
|
13
|
+
.filter(a => a !== '__export')
|
14
|
+
.filter(a => a.startsWith('_'))
|
15
|
+
.map(a => t.spreadElement(t.identifier(a)))))]);
|
16
|
+
const others = Object.keys(obj[key])
|
17
|
+
.filter(a => a !== '__export')
|
18
|
+
.filter(a => !a.startsWith('_'));
|
19
|
+
if (others.length) {
|
20
|
+
throw new Error('namespace and package not supported, yet.');
|
21
|
+
}
|
22
|
+
// return nmspc;
|
23
|
+
return t.exportNamedDeclaration(nmspc, []);
|
24
|
+
}
|
25
|
+
else if (typeof value === 'object' && value) {
|
26
|
+
// you can make a namespace for obj[key]
|
27
|
+
// e.g. libs
|
28
|
+
return t.exportNamedDeclaration(t.tsModuleDeclaration(t.identifier(key), t.tsModuleBlock(recursiveModuleBundle(obj[key]))));
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
throw new Error('Invalid structure for BundleData');
|
32
|
+
}
|
33
|
+
});
|
34
|
+
};
|
35
|
+
export const importNamespace = (ident, path) => t.importDeclaration([
|
36
|
+
t.importNamespaceSpecifier(t.identifier(ident))
|
37
|
+
], t.stringLiteral(path.replace(extname(path), '')));
|
38
|
+
let counter = 0;
|
39
|
+
export const createFileBundle = (pkg, filename, bundleFile, importPaths, bundleVariables) => {
|
40
|
+
let rel = relative(dirname(bundleFile), filename);
|
41
|
+
if (!rel.startsWith('.'))
|
42
|
+
rel = `./${rel}`;
|
43
|
+
const variable = `_${counter++}`;
|
44
|
+
importPaths.push(importNamespace(variable, rel));
|
45
|
+
nested.set(bundleVariables, pkg + '.__export', true);
|
46
|
+
nested.set(bundleVariables, pkg + '.' + variable, true);
|
47
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './bundler';
|
package/esm/cli.js
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
import { prompt } from './utils/prompt';
|
2
|
+
import { Commands as commands } from './cmds';
|
3
|
+
const question = [
|
4
|
+
{
|
5
|
+
_: true,
|
6
|
+
type: 'fuzzy',
|
7
|
+
name: 'cmd',
|
8
|
+
message: 'what do you want to do?',
|
9
|
+
choices: Object.keys(commands)
|
10
|
+
}
|
11
|
+
];
|
12
|
+
export const cli = async (argv) => {
|
13
|
+
var { cmd } = await prompt(question, argv);
|
14
|
+
if (typeof commands[cmd] === 'function') {
|
15
|
+
await commands[cmd](argv);
|
16
|
+
}
|
17
|
+
else {
|
18
|
+
console.log('command not found.');
|
19
|
+
}
|
20
|
+
};
|
package/{src → esm}/cmds.js
RENAMED
@@ -4,14 +4,12 @@ import dargs from 'dargs';
|
|
4
4
|
const glob = require('glob').sync;
|
5
5
|
const fs = require('fs');
|
6
6
|
const path = require('path');
|
7
|
-
|
8
7
|
const repo = 'https://github.com/cosmology-tech/ts-codegen-module-boilerplate';
|
9
|
-
export default async argv => {
|
8
|
+
export default async (argv) => {
|
10
9
|
if (!shell.which('git')) {
|
11
10
|
shell.echo('Sorry, this script requires git');
|
12
11
|
return shell.exit(1);
|
13
12
|
}
|
14
|
-
|
15
13
|
const { name } = await prompt([
|
16
14
|
{
|
17
15
|
type: 'string',
|
@@ -19,115 +17,82 @@ export default async argv => {
|
|
19
17
|
message: 'Enter your new module name',
|
20
18
|
}
|
21
19
|
], argv);
|
22
|
-
|
23
20
|
shell.exec(`git clone ${repo} ${name}`);
|
24
21
|
shell.cd(name);
|
25
|
-
|
26
22
|
const questions = JSON.parse(fs.readFileSync(`.questions.json`));
|
27
|
-
|
28
23
|
const fullname = shell
|
29
24
|
.exec('git config --global user.name', { silent: true })
|
30
25
|
.trim();
|
31
26
|
const email = shell
|
32
27
|
.exec('git config --global user.email', { silent: true })
|
33
28
|
.trim();
|
34
|
-
|
35
29
|
// @__USERNAME__/__MODULENAME__
|
36
30
|
// __PACKAGE_IDENTIFIER__
|
37
|
-
const args = dargs(
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
},
|
45
|
-
{ allowCamelCase: true }
|
46
|
-
);
|
47
|
-
|
31
|
+
const args = dargs({
|
32
|
+
_: [],
|
33
|
+
...argv,
|
34
|
+
__MODULENAME__: name,
|
35
|
+
__USERFULLNAME__: fullname,
|
36
|
+
__USEREMAIL__: email,
|
37
|
+
}, { allowCamelCase: true });
|
48
38
|
const results = await prompt(questions, args);
|
49
39
|
let scopedResults;
|
50
|
-
|
51
|
-
|
52
|
-
|
40
|
+
const license = await prompt([
|
41
|
+
{
|
42
|
+
name: '__LICENSE__',
|
43
|
+
message: 'Which license?',
|
44
|
+
choices: ['MIT', 'closed'],
|
45
|
+
type: 'list',
|
46
|
+
required: true,
|
47
|
+
},
|
48
|
+
], []);
|
49
|
+
if (results.__ACCESS__ === 'public') {
|
50
|
+
scopedResults = await prompt([
|
53
51
|
{
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
type: 'list',
|
52
|
+
type: 'confirm',
|
53
|
+
name: 'scoped',
|
54
|
+
message: 'use npm scopes?',
|
58
55
|
required: true,
|
59
56
|
},
|
60
|
-
],
|
61
|
-
[]
|
62
|
-
);
|
63
|
-
|
64
|
-
if (results.__ACCESS__ === 'public') {
|
65
|
-
scopedResults = await prompt(
|
66
|
-
[
|
67
|
-
{
|
68
|
-
type: 'confirm',
|
69
|
-
name: 'scoped',
|
70
|
-
message: 'use npm scopes?',
|
71
|
-
required: true,
|
72
|
-
},
|
73
|
-
],
|
74
|
-
[]
|
75
|
-
);
|
57
|
+
], []);
|
76
58
|
}
|
77
|
-
|
78
59
|
const files = []
|
79
60
|
.concat(glob(process.cwd() + '/**/.*'))
|
80
61
|
.concat(glob(process.cwd() + '/**/*'));
|
81
|
-
|
82
62
|
for (let i = 0; i < files.length; i++) {
|
83
63
|
const templateFile = files[i];
|
84
|
-
if (fs.lstatSync(templateFile).isDirectory())
|
85
|
-
|
64
|
+
if (fs.lstatSync(templateFile).isDirectory())
|
65
|
+
continue;
|
86
66
|
let content = fs.readFileSync(templateFile).toString();
|
87
|
-
if (
|
88
|
-
|
89
|
-
license.__LICENSE__ === 'closed'
|
90
|
-
) {
|
67
|
+
if (path.basename(templateFile) === 'LICENSE' &&
|
68
|
+
license.__LICENSE__ === 'closed') {
|
91
69
|
content = `Copyright (c) 2023 __USERFULLNAME__ <__USEREMAIL__> - All Rights Reserved
|
92
70
|
Unauthorized copying via any medium is strictly prohibited
|
93
71
|
Proprietary and confidential`;
|
94
72
|
}
|
95
|
-
|
96
73
|
Object.keys(results).forEach(key => {
|
97
74
|
if (/^__/.test(key)) {
|
98
75
|
content = content.replace(new RegExp(key, 'g'), results[key]);
|
99
76
|
}
|
100
77
|
});
|
101
|
-
|
102
78
|
if (results.__ACCESS__ === 'public') {
|
103
79
|
if (scopedResults.scoped) {
|
104
|
-
content = content.replace(
|
105
|
-
|
106
|
-
|
107
|
-
);
|
108
|
-
} else {
|
109
|
-
content = content.replace(
|
110
|
-
/__PACKAGE_IDENTIFIER__/g,
|
111
|
-
`${results.__MODULENAME__}`
|
112
|
-
);
|
80
|
+
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `@${results.__USERNAME__}/${results.__MODULENAME__}`);
|
81
|
+
}
|
82
|
+
else {
|
83
|
+
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `${results.__MODULENAME__}`);
|
113
84
|
}
|
114
|
-
} else {
|
115
|
-
content = content.replace(
|
116
|
-
/__PACKAGE_IDENTIFIER__/g,
|
117
|
-
`@${results.__USERNAME__}/${results.__MODULENAME__}`
|
118
|
-
);
|
119
85
|
}
|
120
|
-
|
86
|
+
else {
|
87
|
+
content = content.replace(/__PACKAGE_IDENTIFIER__/g, `@${results.__USERNAME__}/${results.__MODULENAME__}`);
|
88
|
+
}
|
121
89
|
// if (path.basename(templateFile) === 'README.md') {
|
122
90
|
// content = `# ${results.__MODULENAME__}`;
|
123
91
|
// }
|
124
|
-
|
125
92
|
fs.writeFileSync(templateFile, content);
|
126
93
|
}
|
127
|
-
|
128
94
|
shell.rm('-rf', '.git');
|
129
95
|
shell.rm('-rf', '.questions.json');
|
130
|
-
|
131
96
|
console.log(`
|
132
97
|
|
133
98
|
|||
|
@@ -136,4 +101,4 @@ Proprietary and confidential`;
|
|
136
101
|
|
137
102
|
✨ Great work!
|
138
103
|
`);
|
139
|
-
};
|
104
|
+
};
|