UNPKG

@modern-js/utils

Version:

A Progressive React Framework for modern web development.

49 lines (48 loc) 1.92 kB
import os from "os"; import path from "path"; import { fs, json5 } from "../../compiled.mjs"; import { isDepExists } from "../is/index.mjs"; import { canUsePnpm, canUseYarn } from "../package.mjs"; const MAX_TIMES = 5; async function getPackageManager(cwd = process.cwd()) { let appDirectory = cwd; let times = 0; while(os.homedir() !== appDirectory && times < MAX_TIMES){ times++; if (fs.existsSync(path.resolve(appDirectory, 'pnpm-lock.yaml'))) return 'pnpm'; if (fs.existsSync(path.resolve(appDirectory, 'yarn.lock'))) return 'yarn'; if (fs.existsSync(path.resolve(appDirectory, 'package-lock.json'))) return 'npm'; appDirectory = path.join(appDirectory, '..'); } if (await canUsePnpm()) return 'pnpm'; if (await canUseYarn()) return 'yarn'; return 'npm'; } const getCoreJsVersion = (corejsPkgPath)=>{ try { const { version } = fs.readJSONSync(corejsPkgPath); const [major, minor] = version.split('.'); return `${major}.${minor}`; } catch (err) { return '3'; } }; function getInternalPlugins(appDirectory, internalPlugins = {}) { return [ ...Object.keys(internalPlugins).filter((name)=>{ const config = internalPlugins[name]; if ('string' != typeof config && true === config.forced) return true; return isDepExists(appDirectory, name); }).map((name)=>{ const config = internalPlugins[name]; if ('string' != typeof config) return config.path; return config; }) ]; } const readTsConfig = (root)=>readTsConfigByFile(path.resolve(root, './tsconfig.json')); const readTsConfigByFile = (filename)=>{ const content = fs.readFileSync(path.resolve(filename), 'utf-8'); return json5.parse(content); }; export { getCoreJsVersion, getInternalPlugins, getPackageManager, readTsConfig, readTsConfigByFile };