Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue when several destination files are indicated #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ module.exports = function(grunt) {
src: ['*.js','*.css'],
dest: 'tmp/withPrefixAndSuffix.html'
},
// we want to generates several files at the same time
severalFiles : {
files : [
{ cwd: "test/fixtures/", src: ['first.js','first.css'], dest: 'tmp/first.html'},
{ cwd: "test/fixtures/", src: ['second.js','second.css'], dest: 'tmp/second.html'},
]

}
},

// Unit tests.
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
> Create templated list of files

## Getting Started
This plugin requires Grunt `~0.4.1`
This plugin requires Grunt `~1.0.1`

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:

```shell
npm install grunt-files-list --save-dev
npm install grunt-files-list2 --save-dev
```

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

```js
grunt.loadNpmTasks('grunt-files-list');
grunt.loadNpmTasks('grunt-files-list2');
```

## The "files_list" task
Expand Down Expand Up @@ -87,4 +87,8 @@ grunt.initConfig({
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).

## Release History
_(Nothing yet)_

* 2017-09-13 v0.2.0 Compatibility with Grunt 1.0.1, and rename package to grunt-file-list2
* 2014-01-22 v0.1.3 Fixed issue when several destination files are indicated
* 2013-08-20   v0.1.2   First stable version

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "grunt-files-list",
"name": "grunt-files-list2",
"description": "Create templated list of files",
"version": "0.1.2",
"homepage": "https://github.com/motorin/grunt-files-list",
"version": "0.2.0",
"homepage": "https://github.com/laurentj/grunt-files-list2",
"author": {
"name": "Motorin",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "git://github.com/motorin/grunt-files-list.git"
"url": "git://github.com/laurentj/grunt-files-list2.git"
},
"bugs": {
"url": "https://github.com/motorin/grunt-files-list/issues"
"url": "https://github.com/laurentj/grunt-files-list2/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/motorin/grunt-files-list/blob/master/LICENSE-MIT"
"url": "https://github.com/laurentj/grunt-files-list2/blob/master/LICENSE-MIT"
}
],
"main": "Gruntfile.js",
Expand All @@ -28,13 +28,13 @@
"test": "grunt test"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt": "~0.4.1"
"grunt-contrib-jshint": "~1.1.0",
"grunt-contrib-clean": "~1.1.0",
"grunt-contrib-nodeunit": "~1.0.0",
"grunt": "~1.0.1"
},
"peerDependencies": {
"grunt": "~0.4.1"
"grunt": "~1.0.1"
},
"keywords": [
"gruntplugin",
Expand Down
18 changes: 6 additions & 12 deletions tasks/files_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@

module.exports = function(grunt) {

// Link to Underscore.js
var _ = grunt.util._;
var path = require('path');

// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks

grunt.registerMultiTask('files_list', 'Create files list in for jincluding in HTML', function() {
grunt.registerMultiTask('files_list', 'Create files list in for including in HTML', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
jsTemplate: '<script src="<%= pathPrefix %><%= filename %><%= pathSuffix %>"></script>',
Expand All @@ -30,8 +28,6 @@ module.exports = function(grunt) {
separator: '\r\n'
});

var outputHTML = "";
var targetFile = "";
var filesCounter = 0;
// Iterate over all specified file groups.
this.files.forEach(function(f) {
Expand All @@ -53,9 +49,9 @@ module.exports = function(grunt) {
filesCounter++;
switch(path.extname(filepath)){
case ".js":
return _.template(options.jsTemplate, {filename: filepath, pathPrefix: options.pathPrefix, pathSuffix: options.pathSuffix});
return grunt.template.process(options.jsTemplate, {data:{filename: filepath, pathPrefix: options.pathPrefix, pathSuffix: options.pathSuffix}});
case ".css":
return _.template(options.cssTemplate, {filename: filepath,pathPrefix: options.pathPrefix, pathSuffix: options.pathSuffix});
return grunt.template.process(options.cssTemplate, {data:{filename: filepath,pathPrefix: options.pathPrefix, pathSuffix: options.pathSuffix}});
default:
filesCounter--;
grunt.log.warn('Unrecognized file extension: ' + path.extname(filepath) + '. Must be .js or .css');
Expand All @@ -64,15 +60,13 @@ module.exports = function(grunt) {
}).join(grunt.util.normalizelf(options.separator));

// src += options.punctuation;
targetFile = f.dest;
outputHTML = src;
// Write the destination file.
grunt.file.write(f.dest, src);
grunt.log.writeln('File "' + f.dest + '" created.');
});
// Write the destination file.
grunt.file.write(targetFile, outputHTML);

// Print a success message.
grunt.log.writeln('Result script/link tags: ' + filesCounter + '.');
grunt.log.writeln('File "' + targetFile + '" created.');

});

Expand Down
2 changes: 2 additions & 0 deletions test/expected/first.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="first.js"></script>
<link rel="stylesheet" type="text/css" href="first.css"></link>
2 changes: 2 additions & 0 deletions test/expected/second.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="second.js"></script>
<link rel="stylesheet" type="text/css" href="second.css"></link>
12 changes: 12 additions & 0 deletions test/files_list_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ exports.files_list = {

test.done();
},
several_files: function(test) {
test.expect(2);

var actual = grunt.file.read('tmp/first.html');
var expected = grunt.file.read('test/expected/first.html');
test.equal(actual, expected, 'first generated file is ok');

actual = grunt.file.read('tmp/second.html');
expected = grunt.file.read('test/expected/second.html');
test.equal(actual, expected, 'second generated file is ok');
test.done();
},
};