Skip to content

Commit

Permalink
feat(generators/app): safe-guard against running in non-empty destina…
Browse files Browse the repository at this point in the history
…tion roots
  • Loading branch information
cueedee committed Oct 11, 2017
1 parent c862fba commit 6969a74
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
15 changes: 15 additions & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ class AppGenerator extends Generator
[
{
type: 'input'
, name: 'iKnowWhatImDoing'
, message: chalk.redBright(
chalk.inverse( ' Whoa there!! It would appear that your target directory is not empty! ' )
+ ' Type "YES" if you would like to proceed anyway.'
)
, default: ''
, validate: ( iKnowWhatImDoing ) =>
{
if ( 'YES' === iKnowWhatImDoing ) { return true; }
this.env.error( 'Bailing out' );
}
, when: !( this._isRootSafe() )
}
, {
type: 'input'
, name: 'packageName'
, message: (
'What is the name of this '
Expand Down
23 changes: 22 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ module.exports =
* @param {Array} prompts An array of prompt definitions as would be passed to `this.prompt()`.
*
* @return {Array} The prompts for which no valid value has been supplied through a command line option.
*
*/

_promptsPruneByOptions: function ( prompts )
Expand Down Expand Up @@ -188,5 +187,27 @@ module.exports =
}
);
}

,
/**
* Test whether the destination root is safely empty for generating an app in.
*
* @method _isRootSafe
*
* @return {Boolean Whether the destination root is safely empty for generating an app in.
*/

_isRootSafe: function ()
{
var root = this.destinationRoot()
, entries = fsCore.readdirSync( root )
, safe =
[
'.git'
]
;

return 0 === _.difference( entries, safe ).length;
}
}
;

0 comments on commit 6969a74

Please sign in to comment.