UNPKG

2.6 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const path_1 = require("path");
7const node_fetch_1 = __importDefault(require("node-fetch"));
8const execa_1 = __importDefault(require("execa"));
9const fs_1 = require("fs");
10const build_utils_1 = require("@now/build-utils");
11// portable ruby from https://portableruby.com/ for bootstrapping
12// ruby deployment on nodejs8.10. version 2.5.3 matches ruby2.5.
13const url = 'https://portableruby.s3.amazonaws.com/ruby-2.5.3.zip';
14// downloads `ruby` and `gem` and returns their absolute paths
15async function downloadPortableRuby(path) {
16 console.log('downloading "ruby-2.5.3.zip"...');
17 const res = await node_fetch_1.default(url);
18 if (!res.ok || res.status !== 200) {
19 throw new Error(`Could not download "ruby-2.5.3.zip" from "${url}"`);
20 }
21 const dir = await build_utils_1.getWriteableDirectory();
22 const filePath = path_1.join(dir, 'ruby-2.5.3.zip');
23 const writeStream = fs_1.createWriteStream(filePath);
24 await new Promise((resolve, reject) => {
25 res.body
26 .on('error', reject)
27 .pipe(writeStream)
28 .on('finish', resolve);
29 });
30 try {
31 await execa_1.default('unzip', ['-q', '-d', path, filePath], { stdio: 'inherit' });
32 }
33 catch (err) {
34 console.log('could not unzip ruby-2.5.3.zip');
35 throw err;
36 }
37 return {
38 rubyPath: path_1.join(path, 'bin', 'ruby'),
39 gemPath: path_1.join(path, 'bin', 'gem'),
40 };
41}
42// downloads and installs `bundler` (respecting
43// process.env.GEM_HOME), and returns
44// the absolute path to it
45async function downloadAndInstallBundler() {
46 const { GEM_HOME } = process.env;
47 if (!GEM_HOME) {
48 // this is the directory in which `gem` will be
49 // installed to. `--prefix` will assume `~` if this
50 // is not set, and `~` is not writeable on AWS Lambda.
51 // let's refuse to proceed
52 throw new Error('Could not install "bundler": "GEM_HOME" env var is not set');
53 }
54 console.log('installing ruby...');
55 const { rubyPath, gemPath } = await downloadPortableRuby(GEM_HOME);
56 console.log('installing bundler...');
57 await execa_1.default(gemPath, ['install', 'bundler', '--no-document'], { stdio: 'inherit' });
58 return {
59 rubyPath,
60 gemPath,
61 bundlerPath: path_1.join(GEM_HOME, 'bin', 'bundler'),
62 };
63}
64exports.downloadAndInstallBundler = downloadAndInstallBundler;