import fs from 'fs-extra'; import path from 'path'; // Make sure any symlinks in the project folder are resolved: // https://github.com/facebook/create-react-app/issues/637 /** * 本项目的目录 */ export const projDirectory: string = fs.realpathSync('.'); export const appDirectory: string = fs.realpathSync(process.cwd()); export const resolveProject = (relativePath: string) => path.resolve(projDirectory, relativePath); export const envPublicUrl = process.env.PUBLIC_URL; export const getPublicUrl = (appPackageJson: string) => { console.log(appPackageJson) return envPublicUrl || require(appPackageJson).homepage; } export const moduleFileExtensions = [ 'web.mjs', 'mjs', 'web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', ]; /** * Resolve file paths in the same order as webpack * @param resolveFn * @param filePath */ export const resolveModule = (resolveFn: typeof resolveProject, filePath: string) => { const extension = moduleFileExtensions.find(extension => fs.existsSync(resolveFn(`${filePath}.${extension}`)) ); if (extension) { return resolveFn(`${filePath}.${extension}`); } return resolveFn(`${filePath}.js`); }; export const distKeyName = { jsDir: 'js', cssDir: 'css', publicDir: 'static', distDir: 'dist', entryHtmlName: 'index.html', entryJsName: 'index.js' } export const paths = { dotenv: resolveProject('.env'), appPath: resolveProject('.'), appDist: resolveProject('dist'), appBuild: resolveProject('build'), appDepregistry: resolveProject('dist/registry.json'), appPublic: resolveProject('public'), appLib: resolveProject('lib'), appHtml: resolveProject('public/index.html'), appIndexJs: resolveModule(resolveProject, 'src/index'), appPackageJson: resolveProject('package.json'), appSrc: resolveProject('src'), appTypeGlobal: resolveProject('src/global/index.ts'), appTsConfig: resolveProject('tsconfig.json'), appTsConfigDev: resolveProject('tsconfig.dev.json'), yarnLockFile: resolveProject('yarn.lock'), testsSetup: resolveModule(resolveProject, 'src/setupTests'), proxySetup: resolveProject('src/setupProxy.js'), appNodeModules: resolveProject('node_modules'), publicUrl: getPublicUrl(resolveProject('package.json')), }; export type BasePaths = typeof paths export type BasePathKeys = keyof BasePaths export const pathSearcher = (dir: BasePaths[K], suffix: string = "*") => { return path.join(dir, `**/*.${suffix}`) }; export function preventPath(path: string) { if (!fs.existsSync(path)) { fs.ensureDirSync(path) } return path } export default { ...paths, appDistMainHTML: path.join(paths.appDist, distKeyName.entryHtmlName), appDistCssDir: path.join(paths.appDist, distKeyName.cssDir), appDistScriptsDir: path.join(paths.appDist, distKeyName.jsDir), appDistPublicDir: path.join(paths.appDist, distKeyName.publicDir), appJsMatcher: pathSearcher(paths.appLib, 'js'), appCssMatcher: pathSearcher(paths.appSrc, 'css'), appSvgMatcher: pathSearcher(paths.appSrc, 'svg'), appEntryJs: path.join(paths.appLib, distKeyName.entryJsName), appBrowserifyCache: path.join(paths.appDist, 'browserify-cache.json'), };