Permalink
Browse files

Version 0.1.0.

* 4 types of assets expanding.
* Tests via Vows framework.
* Processing YAML files via yaml.js by TJ.
  • Loading branch information...
1 parent f59efaf commit 09d50f98b69c95c1140821d5c1660049cfe26982 @jakubpawlowicz committed Mar 20, 2011
View
@@ -0,0 +1,10 @@
+0.1.0 / 2011-03-26
+==================
+
+ * First version of assets-expander library.
+ * Implemented assets expanding from YAML files.
+ * Implemented expanding:
+ * simple list - 'asset1,asset2,asset3'
+ * wildcard flat lists - 'asset*'
+ * wildcard multi-level lists - '\*\*/\*'
+ * single level sublists - 'folder/[asset1,asset2,asset3]'
View
@@ -0,0 +1,37 @@
+## What is assets-expander? ##
+
+assets-expander is a node.js library for expanding lists of files defined in YAML file into a flat lists.
+
+## Usage ##
+
+### How to install assets-expander? ###
+
+ npm install assets-expander
+
+### How to user assets-expander? ###
+
+ var AssetsExpander = require('assets-expander');
+ var expander = new AssetsExpander('assets.yml', { root: 'path/to/public/dir' });
+ expander.processGroup('stylesheets', 'public') // gets a flat list of assets in public group
+
+### How to define assets.yml file? ###
+
+It depends what files you want to have in *public* group, but in general the file should look something like this.
+
+ stylesheets:
+ public:
+ - 'reset,shared,base,home'
+
+Then if you have the following directory structure:
+
+ stylesheets
+ - reset.css
+ - shared.css
+ - base.css
+ - home.css
+
+executing the code above will get you a list of full paths to these 4 files:
+
+ ['.../stylesheets/reset.css', '.../stylesheets/shared.css', '.../stylesheets/base.css', '.../stylesheets/home.css']
+
+
View
@@ -1,19 +1,124 @@
var fs = require('fs'),
- path = require('path');
+ path = require('path'),
+ yaml = require('../vendor/yaml'),
+ rightjs = require('rightjs');
-var Assets = {
- expand: function(group, options) {
- var assets = [];
- options = options || {};
+var AssetsExpander = function(pathToYaml, options) {
+ if (pathToYaml) {
+ this.yamlSource = yaml.eval(fs.readFileSync(pathToYaml).toString());
+ this.options = options;
+ }
+};
+
+AssetsExpander.prototype = {
+ allTypes: function() {
+ return Object.keys(this.yamlSource || {});
+ },
+
+ groupsFor: function(type) {
+ return Object.keys(this.yamlSource[type] || {});
+ },
+
+ processList: function(mainGroup, options) {
+ options = options || this.options || {};
+ var self = this;
+
+ var expandAsset = function(root, asset) {
+ var matches = [];
+
+ if (asset.includes('**/*')) {
+ var prefix = asset.substring(0, asset.indexOf('**/*'));
+ matches.push(self._scanDir(path.join(root, prefix), '*', true));
+ } else if (asset.includes('*')) {
+ var prefix = '';
+ if (asset.indexOf('/') >= 0) {
+ prefix = asset.substring(0, asset.indexOf('*'));
+ asset = '*';
+ }
+ matches.push(self._scanDir(path.join(root, prefix), asset));
+ } else {
+ var expanded = path.join(root, asset) + '.' + options.type;
+ if (path.existsSync(expanded)) matches.push(expanded);
+ }
+
+ return matches;
+ };
+
+ var expandGroup = function(root, group) {
+ var groupAssetsExpander = [];
+
+ if (group.includes('[')) {
+ var tokens = group.split(/[\[\]]/);
+ root = path.join(root, tokens[0]);
+ group = tokens[1];
+ }
+
+ group.split(',').forEach(function(asset) {
+ var expanded = expandAsset(root, asset);
+ if (expanded.length == 0) return;
+
+ groupAssetsExpander.push(expanded);
+ });
+
+ return groupAssetsExpander;
+ };
+
+ return expandGroup(options.root, mainGroup).flatten().uniq();
+ },
+
+ processGroup: function(type, name) {
+ var definition = this.yamlSource[type][name],
+ assets = [],
+ options = Object.clone(this.options),
+ self = this;
+
+ options.root = path.join(options.root, type);
- group.split(' ').forEach(function(asset) {
- if (asset.length == 0) return;
+ var processLevel = function(levelDefinition, levelOptions) {
+ if (rightjs.isString(levelDefinition)) {
+ assets.push(self.processList(levelDefinition, levelOptions));
+ } else if (rightjs.isHash(levelDefinition)) {
+ Object.each(levelDefinition, function(key, value) {
+ processLevel(value, Object.merge(levelOptions, { root: path.join(levelOptions.root, key) }));
+ });
+ } else { // Array
+ levelDefinition.each(function(value) {
+ processLevel(value, levelOptions);
+ });
+ }
+ };
+
+ processLevel(definition, options);
+
+ return assets.flatten();
+ },
+
+ // private
+
+ _scanDir: function(root, pathToDir, recursive) {
+ var pattern = new RegExp('^' + pathToDir.replace(/\*/, '.*'), 'g'),
+ dirs = [],
+ matches = [],
+ self = this;
+
+ fs.readdirSync(path.join(root, path.dirname(pathToDir))).forEach(function(match) {
+ var fullPath = path.join(root, match),
+ stat = fs.lstatSync(fullPath);
- assets.push(path.join(options.root, asset));
+ if (match.match(pattern) && !stat.isDirectory())
+ matches.push(fullPath);
+ if (stat.isDirectory())
+ dirs.push(fullPath);
});
- return assets;
+ if (recursive) {
+ dirs.forEach(function(dir) {
+ matches.push(self._scanDir(dir, '*', true));
+ });
+ }
+
+ return matches;
}
};
-module.exports = Assets;
+module.exports = AssetsExpander;
View
@@ -0,0 +1,16 @@
+{
+ "name": "assets-expander",
+ "author": "Jakub Pawlowicz <jakub@goalsmashers.com> (http://twitter.com/GoalSmashers)",
+ "description": "A well-tested Assets expander - specify assets graph in YAML file and easily expand them into a linear list",
+ "keywords": ["css", "javascript", "assets", "yaml"],
+ "homepage": "http://github.com/GoalSmashers/assets-expander",
+ "repository": {
+ "type" : "git",
+ "url" : "http://github.com/GoalSmashers/assets-expander.git"
+ },
+ "version": "0.1.0",
+ "main": "index.js",
+ "dependencies": {
+ "rightjs": "2.2.x"
+ }
+}
Oops, something went wrong.

0 comments on commit 09d50f9

Please sign in to comment.