Skip to content

Commit

Permalink
fix(generators/view): fix ordered insert
Browse files Browse the repository at this point in the history
Fix not exiting search when correct position was found.
Make insertion aware of indentation context.
Refactor already-done detection.
  • Loading branch information
cueedee committed Aug 29, 2016
1 parent 31a4129 commit 7fa2653
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions generators/view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,27 @@ var ViewGenerator = generators.Base.extend(
, statement = '@import "views/_' + data.fileBase + '"'
;

// Look for a place to insert, preferably at an alfanumerically ordered position.
// Do nothing if an `@import` for this sass file seems to exist already.
//
if ( views.indexOf( statement ) !== -1 )
var insertAt, indent, match, matcher = /^([ \t]*)(@import.*)/mg;

while ( (( match = matcher.exec( views ) )) )
{
if ( statement > match[ 2 ] )
{
// Use indent of the (possibly) preceding line.
//
indent = match[ 1 ];
continue;
}

if ( statement < match[ 2 ] )
{
insertAt = match.index;
break;
}

this.log(
'It appears that "' + viewsPath + '" already contains an `@import` for "' + data.fileBase + '.sass".\n'
+ 'Leaving it untouched.'
Expand All @@ -201,28 +218,26 @@ var ViewGenerator = generators.Base.extend(
return;
}

// Avoid the conflict warning and use force for the write
// Avoid the conflict warning and use force for the write
//
this.conflicter.force = true;

// Look for a place to insert, preferably at an alfanumerically ordered position.
//
var insertAt, match, matcher = /^@import.*/mg;

while ( (( match = matcher.exec( views ) )) )
if ( indent == null )
{
if ( statement < match[ 0 ] ) { insertAt = match.index; }
// First @import; use indent of the following line, if any.
//
indent = match ? match[ 1 ] : '';
}

if ( insertAt == null )
{
var pad = (( views.length && views.slice( -1 ) !== '\n' ) ? '\n' : '' );

fs.write( viewsPath, views + pad + statement + '\n' );
// Append at end of file; Take care of possibly missing trailing newline.
//
fs.write( viewsPath, views + (( views.length && views.slice( -1 ) !== '\n' ) ? '\n' : '' ) + indent + statement + '\n' );
}
else
{
fs.write( viewsPath, views.slice( 0, insertAt ) + statement + '\n' + views.slice( insertAt ) );
fs.write( viewsPath, views.slice( 0, insertAt ) + indent + statement + '\n' + views.slice( insertAt ) );
}
}
}
Expand Down

0 comments on commit 7fa2653

Please sign in to comment.