Comparing version 0.0.0 to 0.0.1
@@ -18,3 +18,3 @@ #!/usr/bin/env node | ||
USAGE = [ | ||
"fume [input(s)] -s [path] -s [path] ... --amdify --cjsify", | ||
"fume [input(s)] -s [path] -s [path] ... --amdify --cjsify --umdify", | ||
"\t[input]: Input file path(s)", | ||
@@ -24,2 +24,3 @@ "\t-s (--sibling): Path of a sibling factory", | ||
"\t-c (--cjsify): generate a CJS module", | ||
"\t-a (--umdify): generate a UMD module", | ||
"\t-o (--out): output directory (optional if input is one file)", | ||
@@ -31,3 +32,3 @@ ].join("\n"), | ||
files = Array.prototype.concat.apply(input, siblings).map( | ||
function(fpath) { | ||
function (fpath) { | ||
return path.normalize( | ||
@@ -49,6 +50,6 @@ path.join( | ||
usage("Please specify an output directory", 1); | ||
if (!(parsed.amdify || parsed.cjsify)) | ||
usage("Choose either AMD output or CJS output", 1); | ||
if (parsed.amdify && parsed.cjsify) | ||
usage("Choose either AMD output or CJS output, but not both", 1); | ||
if (!(parsed.amdify || parsed.cjsify || parsed.umdify)) | ||
usage("Choose either AMD, CJS or UMD output", 1); | ||
if (parsed.amdify + parsed.cjsify + parsed.umdify > 1) | ||
usage("Choose either one type of output (AMD, CJS or UMD)", 1); | ||
@@ -58,6 +59,6 @@ async.waterfall([ | ||
// make sure no directories in the input list | ||
function(next) { | ||
function (next) { | ||
async.each(input, function(path, done) { | ||
fs.stat(path, function(err, stats) { | ||
async.each(input, function (path, done) { | ||
fs.stat(path, function (err, stats) { | ||
if (err) return done(err); | ||
@@ -73,6 +74,6 @@ if (stats.isDirectory()) | ||
// make sure no directories in the siblings list | ||
function(next) { | ||
function (next) { | ||
async.each(siblings, function(path, done) { | ||
fs.stat(path, function(err, stats) { | ||
async.each(siblings, function (path, done) { | ||
fs.stat(path, function (err, stats) { | ||
if (err) return done(err); | ||
@@ -88,8 +89,8 @@ if (stats.isDirectory()) | ||
// fume files | ||
function(next) { | ||
function (next) { | ||
async.map(files, function(path, done) { | ||
async.map(files, function (path, done) { | ||
fs.readFile(path, { | ||
encoding: 'utf8' | ||
}, function(err, data) { | ||
}, function (err, data) { | ||
done(err, { | ||
@@ -100,3 +101,3 @@ path: path, | ||
}); | ||
}, function(err, codes) { | ||
}, function (err, codes) { | ||
if (err) return next(err); | ||
@@ -113,8 +114,12 @@ try { | ||
// print results | ||
function(sources, next) { | ||
function (sources, next) { | ||
if (input.length === 1 && !outDir) { | ||
return setImmediate(function() { | ||
return setImmediate(function () { | ||
console.log( | ||
sources[files[0]][parsed.amdify ? 'amdify' : 'cjsify']() | ||
sources[files[0]][ | ||
parsed.amdify ? 'amdify' : | ||
parsed.cjsify ? 'cjsify' : | ||
'umdify' | ||
]() | ||
); | ||
@@ -125,7 +130,11 @@ next(); | ||
async.each(files.slice(0, input.length), function(file, done) { | ||
async.each(files.slice(0, input.length), function (file, done) { | ||
var code, outPath, sourceBase; | ||
try { | ||
code = sources[file][parsed.amdify ? 'amdify' : 'cjsify'](); | ||
code = sources[file][ | ||
parsed.amdify ? 'amdify' : | ||
parsed.cjsify ? 'cjsify' : | ||
'umdify' | ||
](); | ||
} catch (err) { | ||
@@ -152,3 +161,3 @@ done(err); | ||
mkdirp(outPath, function(err) { | ||
mkdirp(outPath, function (err) { | ||
if (err) return done(err); | ||
@@ -169,3 +178,3 @@ fs.writeFile( | ||
], function(err) { | ||
], function (err) { | ||
@@ -172,0 +181,0 @@ if (err) { |
173
lib/fume.js
@@ -1,2 +0,2 @@ | ||
(function(undefined) { | ||
(function (undefined) { | ||
@@ -20,2 +20,20 @@ var path = require('path'), | ||
END: ");" | ||
}, | ||
UMD_TEMPLATE = { | ||
START: "(function(){", | ||
LOADER_START: "if (typeof define === 'function' && define.amd) {", | ||
AMD_START: AMD_TEMPLATE.START, | ||
// <-- amd dependencies --> | ||
AMD_MIDDLE: AMD_TEMPLATE.MIDDLE, | ||
// <-- factory function name --> | ||
AMD_END: AMD_TEMPLATE.END, | ||
LOADER_MIDDLE: "} else if (typeof exports === 'object') {", | ||
CJS_START: CJS_TEMPLATE.START, | ||
// <-- factory function name --> | ||
CJS_MIDDLE: CJS_TEMPLATE.MIDDLE, | ||
// <-- cjs requires --> | ||
CJS_END: CJS_TEMPLATE.END, | ||
LOADER_END: "} else throw Error('Cannot find a module loader');", | ||
// <-- factory function --> | ||
END: "}());" | ||
}; | ||
@@ -45,24 +63,24 @@ | ||
var nameTagRe = new RegExp( | ||
"^.*?" + | ||
this.config.nameTagPrefix + | ||
"\\s*(\\w+)\\W*$" | ||
"^.*?" + | ||
this.config.nameTagPrefix + | ||
"\\s*(\\w+)\\W*$" | ||
), | ||
preferTagRe = new RegExp( | ||
"^.*?" + | ||
this.config.preferTagPrefix + | ||
"\\s*(\\S+)\\s+(\\S+)\\W*$" | ||
"^.*?" + | ||
this.config.preferTagPrefix + | ||
"\\s*(\\S+)\\s+(\\S+)\\W*$" | ||
), | ||
amdTagRe = new RegExp( | ||
"^.*?" + | ||
this.config.amdTagPrefix + | ||
"\\s*(\\S+)\\s+(\\S+)\\W*$" | ||
"^.*?" + | ||
this.config.amdTagPrefix + | ||
"\\s*(\\S+)\\s+(\\S+)\\W*$" | ||
), | ||
cjsTagRe = new RegExp( | ||
"^.*?" + | ||
this.config.cjsTagPrefix + | ||
"\\s*(\\S+)\\s+(\\S+)\\W*$" | ||
"^.*?" + | ||
this.config.cjsTagPrefix + | ||
"\\s*(\\S+)\\s+(\\S+)\\W*$" | ||
); | ||
// parse the sources and get needed information | ||
sources.forEach(function(source) { | ||
sources.forEach(function (source) { | ||
@@ -89,3 +107,3 @@ source.dir = path.dirname(source.path); | ||
// extract annotation from the comment lines: | ||
cLines.forEach(function(line) { | ||
cLines.forEach(function (line) { | ||
@@ -106,11 +124,11 @@ // attempt to detect the factory name: | ||
// check if this name is already taken by another factory: | ||
if (sources.some(function(other) { | ||
return other !== source && | ||
other.dir === source.dir && | ||
other.name === source.name; | ||
})) | ||
if (sources.some(function (other) { | ||
return other !== source && | ||
other.dir === source.dir && | ||
other.name === source.name; | ||
})) | ||
throw Error( | ||
"Factory path '" + | ||
path.join(source.dir, source.name) + | ||
"' cannot be specified more than once" | ||
"Factory path '" + | ||
path.join(source.dir, source.name) + | ||
"' cannot be specified more than once" | ||
); | ||
@@ -135,10 +153,10 @@ | ||
// detect sibling dependencies | ||
sources.forEach(function(source) { | ||
sources.forEach(function (source) { | ||
source.factory.params.forEach(function(param) { | ||
source.factory.params.forEach(function (param) { | ||
// find the siblings which can qualify as this dependency | ||
param.candidatePaths = sources.filter(function(other) { | ||
param.candidatePaths = sources.filter(function (other) { | ||
return other !== source && other.name === param.name; | ||
}).map(function(other) { | ||
}).map(function (other) { | ||
return other.path; | ||
@@ -151,3 +169,3 @@ }); | ||
return sources.reduce(function(p, e, i, o) { | ||
return sources.reduce(function (p, e, i, o) { | ||
p[e.path] = new Factory(e); | ||
@@ -161,2 +179,3 @@ return p; | ||
this.name = source.name; | ||
this.facName = source.factory.id.name; | ||
this.dir = source.dir; | ||
@@ -174,6 +193,6 @@ this.code = source.code; | ||
*/ | ||
Factory.prototype.amdify = function() { | ||
Factory.prototype.amdify = function () { | ||
var self = this, | ||
deps = self.deps.map(function(dep) { | ||
deps = self.deps.map(function (dep) { | ||
@@ -196,3 +215,3 @@ if (dep.candidatePaths.length > 0) | ||
} | ||
}; | ||
@@ -203,6 +222,6 @@ /** | ||
*/ | ||
Factory.prototype.cjsify = function() { | ||
Factory.prototype.cjsify = function () { | ||
var self = this, | ||
deps = self.deps.map(function(dep) { | ||
deps = self.deps.map(function (dep) { | ||
@@ -225,9 +244,46 @@ if (dep.candidatePaths.length > 0) | ||
} | ||
}; | ||
/** | ||
* Return code of this factory wrapped as a UMD module, with dependencies | ||
* mapped and resolved according to annotations. | ||
*/ | ||
Factory.prototype.umdify = function () { | ||
var self = this, | ||
amdDeps = self.deps.map(function (dep) { | ||
if (dep.candidatePaths.length > 0) | ||
return findSiblingPath.call(self, dep); | ||
else | ||
return self.amdMaps[dep.name] || dep.name; | ||
}), | ||
cjsDeps = self.deps.map(function (dep) { | ||
if (dep.candidatePaths.length > 0) | ||
return findSiblingPath.call(self, dep); | ||
else | ||
return self.cjsMaps[dep.name] || dep.name; | ||
}); | ||
return generate( | ||
parse( | ||
umdWrap( | ||
self.code, | ||
self.facName, | ||
amdDeps, | ||
cjsDeps | ||
) | ||
) | ||
); | ||
}; | ||
/** | ||
* Wrap a factory with an AMD loader. | ||
*/ | ||
function amdWrap(factoryCode, deps) { | ||
var deps = deps.map(function(dep) { | ||
var deps = deps.map(function (dep) { | ||
return "'" + dep + "'"; | ||
@@ -246,3 +302,3 @@ }); | ||
function cjsWrap(factoryCode, deps) { | ||
var requires = deps.map(function(dep) { | ||
var requires = deps.map(function (dep) { | ||
return "require('" + dep + "')"; | ||
@@ -257,2 +313,45 @@ }); | ||
/** | ||
* Wrap a factory with a CJS loader. | ||
* UMD_TEMPLATE = { | ||
START: "(function(){if (typeof define === 'function' && define.amd) {", | ||
AMD_START: AMD_TEMPLATE.START, | ||
// <-- amd dependencies --> | ||
AMD_MIDDLE: AMD_TEMPLATE.MIDDLE, | ||
// <-- factory function --> | ||
AMD_END: AMD_TEMPLATE.END, | ||
MIDDLE: "} else if (typeof exports === 'object') {", | ||
CJS_START: CJS_TEMPLATE.START, | ||
// <-- factory function --> | ||
CJS_MIDDLE: CJS_TEMPLATE.MIDDLE, | ||
// <-- cjs requires --> | ||
CJS_END: CJS_TEMPLATE.END, | ||
END: "} else throw Error('Cannot find a module loader');}());" | ||
}; | ||
*/ | ||
function umdWrap(factoryCode, facName, amdDeps, cjsDeps) { | ||
var amdDeps = amdDeps.map(function (dep) { | ||
return "'" + dep + "'"; | ||
}), | ||
cjsDeps = cjsDeps.map(function (dep) { | ||
return "require('" + dep + "')"; | ||
}); | ||
return UMD_TEMPLATE.START + | ||
UMD_TEMPLATE.LOADER_START + | ||
UMD_TEMPLATE.AMD_START + | ||
amdDeps.join(',') + | ||
UMD_TEMPLATE.AMD_MIDDLE + | ||
facName + | ||
UMD_TEMPLATE.AMD_END + | ||
UMD_TEMPLATE.LOADER_MIDDLE + | ||
UMD_TEMPLATE.CJS_START + | ||
facName + | ||
UMD_TEMPLATE.CJS_MIDDLE + | ||
cjsDeps.join(',') + | ||
UMD_TEMPLATE.CJS_END + | ||
UMD_TEMPLATE.LOADER_END + | ||
factoryCode + | ||
UMD_TEMPLATE.END; | ||
} | ||
function findSiblingPath(dep) { | ||
@@ -274,3 +373,3 @@ var self = this; | ||
var matches = dep.candidatePaths.filter(function(candidatePath) { | ||
var matches = dep.candidatePaths.filter(function (candidatePath) { | ||
return path.join(path.dirname(candidatePath), dep.name) | ||
@@ -277,0 +376,0 @@ .indexOf(self.preferMaps[dep.name]) !== -1; |
{ | ||
"name": "fume", | ||
"version": "0.0.0", | ||
"description": "Use factories to fabricate AMD and CommonJS modules", | ||
"main": "index.js", | ||
"bin": { | ||
"fume": "./bin/fume.js" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/EyalAr/fume.git" | ||
}, | ||
"keywords": [], | ||
"author": "Eyal Arubas <eyalarubas@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/EyalAr/fume/issues" | ||
}, | ||
"homepage": "https://github.com/EyalAr/fume", | ||
"dependencies": { | ||
"async": "^0.9.0", | ||
"escodegen": "^1.4.1", | ||
"esprima": "^1.2.2", | ||
"mkdirp": "^0.5.0", | ||
"nopt": "^3.0.1" | ||
} | ||
"name": "fume", | ||
"version": "0.0.1", | ||
"description": "Use factories to fabricate AMD and CommonJS modules", | ||
"main": "index.js", | ||
"bin": { | ||
"fume": "./bin/fume.js" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/EyalAr/fume.git" | ||
}, | ||
"keywords": [], | ||
"author": "Eyal Arubas <eyalarubas@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/EyalAr/fume/issues" | ||
}, | ||
"homepage": "https://github.com/EyalAr/fume", | ||
"dependencies": { | ||
"async": "^0.9.0", | ||
"escodegen": "^1.4.1", | ||
"esprima": "^1.2.2", | ||
"mkdirp": "^0.5.0", | ||
"nopt": "^3.0.1" | ||
} | ||
} |
@@ -0,4 +1,6 @@ | ||
[![Version](http://img.shields.io/npm/v/fume.svg)](https://www.npmjs.org/package/fume) | ||
# Fume | ||
Use factories to fabricate AMD and CommonJS modules. | ||
Use factories to fabricate AMD, CommonJS and UMD modules. | ||
@@ -14,3 +16,3 @@ ## Overview | ||
Write your code inside factory functions which receive dependencies as a list | ||
of arguments. Fume will use your factories to generate AMD and CJS compatible | ||
of arguments. Fume will use your factories to generate AMD, CJS and UMD compatible | ||
modules. | ||
@@ -28,8 +30,9 @@ | ||
```bash | ||
fume [input(s)] -s [path] -s [path] ... --amdify --cjsify | ||
[input]: Input file path(s) | ||
-s (--sibling): Path of a sibling factory | ||
-a (--amdify): generate an AMD module | ||
-c (--cjsify): generate a CJS module | ||
-o (--out): output directory (optional if input is one file) | ||
fume [input(s)] -s [path] -s [path] ... --amdify --cjsify --umdify | ||
[input]: Input file path(s) | ||
-s (--sibling): Path of a sibling factory | ||
-a (--amdify): generate an AMD module | ||
-c (--cjsify): generate a CJS module | ||
-a (--umdify): generate a UMD module | ||
-o (--out): output directory (optional if input is one file) | ||
``` | ||
@@ -36,0 +39,0 @@ |
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
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
24186
522
171