Skip to content

Commit

Permalink
Merge pull request #2 from Zolmeister/master
Browse files Browse the repository at this point in the history
Added support for multiple destinations
  • Loading branch information
scott-laursen committed Aug 1, 2013
2 parents 8038fcf + 8570a9c commit 10257ac
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function(grunt) {
appRoot: 'test/'
},
files: {
'test/fixtures/file.html': 'test/fixtures/*.js'
'test/fixtures/**/*.html': 'test/fixtures/*.js'
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013 scott-laursen
Copyright (c) 2013 scott-laursen, Zolmeister

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# grunt-scriptlinker
# grunt-sails-linker

> Autoinsert script tags (or other filebased tags) in an html file
Expand All @@ -8,23 +8,23 @@ This plugin requires Grunt `~0.4.x`
When the task is run the destination file(s) is updated with script tags pointing to all the source files. The reason this plugin was built was to automate the process of inserting script tags when building large web apps.

```shell
npm install grunt-scriptlinker --save-dev
npm install grunt-sails-linker --save-dev
```

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
grunt.loadNpmTasks('grunt-scriptlinker');
grunt.loadNpmTasks('grunt-sails-linker');
```

## The "scriptlinker" task
## The "sails-linker" task

### Overview
In your project's Gruntfile, add a section named `scriptlinker` to the data object passed into `grunt.initConfig()`.
In your project's Gruntfile, add a section named `sails-linker` to the data object passed into `grunt.initConfig()`.

```js
grunt.initConfig({
scriptlinker: {
'sails-linker': {
defaultOptions: {
options: {
startTag: '<!--SCRIPTS-->',
Expand Down
44 changes: 20 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
{
"name": "grunt-scriptlinker",
"name": "grunt-sails-linker",
"description": "Autoinsert script tags in an html file",
"version": "0.1.1",
"homepage": "https://github.com/scott-laursen/grunt-scriptlinker",
"author": {
"name": "scott-laursen",
"email": "thomas@addgo.com",
"url": "www.addgo.com"
},
"version": "0.9.2",
"homepage": "https://github.com/Zolmeister/grunt-sails-linker",
"contributors": [
"Zolmeister <zolikahan@gmail.com> (http://zolmeister.com)",
"scott-laursen <thomas@addgo.com> (www.addgo.com)"
],
"repository": {
"type": "git",
"url": "git://github.com/scott-laursen/grunt-scriptlinker.git"
},
"bugs": {
"url": "https://github.com/scott-laursen/grunt-scriptlinker/issues"
"url": "https://github.com/Zolmeister/grunt-sails-linker.git"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/scott-laursen/grunt-scriptlinker/blob/master/LICENSE-MIT"
}
],
"main": "Gruntfile.js",
"engines": {
"node": ">= 0.8.0"
Expand All @@ -29,12 +19,18 @@
"test": "grunt test"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt": "~0.4.0rc7"
"grunt-contrib-jshint": "0.1.1",
"grunt-contrib-clean": "0.4.0",
"grunt-contrib-nodeunit": "0.1.2",
"grunt": "0.4.0rc7"
},
"keywords": [
"gruntplugin"
]
}
],
"readmeFilename": "README.md",
"gitHead": "efb0eaaf4e518cc5ff646955a2e03b91ac8b70fb",
"directories": {
"test": "test"
},
"license": "MIT"
}
32 changes: 18 additions & 14 deletions tasks/scriptlinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function(grunt) {
// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks

grunt.registerMultiTask('scriptlinker', 'Your task description goes here.', function() {
grunt.registerMultiTask('sails-linker', 'Your task description goes here.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
startTag: '<!--SCRIPTS-->',
Expand All @@ -42,26 +42,30 @@ module.exports = function(grunt) {
} else { return true; }
}).map(function (filepath) {
return util.format(options.fileTmpl, filepath.replace(options.appRoot, ''));
}).join('');


if (!grunt.file.exists(f.dest)) {
grunt.log.warn('Destination file "' + f.dest + '" not found.');
} else {
page = grunt.file.read(f.dest);
});

grunt.file.expand({}, f.dest).forEach(function(dest){
page = grunt.file.read(dest);
start = page.indexOf(options.startTag);
end = page.indexOf(options.endTag);

end = page.indexOf(options.endTag);
if (start === -1 || end === -1 || start >= end) {
grunt.log.warn('Destination file\'s "' + f.dest + '" start and/or end tag is not correctly setup.');
return;
} else {
newPage = page.substr(0, start + options.startTag.length) + scripts + page.substr(end);
var padding ='';
var ind = start - 1;
// TODO: Fix this hack
while(page.charAt(ind)===' ' || page.charAt(ind)===' '){
padding += page.charAt(ind);
ind -= 1;
}
console.log('padding length', padding.length)
newPage = page.substr(0, start + options.startTag.length)+'\n' + padding + scripts.join('\n'+padding) + '\n' + padding + page.substr(end);
// Insert the scripts
grunt.file.write(f.dest, newPage);
grunt.log.writeln('File "' + f.dest + '" updated.');
grunt.file.write(dest, newPage);
grunt.log.writeln('File "' + dest + '" updated.');
}
}
});
});
});

Expand Down

0 comments on commit 10257ac

Please sign in to comment.