Skip to content

Commit

Permalink
Merge 63d8abf into d023ead
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno committed Jan 16, 2014
2 parents d023ead + 63d8abf commit d9a98bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/actions/actions.js
Expand Up @@ -203,18 +203,24 @@ actions.read = function read(filepath, encoding) {
* @param {String} content
*/

actions.write = function write(filepath, content) {
actions.write = function write(filepath, content, fileMode) {
this.checkForCollision(filepath, content, function (err, config) {
if (err) {
config.callback(err);
return this.emit('error', err);
}

var fileOptions = {};

if (fileMode) {
fileOptions.mode = fileMode;
}

// create or force means file write, identical or skip prevent the
// actual write
if (/force|create/.test(config.status)) {
mkdirp.sync(path.dirname(filepath));
fs.writeFileSync(filepath, content);
fs.writeFileSync(filepath, content, fileOptions);
}

config.callback();
Expand Down Expand Up @@ -280,10 +286,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 fileMode = fs.statSync(source).mode;

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

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

Expand Down
8 changes: 8 additions & 0 deletions test/actions.js
Expand Up @@ -201,6 +201,7 @@ describe('yeoman.generators.Base', function () {
describe('without options', function () {
before(function (done) {
this.dummy.foo = 'fooooooo';
this.dummy.template('foo.js', 'write/to/from-foo.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 Down Expand Up @@ -236,6 +237,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 + '/foo.js');
var bodyStat = fs.statSync('write/to/from-foo.js');
assert.equal(originFileStat.mode, bodyStat.mode);
});

});

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

0 comments on commit d9a98bb

Please sign in to comment.