UNPKG

@ton-actions/tondev-contest

Version:

TON Dev Environment

138 lines 5.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.jsWrapCommand = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const core_1 = require("@tonclient/core"); var ExportFormat; (function (ExportFormat) { ExportFormat["CommonJs"] = "commonjs"; ExportFormat["CommonJsDefault"] = "commonjs-default"; ExportFormat["Es6"] = "es6"; ExportFormat["Es6Default"] = "es6-default"; })(ExportFormat || (ExportFormat = {})); function getExportSection(exportFormat, name) { switch (exportFormat) { case ExportFormat.CommonJs: return `module.exports = { ${name} };`; case ExportFormat.CommonJsDefault: return `module.exports = ${name};`; case ExportFormat.Es6: return `export ${name};`; case ExportFormat.Es6Default: return `export default ${name};`; } throw new Error(`Invalid JS export mode ${exportFormat}`); } exports.jsWrapCommand = { name: "wrap", alias: "w", title: "Wrap ABI file into JavaScript code.", args: [ { isArg: true, name: "file", type: "file", title: "ABI file", nameRegExp: /\.abi\.json$/i, }, { name: "print", alias: "p", type: "boolean", title: "Print code to console", defaultValue: "false", }, { name: "output", alias: "o", type: "string", title: "Set output file name (default is built from source ABI file name)", defaultValue: "", }, { name: "export", alias: "e", type: "string", title: "Export type and options", getVariants() { return [ { value: "commonjs", description: "Use CommonJS modules (NodeJs)", }, { value: "commonjs-default", description: "Use CommonJS modules (NodeJS) with default export", }, { value: "es6", description: "Use ES6 modules", }, { value: "es6-default", description: "Use ES6 modules with default export", }, ]; }, defaultValue: ExportFormat.CommonJs, }, ], async run(terminal, args) { const abiPath = path.resolve(process.cwd(), args.file); const name = path.basename(abiPath).slice(0, -".abi.json".length); const abi = JSON.parse(fs.readFileSync(abiPath, "utf8")); const contractName = `${name.substr(0, 1).toUpperCase()}${name.substr(1)}Contract`; const code = [`const ${contractName} = {`]; const abiCode = JSON .stringify(abi, undefined, " ") .split("\r\n") .join("\n") .split("\n") .map((x, i) => i > 0 ? ` ${x}` : x) .join("\n"); code.push(` abi: ${abiCode},`); const tvcPath = path.resolve(path.dirname(abiPath), `${name}.tvc`); terminal.log(tvcPath); if (fs.existsSync(tvcPath)) { const tvc = fs.readFileSync(tvcPath).toString("base64"); code.push(` tvc: "${tvc}",`); const client = new core_1.TonClient(); const tvcCode = (await client.boc.get_code_from_tvc({ tvc })).code; code.push(` code: "${tvcCode}",`); code.push(` codeHash: "${(await client.boc.get_boc_hash({ boc: tvcCode })).hash}",`); await client.close(); } code.push("};"); code.push(getExportSection(args.export.toLowerCase(), contractName)); const wrapperCode = code.join("\n"); if (args.print) { terminal.log(wrapperCode); } else { const wrapperPath = path.resolve(path.dirname(abiPath), args.output !== "" ? args.output : `${contractName}.js`); fs.writeFileSync(wrapperPath, wrapperCode); terminal.log(`Generated wrapper code written to: ${wrapperPath}`); } }, }; //# sourceMappingURL=wrap.js.map