@taqueria/plugin-contract-types 0.3.0 → 0.4.0-rc2
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 +125 -0
- package/_readme.eta +128 -0
- package/example/example-usage.ts +2 -2
- package/index.js +124 -152
- package/index.js.map +1 -1
- package/index.ts +32 -33
- package/package.json +53 -53
- package/run.ts +2 -2
- package/src/cli-process.ts +102 -95
- package/src/cli.ts +30 -19
- package/src/generator/common.ts +8 -9
- package/src/generator/contract-name.ts +7 -6
- package/src/generator/contract-parser.ts +324 -320
- package/src/generator/process.ts +71 -47
- package/src/generator/schema-output.ts +48 -47
- package/src/generator/typescript-output.ts +238 -205
- package/src/taquito-contract-type-generator.ts +3 -3
- package/src/type-aliases-file-content.ts +5 -1
- package/src/type-aliases.ts +40 -36
- package/src/type-utils-file-content.ts +1 -1
- package/src/type-utils.ts +23 -25
- package/tasks.ts +48 -52
- package/test/generator.spec.ts +61 -51
- package/tsconfig.json +12 -12
- package/Readme.md +0 -212
package/src/cli-process.ts
CHANGED
|
@@ -8,111 +8,118 @@ import { typeAliasesFileContent } from './type-aliases-file-content';
|
|
|
8
8
|
import { typeUtilsFileContent } from './type-utils-file-content';
|
|
9
9
|
|
|
10
10
|
const fs = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
mkdir: promisify(fsRaw.mkdir),
|
|
12
|
+
copyFile: promisify(fsRaw.copyFile),
|
|
13
|
+
readdir: promisify(fsRaw.readdir),
|
|
14
|
+
readFile: promisify(fsRaw.readFile),
|
|
15
|
+
writeFile: promisify(fsRaw.writeFile),
|
|
16
|
+
stat: promisify(fsRaw.stat),
|
|
17
|
+
exists: fsRaw.existsSync,
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
const getAllFiles = async (rootPath: string, filter: (fullPath: string) => boolean): Promise<string[]> => {
|
|
21
|
-
|
|
21
|
+
const allFiles = [] as string[];
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
const getAllFilesRecursive = async (dirPath: string) => {
|
|
24
|
+
let files = await fs.readdir(dirPath, { withFileTypes: true });
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
for (const f of files) {
|
|
27
|
+
const subPath = path.resolve(dirPath, f.name);
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
if (f.isDirectory()) {
|
|
30
|
+
await getAllFilesRecursive(subPath);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
if (!filter(subPath)) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
allFiles.push(subPath);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
42
|
+
await getAllFilesRecursive(rootPath);
|
|
43
|
+
return allFiles;
|
|
44
|
+
};
|
|
45
45
|
|
|
46
46
|
export const generateContractTypesProcessContractFiles = async ({
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
inputTzContractDirectory,
|
|
48
|
+
inputFiles,
|
|
49
|
+
outputTypescriptDirectory,
|
|
50
|
+
format,
|
|
51
|
+
typeAliasMode,
|
|
52
52
|
}: {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
inputTzContractDirectory: string;
|
|
54
|
+
inputFiles?: string[];
|
|
55
|
+
outputTypescriptDirectory: string;
|
|
56
|
+
format: 'tz' | 'json';
|
|
57
|
+
typeAliasMode: 'local' | 'file' | 'library' | 'simple';
|
|
58
58
|
}): Promise<void> => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
59
|
+
console.log(
|
|
60
|
+
`Generating Types: ${path.resolve(inputTzContractDirectory)} => ${path.resolve(outputTypescriptDirectory)}`,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const ext = '.' + format;
|
|
64
|
+
const filesAll = await getAllFiles(inputTzContractDirectory, x => x.endsWith(ext));
|
|
65
|
+
const files = inputFiles ? filesAll.filter(f => inputFiles.some(inputFile => f.endsWith(inputFile))) : filesAll;
|
|
66
|
+
|
|
67
|
+
console.log(`Contracts Found: ${[``, ...files].join(`\n\t- `)}`);
|
|
68
|
+
|
|
69
|
+
const typeAliasImportPath = `@taquito/contract-type-generator`;
|
|
70
|
+
|
|
71
|
+
const typeAliasData: TypeAliasData = typeAliasMode === 'local'
|
|
72
|
+
? { mode: typeAliasMode, fileContent: typeAliasesFileContent }
|
|
73
|
+
: typeAliasMode === 'file'
|
|
74
|
+
? { mode: typeAliasMode, importPath: `./type-aliases` }
|
|
75
|
+
: typeAliasMode === 'library'
|
|
76
|
+
? { mode: typeAliasMode, importPath: typeAliasImportPath }
|
|
77
|
+
: { mode: 'simple' };
|
|
78
|
+
|
|
79
|
+
if (typeAliasMode === 'file') {
|
|
80
|
+
// Copy the type alias file
|
|
81
|
+
await fs.mkdir(outputTypescriptDirectory, { recursive: true });
|
|
82
|
+
await fs.writeFile(path.join(outputTypescriptDirectory, './type-aliases.ts'), typeAliasesFileContent);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Copy the type utils file
|
|
86
|
+
const typeUtilsData: TypeUtilsData = { importPath: `./type-utils` };
|
|
87
|
+
await fs.mkdir(outputTypescriptDirectory, { recursive: true });
|
|
88
|
+
await fs.writeFile(path.join(outputTypescriptDirectory, './type-utils.ts'), typeUtilsFileContent);
|
|
89
|
+
|
|
90
|
+
for (const fullPath of files) {
|
|
91
|
+
const fileRelativePath = fullPath.replace(path.resolve(inputTzContractDirectory), '');
|
|
92
|
+
const fileName = fileRelativePath.replace(ext, '');
|
|
93
|
+
const inputFilePath = path.join(inputTzContractDirectory, fileRelativePath);
|
|
94
|
+
const typesOutputFilePath = path.join(outputTypescriptDirectory, fileRelativePath.replace(ext, `.types.ts`));
|
|
95
|
+
const codeContentOutputFilePath = path.join(outputTypescriptDirectory, fileRelativePath.replace(ext, `.code.ts`));
|
|
96
|
+
const schemaContentOutputFilePath = path.join(
|
|
97
|
+
outputTypescriptDirectory,
|
|
98
|
+
fileRelativePath.replace(ext, `.schema.json`),
|
|
99
|
+
);
|
|
100
|
+
console.log(`Processing ${fileRelativePath}...`);
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const contractTypeName = normalizeContractName(fileName);
|
|
104
|
+
|
|
105
|
+
const michelsonCode = await fs.readFile(inputFilePath, { encoding: `utf8` });
|
|
106
|
+
|
|
107
|
+
const {
|
|
108
|
+
schemaOutput,
|
|
109
|
+
typescriptCodeOutput: { typesFileContent, contractCodeFileContent },
|
|
110
|
+
} = generateContractTypesFromMichelsonCode(michelsonCode, contractTypeName, format, typeAliasData, typeUtilsData);
|
|
111
|
+
|
|
112
|
+
// Write output (ensure dir exists)
|
|
113
|
+
await fs.mkdir(path.dirname(typesOutputFilePath), { recursive: true });
|
|
114
|
+
await fs.writeFile(typesOutputFilePath, typesFileContent);
|
|
115
|
+
await fs.writeFile(codeContentOutputFilePath, contractCodeFileContent);
|
|
116
|
+
|
|
117
|
+
const debugSchema = false;
|
|
118
|
+
if (debugSchema) {
|
|
119
|
+
await fs.writeFile(schemaContentOutputFilePath, JSON.stringify(schemaOutput, null, 2));
|
|
120
|
+
}
|
|
121
|
+
} catch (err: unknown) {
|
|
122
|
+
console.error(`❌ Could not process ${fileRelativePath}`, { err });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
package/src/cli.ts
CHANGED
|
@@ -1,29 +1,40 @@
|
|
|
1
1
|
import { generateContractTypesProcessContractFiles } from './cli-process';
|
|
2
2
|
|
|
3
3
|
export const run = async (): Promise<void> => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const argv = process.argv;
|
|
5
|
+
const argsGenerateFile = argv.some(a => a.startsWith(`--g`))
|
|
6
|
+
? argv.slice(argv.findIndex(a => a.startsWith(`--g`)) + 1)
|
|
7
|
+
: undefined;
|
|
8
|
+
const argsUseJson = argv.some(a => a.startsWith(`--json`)) ? true : false;
|
|
9
|
+
const argsTypeAliasMode = argv.some(a => a.startsWith(`--types`))
|
|
10
|
+
? argv.slice(argv.findIndex(a => a.startsWith(`--types`)) + 1)
|
|
11
|
+
: undefined;
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
console.log(`contract-type-generator\n\t${argv.join(`\n\t`)}`);
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
if (argsGenerateFile) {
|
|
16
|
+
const [inputTzContractDirectory, outputTypescriptDirectory] = argsGenerateFile;
|
|
17
|
+
const format = argsUseJson ? 'json' : 'tz';
|
|
18
|
+
const [typeAliasModeArg] = argsTypeAliasMode ?? [];
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
// Library is default mode
|
|
21
|
+
const typeAliasMode = typeAliasModeArg === 'local'
|
|
22
|
+
? 'local'
|
|
23
|
+
: typeAliasModeArg === 'file'
|
|
24
|
+
? 'file'
|
|
25
|
+
: typeAliasModeArg === 'simple'
|
|
26
|
+
? 'simple'
|
|
27
|
+
: 'library';
|
|
28
|
+
await generateContractTypesProcessContractFiles({
|
|
29
|
+
inputTzContractDirectory,
|
|
30
|
+
outputTypescriptDirectory,
|
|
31
|
+
format,
|
|
32
|
+
typeAliasMode,
|
|
33
|
+
});
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
25
36
|
|
|
26
|
-
|
|
37
|
+
console.log(`
|
|
27
38
|
contract-type-generator
|
|
28
39
|
|
|
29
40
|
Example usages:
|
package/src/generator/common.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
|
|
2
1
|
export class GenerateApiError implements Error {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
name = `GenerateApiError`;
|
|
3
|
+
constructor(public message: string, readonly data: unknown) {
|
|
4
|
+
console.error(`❌ GenerateApiError: ${message}`, data);
|
|
5
|
+
}
|
|
7
6
|
}
|
|
8
7
|
|
|
9
8
|
export const assertExhaustive = (value: never, message: string): void => {
|
|
10
|
-
|
|
9
|
+
console.error(message, { value });
|
|
11
10
|
};
|
|
12
11
|
|
|
13
12
|
export const reduceFlatMap = <T>(out: T[], x: T[]): T[] => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
13
|
+
out.push(...x);
|
|
14
|
+
return out;
|
|
15
|
+
};
|
|
17
16
|
|
|
18
17
|
// const reduceFlatMapTest = () => {
|
|
19
18
|
// const items = [['a'], ['b']];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export const normalizeContractName = (text: string) =>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export const normalizeContractName = (text: string) =>
|
|
2
|
+
text
|
|
3
|
+
.replace(/[^A-Za-z0-9]/g, '_')
|
|
4
|
+
.split('_')
|
|
5
|
+
.filter(x => x)
|
|
6
|
+
.map(x => x[0].toUpperCase() + x.substring(1))
|
|
7
|
+
.join('');
|