react / wstein / node_modules / jest-cli / node_modules / node-haste / lib / resource / ProjectConfiguration.js
80669 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/15/*jslint proto:true*/1617var inherits = require('util').inherits;18var node_path = path = require('path');19var fs = require('fs');2021var Resource = require('./Resource');2223/**24* Resource for package.json files25* @extends {Resource}26* @class27* @param {String} path path of the resource28* @param {Object} data source code of the resource29*/30function ProjectConfiguration(path, data) {31this.path = node_path.normalize(path);32this.id = path;33this.data = data;34}35inherits(ProjectConfiguration, Resource);36ProjectConfiguration.__proto__ = Resource;3738ProjectConfiguration.prototype.type = 'ProjectConfiguration';3940/**41* Returns haste specific prefix42* @return {String}43*/44ProjectConfiguration.prototype.getHastePrefix = function() {45return this.data.haste && this.data.haste.prefix !== undefined ?46this.data.haste.prefix :47path.basename(path.dirname(this.path));48};4950/**51* Returns all roots affected by this package52* @return {Array.<String>}53*/54ProjectConfiguration.prototype.getHasteRoots = function() {55var dirname = path.dirname(this.path);56if (this.data.haste && this.data.haste.roots) {57return this.data.haste.roots.map(function(root) {58return path.join(dirname, root);59});60}61return [dirname];62};6364/**65* The "Project Path" is the absolute path of the directory where "projects"66* live. Projects consist of a root folder `myProject` which contains a67* `package.json` file at `myProject/package.json`.68*69* If a `package.json` lives at /some/dir/myProject/package.json then, the70* inferred project dir would be /some/dir/71*72* Note that the project path is the directory *above* the module root.73*/74ProjectConfiguration.prototype.getInferredProjectPath = function() {75return path.resolve(this.path, '..', '..');76};7778/**79* Simple convention for determining uniqueness of commonJS style modules.80*81* We don't try to "pre-resolve" any IDs here - To do this properly, we'd need a82* more complete picture of the resource map, that is only available at83* `postProcess` time. Our only job at this point is to come up with *something*84* to uniquely identify the 'JS' resource. The convention used here is to take85* the project (`package.json` "name" field) and append the path to the physical86* file.87*88* Attempting to choose an ID that has _meaning_ (by trying to pre-resolve89* `projectName/index.js` to `projectName` etc) is impossible at this time. An90* ambiguity occurs when we later discover that the `package.json` pointed the91* "main" module to another file. We couldn't possibly know which should claim92* the `projectName` ID until we've processed all resources. This is why93* dependency resolution can't *properly* happen until `postProcess`.94*95* By convention, the ID that we use to store commonJS resources is the96* `package.json` project name followed by the relative path from `package.json`97* to the file.98*99* > projectName/index.js100* > projectName/path/to.js101*102* A nice side effect of the particular convention chosen here, is when103* statically analyzing dependencies in `postProcess`:104*105* > require('x/y/z.js')106*107* requiring files by IDs always resolves to the module with that ID. Other108* conventions don't have this property. So if you can simply lookup 'JS'109* resource `'x/y/z.js'` and quickly get a hit, you don't need to fall back to110* more expensive path resolutions - which must analyze `package.json` files111* etc.112*113* Another loader will assign IDs to `@providesModule` JS files.114*115* Any resource "path" identifies the physical resource, but the resource ID116* doesn't yet identify a physical resource, until the "type" of resource is117* specified. You might have two resources, with the same ID and different118* types. For example {id: 'hello/hello.js'} does not identify a physical file,119* but {type: 'JS', id: 'hello/hello.js'} might.120*121* Two physical files might have the same module ID, but different types, as is122* the case with mock files.123*124* A:125* id: myProject/x.js126* path: /home/myProject/x.js127* type: 'JS'128*129* A-mock:130* id: myProject/x.js131* path: /home/myProject/x-mock.js132* type: 'JSMock'133*134* However, no two distinct files have the same resource ID and the same135* resource type and obviously no two distinct files have the same absolute136* "path".137*138*139* @param {String} filePath140* @return {String|null}141*/142ProjectConfiguration.prototype.resolveID = function(filePath) {143var hasteDirectories = this.getHasteRoots();144var prefix = this.getHastePrefix();145146for (var i = 0; i < hasteDirectories.length; i++) {147var hasteDirectory = hasteDirectories[i];148if (filePath.indexOf(hasteDirectory + path.sep) === 0) {149var result = path.relative(hasteDirectory, filePath);150if (prefix) {151result = path.join(prefix, result);152}153return result;154}155}156157return null;158};159160module.exports = ProjectConfiguration;161162163