From 08be00a22672e4f778e7d3f2624dfc294db0c30f Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Sun, 19 Oct 2014 11:16:28 -0400 Subject: [PATCH] Add mem-fs-editor interface --- .jscs.json | 3 +-- lib/base.js | 30 ++++++++++++++++++++++++++++++ lib/util/conflicter.js | 2 ++ package.json | 4 +++- test/base.js | 27 +++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/.jscs.json b/.jscs.json index 7e5f84d3..58082ea1 100644 --- a/.jscs.json +++ b/.jscs.json @@ -23,7 +23,6 @@ "disallowMultipleLineBreaks": true, "disallowKeywordsOnNewLine": ["else"], "requireLineFeedAtFileEnd": true, - "safeContextKeyword": "self", "excludeFiles": ["node_modules/**", "bower_components/**"], - "validateIndentation": 2 + "validateIndentation": 2 } diff --git a/lib/base.js b/lib/base.js index 7f1e6f96..bfa3de64 100644 --- a/lib/base.js +++ b/lib/base.js @@ -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 }; @@ -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()); @@ -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) { @@ -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 diff --git a/lib/util/conflicter.js b/lib/util/conflicter.js index e71c1b76..9b6b68bf 100644 --- a/lib/util/conflicter.js +++ b/lib/util/conflicter.js @@ -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); @@ -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 diff --git a/package.json b/package.json index a67bdc86..4693bce6 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test/base.js b/test/base.js index e7f06dc2..b359ecf1 100644 --- a/test/base.js +++ b/test/base.js @@ -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 () { @@ -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 () {