Skip to content

Commit

Permalink
fix #45
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitgraig committed May 20, 2016
1 parent aa5e77b commit 3ed8bf5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -13,7 +13,7 @@
> Inject references to files into other files (think scripts and stylesheets into an html file)
## Getting Started
This plugin requires Grunt `~0.4.1`
This plugin requires Grunt `>=0.4.x`

If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

Expand Down
53 changes: 38 additions & 15 deletions tasks/injector.js
Expand Up @@ -52,24 +52,14 @@ module.exports = function(grunt) {
grunt.log.writeln('Missing option `template`, using `dest` as template instead'.grey);
useDestTpl = true;
}

if (!options.lineEnding) {
var destination = options.template || options.templateString;
var contents = '';
if (useDestTpl) {
destination = options.destFile;
}
if (options.templateString) {
contents = options.templateString;
} else {
contents = String(grunt.file.read(destination));
}
var returnType = /\r\n/.test(contents) ? '\r\n' : '\n';
options.lineEnding = returnType;
}

var filesToInject = {};

if (!options.lineEnding) {
var that = this;
options.lineEnding = getDefaultLineEnding(options, that, grunt);
}

// Iterate over all specified file groups and gather files to inject:

this.files.forEach(function(f) {
Expand Down Expand Up @@ -299,3 +289,36 @@ function getIndentedTransformations (sources, indent, lineEnding) {
return transformations.join(lineEnding + indent);
}


function getDefaultLineEnding(options, that, grunt) {
var contents = '';

// when destination file is a template
var destination = options.template || options.templateString;

// when destination file is destFile
if (options.destFile) {
destination = options.destFile;
}

// if the destination file does not exist yet
// try to figure out lineEnding through src files
if (!grunt.file.exists(that.files[0].dest)) {
destination = that.filesSrc[0];
} else {
destination = that.files[0].dest;
}

if (typeof destination === 'undefined') {
grunt.log.error('No template found, unable to guess line ending character.');
} else {
if (options.templateString) {
contents = options.templateString;
} else {
contents = String(grunt.file.read(destination));
}
}

return /\r\n/.test(contents) ? '\r\n' : '\n';
}

0 comments on commit 3ed8bf5

Please sign in to comment.