Skip to content

Commit

Permalink
Async #14 creator.js
Browse files Browse the repository at this point in the history
  • Loading branch information
VovanR committed Sep 19, 2015
1 parent 52235e1 commit 3a13690
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 82 deletions.
88 changes: 58 additions & 30 deletions lib/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,71 @@
*/

var parser = require('./parser');
var Promise = require('bluebird');
var fs = require('fs');
var mkdirp = require('mkdirp').sync;
var mkdirp = require('mkdirp');
var path = require('path');
var exists = fs.existsSync || path.existsSync;

module.exports = {
/**
* Makes dirs recursively
*
* @param {String} dir
* @private
*/
_mkdir: function (dir) {
if (!exists(dir)) {
mkdirp(dir);
}
},

/**
* Touches files and writes file data
*
* @param {Object} file
*/
touch: function (file) {
/**
* Makes dirs recursively
*
* @param {String} dir
* @return {Promise}
* @private
*/
function _mkdir(dir) {
return new Promise(function (resolve, reject) {
fs.stat(dir, function (err, stats) {
if (!err || (stats && stats.isDirectory())) {
return reject();
}
mkdirp(dir, function (err) {
if (err) {
return reject();
}
return resolve();
});
});
});
}

/**
* Touches files and writes file data
*
* @param {Object} file
* @return {Promise}
*/
function touch(file) {
return new Promise(function (resolve, reject) {
if (!file.dir || !file.name) {
return;
return reject();
}

this._mkdir(file.dir);
_mkdir(file.dir)
.then(function () {
var filePath = path.join(file.dir, file.name + '.' + file.ext);

var filePath = path.join(file.dir, file.name + '.' + file.ext);
fs.stat(filePath, function (err, stats) {
if (!err || (stats && stats.isFile())) {
return reject();
}

if (exists(filePath)) {
return;
}
var text = '.' + file.name + '\n {}\n';

fs.writeFile(filePath, text, function (err) {
if (err) {
return reject();
}

var text = '.' + file.name + '\n {}\n';
fs.writeFileSync(filePath, text);
},
resolve();
});
});
})
.catch(reject);
});
}

module.exports = {
_mkdir: _mkdir,
touch: touch,
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"homepage": "https://github.com/VovanR/bemstyla",
"license": "MIT",
"dependencies": {
"bluebird": "^2.10.0",
"commander": "^2.7.1",
"fs": "0.0.2",
"lodash": "^3.8.0",
Expand Down
127 changes: 75 additions & 52 deletions test/creator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,90 +27,109 @@ describe('creator', function () {
describe('_mkdir', function () {
/**
* @param {String} term
* @param {Function} done
* @param {Number} [index=0]
*/
var testTerm = function (term) {
_.forEach(testData, function (data) {
clearTemp();
var _dir = data.output[term].file.dir;
var dir = path.join(TEMP_DIR, _dir);

if (_dir === '') {
return;
}

assert.notOk(exists(dir), 'rm ' + _dir);
creator._mkdir(dir);
assert.ok(exists(dir), 'mk ' + _dir);
});
};
function test(term, done, index) {
index = index === undefined ? -1 : index;
if (++index >= testData.length) {
done();
return;
}

clearTemp();
var data = testData[index];
var _dir = data.output[term].file.dir;
var dir = path.join(TEMP_DIR, _dir);

if (_dir === '') {
test(term, done, index);
return;
}

assert.notOk(exists(dir), 'rm ' + _dir);

creator._mkdir(dir)
.then(function () {
assert.ok(exists(dir), 'mk ' + _dir);
})
.finally(function () {
test(term, done, index);
});
}

it('should add block dir', function (done) {
testTerm('block');
done();
test('block', done);
});

it('should add block mod dir', function (done) {
testTerm('bmod');
done();
test('bmod', done);
});

it('should add elem dir', function (done) {
testTerm('elem');
done();
test('elem', done);
});

it('should add elem mod dir', function (done) {
testTerm('emod');
done();
test('emod', done);
});
});

describe('#touch', function () {
/**
* @param {String} term
* @param {Function} done
* @param {Number} [index=0]
*/
var testTerm = function (term) {
_.forEach(testData, function (data) {
clearTemp();
var fileData = _.clone(data.output[term].file);

if (!fileData.dir || !fileData.name) {
return;
}
function test(term, done, index) {
index = index === undefined ? -1 : index;
if (++index >= testData.length) {
done();
return;
}

clearTemp();
var data = testData[index];
var fileData = _.clone(data.output[term].file);

if (!fileData.dir || !fileData.name) {
test(term, done, index);
return;
}

_.merge(fileData, {
dir: path.join(TEMP_DIR, fileData.dir),
});

_.merge(fileData, {
dir: path.join(TEMP_DIR, fileData.dir),
creator.touch(fileData)
.then(function () {
var filePath = path.join(fileData.dir, fileData.name + '.' + fileData.ext);
assert.isTrue(fs.statSync(filePath).isFile());
var text = '.' + fileData.name + '\n {}\n';
assert.equal(text, fs.readFileSync(filePath));
})
.finally(function () {
test(term, done, index);
});
creator.touch(fileData);

var filePath = path.join(fileData.dir, fileData.name + '.' + fileData.ext);
assert.isTrue(fs.statSync(filePath).isFile());
var text = '.' + fileData.name + '\n {}\n';
assert.equal(text, fs.readFileSync(filePath));
});
};
}

it('should add block file', function (done) {
testTerm('block');
done();
test('block', done);
});

it('should add block mod file', function (done) {
testTerm('bmod');
done();
test('bmod', done);
});

it('should add elem file', function (done) {
testTerm('elem');
done();
test('elem', done);
});

it('should add elem mod file', function (done) {
testTerm('emod');
done();
test('emod', done);
});

it('should not rewrite exists files', function () {
it('should not rewrite exists files', function (done) {
mockfs.restore();

var fileData = {
Expand All @@ -126,8 +145,12 @@ describe('creator', function () {
mockfs(mockData);

assert.equal(fs.readFileSync(filePath), testText);
creator.touch(fileData);
assert.equal(fs.readFileSync(filePath), testText);
creator.touch(fileData)
.finally(function () {
assert.equal(fs.readFileSync(filePath).toString(), testText);
done();
})
.catch(function (err) {});
});
});
});

0 comments on commit 3a13690

Please sign in to comment.