<commit_before>'use strict';

var rmdir = require('rimraf');
var fs = require('fs-extra');
var path = require('path');

/**
 * This contains all the core commands that kalabox can run on every machine
 */

module.exports = function(kbox) {

  var events = kbox.core.events;

  kbox.whenAppRegistered(function(app) {

    kbox.tasks.add(function(task) {
      task.path = [app.name, 'destroy'];
      task.category = 'appAction';
      task.description = 'Completely destroys and removes an app.';
      task.func = function(done) {

        // @todo: This needs to happen inside of uninstall as an option at some point
        // Do this even ONLY when uninstall is run
        // @todo: what would this do on the GUI?
        events.on('pre-uninstall', function(app, done) {
          var containerIdFile = path.join(app.config.appCidsRoot, 'data');
          var containerId;
          if (fs.existsSync(containerIdFile)) {
            containerId = fs.readFileSync(containerIdFile, 'utf8');
          }
          kbox.engine.remove(containerId, function() {
            fs.unlinkSync(containerIdFile);
            console.log('Removing the codez.');
            rmdir(app.config.codeRoot, function(err) {
              if (err) {
                done(err);
              }
              else {
                done();
              }
            });
          });
        });

        // Actually do the uninstall
        kbox.app.uninstall(app, done);
      };
    });

  });

};<commit_msg>#448: Stop app before a destroy<commit_after>'use strict';

var rmdir = require('rimraf');
var fs = require('fs-extra');
var path = require('path');

/**
 * This contains all the core commands that kalabox can run on every machine
 */

module.exports = function(kbox) {

  var events = kbox.core.events;
  var Promise = kbox.Promise;
  var util = require('./../util.js')(kbox);

  kbox.whenAppRegistered(function(app) {

    kbox.tasks.add(function(task) {
      task.path = [app.name, 'destroy'];
      task.category = 'appAction';
      task.description = 'Completely destroys and removes an app.';
      task.func = function(done) {

        // Stop the app if its running
        // @todo: what would this do on the GUI?
        events.on('pre-uninstall', 1, function(app, done) {

          // Keep refernece for later.
          var self = this;

          // Get list of app names.
          util.getAppContainers(app.name)
          // Map app names to an object.
          .then(function(containers) {
            // Reduce app names to an object of app stats.
            return Promise.reduce(containers, function(result, container) {
              return result || container.running;
            });
          })

          .then(function(appIsRunning) {
            // Turn off the engine if we dont have any running apps
            if (appIsRunning) {
              return kbox.app.stop(app);
            }
          })

          .nodeify(done);

        });

        // Remove the data container and the code directory
        // @todo: what would this do on the GUI?
        events.on('pre-uninstall', 2, function(app, done) {

          /*
           * Helper method to promisigy fs.exists
           */
          var existsAsync = function() {
            return new Promise(function(exists) {
              fs.exists(containerIdFile, exists);
            });
          };

          var containerIdFile = path.join(app.config.appCidsRoot, 'data');

          // Check to see if we have a data container to remove
          return existsAsync()

          // If it does we need to remove the container
          .then(function(exists) {
            if (exists) {
              var containerId = fs.readFileSync(containerIdFile, 'utf8');
              // Clean up the container ID file
              fs.unlinkSync(containerIdFile);
              // Remove the data container
              return kbox.engine.remove(containerId);
            }
          })

          // Remove our code directory
          // @todo: user kbox.log
          .then(function() {
            console.log('Removing the codez.');
            return Promise.fromNode(function(done) {
              rmdir(app.config.codeRoot, done);
            });
          })

          .nodeify(done);

        });

        // Actually do the uninstall
        kbox.app.uninstall(app, done);
      };
    });

  });

};
{'fraction_non_alphanumeric': 0.11695108077360637, 'fraction_numerical': 0.0015927189988623437, 'mean_word_length': 1.7613065326633166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4025596', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1204, 'n_words': 336}