1 | const fse = require('fs-extra');
|
2 | const path = require('path');
|
3 | const urljoin = require('urljoin');
|
4 | const { promisify } = require('util');
|
5 | const { compress } = require('targz');
|
6 |
|
7 | const tgzCompress = promisify(compress);
|
8 |
|
9 | const { getAPIKey } = require('./userConf');
|
10 | const { deploy } = require('../sdk');
|
11 | const { getInvokeEndpoint } = require('../sdk/config');
|
12 | const { checkAndHandleError } = require('./handleError');
|
13 |
|
14 | const binarisDir = '.binaris/';
|
15 | const ignoredTarFiles = ['.git', '.binaris', 'binaris.yml'];
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const genBinarisDir = async function genBinarisDir(genPath) {
|
26 | const fullPath = path.join(genPath, binarisDir);
|
27 | await fse.mkdirp(fullPath);
|
28 | return fullPath;
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | const deployCLI = async function deployCLI(funcName, funcPath, funcConf) {
|
41 |
|
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 |
|
58 | module.exports = deployCLI;
|