{"size": 2172, "ext": "js", "max_stars_repo_path": ".core/.cli/commands/cloud/actions.js", "max_stars_repo_name": "Atomic-Reactor/actinium-ldap", "max_stars_repo_stars_event_min_datetime": "2019-10-11T18:27:04.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-27T04:17:11.000Z", "max_issues_repo_path": ".core/.cli/commands/cloud/actions.js", "max_issues_repo_name": "Atomic-Reactor/actinium-ldap", "max_issues_repo_issues_event_min_datetime": "2019-09-09T18:20:18.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-27T18:20:44.000Z", "max_forks_repo_path": ".core/.cli/commands/cloud/actions.js", "max_forks_repo_name": "Atomic-Reactor/actinium-ldap", "max_forks_repo_forks_event_min_datetime": "2022-02-28T18:57:02.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-28T21:51:19.000Z"}
{"max_stars_count": 3, "max_issues_count": 8, "max_forks_count": 2, "avg_line_length": 30.1666666667, "max_line_length": 80, "alphanum_fraction": 0.4774401473}
const path = require('path');
const chalk = require('chalk');
const fs = require('fs-extra');
const op = require('object-path');
const zip = require('folder-zipper');
const homedir = require('os').homedir();
const handlebars = require('handlebars').compile;

module.exports = spinner => {
    const message = text => {
        if (spinner) {
            spinner.text = text;
        }
    };

    return {
        backup: ({ action, params, props }) => {
            const { cwd } = props;
            const { destination } = params;

            const name = destination.replace(cwd, '').replace(/\//g, '-');

            message(`backing up ${name} cloud function...`);

            const now = Date.now();
            const backupDir = path.join(
                homedir,
                '.arcl',
                cwd,
                '.BACKUP',
                'cloud',
            );
            const backupZip = path.normalize(`${backupDir}/${now}${name}.zip`);

            // Create the backup directory
            fs.ensureDirSync(backupDir);

            // Backup the component directory then empty the existing
            return zip(destination, backupZip).then(() => {
                return { action, status: 200 };
            });
        },

        create: ({ action, params, props }) => {
            const { cwd } = props;
            const { destination, overwrite } = params;

            const actionType = overwrite === true ? 'overwritting' : 'creating';

            message(`${actionType} cloud function...`);

            fs.ensureFileSync(destination);

            // Template content
            const template = path.normalize(`${__dirname}/template/cloud.hbs`);
            const content = handlebars(fs.readFileSync(template, 'utf-8'))(
                params,
            );

            return new Promise((resolve, reject) => {
                fs.writeFile(destination, content, error => {
                    if (error) {
                        reject(error.Error);
                    } else {
                        resolve({ action, status: 200 });
                    }
                });
            });
        },
    };
};
