Skip to content

Commit

Permalink
Released Version v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xitedemon committed Dec 11, 2020
1 parent b2e9b60 commit 0272c23
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "grunt-file-comment",
"version": "1.0.0",
"description": "Add Comments To Files.",
"main": "tasks/file-comment.js",
"name": "grunt-file-comment",
"scripts": {},
"engines": {
"node": ">= 8.9.4",
"npm": ">= 5.6.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/xitedemon/grunt-file-comment.git"
},
"keywords": [],
"author": "xitedemon",
"license": "ISC",
"bugs": {
"url": "https://github.com/xitedemon/grunt-file-comment/issues"
},
"homepage": "https://github.com/xitedemon/grunt-file-comment#readme"
}
52 changes: 52 additions & 0 deletions tasks/file-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module.exports = function (grunt) {
'use strict';
var Path = require('path');
grunt.registerMultiTask('file-comment', 'add comments to file', function () {
var options = this.options({
comments: ['autogenerated'],
carriageReturn: "\n",
prepend: true,
syntaxes: {
'*': '//',
}
});
if(!options.syntaxes['*'])
options.syntaxes['*'] = '//';

var count = 0;

var comment = '', content, extension, commentSyntax;
this.files.forEach(function(el) {

el.src.filter(function(filePath) {
var witness = grunt.file.exists(filePath);
if(!witness)
grunt.log.warn('Source file "' + filePath + '" not found.');

return witness;
}).map(function(filePath) {
content = grunt.file.read(filePath);

extension = Path.extname(filePath);
commentSyntax = ( extension in options.syntaxes ? options.syntaxes[extension] : options.syntaxes['*'] );


comment = '';
for(var i = 0; i<options.comments.length; i++) {
if(typeof commentSyntax === 'string')
comment += commentSyntax + ' ' + options.comments[i] + options.carriageReturn;
else
comment += commentSyntax[0] + ' ' + options.comments[i] + (commentSyntax.length > 1 ? ' ' + commentSyntax[1] : '') + options.carriageReturn;
}

content = options.prepend ? comment + grunt.file.read(el.src[0]) : grunt.file.read(el.src[0]) + options.carriageReturn + comment;

grunt.file.write(el.dest, content);
grunt.log.debug('File "' + el.dest + '" edited.');
count++;
});

});
grunt.log.ok(count + ' files edited');
});
};

0 comments on commit 0272c23

Please sign in to comment.