extract-comments
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -8,4 +8,44 @@ # {%= name %} {%= badge('fury') %} | ||
## Usage | ||
```js | ||
var extract = require('extract-comments'); | ||
``` | ||
Pass file paths or glob patterns for the files with comments to be extracted: | ||
```js | ||
extract('lib/*.js'); | ||
``` | ||
Results in an object like this: | ||
```js | ||
{ | ||
"somefile": { | ||
"filepath": "lib/somefile.js", | ||
"comments": [ | ||
// array of code comments here | ||
] | ||
}, | ||
"anotherfile": { | ||
"filepath": "lib/another.js", | ||
"comments": [ | ||
// array of code comments here | ||
] | ||
} | ||
// ... etc | ||
} | ||
``` | ||
Pass `true` as a second parameter to generate an array instead of an object. | ||
## Tests | ||
Run `mocha` | ||
## Author | ||
{%= author.name %} | ||
{%= contrib("jon") %} | ||
@@ -12,0 +52,0 @@ ## License |
44
index.js
@@ -1,29 +0,33 @@ | ||
var file = require('fs-utils'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var glob = require('globule'); | ||
var esprima = require('esprima'); | ||
// var extract = function (code) { | ||
// return esprima.parse(code, { comment: true }) | ||
// .comments.map(function (comment) { | ||
// return comment.value; | ||
// }).join('\n\n'); | ||
// }; | ||
var extract = function (code) { | ||
var extractComments = function (code) { | ||
return esprima.parse(code, { comment: true }) | ||
.comments.map(function (comment) { | ||
return comment.value; | ||
}).join('\n\n'); | ||
return comment; | ||
}); | ||
}; | ||
module.exports = function(patterns, arr) { | ||
var comments = arr ? [] : {}; | ||
var files = glob.find(patterns); | ||
var files = file.find('test/fixtures/*.js'); | ||
files.forEach(function(filepath) { | ||
var name = path.basename(filepath, path.extname(filepath)); | ||
var str = fs.readFileSync(filepath, 'utf8'); | ||
files.forEach(function(filepath) { | ||
var str = file.readFileSync(filepath); | ||
var comments = extract(str); | ||
// console.log(comments); | ||
console.log(JSON.stringify(comments, null, 2)); | ||
}); | ||
var fileComments = { | ||
filepath: filepath, | ||
comments: extractComments(str) | ||
}; | ||
module.exports = extract; | ||
if (arr) { | ||
comments.push(fileComments); | ||
} else { | ||
comments[name] = fileComments; | ||
} | ||
}); | ||
return comments; | ||
}; |
{ | ||
"name": "extract-comments", | ||
"description": "Extract code comments from a string.", | ||
"version": "0.1.0", | ||
"description": "Thin wrapper around esprima for extracting code comments from a glob of files.", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/jonschlinkert/extract-comments", | ||
@@ -24,9 +24,7 @@ "author": { | ||
"keywords": [ | ||
"docs", | ||
"documentation", | ||
"generate", | ||
"generator", | ||
"markdown", | ||
"templates", | ||
"verb" | ||
"code", | ||
"comments", | ||
"extract", | ||
"esprima", | ||
"glob" | ||
], | ||
@@ -43,3 +41,2 @@ "main": "index.js", | ||
"fs-utils": "^0.4.3", | ||
"lodash": "^2.4.1", | ||
"mocha": "~1.18.2", | ||
@@ -49,4 +46,5 @@ "verb": "~0.2.0" | ||
"dependencies": { | ||
"esprima": "^1.2.2" | ||
"esprima": "^1.2.2", | ||
"globule": "^0.2.0" | ||
} | ||
} |
@@ -0,13 +1,25 @@ | ||
var file = require('fs-utils'); | ||
var expect = require('chai').expect; | ||
var extract comments = require('../'); | ||
var extract = require('../'); | ||
var fixtures = 'test/fixtures/**/*.js'; | ||
describe('when extract-comments is passed an array of file paths:', function () { | ||
it('should read each file as a string and extract comments from the code.', function () { | ||
var files = file.find(fixtures); | ||
var firstFileName = file.basename(files[0]); | ||
var actual = extract(fixtures); | ||
describe('when foo is passed:', function () { | ||
it('should convert foo to bar.', function () { | ||
var fixture = 'foo'; | ||
var actual = 'bar'; | ||
var expected = 'bar'; | ||
expect(extract comments(actual)).to.eql(expected); | ||
expect(actual).to.be.an('object'); | ||
expect(Object.keys(actual)).to.have.length.above(1); | ||
expect(actual).to.have.property(firstFileName); | ||
}); | ||
}); | ||
describe('when true is passed as a second parameter', function () { | ||
it('should generate an array, instead of an object', function () { | ||
var actual = extract(fixtures, true); | ||
expect(actual).to.be.an('array'); | ||
expect(actual).to.have.length.above(1); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
6884
4
0
65
2
10
47
2
+ Addedglobule@^0.2.0
+ Addedglob@3.2.11(transitive)
+ Addedglobule@0.2.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedlodash@2.4.2(transitive)
+ Addedlru-cache@2.7.3(transitive)
+ Addedminimatch@0.2.140.3.0(transitive)
+ Addedsigmund@1.0.1(transitive)