xref: /expo/packages/@expo/config/build/paths/paths.js (revision 8a424beb)
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.ensureSlash = ensureSlash;
7exports.getEntryPoint = getEntryPoint;
8exports.getEntryPointWithExtensions = getEntryPointWithExtensions;
9exports.getFileWithExtensions = getFileWithExtensions;
10exports.getPossibleProjectRoot = getPossibleProjectRoot;
11exports.resolveEntryPoint = resolveEntryPoint;
12exports.resolveFromSilentWithExtensions = resolveFromSilentWithExtensions;
13function _fs() {
14  const data = _interopRequireDefault(require("fs"));
15  _fs = function () {
16    return data;
17  };
18  return data;
19}
20function _path() {
21  const data = _interopRequireDefault(require("path"));
22  _path = function () {
23    return data;
24  };
25  return data;
26}
27function _resolveFrom() {
28  const data = _interopRequireDefault(require("resolve-from"));
29  _resolveFrom = function () {
30    return data;
31  };
32  return data;
33}
34function _extensions() {
35  const data = require("./extensions");
36  _extensions = function () {
37    return data;
38  };
39  return data;
40}
41function _Config() {
42  const data = require("../Config");
43  _Config = function () {
44    return data;
45  };
46  return data;
47}
48function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
49// https://github.com/facebook/create-react-app/blob/9750738cce89a967cc71f28390daf5d4311b193c/packages/react-scripts/config/paths.js#L22
50function ensureSlash(inputPath, needsSlash) {
51  const hasSlash = inputPath.endsWith('/');
52  if (hasSlash && !needsSlash) {
53    return inputPath.substr(0, inputPath.length - 1);
54  } else if (!hasSlash && needsSlash) {
55    return `${inputPath}/`;
56  } else {
57    return inputPath;
58  }
59}
60function getPossibleProjectRoot() {
61  return _fs().default.realpathSync(process.cwd());
62}
63const nativePlatforms = ['ios', 'android'];
64function resolveEntryPoint(projectRoot, {
65  platform,
66  projectConfig
67}) {
68  const platforms = nativePlatforms.includes(platform) ? [platform, 'native'] : [platform];
69  return getEntryPoint(projectRoot, ['./index'], platforms, projectConfig);
70}
71function getEntryPoint(projectRoot, entryFiles, platforms, projectConfig) {
72  const extensions = (0, _extensions().getBareExtensions)(platforms);
73  return getEntryPointWithExtensions(projectRoot, entryFiles, extensions, projectConfig);
74}
75
76// Used to resolve the main entry file for a project.
77function getEntryPointWithExtensions(projectRoot, entryFiles, extensions, projectConfig) {
78  if (!projectConfig) {
79    // drop all logging abilities
80    const original = process.stdout.write;
81    process.stdout.write = () => true;
82    try {
83      projectConfig = (0, _Config().getConfig)(projectRoot, {
84        skipSDKVersionRequirement: true
85      });
86    } finally {
87      process.stdout.write = original;
88    }
89  }
90  const {
91    pkg
92  } = projectConfig;
93  if (pkg) {
94    // If the config doesn't define a custom entry then we want to look at the `package.json`s `main` field, and try again.
95    const {
96      main
97    } = pkg;
98    if (main && typeof main === 'string') {
99      // Testing the main field against all of the provided extensions - for legacy reasons we can't use node module resolution as the package.json allows you to pass in a file without a relative path and expect it as a relative path.
100      let entry = getFileWithExtensions(projectRoot, main, extensions);
101      if (!entry) {
102        // Allow for paths like: `{ "main": "expo/AppEntry" }`
103        entry = resolveFromSilentWithExtensions(projectRoot, main, extensions);
104        if (!entry) throw new Error(`Cannot resolve entry file: The \`main\` field defined in your \`package.json\` points to a non-existent path.`);
105      }
106      return entry;
107    }
108  }
109
110  // Now we will start looking for a default entry point using the provided `entryFiles` argument.
111  // This will add support for create-react-app (src/index.js) and react-native-cli (index.js) which don't define a main.
112  for (const fileName of entryFiles) {
113    const entry = resolveFromSilentWithExtensions(projectRoot, fileName, extensions);
114    if (entry) return entry;
115  }
116  try {
117    // If none of the default files exist then we will attempt to use the main Expo entry point.
118    // This requires `expo` to be installed in the project to work as it will use `node_module/expo/AppEntry.js`
119    // Doing this enables us to create a bare minimum Expo project.
120
121    // TODO(Bacon): We may want to do a check against `./App` and `expo` in the `package.json` `dependencies` as we can more accurately ensure that the project is expo-min without needing the modules installed.
122    return (0, _resolveFrom().default)(projectRoot, 'expo/AppEntry');
123  } catch {
124    throw new Error(`The project entry file could not be resolved. Please define it in the \`main\` field of the \`package.json\`, create an \`index.js\`, or install the \`expo\` package.`);
125  }
126}
127
128// Resolve from but with the ability to resolve like a bundler
129function resolveFromSilentWithExtensions(fromDirectory, moduleId, extensions) {
130  for (const extension of extensions) {
131    const modulePath = _resolveFrom().default.silent(fromDirectory, `${moduleId}.${extension}`);
132    if (modulePath && modulePath.endsWith(extension)) {
133      return modulePath;
134    }
135  }
136  return _resolveFrom().default.silent(fromDirectory, moduleId) || null;
137}
138
139// Statically attempt to resolve a module but with the ability to resolve like a bundler.
140// This won't use node module resolution.
141function getFileWithExtensions(fromDirectory, moduleId, extensions) {
142  const modulePath = _path().default.join(fromDirectory, moduleId);
143  if (_fs().default.existsSync(modulePath)) {
144    return modulePath;
145  }
146  for (const extension of extensions) {
147    const modulePath = _path().default.join(fromDirectory, `${moduleId}.${extension}`);
148    if (_fs().default.existsSync(modulePath)) {
149      return modulePath;
150    }
151  }
152  return null;
153}
154//# sourceMappingURL=paths.js.map