Skip to content

Commit

Permalink
Merge branch 'pr/79'
Browse files Browse the repository at this point in the history
  • Loading branch information
arikon committed Apr 3, 2013
2 parents 1233dda + 3334308 commit aa56a5c
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/coa.js
Expand Up @@ -53,6 +53,34 @@ module.exports = require('coa').Cmd()
return U.stringToBoolean(v, true);
})
.end()
// borschik freeze
.cmd()
.name('freeze')
.title('Freeze all files in dirs according to .borschik config')
.helpful()
.opt()
.name('input').title('Input path (default: .).')
.short('i').long('input')
.def('.')
.end()
.opt()
.name('output').title('Output path for resulting JSON')
.short('o').long('output')
.output()
.end()
.opt()
.name('minimize').title('Minimize resulting JSON (default: yes)')
.short('m').long('minimize')
.def(true)
.val(function(v) {
return U.stringToBoolean(v, true);
})
.end()
.act(function(opts) {
var result = require('./freeze').freezeAll(opts.input);
return U.writeFile(opts.output, opts.minimize? JSON.stringify(result) : JSON.stringify(result, null, 4));
})
.end()
.act(function(opts) {

var defTech = PATH.join(__dirname, 'tech.js'),
Expand Down
62 changes: 62 additions & 0 deletions lib/freeze.js
Expand Up @@ -105,6 +105,68 @@ var freezeDir = exports.freezeDir = function(filePath) {
return freezeDir;
};

/**
* Recursivly freeze all files in path.
* @param {String} input File or directory path
*/
exports.freezeAll = function(input) {
/**
* Result JSON
* @type {Object}
*/
var result = {};

var basePath = PATH.dirname(input);
var stat = FS.statSync(input);
if (stat.isFile()) {
freezeAllProcessFile(input, process.cwd(), result);

} else if (stat.isDirectory()) {
freezeAllProcessDir(input, basePath, result);
}

return result;
};

/**
* Process file: freeze and write meta-data to result JSON
* @param absPath
* @param basePath
* @param {Object} result Result JSON
*/
function freezeAllProcessFile(absPath, basePath, result) {
var url = absPath;

if (freezableRe.test(url) || /\.(?:css|js|swf)$/.test(url)) {
url = freeze(url);
}

var relOriginalPath = PATH.relative(basePath, absPath);
var resolved = resolveUrl2(url);
url = (resolved == url ? PATH.relative(basePath, url) : resolved);

result[relOriginalPath] = url;
}

/**
* Read dir recursivly and process files
* @param dir
* @param basePath
* @param {Object} result Result JSON
*/
function freezeAllProcessDir(dir, basePath, result) {
FS.readdirSync(dir).forEach(function(file) {
file = PATH.resolve(dir, file);
var stat = FS.statSync(file);
if (stat.isFile()) {
freezeAllProcessFile(file, basePath, result);

} else if (stat.isDirectory()) {
freezeAllProcessDir(file, basePath, result);
}
});
}

/**
* Get path from "path" config by path if any.
*
Expand Down
51 changes: 51 additions & 0 deletions test/borschik-freeze.js
@@ -0,0 +1,51 @@
var ASSERT = require('assert');

describe('borschik freeze', function() {

var PATH = require('path');
var FS = require('fs');
var BORSCHIK = require('..');
var CP = require('child_process');

const testDir = PATH.resolve(__dirname, 'borschik-freeze');
const freezeDir = PATH.resolve(testDir, '_');
const resFile = PATH.resolve(testDir, 'result.json');

afterEach(function(cb) {
CP.exec('rm -rf ' + [freezeDir, resFile].join(' '), function() {
cb();
});
});

it('should freeze all files in dir and subdirs', function(cb) {

// proccess it
BORSCHIK.api
.freeze({
'input': testDir,
'output': resFile,
'minimize': true
})
.then(function() {
try {
var json = FS.readFileSync(resFile, 'utf-8');
var res = {
"borschik-freeze/.borschik": "borschik-freeze/.borschik",
"borschik-freeze/a.css": "//yandex.st/prj/_LWJmVQ8Q4Kn5C4mK3iYXHyieR7g.css",
"borschik-freeze/js/1.js": "//yandex.st/prj/_s2waZ-cd23dy_WqyHXmzbhscY_k.js",
"borschik-freeze/js/subdir/2.js": "//yandex.st/prj/_MGOy381jDKO0QzbHxeo46xQNEtQ.js",
"borschik-freeze/result.json": "borschik-freeze/result.json",
"borschik-freeze/test.png": "//yandex.st/prj/_wFPs-e1B3wMRud8TzGw7YHjS08I.png"
};
ASSERT.deepEqual(JSON.parse(json), res);
cb();
} catch(e) {
cb(e.toString());
}
})
.fail(function(e) {
cb(e.message);
});
});

});
9 changes: 9 additions & 0 deletions test/borschik-freeze/.borschik
@@ -0,0 +1,9 @@
{
"paths" : {
"_": "//yandex.st/prj/_"
},

"freeze_paths" : {
"./": "_"
}
}
7 changes: 7 additions & 0 deletions test/borschik-freeze/a.css
@@ -0,0 +1,7 @@
a {
p: url(test.png);
}

b {
p: url(test.png);
}
1 change: 1 addition & 0 deletions test/borschik-freeze/js/1.js
@@ -0,0 +1 @@
"1.js";
1 change: 1 addition & 0 deletions test/borschik-freeze/js/subdir/2.js
@@ -0,0 +1 @@
"2.js";
1 change: 1 addition & 0 deletions test/borschik-freeze/test.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit aa56a5c

Please sign in to comment.