motoko
Version:
Compile and run Motoko smart contracts in Node.js or the browser.
186 lines • 6.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ast_1 = require("./ast");
const file_1 = require("./file");
const package_1 = require("./package");
const asciiToUtf8_1 = require("./utils/asciiToUtf8");
const resolveEntryPoint_1 = require("./utils/resolveEntryPoint");
function wrapMotoko(compiler) {
const version = compiler.version || '(unknown)';
const debug = require('debug')(`motoko:${version}`);
const invoke = (key, unwrap, args) => {
if (typeof compiler[key] !== 'function') {
throw new Error(`Unknown compiler function: '${key}'`);
}
let result;
try {
result = compiler[key](...args);
}
catch (err) {
if (err instanceof Error) {
throw err;
}
throw new Error(`Unable to execute ${key}(${[...args]
.map((x) => typeof x)
.join(', ')}):\n${JSON.stringify(err)}`);
}
if (!unwrap) {
return result;
}
if (!result.code) {
throw new Error(result.diagnostics
? result.diagnostics
.map(({ message }) => message)
.join('; ')
: '(no diagnostics)');
}
return result.code;
};
function parseMotokoTyped(paths) {
if (typeof paths === 'string') {
return mo.parseMotokoTyped([paths])[0];
}
return invoke('parseMotokoTyped', true, [paths]).map(({ ast, typ }) => {
return {
ast: (0, ast_1.simplifyAST)(ast),
type: (0, ast_1.simplifyAST)(typ),
};
});
}
function parseMotokoTypedWithScopeCache(paths, scopeCache, enableRecovery) {
if (enableRecovery === undefined) {
enableRecovery = false;
}
if (typeof paths === 'string') {
const [progs, outCache] = mo.parseMotokoTypedWithScopeCache([paths], scopeCache, enableRecovery);
return [progs[0], outCache];
}
const [progs, outCache] = invoke('parseMotokoTypedWithScopeCache', true, [enableRecovery, paths, scopeCache]);
return [
progs.map(({ ast, typ, immediateImports }) => {
return {
ast: (0, ast_1.simplifyAST)(ast),
type: (0, ast_1.simplifyAST)(typ),
immediateImports,
};
}),
outCache,
];
}
const mo = {
version,
compiler,
file(path) {
return (0, file_1.file)(mo, path);
},
read(path) {
return invoke('readFile', false, [path]);
},
write(path, content = '') {
if (typeof content !== 'string') {
throw new Error('Non-string file content');
}
debug('+file', path);
invoke('saveFile', false, [path, content]);
},
rename(path, newPath) {
invoke('renameFile', false, [path, newPath]);
},
delete(path) {
debug('-file', path);
invoke('removeFile', false, [path]);
},
list(directory) {
return invoke('readDir', false, [directory]);
},
async fetchPackage(name, info) {
if (!info) {
throw new Error('Please specify both a name and source');
}
return (0, package_1.fetchPackage)(name, info);
},
async installPackages(packages) {
return (0, package_1.installPackages)(mo, packages);
},
loadPackage(pkg) {
debug('+package', pkg.name);
mo.validatePackage(pkg);
const directory = `.node-motoko/${pkg.name}/${pkg.version}`;
Object.entries(pkg.files).forEach(([path, file]) => {
mo.write(`${directory}/${path}`, file.content);
});
mo.usePackage(pkg.name, directory);
},
usePackage(name, directory) {
debug('@package', name, directory);
invoke('addPackage', false, [name, directory]);
},
clearPackages() {
debug('-packages');
invoke('clearPackage', false, []);
},
validatePackage(pkg) {
(0, package_1.validatePackage)(pkg);
},
setAliases(directory, aliases) {
debug('aliases', directory, aliases);
invoke('setCandidPath', false, [directory]);
invoke('setActorAliases', false, [Object.entries(aliases)]);
},
setPublicMetadata(values) {
invoke('setPublicMetadata', false, [values]);
},
setRunStepLimit(limit) {
invoke('setRunStepLimit', false, [limit]);
},
setTypecheckerCombineSrcs(combineSrcs) {
invoke('setTypecheckerCombineSrcs', false, [combineSrcs]);
},
check(path) {
const result = invoke('check', false, [path]);
return result.diagnostics;
},
run(path, libPaths) {
const run = invoke('run', false, [libPaths || [], path]);
run.stdout = (0, asciiToUtf8_1.asciiToUtf8)(run.stdout);
run.stderr = (0, asciiToUtf8_1.asciiToUtf8)(run.stderr);
return run;
},
candid(path) {
return invoke('candid', true, [path]);
},
wasm(path, mode) {
if (!mode) {
mode = 'ic';
}
else if (mode !== 'ic' && mode !== 'wasi') {
throw new Error(`Invalid WASM format: ${mode}`);
}
return invoke('compileWasm', true, [mode, path]);
},
parseCandid(content) {
return invoke('parseCandid', true, [content]);
},
parseMotoko(content, enableRecovery = false) {
const ast = invoke('parseMotoko', true, [enableRecovery, content]);
return (0, ast_1.simplifyAST)(ast);
},
parseMotokoWithDeps(path, content, enableRecovery = false) {
const { ast, immediateImports } = invoke('parseMotokoWithDeps', true, [enableRecovery, path, content]);
return { ast: (0, ast_1.simplifyAST)(ast), immediateImports };
},
parseMotokoTyped,
parseMotokoTypedWithScopeCache,
resolveMain(directory = '') {
return (0, resolveEntryPoint_1.resolveMain)(mo, directory);
},
resolveLib(directory = '') {
return (0, resolveEntryPoint_1.resolveLib)(mo, directory);
},
};
// @ts-ignore
mo.default = mo;
return mo;
}
exports.default = wrapMotoko;
//# sourceMappingURL=index.js.map