[react-packager] Pick up package changes while running

This commit is contained in:
Amjad Masad 2015-03-23 11:36:20 -07:00
parent ac8cd5a5d0
commit a6b154332b
4 changed files with 57 additions and 32 deletions

View File

@ -8,7 +8,6 @@
var sharedBlacklist = [
__dirname,
'website',
'node_modules/parse/node_modules/xmlhttprequest/lib/XMLHttpRequest.js',
'node_modules/react-tools/src/utils/ImmutableObject.js',
'node_modules/react-tools/src/core/ReactInstanceHandles.js',
'node_modules/react-tools/src/event/EventPropagators.js'

View File

@ -296,35 +296,50 @@ DependecyGraph.prototype._findAndProcessPackage = function(files, root) {
}
if (packagePath != null) {
return readFile(packagePath, 'utf8')
.then(function(content) {
var packageJson;
try {
packageJson = JSON.parse(content);
} catch (e) {
debug('WARNING: malformed package.json: ', packagePath);
return q();
}
if (packageJson.name == null) {
debug(
'WARNING: package.json `%s` is missing a name field',
packagePath
);
return q();
}
packageJson._root = root;
self._packageByRoot[root] = packageJson;
self._packagesById[packageJson.name] = packageJson;
return packageJson;
});
return this._processPackage(packagePath);
} else {
return q();
}
};
DependecyGraph.prototype._processPackage = function(packagePath) {
var packageRoot = path.dirname(packagePath);
var self = this;
return readFile(packagePath, 'utf8')
.then(function(content) {
var packageJson;
try {
packageJson = JSON.parse(content);
} catch (e) {
debug('WARNING: malformed package.json: ', packagePath);
return q();
}
if (packageJson.name == null) {
debug(
'WARNING: package.json `%s` is missing a name field',
packagePath
);
return q();
}
packageJson._root = packageRoot;
self._addPackageToIndices(packageJson);
return packageJson;
});
};
DependecyGraph.prototype._addPackageToIndices = function(packageJson) {
this._packageByRoot[packageJson._root] = packageJson;
this._packagesById[packageJson.name] = packageJson;
};
DependecyGraph.prototype._removePackageFromIndices = function(packageJson) {
delete this._packageByRoot[packageJson._root];
delete this._packagesById[packageJson.name];
};
/**
* Parse a module and update indices.
*/
@ -436,16 +451,27 @@ DependecyGraph.prototype._processFileChange = function(eventType, filePath, root
this._debugUpdateEvents.push({event: eventType, path: filePath});
var isPackage = path.basename(filePath) === 'package.json';
if (eventType === 'delete') {
var module = this._graph[absPath];
if (module == null) {
return;
}
if (isPackage) {
var packageJson = this._packageByRoot[path.dirname(absPath)];
if (packageJson) {
this._removePackageFromIndices(packageJson);
}
} else {
var module = this._graph[absPath];
if (module == null) {
return;
}
this._deleteModule(module);
this._deleteModule(module);
}
} else if (!(stat && stat.isDirectory())) {
var self = this;
this._loading = this._loading.then(function() {
if (isPackage) {
return self._processPackage(absPath);
}
return self._processModule(absPath);
});
}

View File

@ -56,7 +56,7 @@ function createWatcher(root) {
}
return detectingWatcherClass.then(function(Watcher) {
var watcher = new Watcher(root, {glob: '**/*.js'});
var watcher = new Watcher(root, {glob: ['**/*.js', '**/package.json']});
return new Promise(function(resolve, reject) {
var rejectTimeout = setTimeout(function() {

View File

@ -67,7 +67,7 @@ function Server(options) {
this._debouncedFileChangeHandler = _.debounce(function(filePath) {
self._rebuildPackages(filePath);
self._informChangeWatchers();
}, 50, true);
}, 50);
}
Server.prototype._onFileChange = function(type, filepath, root) {