Skip to content

Commit

Permalink
Add save method
Browse files Browse the repository at this point in the history
  • Loading branch information
belozer committed Jan 27, 2017
1 parent b7bd3ef commit cdcccb9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ module.exports = {
parse: require('./parse'),
assign: require('./assign'),
load: require('./load'),
stringify: require('./stringify')
stringify: require('./stringify'),
save: require('./save')
};
27 changes: 27 additions & 0 deletions lib/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const fs = require('fs');
const promisify = require('es6-promisify');
const stringify = require('./stringify');

const writeFile = promisify(fs.writeFile);

/**
* Save normalized declaration to target format
*
* @param {String} filename Filename for save declaration
* @param {BemCell[]} cells Normalized declaraions
* @param {Object} [opts] Addtional options
* @param {String} [opts.format='v2'] The desired format
* @param {String} [opts.exportType='cjs'] The desired type for export
* @returns {Promise.<void>}
*/
module.exports = (filename, cells, opts) => {
const defaults = {
format: 'v2',
exportType: 'cjs'
};
const data = stringify(cells, Object.assign({}, defaults, opts));

return writeFile(data);
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"eslint-config-pedant": "^0.8.0",
"jscs": "^3.0.3",
"matcha": "^0.7.0",
"nyc": "^10.0.0"
"nyc": "^10.0.0",
"proxyquire": "^1.7.10",
"sinon": "^1.17.7"
},
"scripts": {
"lint": "eslint . && jscs .",
Expand Down
51 changes: 51 additions & 0 deletions test/save.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const test = require('ava');
const sinon = require('sinon');
const proxyquire = require('proxyquire');

test.beforeEach(t => {
const stringifyStub = sinon.stub();
const writeFileStub = (a, cb) => setTimeout(cb(), 10);

t.context = {
stringifyStub: stringifyStub,
writeFileStub: writeFileStub,
save: proxyquire('../lib/save', {
'./stringify': stringifyStub,
fs: { writeFile: writeFileStub }
})
}
});

test('method save should be returns Promise', t => {
const promise = t.context.save();

t.truthy(promise.then, 'not a Promise')
t.notThrows(promise);
});

test('method save should be save file in cjs by default', t => {
const save = t.context.save;
const stringifyStub = t.context.stringifyStub;

save('decl-test.js');

t.truthy(stringifyStub.calledWith(undefined, { format: 'v2', exportType: 'cjs' }));
});

test('method save should be save file in custom format', t => {
const save = t.context.save;
const stringifyStub = t.context.stringifyStub;

save('decl-test.js', null, { format: 'v5' });

t.truthy(stringifyStub.calledWith(null, { format: 'v5', exportType: 'cjs' }));
});

test('method save should be save file in custom type', t => {
const save = t.context.save;
const stringifyStub = t.context.stringifyStub;

save('decl-test.js', null, { exportType: 'txt' });

t.truthy(stringifyStub.calledWith(null, { format: 'v2', exportType: 'txt' }));
});

0 comments on commit cdcccb9

Please sign in to comment.