Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-packager: Switch from Q to Bluebird as promises library #516

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react-native-start": "packager/packager.sh"
},
"dependencies": {
"bluebird": "2.9.21",
"connect": "2.8.3",
"jstransform": "10.0.1",
"react-timer-mixin": "^0.13.1",
Expand All @@ -53,7 +54,6 @@
"joi": "~5.1.0",
"module-deps": "3.5.6",
"optimist": "0.6.1",
"q": "1.0.1",
"sane": "1.0.1",
"uglify-js": "~2.4.16",
"underscore": "1.7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
'use strict';

// Bug with Jest because we're going to the node_modules that is a sibling
// of what jest thinks our root (the dir with the package.json) should be.
jest.autoMockOff();

module.exports = require.requireActual('q');
module.exports = require.requireActual('bluebird');

jest.autoMockOn();
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

jest
.dontMock('../index')
.dontMock('q')
.dontMock('path')
.dontMock('absolute-path')
.dontMock('../docblock')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

var ModuleDescriptor = require('../../ModuleDescriptor');
var q = require('q');
var Promise = require('bluebird');
var fs = require('fs');
var docblock = require('./docblock');
var path = require('path');
Expand All @@ -18,10 +18,10 @@ var debug = require('debug')('DependecyGraph');
var util = require('util');
var declareOpts = require('../../../lib/declareOpts');

var readFile = q.nfbind(fs.readFile);
var readDir = q.nfbind(fs.readdir);
var lstat = q.nfbind(fs.lstat);
var realpath = q.nfbind(fs.realpath);
var readFile = Promise.promisify(fs.readFile);
var readDir = Promise.promisify(fs.readdir);
var lstat = Promise.promisify(fs.lstat);
var realpath = Promise.promisify(fs.realpath);

var validateOpts = declareOpts({
roots: {
Expand Down Expand Up @@ -72,7 +72,7 @@ DependecyGraph.prototype.load = function() {
return this._loading;
}

this._loading = q.all([
this._loading = Promise.all([
this._search(),
this._buildAssetMap(),
]);
Expand Down Expand Up @@ -262,7 +262,7 @@ DependecyGraph.prototype._search = function() {
var dir = this._queue.shift();

if (dir == null) {
return q.Promise.resolve(this._graph);
return Promise.resolve(this._graph);
}

// Steps:
Expand Down Expand Up @@ -291,10 +291,10 @@ DependecyGraph.prototype._search = function() {

var processing = self._findAndProcessPackage(files, dir)
.then(function() {
return q.all(modulePaths.map(self._processModule.bind(self)));
return Promise.all(modulePaths.map(self._processModule.bind(self)));
});

return q.all([
return Promise.all([
processing,
self._search()
]);
Expand Down Expand Up @@ -323,7 +323,7 @@ DependecyGraph.prototype._findAndProcessPackage = function(files, root) {
if (packagePath != null) {
return this._processPackage(packagePath);
} else {
return q();
return Promise.resolve();
}
};

Expand All @@ -337,15 +337,15 @@ DependecyGraph.prototype._processPackage = function(packagePath) {
packageJson = JSON.parse(content);
} catch (e) {
debug('WARNING: malformed package.json: ', packagePath);
return q();
return Promise.resolve();
}

if (packageJson.name == null) {
debug(
'WARNING: package.json `%s` is missing a name field',
packagePath
);
return q();
return Promise.resolve();
}

packageJson._root = packageRoot;
Expand Down Expand Up @@ -555,7 +555,7 @@ DependecyGraph.prototype._getAbsolutePath = function(filePath) {

DependecyGraph.prototype._buildAssetMap = function() {
if (this._assetRoots == null || this._assetRoots.length === 0) {
return q();
return Promise.resolve();
}

this._assetMap = Object.create(null);
Expand Down Expand Up @@ -640,13 +640,13 @@ function withExtJs(file) {

function handleBrokenLink(e) {
debug('WARNING: error stating, possibly broken symlink', e.message);
return q();
return Promise.resolve();
}

function readAndStatDir(dir) {
return readDir(dir)
.then(function(files){
return q.all(files.map(function(filePath) {
return Promise.all(files.map(function(filePath) {
return realpath(path.join(dir, filePath)).catch(handleBrokenLink);
}));
}).then(function(files) {
Expand All @@ -660,7 +660,7 @@ function readAndStatDir(dir) {

return [
files,
q.all(stats),
Promise.all(stats),
];
});
}
Expand All @@ -676,7 +676,7 @@ function buildAssetMap(roots, processAsset) {
var root = queue.shift();

if (root == null) {
return q();
return Promise.resolve();
}

return readAndStatDir(root).spread(function(files, stats) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
'use strict';

jest.dontMock('../')
.dontMock('q')
.setMock('../../ModuleDescriptor', function(data) {return data;});

var q = require('q');
var Promise = require('bluebird');

describe('HasteDependencyResolver', function() {
var HasteDependencyResolver;
Expand Down Expand Up @@ -40,7 +39,7 @@ describe('HasteDependencyResolver', function() {
return deps;
});
depGraph.load.mockImpl(function() {
return q();
return Promise.resolve();
});

return depResolver.getDependencies('/root/index.js', { dev: false })
Expand Down Expand Up @@ -100,7 +99,7 @@ describe('HasteDependencyResolver', function() {
return deps;
});
depGraph.load.mockImpl(function() {
return q();
return Promise.resolve();
});

return depResolver.getDependencies('/root/index.js', { dev: true })
Expand Down Expand Up @@ -161,7 +160,7 @@ describe('HasteDependencyResolver', function() {
return deps;
});
depGraph.load.mockImpl(function() {
return q();
return Promise.resolve();
});

return depResolver.getDependencies('/root/index.js', { dev: false })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

var Promise = require('q').Promise;
var Promise = require('bluebird');
var ModuleDescriptor = require('../ModuleDescriptor');

var mdeps = require('module-deps');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

jest
.dontMock('../')
.dontMock('q')
.setMock(
'child_process',
{ exec: function(cmd, cb) { cb(null, '/usr/bin/watchman'); } }
Expand Down
8 changes: 3 additions & 5 deletions packager/react-packager/src/FileWatcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

var EventEmitter = require('events').EventEmitter;
var sane = require('sane');
var q = require('q');
var Promise = require('bluebird');
var util = require('util');
var exec = require('child_process').exec;

var Promise = q.Promise;

var detectingWatcherClass = new Promise(function(resolve) {
exec('which watchman', function(err, out) {
if (err || out.length === 0) {
Expand All @@ -41,7 +39,7 @@ function FileWatcher(rootConfigs) {

fileWatcher = this;

this._loading = q.all(
this._loading = Promise.all(
rootConfigs.map(createWatcher)
).then(function(watchers) {
watchers.forEach(function(watcher) {
Expand All @@ -59,7 +57,7 @@ util.inherits(FileWatcher, EventEmitter);
FileWatcher.prototype.end = function() {
return this._loading.then(function(watchers) {
watchers.forEach(function(watcher) {
return q.ninvoke(watcher, 'close');
return Promise.promisify(watcher.close, watcher)();
});
});
};
Expand Down
11 changes: 6 additions & 5 deletions packager/react-packager/src/JSTransformer/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var declareOpts = require('../lib/declareOpts');
var fs = require('fs');
var isAbsolutePath = require('absolute-path');
var path = require('path');
var q = require('q');
var Promise = require('bluebird');
var tmpdir = require('os').tmpDir();
var version = require('../../../../package.json').version;

var Promise = q.Promise;
var fsStat = Promise.promisify(fs.stat);
var fsWriteFile = Promise.promisify(fs.writeFile)

var validateOpts = declareOpts({
resetCache: {
Expand Down Expand Up @@ -74,7 +75,7 @@ Cache.prototype._set = function(filepath, loaderPromise) {
this._data[filepath] = loaderPromise.then(function(data) {
return [
data,
q.nfbind(fs.stat)(filepath)
fsStat(filepath)
];
}).spread(function(data, stat) {
this._persistEventually();
Expand Down Expand Up @@ -105,13 +106,13 @@ Cache.prototype._persistCache = function() {
var data = this._data;
var cacheFilepath = this._cacheFilePath;

this._persisting = q.all(_.values(data))
this._persisting = Promise.all(_.values(data))
.then(function(values) {
var json = Object.create(null);
Object.keys(data).forEach(function(key, i) {
json[key] = values[i];
});
return q.nfbind(fs.writeFile)(cacheFilepath, JSON.stringify(json));
return fsWriteFile(cacheFilepath, JSON.stringify(json));
})
.then(function() {
this._persisting = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jest
.dontMock('crypto')
.dontMock('../Cache');

var q = require('q');
var Promise = require('bluebird');

describe('JSTransformer Cache', function() {
var Cache;
Expand All @@ -32,7 +32,7 @@ describe('JSTransformer Cache', function() {
it('calls loader callback for uncached file', function() {
var cache = new Cache({projectRoots: ['/rootDir']});
var loaderCb = jest.genMockFn().mockImpl(function() {
return q();
return Promise.resolve();
});
cache.get('/rootDir/someFile', loaderCb);
expect(loaderCb).toBeCalledWith('/rootDir/someFile');
Expand All @@ -48,7 +48,7 @@ describe('JSTransformer Cache', function() {
});
var cache = new Cache({projectRoots: ['/rootDir']});
var loaderCb = jest.genMockFn().mockImpl(function() {
return q('lol');
return Promise.resolve('lol');
});
return cache.get('/rootDir/someFile', loaderCb).then(function(value) {
expect(value).toBe('lol');
Expand All @@ -65,7 +65,7 @@ describe('JSTransformer Cache', function() {
});
var cache = new Cache({projectRoots: ['/rootDir']});
var loaderCb = jest.genMockFn().mockImpl(function() {
return q('lol');
return Promise.resolve('lol');
});
return cache.get('/rootDir/someFile', loaderCb).then(function() {
var shouldNotBeCalled = jest.genMockFn();
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('JSTransformer Cache', function() {

var cache = new Cache({projectRoots: ['/rootDir']});
var loaderCb = jest.genMockFn().mockImpl(function() {
return q('new value');
return Promise.resolve('new value');
});

return cache.get('/rootDir/someFile', loaderCb).then(function(value) {
Expand Down Expand Up @@ -193,13 +193,13 @@ describe('JSTransformer Cache', function() {

var cache = new Cache({projectRoots: ['/rootDir']});
cache.get('/rootDir/bar', function() {
return q('bar value');
return Promise.resolve('bar value');
});
cache.get('/rootDir/foo', function() {
return q('foo value');
return Promise.resolve('foo value');
});
cache.get('/rootDir/baz', function() {
return q('baz value');
return Promise.resolve('baz value');
});

jest.runAllTicks();
Expand Down
8 changes: 4 additions & 4 deletions packager/react-packager/src/JSTransformer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
'use strict';

var fs = require('fs');
var q = require('q');
var Promise = require('bluebird');
var Cache = require('./Cache');
var _ = require('underscore');
var workerFarm = require('worker-farm');
var declareOpts = require('../lib/declareOpts');
var util = require('util');

var readFile = q.nfbind(fs.readFile);
var readFile = Promise.promisify(fs.readFile);

module.exports = Transformer;
Transformer.TransformError = TransformError;
Expand Down Expand Up @@ -63,7 +63,7 @@ function Transformer(options) {
});

if (options.transformModulePath == null) {
this._failedToStart = q.Promise.reject(new Error('No transfrom module'));
this._failedToStart = Promise.reject(new Error('No transfrom module'));
} else {
this._workers = workerFarm(
{autoStart: true, maxConcurrentCallsPerWorker: 1},
Expand Down Expand Up @@ -92,7 +92,7 @@ Transformer.prototype.loadFileAndTransform = function(filePath) {
.then(function(buffer) {
var sourceCode = buffer.toString();

return q.nfbind(workers)({
return Promise.promisify(workers)({
sourceCode: sourceCode,
filename: filePath,
}).then(
Expand Down
Loading