Skip to content

Commit

Permalink
Merge branch 'feature/FileExtension' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
VovanR committed Dec 11, 2015
2 parents 09899f2 + 64876e2 commit f59ddf3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
3 changes: 2 additions & 1 deletion bin/bemstyla.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var pkg = require('../package.json');

program
.version(pkg.version)
.option('-t, --type [type]', 'file type [styl]', 'styl')
.parse(process.argv);

if (program.args.length < 1) {
Expand All @@ -16,5 +17,5 @@ if (program.args.length < 1) {
var index = require('../lib/index');

_.forEach(program.args, function (arg) {
index(arg);
index(arg, program.type);
});
7 changes: 5 additions & 2 deletions lib/format-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ module.exports = {
/**
* File fish data
*
* @param {Object} [options]
* @param {String} [options.fileType='styl']
* @return {Object}
*/
format: function () {
format: function (options) {
options = options || {};
var result = {};

_.forEach(TERMS, function (term) {
result[term] = {
file: {
name: '',
dir: '',
ext: 'styl'
ext: options.fileType || 'styl'
}
};
});
Expand Down
24 changes: 18 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ var formatFileName = require('./format-file-name');
var creator = require('./creator');

/**
* @param {String} source
* @param {Object} options
* @param {String} options.source
* @param {String} [options.fileType='styl']
*/
var maker = function (source) {
var maker = function (options) {
var source = options.source;
var parsed = _.cloneDeep(parser.parse(source));

_.merge(parsed, formatFile.format());
_.merge(parsed, formatFile.format({
fileType: options.fileType
}));
_.merge(parsed, formatFileDir.format(source));
_.merge(parsed, formatFileName.format(source));

Expand All @@ -32,9 +37,10 @@ var maker = function (source) {

/**
* @param {String} source
* @param {String} [fileType='styl']
* @return {Promise}
*/
module.exports = function (source) {
module.exports = function (source, fileType) {
var promise;

if (/\.jade$/.test(source)) {
Expand All @@ -50,10 +56,16 @@ module.exports = function (source) {
return promise.then(function (data) {
if (_.isArray(data)) {
_.forEach(data, function (item) {
maker(item);
maker({
source: item,
fileType: fileType
});
});
} else {
maker(data);
maker({
source: data,
fileType: fileType
});
}
});
};
16 changes: 14 additions & 2 deletions test/format-file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,31 @@ describe('formatFile', function () {
};

it('should return mixed JSON object', function () {
var res = _.clone(testResult);
var res = _.cloneDeep(testResult);

assert.deepEqual(formatFile.format(), res);
});

it('should not assign objects', function () {
var res = _.clone(testResult);
var res = _.cloneDeep(testResult);

var formatted = formatFile.format();
assert.deepEqual(formatted, res);
formatted.block.file.dir = 'foo';
res.block.file.dir = 'foo';
assert.deepEqual(formatted, res);
});

it('should set filetype', function () {
var res = _.cloneDeep(testResult);
_.forEach(res, function (r) {
r.file.ext = 'css';
});

var formatted = formatFile.format({
fileType: 'css'
});
assert.deepEqual(formatted, res);
});
});
});
26 changes: 26 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,30 @@ describe('index', function () {
done();
});
});

it('should fire formatFile.format with options', function (done) {
sinon.stub(formatFile, 'format');
sinon.stub(creator, 'touch', function () {
return {
/**
*/
then: function () {},
/**
*/
catch: function () {}
};
});

index('block', 'css')
.then(function () {
assert.isTrue(formatFile.format.called);
assert.deepEqual(formatFile.format.getCall(0).args[0], {
fileType: 'css'
});

creator.touch.restore();
formatFile.format.restore();
done();
});
});
});

0 comments on commit f59ddf3

Please sign in to comment.