Skip to content

Commit

Permalink
Merge pull request #679 from SBoudrias/mem-fs
Browse files Browse the repository at this point in the history
Fix yeoman file system
  • Loading branch information
SBoudrias committed Oct 27, 2014
2 parents 9df612a + 08be00a commit a0ae1f3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .jscs.json
Expand Up @@ -23,7 +23,6 @@
"disallowMultipleLineBreaks": true,
"disallowKeywordsOnNewLine": ["else"],
"requireLineFeedAtFileEnd": true,
"safeContextKeyword": "self",
"excludeFiles": ["node_modules/**", "bower_components/**"],
"validateIndentation": 2
"validateIndentation": 2
}
30 changes: 30 additions & 0 deletions lib/base.js
Expand Up @@ -10,11 +10,13 @@ var findup = require('findup-sync');
var chalk = require('chalk');
var file = require('file-utils');
var nopt = require('nopt');
var through = require('through2');

var engines = require('./util/engines');
var Conflicter = require('./util/conflicter');
var Storage = require('./util/storage');
var GruntfileEditor = require('gruntfile-editor');
var FileEditor = require('mem-fs-editor');

var noop = function () {};
var fileLogger = { write: noop, warn: noop };
Expand Down Expand Up @@ -142,6 +144,7 @@ var Base = module.exports = function Base(args, options) {
dest: this.sourceRoot.bind(this),
logger: fileLogger
});
this.fs = FileEditor.create(this.env.sharedFs);

// Register collision filters which return true if the file can be written to
this.dest.registerValidationFilter('collision', this.getCollisionFilter());
Expand Down Expand Up @@ -422,6 +425,10 @@ Base.prototype.run = function run(args, cb) {

methods.filter(methodIsValid).forEach(addInQueue);

this.env.runLoop.add('conflicts', this._writeFiles.bind(this), {
once: 'write memory fs to disk'
});

// Add the default conflicts handling
this.env.runLoop.add('conflicts', function (done) {
this.conflicter.resolve(function (err) {
Expand Down Expand Up @@ -685,6 +692,29 @@ Base.prototype.determineAppname = function () {
return appname.replace(/[^\w\s]+?/g, ' ');
};

/**
* Write memory fs file to disk and logging results
* @param {Function} done - callback once files are written
*/
Base.prototype._writeFiles = function (done) {
var self = this;
this.fs.commit([
through.obj(function (file, enc, cb) {
var stream = this;
self.checkForCollision(file.path, file.contents.toString(), function (err, status) {
if (err) return cb(err);
if (status !== 'skip') {
stream.push(file);
}
cb();
});
self.conflicter.resolve();
})
], function () {
done();
});
};

/**
* Extend this Class to create a new one inherithing this one.
* Also add a helper \_\_super__ object pointing to the parent prototypes methods
Expand Down
2 changes: 2 additions & 0 deletions lib/util/conflicter.js
Expand Up @@ -7,6 +7,7 @@ var assert = require('assert');
var async = require('async');
var isBinaryFile = require('isbinaryfile');
var util = require('util');
var _ = require('lodash');

var Conflicter = module.exports = function Conflicter(adapter) {
events.EventEmitter.call(this);
Expand Down Expand Up @@ -52,6 +53,7 @@ Conflicter.prototype.resolve = function resolve(cb) {
}

this.collision(conflict.file, conflict.content, function (status) {
_.pull(this.conflicts, conflict);
this.emit('resolved:' + conflict.file, {
status: status,
callback: next
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -46,6 +46,7 @@
"inquirer": "^0.8.0",
"isbinaryfile": "^2.0.0",
"lodash": "^2.4.1",
"mem-fs-editor": "SBoudrias/mem-fs-editor",
"mime": "^1.2.9",
"mkdirp": "^0.5.0",
"nopt": "^3.0.0",
Expand All @@ -54,9 +55,10 @@
"run-async": "^0.1.0",
"shelljs": "^0.3.0",
"text-table": "^0.2.0",
"through2": "^0.6.3",
"underscore.string": "^2.3.1",
"xdg-basedir": "^1.0.0",
"yeoman-environment": "^1.0.2"
"yeoman-environment": "yeoman/environment#mem-fs"
},
"devDependencies": {
"gulp": "^3.6.0",
Expand Down
27 changes: 27 additions & 0 deletions test/base.js
Expand Up @@ -106,6 +106,14 @@ describe('generators.Base', function () {
done();
});
});

it('setup fs editor', function () {
var generator = new generators.Base([], {
env: this.env,
resolved: 'test'
});
assert(generator.fs);
});
});

describe('prototype', function () {
Expand Down Expand Up @@ -349,6 +357,25 @@ describe('generators.Base', function () {
done();
}.bind(this));
});

it('commit mem-fs to disk', function (done) {
var filepath;
var oldFilePath;
this.TestGenerator.prototype.writing = function () {
oldFilePath = path.join(this.destinationRoot(), 'old-system.txt');
// Just ensure we don't have issue if both old and new system run.
this.write(oldFilePath, 'hey');
this.fs.write(
filepath = path.join(this.destinationRoot(), 'fromfs.txt'),
'generated'
);
};
this.testGen.run(function () {
assert(fs.existsSync(oldFilePath));
assert(fs.existsSync(filepath));
done();
}.bind(this));
});
});

describe('#_', function () {
Expand Down

0 comments on commit a0ae1f3

Please sign in to comment.