Skip to content

Commit

Permalink
Updated activate/deactive to pass tests #20
Browse files Browse the repository at this point in the history
- refactored code in writing prio to tasks.js
- changed integ tests to symlink the dir to simulate real behavior
  exactly (and not letting unlinking fail)
  • Loading branch information
sthzg committed Jul 8, 2016
1 parent 811326d commit 443283e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
21 changes: 9 additions & 12 deletions generators/activate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

const fs = require('fs');
const generators = require('yeoman-generator');

const tasks = require('../../utils/tasks');
const utils = require('../../utils/utils');



class Generator extends generators.Base {

constructor(...args) {
Expand All @@ -15,7 +17,6 @@ class Generator extends generators.Base {
tasks.makeSubgenAware(this);
}


get initializing() {
return {
loadSubgenConfig() {
Expand All @@ -39,7 +40,6 @@ class Generator extends generators.Base {
};
}


get default() {
return {
scanForInstalledSubgens() {
Expand All @@ -66,28 +66,25 @@ class Generator extends generators.Base {
};
}


get writing() {
return {

/**
* Copies the subgen to the host generator's directory.
*/
copySubgen() {
fs.symlinkSync(this.subgenSrc, this.subgenDest);
activateSubgen() {
tasks.activateSubgen(this);
}

}
}

get end() {
return {
output() {
this.log.ok(`Activated ${this.subgenName}!`);
if (this.genData.activation.get('hasError')) {
this.log.error(this.genData.activation.getIn(['data', 'err', 'data']));
} else {
this.log.ok(`Activated ${this.subgenName}!`);
}
}
}
}

}

module.exports = Generator;
21 changes: 8 additions & 13 deletions generators/deactivate/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var existsSync = require('fs').existsSync;
const fs = require('fs');
const generators = require('yeoman-generator');
const rimraf = require('rimraf');
const tasks = require('../../utils/tasks');


Expand All @@ -15,7 +15,6 @@ class Generator extends generators.Base {
tasks.makeSubgenAware(this);
}


get initializing() {
return {
loadSubgenConfig() {
Expand All @@ -39,7 +38,6 @@ class Generator extends generators.Base {
};
}


get default() {
return {
scanForInstalledSubgens() {
Expand All @@ -66,28 +64,25 @@ class Generator extends generators.Base {
};
}


get writing() {
return {

/**
* Removes the subgen from the hostgen directory.
*/
deleteSubgen() {
fs.unlinkSync(this.subgenDest);
deactivateSubgen() {
tasks.deactivateSubgen(this);
}

}
}

get end() {
return {
output() {
this.log.ok(`Deactivated ${this.subgenName}!`);
if (this.genData.deactivation.get('hasError')) {
this.log.error(this.genData.deactivation.getIn(['data', 'err', 'data']));
} else {
this.log.ok(`Deactivated ${this.subgenName}!`);
}
}
}
}

}

module.exports = Generator;
2 changes: 1 addition & 1 deletion test/_helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function moveDefaultFiles(dir, includeSubgenextJson = false, includeExtgen = fal
}

if (includeExtgen) {
fs.copySync(
fs.symlinkSync(
path.join(nodeModDir, 'contrib-subgen-yoburger-bbq/generators/bbq'),
path.join(dir, '/node_modules/generator-yoburger/generators/bbq')
);
Expand Down
45 changes: 43 additions & 2 deletions utils/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

'use strict';

const constants = require('./constants');
const fs = require('fs');
const Immutable = require('immutable');

const constants = require('./constants');
const path = require('path');
const utils = require('./utils');

Expand All @@ -24,6 +26,13 @@ function injectDefaultConstructor(generator) {
*/
generator.availableExtgens = [];

/**
* Free to use message object on the generator.
* Generators may use this pojo to store arbitrary data on the generator (e.g. to pass data to another priority).
* @type {{}}
*/
generator.genData = {};

/**
* Basename of the host generator.
* @type String
Expand All @@ -45,7 +54,6 @@ function injectDefaultConstructor(generator) {
/**
* Json representation of all installed packages in depth=0
* We cache this value in a variable since invoking the shell command is expensive and slow.
*
* @type {?Json}
*/
generator.pkgList = null;
Expand Down Expand Up @@ -102,6 +110,37 @@ function makeSubgenAware(generator) {
}


/**
* Activates an external subgen by linking inside the hostgen.
* @param generator
*/
function activateSubgen(generator) {
if (!fs.existsSync(generator.subgenDest)) {
fs.symlinkSync(generator.subgenSrc, generator.subgenDest);
generator.genData.activation = utils.buildSuccess({});
} else {
const err = `Subgen with name "${generator.subgenName}" is already activated. If you want to update it, make sure
to deactivate it first. This is a NOOP`;
generator.genData.activation = utils.buildError(err);
}
}


/**
* Activates an external subgen by unlinking it from the hostgen.
* @param generator
*/
function deactivateSubgen(generator) {
if (fs.existsSync(generator.subgenDest)) {
fs.unlinkSync(generator.subgenDest);
generator.genData.deactivation = utils.buildSuccess({});
} else {
const err = `Subgen with name "${generator.subgenName}" doesn't seem to be activated. This is a NOOP.`;
generator.genData.deactivation = utils.buildError(err);
}
}


function loadSubgenConfig(generator) {
const cfgPath = path.join(generator.env.cwd, 'subgenext.json');
if (generator.fs.exists(cfgPath)) {
Expand Down Expand Up @@ -252,8 +291,10 @@ function validateCompatibility() {


module.exports = {
activateSubgen,
cacheInstalledPackages,
checkActivationState,
deactivateSubgen,
setSubgenDestPath,
setSubgenPkg,
setSubgenSrcPath,
Expand Down
2 changes: 2 additions & 0 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ function sortCharArrByLength(arr, desc=true) {


module.exports = {
buildError,
buildPrefixRegexps,
buildSuccess,
checkActivationState,
checkPkgExists,
getPkgInfo,
Expand Down

0 comments on commit 443283e

Please sign in to comment.