paths.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const path = require('path');
  2. const fs = require('fs');
  3. const url = require('url');
  4. const appDirectory = fs.realpathSync(process.cwd());
  5. const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
  6. const envPublicUrl = process.env.PUBLIC_URL;
  7. function ensureSlash(_path, needsSlash) {
  8. const hasSlash = _path.endsWith('/');
  9. if (hasSlash && !needsSlash) {
  10. return _path.substr(_path, _path.length - 1);
  11. } else if (!hasSlash && needsSlash) {
  12. return `${_path}/`;
  13. }
  14. return _path;
  15. }
  16. const getPublicUrl = appPackageJson => envPublicUrl || require(appPackageJson).homepage;
  17. function getServedPath(appPackageJson) {
  18. const publicUrl = getPublicUrl(appPackageJson);
  19. const servedUrl = envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
  20. return ensureSlash(servedUrl, true);
  21. }
  22. module.exports = {
  23. dotenv: resolveApp('.env'),
  24. appBuild: resolveApp('build'),
  25. appDist: resolveApp('dist'),
  26. appPublic: resolveApp('public'),
  27. appHtml: resolveApp('public/index.html'),
  28. appIndexJs: resolveApp('src/index.js'),
  29. appPackageJson: resolveApp('package.json'),
  30. appSrc: resolveApp('src'),
  31. yarnLockFile: resolveApp('yarn.lock'),
  32. appNodeModules: resolveApp('node_modules'),
  33. publicUrl: getPublicUrl(resolveApp('package.json')),
  34. servedPath: getServedPath(resolveApp('package.json')),
  35. };