UNPKG

@vercel/fun

Version:

Local Lambda development environment

45 lines 2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = init; const node_path_1 = require("node:path"); const tinyexec_1 = require("tinyexec"); const debug_1 = __importDefault(require("debug")); const promises_1 = require("node:fs/promises"); const filename_1 = require("./filename"); const debug = (0, debug_1.default)('@vercel/fun:runtimes/go1.x'); function _go(opts) { return function go(...args) { debug('Exec %o', `go ${args.join(' ')}`); return (0, tinyexec_1.exec)('go', args, { stdio: 'inherit', ...opts }); }; } async function init({ cacheDir }) { const source = (0, node_path_1.join)(cacheDir, 'bootstrap.go'); const out = (0, filename_1.getOutputFile)(); let data = await (0, promises_1.readFile)(source, 'utf8'); // Fix windows if (process.platform === 'win32') { debug('detected windows, so stripping Setpgid'); data = data .split('\n') .filter(line => !line.includes('Setpgid')) .join('\n'); } // Prepare a temporary `$GOPATH` const GOPATH = (0, node_path_1.join)(cacheDir, 'go'); // The source code must reside in `$GOPATH/src` for `go get` to work const bootstrapDir = (0, node_path_1.join)(GOPATH, 'src', out); await (0, promises_1.mkdir)(bootstrapDir, { recursive: true }); await (0, promises_1.writeFile)((0, node_path_1.join)(bootstrapDir, 'bootstrap.go'), data); const go = _go({ cwd: bootstrapDir, env: { ...process.env, GOPATH } }); const bootstrap = (0, node_path_1.join)(cacheDir, out); debug('Compiling Go runtime binary %o -> %o', source, bootstrap); await go('get'); await go('build', '-o', bootstrap, 'bootstrap.go'); // Clean up `$GOPATH` from the cacheDir await (0, promises_1.rm)(GOPATH, { recursive: true }); } //# sourceMappingURL=index.js.map