tondev
Version:
TON Dev Environment
218 lines • 8.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Solidity = exports.soliditySetCommand = exports.solidityUpdateCommand = exports.solidityAstCommand = exports.solidityCompileCommand = exports.solidityCreateCommand = exports.solidityVersionCommand = void 0;
const core_1 = require("../../core");
const path_1 = __importDefault(require("path"));
const utils_1 = require("../../core/utils");
const fs_1 = __importDefault(require("fs"));
const mv_1 = __importDefault(require("mv"));
const snippets_1 = require("./snippets");
const components_1 = require("./components");
exports.solidityVersionCommand = {
name: "version",
title: "Show Solidity Version",
async run(terminal, _args) {
terminal.log(await core_1.Component.getInfoAll(components_1.components));
},
};
exports.solidityCreateCommand = {
name: "create",
title: "Create Solidity Contract",
args: [{
isArg: true,
name: "name",
title: "Contract Name",
type: "string",
defaultValue: "Contract",
}, {
name: "folder",
type: "folder",
title: "Target folder (current is default)",
}],
async run(terminal, args) {
const filePath = (0, utils_1.uniqueFilePath)(args.folder, `${args.name}{}.sol`);
const text = snippets_1.BasicContract.split("{name}").join(args.name);
(0, utils_1.writeTextFile)(filePath, text);
terminal.log(`Solidity contract ${path_1.default.basename(filePath)} created.`);
},
};
exports.solidityCompileCommand = {
name: "compile",
title: "Compile Solidity Contract",
args: [
{
isArg: true,
name: "file",
type: "file",
title: "Source file",
nameRegExp: /\.sol$/i,
},
{
name: "code",
alias: "c",
title: "Save .code file (false is default)",
type: "boolean",
defaultValue: "false",
},
{
name: "output-dir",
alias: "o",
type: "folder",
title: "Output folder (current is default)",
defaultValue: "",
},
],
async run(terminal, args) {
var _a, _b;
await core_1.Component.ensureInstalledAll(terminal, components_1.components);
const fileDir = path_1.default.dirname(args.file);
const fileName = path_1.default.basename(args.file);
const outputDir = path_1.default.resolve((_a = args.outputDir) !== null && _a !== void 0 ? _a : ".");
const preserveCode = args.code;
const tvcName = path_1.default.resolve(outputDir, (0, utils_1.changeExt)(fileName, ".tvc"));
const abiName = path_1.default.resolve(outputDir, (0, utils_1.changeExt)(fileName, ".abi.json"));
const codeName = path_1.default.resolve(outputDir, (0, utils_1.changeExt)(fileName, ".code"));
const isDeprecatedVersion = (await components_1.components.compiler.getCurrentVersion()) <= '0.21.0';
let linkerOut;
if (isDeprecatedVersion) {
terminal.log("You use an obsolete version of the compiler.\nThe output files are saved in the current directory");
await components_1.components.compiler.silentRun(terminal, fileDir, [fileName]);
linkerOut = await components_1.components.linker.silentRun(terminal, fileDir, ["compile", codeName, "-a", abiName, "--lib", components_1.components.stdlib.path()]);
}
else {
await components_1.components.compiler.silentRun(terminal, fileDir, ["-o", outputDir, fileName]);
linkerOut = await components_1.components.linker.silentRun(terminal, fileDir, ["compile", codeName, "--lib", components_1.components.stdlib.path()]);
}
const generatedTvcName = `${(_b = /Saved contract to file (.*)$/mg.exec(linkerOut)) === null || _b === void 0 ? void 0 : _b[1]}`;
// fs.renameSync was replaces by this code, because of an error: EXDEV: cross-device link not permitted
await new Promise((res, rej) => (0, mv_1.default)(path_1.default.resolve(fileDir, generatedTvcName), path_1.default.resolve(outputDir, tvcName), {
mkdirp: true,
clobber: true,
}, (err) => (err ? rej(err) : res(true))));
if (!preserveCode)
fs_1.default.unlinkSync(path_1.default.resolve(fileDir, codeName));
},
};
exports.solidityAstCommand = {
name: "ast",
title: "AST of all source files in a JSON or compact-JSON format.",
args: [
{
isArg: true,
name: "file",
type: "file",
title: "Source file",
nameRegExp: /\.sol$/i,
},
{
name: "format",
alias: "f",
type: "string",
title: "-f, --format <json | compact-json>",
defaultValue: "compact-json",
},
{
name: "output-dir",
alias: "o",
type: "folder",
title: "Output folder (current is default)",
defaultValue: "",
}
],
async run(terminal, args) {
var _a;
const ext = path_1.default.extname(args.file);
if (ext !== ".sol") {
terminal.log(`Choose solidity source file.`);
return;
}
if (args.format.match(/^(compact-json|json)$/i) == null) {
terminal.log(`Wrong ast format.`);
return;
}
await core_1.Component.ensureInstalledAll(terminal, components_1.components);
const fileDir = path_1.default.dirname(args.file);
const fileName = path_1.default.basename(args.file);
args.outputDir = path_1.default.resolve((_a = args.outputDir) !== null && _a !== void 0 ? _a : ".");
await components_1.components.compiler.silentRun(terminal, fileDir, [
`--ast-${args.format}`,
'--output-dir',
args.outputDir,
fileName,
]);
const astPath = path_1.default.join(args.outputDir, `${path_1.default.parse(fileName).name}.ast.json`);
let ast = fs_1.default.readFileSync(astPath, 'utf-8');
if (ast[0] !== '{') { // This is necessary because the original result may not be in JSON format
ast = ast.split('\n').slice(4).join('\n');
}
(0, utils_1.writeTextFile)(astPath, ast);
},
};
exports.solidityUpdateCommand = {
name: "update",
title: "Update Solidity Compiler",
args: [{
name: "force",
alias: "f",
title: "Force reinstall even if up to date",
type: "boolean",
defaultValue: "false",
}],
async run(terminal, args) {
await core_1.Component.updateAll(terminal, args.force, components_1.components);
},
};
exports.soliditySetCommand = {
name: "set",
title: "Change Solidity Config",
args: [
{
name: "compiler",
alias: "c",
title: "Compiler version (version number or `latest`)",
type: "string",
defaultValue: "",
},
{
name: "linker",
alias: "l",
title: "Linker version (version number or `latest`)",
type: "string",
defaultValue: "",
},
{
name: "force",
alias: "f",
title: "Force reinstall even if up to date",
type: "boolean",
defaultValue: "false",
},
],
async run(terminal, args) {
const versions = {};
if (args.compiler !== "") {
versions.compiler = args.compiler;
versions.stdlib = args.compiler;
}
if (args.linker !== "") {
versions.linker = args.linker;
}
await core_1.Component.setVersions(terminal, args.force, components_1.components, versions);
},
};
exports.Solidity = {
name: "sol",
title: "Solidity Compiler",
commands: [
exports.solidityCreateCommand,
exports.solidityCompileCommand,
exports.solidityAstCommand,
exports.solidityVersionCommand,
exports.soliditySetCommand,
exports.solidityUpdateCommand,
],
};
//# sourceMappingURL=index.js.map