Skip to content

Commit

Permalink
Merge pull request #474 from ruyadorno/iss-464
Browse files Browse the repository at this point in the history
Added the ability to preserve file mode on method actions.template
  • Loading branch information
SBoudrias committed Jan 19, 2014
2 parents 0c94628 + a3ef9b1 commit 2c0116d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/actions/actions.js
Expand Up @@ -199,9 +199,10 @@ actions.read = function read(filepath, encoding) {
*
* @param {String} filepath
* @param {String} content
* @param {Object} writeFile An object containing options for the file writing, as shown here: http://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback
*/

actions.write = function write(filepath, content) {
actions.write = function write(filepath, content, writeFile) {
this.checkForCollision(filepath, content, function (err, config) {
if (err) {
config.callback(err);
Expand All @@ -212,7 +213,7 @@ actions.write = function write(filepath, content) {
// actual write
if (/force|create/.test(config.status)) {
mkdirp.sync(path.dirname(filepath));
fs.writeFileSync(filepath, content);
fs.writeFileSync(filepath, content, writeFile);
}

config.callback();
Expand Down Expand Up @@ -278,10 +279,16 @@ actions.template = function template(source, destination, data, options) {
data = data || this;
destination = destination || source;

if (!this.isPathAbsolute(source)) {
source = path.join(this.sourceRoot(), source);
}

var body = this.read(source, 'utf8');
var writeFile = { mode: fs.statSync(source).mode };

body = this.engine(body, data, options);

this.write(destination, body);
this.write(destination, body, writeFile);
return this;
};

Expand Down
17 changes: 17 additions & 0 deletions test/actions.js
Expand Up @@ -194,8 +194,14 @@ describe('yeoman.generators.Base', function () {

describe('#template()', function () {
describe('without options', function () {

before(function (done) {
// Create file with weird permission for testing
var permFileName = this.fixtures + '/perm-test.js';
fs.writeFileSync(permFileName, 'var foo;', { mode: parseInt(733, 8) });

this.dummy.foo = 'fooooooo';
this.dummy.template('perm-test.js', 'write/to/perm-test.js');
this.dummy.template('foo-template.js', 'write/to/from-template.js');
this.dummy.template('foo-template.js');
this.dummy.template('foo-template.js', 'write/to/from-template-bar.js', {
Expand All @@ -208,6 +214,10 @@ describe('yeoman.generators.Base', function () {
this.dummy.conflicter.resolve(done);
});

after(function () {
fs.unlinkSync(this.fixtures + '/perm-test.js');
});

it('copy and process source file to destination', function (done) {
fs.stat('write/to/from-template.js', done);
});
Expand All @@ -231,6 +241,13 @@ describe('yeoman.generators.Base', function () {
var body = fs.readFileSync('write/to/template-tags.js', 'utf8');
assert.textEqual(body, 'foo = bar\n');
});

it('should keep file mode', function () {
var originFileStat = fs.statSync(this.fixtures + '/perm-test.js');
var bodyStat = fs.statSync('write/to/perm-test.js');
assert.equal(originFileStat.mode, bodyStat.mode);
});

});

describe('with options', function () {
Expand Down

0 comments on commit 2c0116d

Please sign in to comment.