"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const child_process_1 = tslib_1.__importDefault(require("child_process"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const path_1 = tslib_1.__importDefault(require("path"));
const command_1 = require("@oclif/command");
const inquirer_1 = require("inquirer");
const base_1 = tslib_1.__importDefault(require("../base"));
const logger_1 = tslib_1.__importDefault(require("../utils/logger"));
const git_1 = require("../utils/git");
const assets_1 = require("../utils/assets");
const collection_1 = require("../utils/collection");
const constants_1 = require("../constants");
class Commit extends base_1.default {
    async run() {
        const { flags } = this.parse(Commit);
        this.userConfig = Object.assign(this.userConfig, flags);
        const message = flags.message ||
            (await inquirer_1.prompt([
                {
                    name: 'message',
                    type: 'input',
                    default: `Commit on ${new Date()}`,
                },
            ])).message;
        await collection_1.initializeTutureBranch();
        // Checkout tuture branch and add tuture.yml.
        await git_1.git.checkout(constants_1.TUTURE_BRANCH);
        // Trying to copy and add tuture.yml to staging.
        const targetTutureYML = path_1.default.join(process.cwd(), path_1.default.basename(collection_1.collectionPath));
        fs_extra_1.default.copySync(collection_1.collectionPath, targetTutureYML);
        await git_1.git.add(targetTutureYML);
        // Trying to copy and add tuture-assets.json to staging.
        const targetAssetsTable = path_1.default.join(process.cwd(), path_1.default.basename(assets_1.assetsTablePath));
        if (fs_extra_1.default.existsSync(assets_1.assetsTablePath)) {
            fs_extra_1.default.copySync(assets_1.assetsTablePath, targetAssetsTable);
            await git_1.git.add(targetAssetsTable);
        }
        fs_extra_1.default.removeSync(collection_1.collectionCheckpoint);
        fs_extra_1.default.removeSync(assets_1.assetsTableCheckpoint);
        // Commit changes to tuture branch.
        child_process_1.default.execSync(`git commit --allow-empty -m "tuture: ${message}"`);
        logger_1.default.log('success', `Committed to branch ${constants_1.TUTURE_BRANCH} (${message})`);
    }
}
exports.default = Commit;
Commit.description = 'Commit your tutorial to VCS (Git)';
Commit.flags = {
    help: command_1.flags.help({ char: 'h' }),
    message: command_1.flags.string({
        char: 'm',
        description: 'commit message',
    }),
};
