Permalink
Browse files

improve it

  • Loading branch information...
1 parent 7a0ea6d commit b1e03fbf132db53c3d4de9d969623615ba900e38 @millermedeiros committed Nov 20, 2012
Showing with 79 additions and 27 deletions.
  1. +55 −5 README.md
  2. +6 −3 bin/nodefy
  3. +4 −4 index.js
  4. +12 −10 src/converter.js
  5. +1 −5 src/parser.js
  6. +1 −0 test/convert.spec.js
View
@@ -2,9 +2,11 @@
convert AMD modules into a node.js compatible format.
+
+
## How?
-this tool uses [Esprima](http://esprima.org/) to parse the code and replace
+This tool uses [Esprima](http://esprima.org/) to parse the code and replace
`define()` calls, doing the less amount of changes as possible to the code.
### Input
@@ -39,15 +41,59 @@ define(['foo', '../bar/baz'], function(foo, baz){
```
+## CLI
+
+You can use it as a CLI tool:
+
+ [sudo] npm install -g nodefy
+ nodefy -o cjs "src/**.js"
+
+This will convert all ".js" files inside the `src` folder and put the results
+on the `cjs` folder.
+
+It also works with `stdin` and `stdout`:
+
+ cat "src/foo.js" | nodefy
+
+For a list of available options run:
+
+ nodefy -h
+
+
+## Standalone Lib
+
+It is also available as a regular node.js library.
+
+```js
+var nodefy = require('nodefy');
+
+// nodefy.parse(string)
+// returns node.js style module
+var cjsModule = nodefy.parse(jsString);
+
+// nodefy.convert(inputPath [, outputPath], callback)
+// if outputPath is omitted it won't write the result
+nodefy.convert('foo.js', function(err, result){
+ console.log(result);
+});
+
+// nodefy.batchConvert(glob [, outputFolder], callback)
+// if outputFolder is omitted it won't write the result
+nodefy.batchConvert('src/**.js', 'cjs', function(err, results){
+ // results is an array of {sourcePath, outputPath, result}
+});
+```
+
+
## Inspiration / Why?
I couldn't find any tool that did what I wanted - convert AMD modules into
plain node.js - so I decided to code my own. There are alternatives but they
-all add more complexity than I would like.
+all add more complexity than I wanted.
-This project was created mainly because of the
-[amd-utils](http://millermedeiros.github.com/amd-utils/) project, since many
-methods are useful on both environments.
+This project was created mainly because of
+[amd-utils](http://millermedeiros.github.com/amd-utils/), since many methods
+are useful on both environments.
The name was inpired by
[browserify](https://github.com/substack/node-browserify).
@@ -61,3 +107,7 @@ The name was inpired by
- [uRequire](https://github.com/anodynos/uRequire)
+## License
+
+Released under the MIT license
+
View
@@ -10,13 +10,16 @@ _cli
.parse(process.argv);
-if (_cli.args) {
+if (_cli.args.length) {
var glob = _cli.args[0];
- nodefy.batchConvert(glob, _cli.output, function(err, result){
+ nodefy.batchConvert(glob, _cli.output, function(err, results){
if (err) throw err;
if (! _cli.output) {
- console.log(result);
+ results = results.map(function(r){
+ return r.result;
+ });
+ console.log( results.join('\n') );
}
});
View
@@ -14,16 +14,16 @@ exports.parse = _parser.parse;
/**
* Read file content and output into destination path.
* @param String inputPath
- * @param String outputPath
- * @param Function callback
+ * @param String [outputPath]
+ * @param Function callback(err, result)
*/
exports.convert = _converter.convert;
/**
* Read glob content and output files into output folder
* @param Glob inputGlob Glob that matches files that should be converted
- * @param String outputFolder
- * @param Function callback
+ * @param String [outputFolder]
+ * @param Function callback(err, results)
*/
exports.batchConvert = _converter.batchConvert;
View
@@ -8,10 +8,10 @@ var _parser = require('./parser');
-/**
- * Read file content and output into destination path.
- */
+
+// Read file content and output into destination path.
exports.convert = function(inputPath, outputPath, callback){
+ // outputPath is optional
if (typeof outputPath === 'function' && arguments.length < 3) {
callback = outputPath;
outputPath = null;
@@ -59,9 +59,7 @@ function safeCreateDir(filePath, callback){
}
-/**
- * Read folder content and output files into output folder
- */
+// Read folder content and output files into output folder
exports.batchConvert = function(inputGlob, outputFolder, callback){
// outputFolder is optional
if (typeof outputFolder === 'function' && arguments.length < 3) {
@@ -76,10 +74,14 @@ exports.batchConvert = function(inputGlob, outputFolder, callback){
}
// convert all files in parallel
- _async.reduce(files, '', function(memo, filePath, cb){
- var outputPath = outputFolder? _path.join(outputFolder, _path.basename(filePath)) : null;
- exports.convert(filePath, outputPath, function(err, content){
- cb(err, memo + content);
+ _async.map(files, function(sourcePath, cb){
+ var outputPath = outputFolder? _path.join(outputFolder, _path.basename(sourcePath)) : null;
+ exports.convert(sourcePath, outputPath, function(err, result){
+ cb(err, {
+ sourcePath : sourcePath,
+ outputPath : outputPath,
+ result : result
+ });
});
}, callback);
});
View
@@ -11,11 +11,7 @@ var MAGIC_DEPS = {
-/**
- * Convert AMD-style JavaScript string into node.js compatible module
- * @param String raw
- * @return String
- */
+// Convert AMD-style JavaScript string into node.js compatible module
exports.parse = function(raw){
var output = '';
var ast = esprima.parse(raw, {
View
@@ -97,6 +97,7 @@ describe('convert', function () {
expect( err ).toBe(null);
var expected = readOut('basic') + readOut('magic');
+ result = result.map(function(r){ return r.result; }).join('');
expect( result ).toBe( expected );
done();

0 comments on commit b1e03fb

Please sign in to comment.