ecology9-src4js-tools
Version:
src4js-tools
103 lines (91 loc) • 3.79 kB
JavaScript
;
const path = require('path');
const fs = require('fs');
const url = require('url');
// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebookincubator/create-react-app/issues/637
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
const weaverConfig = require('../webpack-config');
const envPublicUrl = process.env.PUBLIC_URL;
function ensureSlash(path, needsSlash) {
const hasSlash = path.endsWith('/');
if (hasSlash && !needsSlash) {
return path.substr(path, path.length - 1);
} else if (!hasSlash && needsSlash) {
return `${path}/`;
} else {
return path;
}
}
const getPublicUrl = appPackageJson =>
envPublicUrl || require(appPackageJson).homepage;
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// Webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
function getServedPath(appPackageJson) {
const publicUrl = getPublicUrl(appPackageJson);
const servedUrl =
envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
return ensureSlash(servedUrl, true);
}
const paths = {
dotenv: resolveApp('.env'),
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
appIndexJs: resolveApp('src/index.js'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
yarnLockFile: resolveApp('yarn.lock'),
testsSetup: resolveApp('src/setupTests.js'),
appNodeModules: resolveApp('node_modules'),
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json')),
};
paths.appIndexJs = resolveApp(weaverConfig.appIndexJs); //resolveApp('../test/src/index.js');
paths.appSrc = resolveApp(weaverConfig.appSrc); //resolveApp('../test');
paths.appPath = resolveApp(weaverConfig.appSrc); //resolveApp('../test');
paths.appHtml = resolveApp(weaverConfig.appHtml);
paths.appBuild = resolveApp(weaverConfig.appBuild);
paths.appWealink = weaverConfig.appWealink ? resolveApp(weaverConfig.appWealink) : weaverConfig.appWealink;
paths.servedPath = weaverConfig.servedPath;
paths.appThemePath = resolveApp(weaverConfig.appThemePath);
paths.skeletonLoadingType = weaverConfig.skeletonLoadingType;
let copyFilesPath = [];
if (Array.isArray(weaverConfig.copyFiles) && weaverConfig.copyFiles.length > 0) {
copyFilesPath = weaverConfig.copyFiles.map(copyFile => ({
from: resolveApp(copyFile.from),
to: resolveApp(copyFile.to),
}));
}
const fse = require('fs-extra');
const chalk = require('chalk');
const argv = require('yargs').argv;
if (process.env.NODE_ENV === 'production') {
if (argv.local) {
fse.emptyDirSync(paths.appWealink);
fse.copySync(resolveApp('public4local'), paths.appWealink, { dereference: true });
}
paths.appPublic = resolveApp('public4build');
if (Array.isArray(copyFilesPath)) {
setTimeout(() => {
// const reg = /[^\/]+\/[^\/]+$/;
copyFilesPath.forEach(copyFile => {
fse.copy(copyFile.from, copyFile.to, err => {
if (err) {
console.log(chalk.red('Failed to copy files.\n'));
} else {
console.log('The ' + chalk.green(copyFile.from));
console.log('has copied to ' + chalk.green(copyFile.to) + '\n');
}
})
});
}, 5000); // Failed to copy files without timeout
}
}
// config after eject: we're in ./config/
module.exports = paths;