Comparing version 0.0.2 to 0.0.3-0
#!/usr/bin/env node | ||
var os = require("os"); | ||
function list(val) { | ||
// @see less.js/bin/lessc | ||
return val.split(os.type().match(/Windows/) ? ';' : ':') | ||
} | ||
var program = require('commander'); | ||
program | ||
.option("-d --debug", "Debugging") | ||
.option("-p --path <path ...>", "searching paths.",list) | ||
.option("-p --include-path <path ...>", "Same as path option", list) | ||
.usage("[options] <input>") | ||
.parse(process.argv); | ||
var cssjoin = require("../lib/cssjoin.js"); | ||
var options = {}; | ||
var paths = program.path || program.includePath | ||
var options = { | ||
paths : paths | ||
}; | ||
cssjoin(program.args[0],options, function(err,result){ | ||
if(err){ | ||
process.stderr.write(err + "\n"); | ||
process.stderr.write(err + "\n"); | ||
}else{ | ||
@@ -14,0 +26,0 @@ process.stdout.write(result); |
20
index.js
var fs = require("fs"); | ||
var path = require("path"); | ||
var util = require("util"); | ||
var CssInclude = require("./lib/cssinclude.js"); | ||
module.exports = function(cssFilePath, options, cb){ | ||
if (typeof options === "function") cb = options, options = {} | ||
if (!options) options = {}; | ||
fs.readFile(cssFilePath, 'utf-8', function(err, data){ | ||
if(err) throw err; | ||
var cssInclude = new CssInclude(); | ||
var paths = options.path || []; | ||
paths.unshift(path.dirname(cssFilePath)); | ||
var a = path.resolve(process.cwd(), cssFilePath); | ||
var css = cssInclude.extend(data, paths); | ||
var error = undefined; | ||
cb(error, css); | ||
}) | ||
} | ||
var cssjoin = require("./lib/cssjoin.js"); | ||
module.exports = cssjoin; |
@@ -1,2 +0,1 @@ | ||
var inutil = require("./util.js"); | ||
@@ -6,4 +5,4 @@ var util = require("util"); | ||
var path = require("path"); | ||
//var replaceMap = {}; | ||
//var cssContent = {}; | ||
var os = require("os"); | ||
var debug = false; | ||
@@ -14,3 +13,3 @@ console.debug = function(msg){ | ||
} | ||
} | ||
}; | ||
@@ -23,7 +22,28 @@ module.exports = function(cssFile, options, cb){ | ||
} | ||
var cssJoin = new CssJoin(cssFile, options); | ||
cssJoin.parseCss(cssFile, [], function(err, file){ | ||
//console.debug(cssJoin); | ||
var css = cssJoin.cssContent[file]; | ||
css = cssJoin.extend(css,file); | ||
options.file = cssFile; | ||
options.callback = cb; | ||
var cssJoin = new CssJoin(options); | ||
return cssJoin; | ||
}; | ||
var CssJoin = function(options){ | ||
this.replaceMap = {}; | ||
this.cssContent = {}; | ||
var options = options || {}; | ||
if(options.paths){ | ||
this.paths = (util.isArray(options.paths)) ? options.paths : [options.paths]; | ||
}else{ | ||
this.paths = []; | ||
} | ||
this.baseFile = options.file; | ||
this.callback = options.callback; | ||
this.execute(); | ||
} | ||
CssJoin.prototype.execute = function(){ | ||
var cb = this.callback; | ||
var _this = this; | ||
this.parseCss(this.baseFile, function(err, file){ | ||
var css = _this.cssContent[file]; | ||
css = _this.extend(css,file); | ||
if(typeof cb === "function"){ | ||
@@ -33,12 +53,12 @@ cb(err, css); | ||
}); | ||
}; | ||
} | ||
var CssJoin = function(cssFile, options){ | ||
this.replaceMap = {}; | ||
this.cssContent = {}; | ||
this.option = options || {}; | ||
this.baseFile = cssFile; | ||
CssJoin.prototype.getPaths = function(addingPathFile){ | ||
var base = inutil.cloneArray(this.paths); | ||
if(addingPathFile){ | ||
base.unshift(path.dirname(addingPathFile)); | ||
} | ||
return base; | ||
} | ||
CssJoin.prototype.extend = function(joinedCss, file){ | ||
@@ -59,3 +79,3 @@ var map = this.replaceMap[file]; | ||
CssJoin.prototype.parseCss = function(cssFilePath, resolvePaths, callback) { | ||
CssJoin.prototype.parseCss = function(cssFilePath, callback) { | ||
if(this.cssContent[cssFilePath] && this.replaceMap[cssFilePath]){ | ||
@@ -65,8 +85,8 @@ callback(null, this.cssFilePath); | ||
} | ||
var _resolvePaths = inutil.cloneArray(resolvePaths); | ||
_resolvePaths.unshift(path.dirname(cssFilePath)); | ||
var _resolvePaths = this.getPaths(cssFilePath); | ||
var _this = this; | ||
fs.stat(cssFilePath, function(err, result){ | ||
if(result == undefined){ | ||
callback(null, this.cssFilePath); | ||
callback(err, this.cssFilePath); | ||
return; | ||
@@ -88,4 +108,3 @@ } | ||
var _childFilePath = _replaceMap[key]; | ||
_this.parseCss(_childFilePath, resolvePaths, function(err, childFilePath){ | ||
_this.parseCss(_childFilePath, function(err, childFilePath){ | ||
children--; | ||
@@ -92,0 +111,0 @@ if(children > 0){ |
@@ -18,3 +18,3 @@ var fs = require("fs"); | ||
function resolvePath(file, resolvePaths){ | ||
var pathname; | ||
var pathname = null; | ||
//paths | ||
@@ -32,3 +32,2 @@ var paths = cloneArray(resolvePaths); | ||
} | ||
console.warn("[WARN]Cannot resolve path: "+ file); | ||
return pathname; | ||
@@ -41,33 +40,35 @@ } | ||
} | ||
return css; | ||
} | ||
function getReplaceMap(css, resolvePaths){ | ||
var _replaceMap = {}; | ||
// remove comment | ||
css = this.removeComment(css); | ||
// get import sytax; | ||
var importMatches = css.match(IMPORT_REGEXP); | ||
if(importMatches === null){ | ||
return _replaceMap; | ||
var _replaceMap = {}; | ||
// remove comment | ||
css = this.removeComment(css); | ||
// get import sytax; | ||
var importMatches = css.match(IMPORT_REGEXP); | ||
if(importMatches === null){ | ||
return _replaceMap; | ||
} | ||
for(var i=0; i < importMatches.length; i++ ){ | ||
var importSyntax = importMatches[i]; | ||
if(FILE_REGEXP.test(importSyntax) == false){ | ||
continue; | ||
} | ||
for(var i=0; i < importMatches.length; i++ ){ | ||
var importSyntax = importMatches[i]; | ||
if(FILE_REGEXP.test(importSyntax) == false){ | ||
continue; | ||
} | ||
var matches = importSyntax.match(FILE_REGEXP); | ||
var fileName = matches[1] || matches[2]; | ||
if(/\.css$/.test(fileName) == false){ | ||
continue; | ||
} | ||
var file = resolvePath(fileName, resolvePaths); | ||
if(!file){ | ||
continue; | ||
} | ||
_replaceMap[importSyntax] = file; | ||
var matches = importSyntax.match(FILE_REGEXP); | ||
var fileName = matches[1] || matches[2]; | ||
if(/\.css$/.test(fileName) == false){ | ||
continue; | ||
} | ||
return _replaceMap; | ||
var file = resolvePath(fileName, resolvePaths); | ||
if(!file){ | ||
continue; | ||
} | ||
_replaceMap[importSyntax] = file; | ||
} | ||
return _replaceMap; | ||
} | ||
module.exports = { | ||
@@ -77,4 +78,3 @@ cloneArray : cloneArray, | ||
removeComment : removeComment, | ||
getReplaceMap : getReplaceMap, | ||
getReplaceMap : getReplaceMap, | ||
getReplaceMapByFile : function(cssFilePath, resolvePaths, cb){ | ||
@@ -81,0 +81,0 @@ var _this = this; |
{ | ||
"name": "cssjoin", | ||
"version": "0.0.2", | ||
"version": "0.0.3-0", | ||
"description": "Extend css @import loaded file", | ||
@@ -21,3 +21,3 @@ "main": "index.js", | ||
"test": "node node_modules/mocha/bin/mocha test --reporter spec", | ||
"testd": "node node_modules/mocha/bin/mocha test -w --reporter nyan " | ||
"testdev": "node node_modules/mocha/bin/mocha test -w -b -d --reporter nyan " | ||
}, | ||
@@ -36,3 +36,7 @@ "repository": { | ||
"license": "BSD", | ||
"readmeFilename": "README.md" | ||
"readmeFilename": "README.md", | ||
"optionalDependencies": {}, | ||
"engines": { | ||
"node": "*" | ||
} | ||
} |
@@ -19,2 +19,3 @@ #CssJoin | ||
//without option | ||
cssInclude("sample.css",function(err,extendedCss){ | ||
@@ -24,2 +25,13 @@ console.log(extendedCss); | ||
// with option | ||
cssInclude("sample.css" | ||
,{ | ||
"paths" : "./include/path" | ||
} | ||
,function(err,extendedCss){ | ||
console.log(extendedCss); | ||
} | ||
); | ||
``` | ||
@@ -46,3 +58,3 @@ | ||
```sh | ||
$ node bin/css-inclde.js main.css | ||
$ cssjoin main.css | ||
``` | ||
@@ -59,1 +71,16 @@ ### Output | ||
``` | ||
## Options | ||
#### -p --include-paths | ||
Add @import paths. | ||
```sh | ||
$ cssjoin some.css -p /include/path | ||
``` | ||
if need set some paths can use separator **":"** | ||
```sh | ||
$ cssjoin some.css -p /include/path:/include/path2:/include/path3 | ||
``` | ||
or on windows, use separator **";"** | ||
```sh | ||
$ cssjoin some.css -p C:\\include\\path\\;C:\\include\\path2\\;C:\\include\\path3\\; | ||
``` |
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
82
3
0
7772
6
212
1