UNPKG

@metacall/deploy

Version:

Tool for deploying into MetaCall FaaS platform.

121 lines (118 loc) 4.94 kB
"use strict"; /* * About File: it contains utility functions to deal with files/folders and zipping filed */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEnv = exports.zip = exports.loadFilesToRun = exports.filter = exports.forever = exports.opt = exports.loadFile = exports.ensureFolderExists = exports.exists = exports.configDir = exports.sleep = void 0; const archiver_1 = __importDefault(require("archiver")); const dotenv_1 = require("dotenv"); const fs_1 = require("fs"); const inquirer_1 = require("inquirer"); const os_1 = require("os"); const path_1 = require("path"); const messages_1 = require("./cli/messages"); const selection_1 = require("./cli/selection"); const tty_1 = require("./tty"); const missing = (name) => `Missing ${name} environment variable! Unable to load config`; const sleep = (ms) => { return new Promise(resolve => setTimeout(resolve, ms)); }; exports.sleep = sleep; const configDir = (name) => (0, os_1.platform)() === 'win32' ? process.env.APPDATA ? (0, path_1.join)(process.env.APPDATA, name) : (0, messages_1.error)(missing('APPDATA')) : process.env.HOME ? (0, path_1.join)(process.env.HOME, `.${name}`) : (0, messages_1.error)(missing('HOME')); exports.configDir = configDir; const exists = (path) => fs_1.promises.stat(path).then(() => true, () => false); exports.exists = exists; const ensureFolderExists = async (path) => ((await (0, exports.exists)(path)) || (await fs_1.promises.mkdir(path, { recursive: true })), path); exports.ensureFolderExists = ensureFolderExists; const loadFile = async (path) => (await (0, exports.exists)(path)) ? fs_1.promises.readFile(path, 'utf8') : ''; exports.loadFile = loadFile; const opt = (f, x) => x ? f(x) : ''; exports.opt = opt; exports.forever = true; const filter = (a, b) => Object.entries(b).reduce((acc, [k, v]) => (a[k] === v ? acc : { ...acc, [k]: v }), {}); exports.filter = filter; const loadFilesToRun = async (packages) => { for (const pkg of packages) { pkg.scripts = await (0, selection_1.fileSelection)(`Select files to load with ${(0, messages_1.printLanguage)(pkg.language_id)}`, pkg.scripts); } }; exports.loadFilesToRun = loadFilesToRun; const zip = async (source, files, progress, pulse, hide) => { const archive = (0, archiver_1.default)('zip', { zlib: { level: 9 } }); if (progress) { archive.on('progress', data => progress('Compressing and deploying...', data.fs.processedBytes / data.fs.totalBytes)); } if (pulse) { archive.on('entry', (entry) => pulse(entry.name)); } files = files.map(file => (0, path_1.join)(source, file)); for (const file of files) { (await fs_1.promises.stat(file)).isDirectory() ? archive.directory(file, (0, path_1.basename)(file)) : archive.file(file, { name: (0, path_1.relative)(source, file) }); } if (hide) { archive.on('finish', () => hide()); } await archive.finalize(); return archive; }; exports.zip = zip; const getEnv = async (rootPath) => { if (rootPath !== undefined) { const envFilePath = (0, path_1.join)(rootPath, '.env'); if (await (0, exports.exists)(envFilePath)) { try { const source = await fs_1.promises.readFile(envFilePath, 'utf8'); const parsedEnv = (0, dotenv_1.parse)(source); (0, messages_1.info)('Detected and loaded environment variables from .env file.'); return Object.entries(parsedEnv).map(([name, value]) => ({ name, value })); } catch (err) { (0, messages_1.error)(`Error while reading the .env file: ${err.toString()}`); } } } // If the input is not interactive skip asking the end user if (!(0, tty_1.isInteractive)()) { // TODO: We should implement support for all the inputs and prompts for non-interactive terminal return []; } const enableEnv = await (0, selection_1.consentSelection)('Do you want to add environment variables?'); const env = enableEnv ? await (0, inquirer_1.prompt)([ { type: 'input', name: 'env', message: 'Type env vars in the format: K1=V1, K2=V2' } ]).then(({ env }) => env .split(',') .map(kv => { const [k, v] = kv.trim().split('='); return { [k]: v }; }) .reduce((obj, kv) => Object.assign(obj, kv), {})) : {}; const envArr = Object.entries(env).map(el => { const [k, v] = el; return { name: k, value: v }; }); return envArr; }; exports.getEnv = getEnv;