import { execSync } from 'child_process' ; import { pathExists } from 'fs-extra' ; import { dirname , resolve } from 'path' ; import getInstallCmd from './getInstallCmd' ; import getInstallDepsCmd from './getInstallDepsCmd' ; import isMonorepo from './isMonorepo' ; import logError from './logError' ; export const SCOPE_NAME_REGEXP = /^@(.+)\// ; export const BLANK_CHARACTER_REGEXP = /((^[^a-zA-Z]+)|[^\w.-])|([^a-zA-Z0-9]+$)/g ; export { getInstallCmd , getInstallDepsCmd , isMonorepo , logError } ; param { string } packageName export function safePackageName ( packageName ) { return packageName . toLowerCase ( ) . replace ( SCOPE_NAME_REGEXP , ( _ , name ) => `${ name }-` ) . replace ( BLANK_CHARACTER_REGEXP , '' ) ; } param { string } packageName export function safeVariableName ( packageName ) { return packageName . toLowerCase ( ) . replace ( SCOPE_NAME_REGEXP , '' ) . replace ( /-(.)/g , ( _ , character ) => character . toUpperCase ( ) ) . replace ( BLANK_CHARACTER_REGEXP , '' ) ; } /**
 * 判断是否已经安装Git
 */ export function isGitInstalled ( ) { try { execSync ( 'git --version' , { stdio : 'ignore' } ) ; return true ; } catch ( e ) { return false ; } } param { string } appPath export function isInGitRepository ( appPath ) { try { execSync ( 'git rev-parse --is-inside-work-tree' , { stdio : 'ignore' , cwd : appPath , } ) ; return true ; } catch ( e ) { return false ; } } param path function getParents ( path ) { let currentPath = path ; let parentPath = dirname ( path ) ; const paths = [ ] ; while ( parentPath !== currentPath ) { paths . push ( parentPath ) ; currentPath = parentPath ; parentPath = dirname ( parentPath ) ; } return paths ; } /**
 * 判断是否在 monorepo 项目中
 */ export async function isInMonorepo ( ) { const isMonoTsLib = async ( path ) : Promise < boolean > => { const isMono = await isMonorepo ( path ) ; const isTsLib = await pathExists ( resolve ( path , 'ts-lib.config.json' ) ) ; return isMono && isTsLib ; } ; const results = await Promise . all ( getParents ( process . cwd ( ) ) . map ( isMonoTsLib ) ) ; return results . some ( Boolean ) ; }