Completed
Push — master ( c12867...9cb664 )
by Junior
28s
created

paths.js ➔ ensureSlash   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
c 1
b 0
f 1
nc 3
dl 0
loc 10
rs 8.8571
nop 2
1
'use strict';
2
3
const path = require('path');
4
const fs = require('fs');
5
const url = require('url');
6
7
// Make sure any symlinks in the project folder are resolved:
8
// https://github.com/facebookincubator/create-react-app/issues/637
9
const appDirectory = fs.realpathSync(process.cwd());
10
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
11
12
const envPublicUrl = process.env.PUBLIC_URL;
13
14
function ensureSlash(path, needsSlash) {
15
  const hasSlash = path.endsWith('/');
16
  if (hasSlash && !needsSlash) {
17
    return path.substr(path, path.length - 1);
18
  } else if (!hasSlash && needsSlash) {
19
    return `${path}/`;
20
  } else {
21
    return path;
22
  }
23
}
24
25
const getPublicUrl = appPackageJson =>
26
  envPublicUrl || require(appPackageJson).homepage;
27
28
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
29
// "public path" at which the app is served.
30
// Webpack needs to know it to put the right <script> hrefs into HTML even in
31
// single-page apps that may serve index.html for nested URLs like /todos/42.
32
// We can't use a relative path in HTML because we don't want to load something
33
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
34
function getServedPath(appPackageJson) {
35
  const publicUrl = getPublicUrl(appPackageJson);
36
  const servedUrl =
37
    envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
38
  return ensureSlash(servedUrl, true);
39
}
40
41
// config after eject: we're in ./config/
42
module.exports = {
43
  dotenv: resolveApp('.env'),
44
  appBuild: resolveApp('build'),
45
  appPublic: resolveApp('public'),
46
  appHtml: resolveApp('public/index.html'),
47
  appIndexJs: resolveApp('src/index.js'),
48
  appPackageJson: resolveApp('package.json'),
49
  appSrc: resolveApp('src'),
50
  yarnLockFile: resolveApp('yarn.lock'),
51
  testsSetup: resolveApp('src/setupTests.js'),
52
  appNodeModules: resolveApp('node_modules'),
53
  publicUrl: getPublicUrl(resolveApp('package.json')),
54
  servedPath: getServedPath(resolveApp('package.json')),
55
};
56