All files / piscosour/steps/add-steps index.js

0% Statements 0/41
0% Branches 0/8
0% Functions 0/7
0% Lines 0/41
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                                                                                                                                                                             
'use strict';
 
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
 
module.exports = {
 
  showContexts() {
    const choices = [];
    choices.push({
      name: 'all contexts',
      value: '*'
    });
    const piscoConfig = this.piscoConfig.get();
    Object.getOwnPropertyNames(piscoConfig.contexts).forEach((name) => {
      choices.push({
        name: `${piscoConfig.contexts[name].name} (${piscoConfig.contexts[name].description})`,
        value: name
      });
    });
    return choices;
  },
 
  check: function(go, stop) {
    this.logger.info('#magenta', 'check', 'Check if this is a piscosour recipe');
    const dest = path.join('steps', this.params.stepName);
    if (this.fsExists(dest)) {
      stop('Step "' + this.params.stepName + '" already exists for context: "' + this.params.context + '" in this recipe, edit it to change!');
    }
  },
 
  run: function(resolve, reject) {
    this.logger.info('#magenta', 'run', 'Creating new step for this recipe');
 
    const dest = path.join('steps', this.params.stepName);
    const origin = path.join(this.piscoConfig.getDir('piscosour'), 'templates', '_step');
 
    this.fsCreateDir('steps');
    this.fsCreateDir(path.join('steps', this.params.stepName));
    this.fsCreateDir(dest);
 
    const _writeConfig = () => {
      try {
        const configFile = path.join(dest, 'config.json');
 
        let config = this.fsReadConfig(configFile);
        config.name = this.params.stepName;
        if (this.params.context === '*') {
          config.contexts = this.params.context;
        } else {
          config.contexts.push(this.params.context);
        }
 
        fs.writeFileSync(configFile, JSON.stringify(config, null, 2));
 
        return Promise.resolve();
      } catch (err) {
        return Promise.reject(err);
      }
    };
 
    return this.fsCopyDirFiltered(origin, dest)
      .then(() => _writeConfig())
      .then(resolve, reject);
  },
 
  prove: function(resolve, reject) {
    this.logger.info('#magenta', 'prove', 'Prove if the step is propelly executed');
    const dest = path.join('steps', this.params.stepName);
    this.sh('node bin/pisco.js -w');
    const result = this.sh(`node bin/pisco.js ${this.params.context === '*' ? 'all' : this.params.context}::${this.params.stepName} --b-disableContextCheck`, reject, false);
    if (result.status !== 0) {
      this.logger.error('#red', 'Error: step not propelly created!', 'cleaning files!');
      rimraf.sync(dest);
    }
  },
 
  emit: function() {
    return {
      stepName: this.params.stepName,
      context: this.params.context
    };
  }
 
};