Skip to content

Commit

Permalink
feat(all generators): replace custom determineRoot() sub-generator …
Browse files Browse the repository at this point in the history
…functionality with the Yeoman default '.yo-rc.json' based one.

Let the app generator create a '.yo-rc.json' config file, containing a 'version' entry ' entry, designating the generator's version.
Sub-generators use the presence of this entry to assert whether they are running as part of a BAT app project.
Yeoman itself will take care of locating the project root.
  • Loading branch information
cueedee committed May 7, 2015
1 parent b514a31 commit 0435ab4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 174 deletions.
25 changes: 24 additions & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ var tpl_tpl_settings =

module.exports = generators.Base.extend(
{
prompting:
initializing: function ()
{
// Load the BAT generator's 'package.json'.
//
this.pkg = require( './../../package.json' );
}

, prompting:
{
askSomeQuestions: function ()
{
Expand Down Expand Up @@ -101,6 +108,22 @@ module.exports = generators.Base.extend(
}
}

, configuring: function ()
{
//
// Save a '.yo-rc.json' config file.
// At the very least this marks your project root for sub-generators.
//
// Note that answers to prompts that have `store: true` defined aren't stored here, but in '~/.yo-rc-global.json'.
//

this.config.set(
{
'generator-version': this.pkg.version
}
);
}

, writing:
{
setupDirectoryStructure: function ()
Expand Down
68 changes: 11 additions & 57 deletions generators/collection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,14 @@
var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
, varname = require( 'varname' )
, fs = require( 'fs' )
, _ = require( 'lodash' )
;

module.exports = generators.Base.extend(
var CollectionGenerator = generators.Base.extend(
{
initializing:
initializing: function ()
{
// Function is used to determine if we are currently in the root off the project
// if not, try to find the root and change to that directory
//
determineRoot: function ()
{
var done = this.async()
, rootFound = false
, tries = 0
;

// Get the current running directory name
//
var fullPath = process.cwd()
, rootLocation = fullPath
;

if ( fs.existsSync( 'src' ) === false )
{
while ( rootFound === false && tries < 10 )
{
// Split old path
//
var previousLocation = rootLocation.split( '/' );

// Pop the last folder from the path
//
previousLocation.pop();

// Create the new path and open it
//
rootLocation = previousLocation.join( '/' );

// Change the process location
//
process.chdir( rootLocation );

// Check if we found the project root, up the counter
// we should stop looking some time.....
//
rootFound = fs.existsSync( 'src' );
tries++;
}

// If we couldn't find the root, let the user know and exit the proces...
//
if ( rootFound === false )
{
yeoman.log( 'Failed to find root of the project, check that you are somewhere within your project.' );
process.exit();
}
}

done();
}
this._assertBatApp();
}

, prompting:
Expand Down Expand Up @@ -189,3 +136,10 @@ module.exports = generators.Base.extend(
}
}
);

_.merge(
CollectionGenerator.prototype
, require( './../../lib/sub-generator.js' )
);

module.exports = CollectionGenerator;
69 changes: 11 additions & 58 deletions generators/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
, varname = require( 'varname' )
, fs = require( 'fs' )
, _ = require( 'lodash' )
;

module.exports = generators.Base.extend(
var ModelGenerator = generators.Base.extend(
{
constructor: function ( args, options )
{
Expand All @@ -37,63 +37,9 @@ module.exports = generators.Base.extend(
}
}

, initializing:
, initializing: function ()
{
// Function is used to determine if we are currently in the root off the project
// if not, try to find the root and change to that directory
//
determineRoot: function ()
{
var done = this.async()
, rootFound = false
, tries = 0
;

// Get the current running directory name
//
var fullPath = process.cwd()
, rootLocation = fullPath
;


if ( fs.existsSync( 'src' ) === false )
{
while ( rootFound === false && tries < 10 )
{
// Split old path
//
var previousLocation = rootLocation.split( '/' );

// Pop the last folder from the path
//
previousLocation.pop();

// Create the new path and open it
//
rootLocation = previousLocation.join( '/' );

// Change the process location
//
process.chdir( rootLocation );

// Check if we found the project root, up the counter
// we should stop looking some time.....
//
rootFound = fs.existsSync( 'src' );
tries++;
}

// If we couldn't find the root, let the user know and exit the proces...
//
if ( rootFound === false )
{
yeoman.log( 'Failed to find root of the project, check that you are somewhere within your project.' );
process.exit();
}
}

done();
}
this._assertBatApp();
}

, prompting:
Expand Down Expand Up @@ -163,3 +109,10 @@ module.exports = generators.Base.extend(
}
}
);

_.merge(
ModelGenerator.prototype
, require( './../../lib/sub-generator.js' )
);

module.exports = ModelGenerator;
68 changes: 11 additions & 57 deletions generators/view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,14 @@
var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
, varname = require( 'varname' )
, fs = require( 'fs' )
, _ = require( 'lodash' )
;

module.exports = generators.Base.extend(
var ViewGenerator = generators.Base.extend(
{
initializing:
initializing: function ()
{
// Function is used to determine if we are currently in the root off the project
// if not, try to find the root and change to that directory
//
determineRoot: function ()
{
var done = this.async()
, rootFound = false
, tries = 0
;

// Get the current running directory name
//
var fullPath = process.cwd()
, rootLocation = fullPath
;

if ( fs.existsSync( 'src' ) === false )
{
while ( rootFound === false && tries < 10 )
{
// Split old path
//
var previousLocation = rootLocation.split( '/' );

// Pop the last folder from the path
//
previousLocation.pop();

// Create the new path and open it
//
rootLocation = previousLocation.join( '/' );

// Change the process location
//
process.chdir( rootLocation );

// Check if we found the project root, up the counter
// we should stop looking some time.....
//
rootFound = fs.existsSync( 'src' );
tries++;
}

// If we couldn't find the root, let the user know and exit the proces...
//
if ( rootFound === false )
{
yeoman.log( 'Failed to find root of the project, check that you are somewhere within your project.' );
process.exit();
}
}

done();
}
this._assertBatApp();
}

, prompting:
Expand Down Expand Up @@ -169,3 +116,10 @@ module.exports = generators.Base.extend(
}
}
);

_.merge(
ViewGenerator.prototype
, require( './../../lib/sub-generator.js' )
);

module.exports = ViewGenerator;
27 changes: 27 additions & 0 deletions lib/sub-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Mix in common ground for sub-generators.
//

'use strict';

var chalk = require( 'chalk' );

module.exports =
{
_assertBatApp: function ()
{
/* jshint laxbreak: true */

if ( !( this.config.get( 'generator-version' ) ))
{
this.env.error(
chalk.bold.red( 'I cannot find a BAT app to hook into!\n' )
+ '\n'
+ 'Is your current working directory part of your project tree?\n'
+ 'Does your project root have a valid "' + chalk.bold( '.yo-rc.json' ) + '" file?\n'
+ 'If you have never done so before, please run "' + chalk.yellow.bold( 'yo bat' ) + '" from your project root first.\n'
);
}
}
}
;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"name": "Raymond de Wit"
},
"dependencies": {
"chalk": "^1.0.0",
"varname": "^1.0.2",
"yeoman-generator": "^0.18.10",
"yosay": "^1.0.3"

},
"description": "BAT ( Backbone Application Template ). A yeoman-generator created by Marviq.",
"devDependencies": {
Expand Down

0 comments on commit 0435ab4

Please sign in to comment.