UNPKG

@taqueria/plugin-archetype

Version:

A taqueria plugin for compiling Archetype smart contracts

147 lines (140 loc) 5.09 kB
// index.ts import { Plugin, PositionalArg, Task, Template } from "@taqueria/node-sdk"; // compile.ts import { execCmd, getContracts, getDockerImage, sendAsyncErr, sendAsyncRes, sendErr, sendJsonRes } from "@taqueria/node-sdk"; import { basename, extname, join } from "path"; import { match } from "ts-pattern"; var ARCHETYPE_DEFAULT_IMAGE = "completium/archetype:1.5.2"; var ARCHETYPE_IMAGE_ENV_VAR = "TAQ_ARCHETYPE_IMAGE"; var getArchetypeDockerImage = () => getDockerImage(ARCHETYPE_DEFAULT_IMAGE, ARCHETYPE_IMAGE_ENV_VAR); var getInputFilename = (opts) => (sourceFile) => { const inputFile = basename(sourceFile, extname(sourceFile)); return join(opts.config.contractsDir ?? "contracts", `${inputFile}.arl`); }; var getContractArtifactFilename = (opts) => (sourceFile) => { const outFile = basename(sourceFile, extname(sourceFile)); return join(opts.config.artifactsDir ?? "contracts", `${outFile}.tz`); }; var getCompileCommand = (opts) => (sourceFile) => { const { projectDir } = opts; const inputFile = getInputFilename(opts)(sourceFile); const baseCommand = `DOCKER_DEFAULT_PLATFORM=linux/amd64 docker run --rm -v "${projectDir}":/project -u $(id -u):$(id -g) -w /project ${getArchetypeDockerImage()} ${inputFile}`; const outFile = `-o ${getContractArtifactFilename(opts)(sourceFile)}`; const cmd = `${baseCommand} ${outFile}`; return cmd; }; var compileContract = (opts) => (sourceFile) => ( // const sourceAbspath = join(opts.contractsDir, sourceFile) execCmd(getCompileCommand(opts)(sourceFile)).then(({ stderr }) => { if (stderr.length > 0) sendErr(stderr); return { contract: sourceFile, artifact: getContractArtifactFilename(opts)(sourceFile) }; }).catch((err) => { sendErr(" "); sendErr(err.message.split("\n").slice(1).join("\n")); return Promise.resolve({ contract: sourceFile, artifact: "Not compiled" }); }) ); var compileAll = (opts) => { const contracts = getContracts(/\.arl$/, opts.config); return Promise.all(contracts).then((entries) => entries.map(compileContract(opts))).then( (processes) => processes.length > 0 ? processes : [{ contract: "None found", artifact: "N/A" }] ).then((promises) => Promise.all(promises)); }; var compile = (parsedArgs) => { const unsafeOpts = parsedArgs; return match(unsafeOpts).when((unsafeOpts2) => unsafeOpts2.task === "get-image", () => sendAsyncRes(getArchetypeDockerImage())).otherwise(() => { const p = unsafeOpts.sourceFile ? compileContract(unsafeOpts)(unsafeOpts.sourceFile).then((result) => [result]) : compileAll(unsafeOpts).then((results) => { if (results.length === 0) sendErr("No contracts found to compile."); return results; }); return p.then(sendJsonRes).catch((err) => sendAsyncErr(err, false)); }); }; var compile_default = compile; // createContract.ts import { sendAsyncErr as sendAsyncErr2 } from "@taqueria/node-sdk"; import { writeFile } from "fs/promises"; // archetype_template.ts var arl_template = ` archetype hello variable msg : string = "Hello" entry input(name : string) { msg += " " + name } `; // createContract.ts var validateExtension = async (contractName) => { const matchResult = contractName.match(/\.arl$/); if (matchResult) return; return sendAsyncErr2(`"${contractName}" doesn't have extension "arl".`); }; var createContract = (args) => { const contractName = args.sourceFileName; const contractsDir = `${args.config.projectDir}/${args.config.contractsDir}`; return validateExtension(contractName).then((_) => writeFile(`${contractsDir}/${contractName}`, arl_template)); }; var createContract_default = createContract; // index.ts Plugin.create((i18n) => ({ schema: "1.0", version: "0.1", alias: "archetype", tasks: [ Task.create({ task: "compile", command: "compile [sourceFile]", aliases: ["c", "compile-archetype"], description: "Compile a smart contract written in a Archetype syntax to Michelson code", options: [], handler: "proxy", encoding: "json" }), Task.create({ task: "decompile", command: "decompile [sourceFile]", aliases: ["d", "decompile-archetype"], description: "Decompile a Michelson smart contract to Archetype source code", options: [], handler: "proxy", encoding: "json" }), Task.create({ task: "get-image", command: "get-image", description: "Gets the name of the image to be used", handler: "proxy", hidden: true }) ], templates: [ Template.create({ template: "archetypeContract", command: "archetypeContract <sourceFileName>", description: "Create a Archetype contract with boilerplate code", positionals: [ PositionalArg.create({ placeholder: "sourceFileName", type: "string", description: "The name of the Archetype contract to generate" }) ], handler: createContract_default }) ], proxy: compile_default }), process.argv); //# sourceMappingURL=index.mjs.map