UNPKG

runas-core

Version:

The adhesive orchestrator

87 lines (71 loc) 2.62 kB
'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 runasConfig = this.runasConfig.get(); Object.getOwnPropertyNames(runasConfig.contexts).forEach((name) => { choices.push({ name: `${runasConfig.contexts[name].name} (${runasConfig.contexts[name].description})`, value: name }); }); return choices; }, check: function(go, stop) { this.logger.info('#magenta', 'check', 'Check if this is a runas 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.runasConfig.getDir('runas'), '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/runas.js -w'); const result = this.sh(`node bin/runas.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 }; } };