This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
forked from EOSIO/eosio-toppings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
124 lines (99 loc) · 3.83 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
119
120
121
122
123
124
const path = require('path');
const stripAnsi = require('strip-ansi');
const fileManip = require('file');
const fs = require('fs');
const resolveHomePath = (filepath) => {
if (filepath[0] === '~') {
// '~' is bash only, therefore we must detect it in the file path and replace it with process.env.HOME
return path.join(process.env.HOME, filepath.slice(1));
} else if (!path.isAbsolute(filepath) && filepath[0] !== '/') {
// We expect absolute file path so we prepend "/" if it doesn't already exist.
return "/"+filepath;
}
// Return filepath as is if there are no issues
return filepath;
}
const getStringDiff = (sourcePath, path) => path.split(sourcePath).join('').substring(1);
const abiIsEmpty = (abi) => {
const abiProps = [
'structs', 'types', 'actions',
'tables', 'ricardian_clauses', 'variants',
'abi_extensions'
];
let jsonFormattedAbi = JSON.parse(abi);
let isEmptyAbi = true;
abiProps.forEach(prop => {
let coll = jsonFormattedAbi[prop];
if (coll && coll.length > 0) isEmptyAbi = false;
});
return isEmptyAbi;
}
const fetchDeployableFilesFromDirectory = (directory) => {
let response = {
wasmPath: "",
abiPath: "",
programErrors: [],
}
try {
const directoryContents = fs.readdirSync(directory);
const wasmFileName = directoryContents.find(filePath => filePath.match(/.*\.(wasm)$/gi));
const abiFileName = directoryContents.find(filePath => filePath.match(/.*\.(abi)$/gi));
response["wasmPath"] = (!wasmFileName) ? "" : path.join(directory, wasmFileName);
response["abiPath"] = (!abiFileName) ? "" : path.join(directory, abiFileName);
if (!wasmFileName)
response["programErrors"].push(`Cannot find '.wasm' file in ${directory}`);
if (!abiFileName)
response["programErrors"].push(`Cannot find '.abi' file in ${directory}`);
else {
if (abiIsEmpty(fs.readFileSync(path.join(directory, abiFileName)), 'utf-8')) {
response["programErrors"].push(`Warning: Unfortunately, the contract structure is too complex for --abigen to generate correct ABI file`);
response["programErrors"].push(`Warning: That's normal for complex projects. ABI should be created manually. Please refer to https://developers.eos.io/eosio-home/docs/the-abi`);
} else {
response["abiContents"] = fs.readFileSync(path.join(directory, abiFileName), "utf-8");
}
}
return response;
} catch (ex) {
throw new Error(ex.message);
}
}
const filterHeaderFiles = (fileName) => (fileName.includes('.hpp'));
const parseDirectoriesToInclude = (sourcePath) => {
let directories = [];
let i = 0;
try {
fileManip.walkSync(sourcePath, (ds, dirPath, dirs, files) => {
const sourceFiles = (dirs) ? dirs.filter(filterHeaderFiles) : [];
if (sourceFiles.length > 0 && sourcePath !== ds) {
let fileDiff = getStringDiff(sourcePath, ds);
if (fileDiff[0] == '/') fileDiff = fileDiff.substring(1);
directories.push(
"/opt/eosio/bin/contracts/"+fileDiff
);
}
});
return directories;
} catch (ex) {
throw new Error(ex.message);
}
}
const parseLog = (logContent) => {
let strippedContent = stripAnsi(logContent);
let output = strippedContent.split('\n');
output.pop();
return output;
}
const getFile = (filePath) => {
if(!fs.existsSync(filePath)) {
fs.closeSync(fs.openSync(filePath, 'a'));
}
return fs.readFileSync(filePath, 'utf-8');
}
module.exports = {
resolveHomePath: resolveHomePath,
getStringDiff: getStringDiff,
fetchDeployableFilesFromDirectory: fetchDeployableFilesFromDirectory,
parseDirectoriesToInclude: parseDirectoriesToInclude,
parseLog: parseLog,
getFile: getFile
}