1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | const path = require('path');
|
12 | const fs = require('fs');
|
13 | const url = require('url');
|
14 | const findMonorepo = require('react-dev-utils-fresh/workspaceUtils')
|
15 | .findMonorepo;
|
16 |
|
17 |
|
18 |
|
19 | const appDirectory = fs.realpathSync(process.cwd());
|
20 | const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
|
21 |
|
22 | const envPublicUrl = process.env.PUBLIC_URL;
|
23 |
|
24 | function ensureSlash(path, needsSlash) {
|
25 | const hasSlash = path.endsWith('/');
|
26 | if (hasSlash && !needsSlash) {
|
27 | return path.substr(path, path.length - 1);
|
28 | } else if (!hasSlash && needsSlash) {
|
29 | return `${path}/`;
|
30 | } else {
|
31 | return path;
|
32 | }
|
33 | }
|
34 |
|
35 | const getPublicUrl = appPackageJson =>
|
36 | envPublicUrl || require(appPackageJson).homepage;
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function getServedPath(appPackageJson) {
|
45 | const publicUrl = getPublicUrl(appPackageJson);
|
46 | const servedUrl =
|
47 | envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
|
48 | return ensureSlash(servedUrl, true);
|
49 | }
|
50 |
|
51 |
|
52 | module.exports = {
|
53 | dotenv: resolveApp('.env'),
|
54 | appPath: resolveApp('.'),
|
55 | appBuild: resolveApp('build'),
|
56 | appPublic: resolveApp('public'),
|
57 | appHtml: resolveApp('public/index.html'),
|
58 | appIndexJs: resolveApp('src/index.js'),
|
59 | appPackageJson: resolveApp('package.json'),
|
60 | appSrc: resolveApp('src'),
|
61 | testsSetup: resolveApp('src/setupTests.js'),
|
62 | appNodeModules: resolveApp('node_modules'),
|
63 | publicUrl: getPublicUrl(resolveApp('package.json')),
|
64 | servedPath: getServedPath(resolveApp('package.json')),
|
65 | };
|
66 |
|
67 | let checkForMonorepo = true;
|
68 |
|
69 |
|
70 | const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
|
71 |
|
72 |
|
73 | module.exports = {
|
74 | dotenv: resolveApp('.env'),
|
75 | appPath: resolveApp('.'),
|
76 | appBuild: resolveApp('build'),
|
77 | appPublic: resolveApp('public'),
|
78 | appHtml: resolveApp('public/index.html'),
|
79 | appIndexJs: resolveApp('src/index.js'),
|
80 | appPackageJson: resolveApp('package.json'),
|
81 | appSrc: resolveApp('src'),
|
82 | testsSetup: resolveApp('src/setupTests.js'),
|
83 | appNodeModules: resolveApp('node_modules'),
|
84 | publicUrl: getPublicUrl(resolveApp('package.json')),
|
85 | servedPath: getServedPath(resolveApp('package.json')),
|
86 |
|
87 | ownPath: resolveOwn('.'),
|
88 | ownNodeModules: resolveOwn('node_modules'),
|
89 | };
|
90 |
|
91 |
|
92 | const useTemplate =
|
93 | appDirectory === fs.realpathSync(path.join(__dirname, '..'));
|
94 |
|
95 | checkForMonorepo = !useTemplate;
|
96 |
|
97 | if (useTemplate) {
|
98 | module.exports = {
|
99 | dotenv: resolveOwn('template/.env'),
|
100 | appPath: resolveApp('.'),
|
101 | appBuild: resolveOwn('../../build'),
|
102 | appPublic: resolveOwn('template/public'),
|
103 | appHtml: resolveOwn('template/public/index.html'),
|
104 | appIndexJs: resolveOwn('template/src/index.js'),
|
105 | appPackageJson: resolveOwn('package.json'),
|
106 | appSrc: resolveOwn('template/src'),
|
107 | testsSetup: resolveOwn('template/src/setupTests.js'),
|
108 | appNodeModules: resolveOwn('node_modules'),
|
109 | publicUrl: getPublicUrl(resolveOwn('package.json')),
|
110 | servedPath: getServedPath(resolveOwn('package.json')),
|
111 |
|
112 | ownPath: resolveOwn('.'),
|
113 | ownNodeModules: resolveOwn('node_modules'),
|
114 | };
|
115 | }
|
116 |
|
117 |
|
118 | module.exports.srcPaths = [module.exports.appSrc];
|
119 |
|
120 | module.exports.useYarn = fs.existsSync(
|
121 | path.join(module.exports.appPath, 'yarn.lock')
|
122 | );
|
123 |
|
124 | if (checkForMonorepo) {
|
125 |
|
126 |
|
127 | const mono = findMonorepo(appDirectory);
|
128 | if (mono.isAppIncluded) {
|
129 | Array.prototype.push.apply(module.exports.srcPaths, mono.pkgs);
|
130 | }
|
131 | module.exports.useYarn = module.exports.useYarn || mono.isYarnWs;
|
132 | }
|