UNPKG

2.03 kBJavaScriptView Raw
1const fse = require('fs-extra');
2const path = require('path');
3const urljoin = require('urljoin');
4const { promisify } = require('util');
5const { compress } = require('targz');
6
7const tgzCompress = promisify(compress);
8
9const { getAPIKey } = require('./userConf');
10const { deploy } = require('../sdk');
11const { getInvokeEndpoint } = require('../sdk/config');
12const { checkAndHandleError } = require('./handleError');
13
14const binarisDir = '.binaris/';
15const ignoredTarFiles = ['.git', '.binaris', 'binaris.yml'];
16
17/**
18 * Generates the .binaris directory inside of the current
19 * functions directory. This directory is used to hold temporary
20 * CLI files related to the function.
21 *
22 * @param {string} genPath - path at which to generate the .binaris dir
23 * @returns {string} - full path to the generated .binaris dir
24 */
25const genBinarisDir = async function genBinarisDir(genPath) {
26 const fullPath = path.join(genPath, binarisDir);
27 await fse.mkdirp(fullPath);
28 return fullPath;
29};
30
31/**
32 * Deploy a function to the Binaris cloud.
33 *
34 * @param {string} funcName - name of the function to deploy
35 * @param {string} funcPath - path of the function to deploy
36 * @param {object} funcConf - configuration of the function to deploy
37 *
38 * @returns {string} - curlable URL of the endpoint used to invoke your function
39 */
40const deployCLI = async function deployCLI(funcName, funcPath, funcConf) {
41 // path where temporary function tar will be stored
42 const funcTarPath = path.join(await genBinarisDir(funcPath), `${funcName}.tgz`);
43 const fullIgnorePaths = ignoredTarFiles.map(entry =>
44 path.join(funcPath, entry));
45 await tgzCompress({
46 src: funcPath,
47 dest: funcTarPath,
48 tar: {
49 ignore: name => fullIgnorePaths.includes(name),
50 },
51 });
52 const apiKey = await getAPIKey();
53 const deployResponse = await deploy(funcName, apiKey, funcConf, funcTarPath);
54 checkAndHandleError(deployResponse);
55 return urljoin(`https://${getInvokeEndpoint()}`, 'v1', 'run', apiKey, funcName);
56};
57
58module.exports = deployCLI;