All files / Cognigy-CLI/build/lib snapshots.js

0% Statements 0/76
0% Branches 0/50
0% Functions 0/13
0% Lines 0/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                                                                                                                                                                           
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSnapshot = void 0;
const config_1 = __importDefault(require("../utils/config"));
const cognigyClient_1 = __importDefault(require("../utils/cognigyClient"));
const checks_1 = require("../utils/checks");
const cli_spinner_1 = require("cli-spinner");
const axios_1 = __importDefault(require("axios"));
const fs = __importStar(require("fs"));
const createSnapshot = async (name, description, timeout, skipDownload) => {
    const snapshotDir = config_1.default.agentDir + '/snapshots';
    await (0, checks_1.checkCreateDir)(snapshotDir);
    const spinner = new cli_spinner_1.Spinner(`Creating Cognigy.AI Snapshot ${name} ... %s`);
    spinner.setSpinnerString('|/-\\');
    try {
        spinner.start();
        const createSnapshotTask = await cognigyClient_1.default.createSnapshot({
            projectId: config_1.default.agent,
            description: description || 'Created by Cognigy.AI CLI',
            name: name,
        });
        await (0, checks_1.checkTask)(createSnapshotTask._id, timeout);
        const allSnapshots = await cognigyClient_1.default.indexSnapshots({
            projectId: config_1.default.agent,
        });
        let snap = allSnapshots.items.find((snap) => snap.name === name);
        if (!snap)
            throw new Error("Snapshot can't be found remotely");
        if (!skipDownload) {
            const prepareSnapshotTask = await cognigyClient_1.default.packageSnapshot({
                snapshotId: snap._id,
            });
            spinner.setSpinnerTitle(`Packaging Snapshot for download... %s`);
            await (0, checks_1.checkTask)(prepareSnapshotTask._id, timeout);
            const downloadLink = await cognigyClient_1.default.composeSnapshotDownloadLink({
                snapshotId: snap._id,
            });
            spinner.setSpinnerTitle(`Downloading Snapshot ... %s`);
            const snapshotFile = (await axios_1.default.get(downloadLink.downloadLink, {
                responseType: 'stream',
            })).data;
            const writer = fs.createWriteStream(snapshotDir + '/' + snap.name + '.csnap');
            await new Promise((resolve, reject) => {
                snapshotFile.pipe(writer);
                writer.on('finish', resolve);
                writer.on('error', reject);
            });
        }
        spinner.stop();
        return;
    }
    catch (err) {
        spinner.stop();
        throw err;
    }
};
exports.createSnapshot = createSnapshot;