"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const command_1 = require("@oclif/command");
const sync_1 = tslib_1.__importDefault(require("./sync"));
const base_1 = tslib_1.__importDefault(require("../base"));
const logger_1 = tslib_1.__importDefault(require("../utils/logger"));
const utils_1 = require("../utils");
const git_1 = require("../utils/git");
const collection_1 = require("../utils/collection");
class Reload extends base_1.default {
    async run() {
        this.parse(Reload);
        try {
            await utils_1.checkInitStatus();
        }
        catch (err) {
            logger_1.default.log('error', err.message);
            this.exit(1);
        }
        // Run sync command if workspace is not created.
        if (!fs_extra_1.default.existsSync(collection_1.collectionPath)) {
            await sync_1.default.run([]);
        }
        const collection = collection_1.loadCollection();
        // Checkout master branch and add tuture.yml.
        await git_1.git.checkout('master');
        const currentSteps = await utils_1.makeSteps(this.userConfig.ignoredFiles);
        const lastArticleId = collection.articles.slice(-1)[0].id;
        currentSteps.forEach((step) => {
            // For newly added steps, assign it to the last article.
            if (!collection.steps.map((step) => step.id).includes(step.id)) {
                step.articleId = lastArticleId;
            }
        });
        collection.steps = utils_1.mergeSteps(collection.steps, currentSteps);
        collection_1.saveCollection(collection);
        logger_1.default.log('success', 'Reload complete!');
    }
}
exports.default = Reload;
Reload.description = 'Update workspace with latest commit history';
Reload.flags = {
    help: command_1.flags.help({ char: 'h' }),
};
