-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: doctor command add check redundant and required denpendencies
- Loading branch information
Showing
8 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import chalk from 'chalk'; | ||
|
||
import { checkRequiredContentInstalled } from '@helpers/check'; | ||
import { Logger, type PrefixLogType } from '@helpers/logger'; | ||
import { getPackageInfo } from '@helpers/package'; | ||
import { resolver } from 'src/constants/path'; | ||
|
||
interface DoctorActionOptions { | ||
packagePath?: string; | ||
} | ||
|
||
interface ProblemRecord { | ||
name: string; | ||
level: Extract<PrefixLogType, 'error' | 'warn'>; | ||
outputFn: () => void; | ||
} | ||
|
||
export async function doctorAction(options: DoctorActionOptions) { | ||
const { packagePath = resolver('package.json') } = options; | ||
|
||
const { allDependenciesKeys, currentComponents, isAllComponents } = | ||
await getPackageInfo(packagePath); | ||
|
||
/** ======================== Problem record ======================== */ | ||
const problemRecord: ProblemRecord[] = []; | ||
|
||
/** ======================== Check whether installed redundant dependencies ======================== */ | ||
if (isAllComponents && currentComponents.length) { | ||
problemRecord.push({ | ||
level: 'warn', | ||
name: 'redundantDependencies', | ||
outputFn: () => { | ||
Logger.warn('you have installed redundant dependencies, please remove them'); | ||
Logger.newLine(); | ||
Logger.info('The redundant dependencies are:'); | ||
Logger.newLine(); | ||
currentComponents.forEach((component) => { | ||
Logger.info(`- ${component.package}`); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
/** ======================== Check if the allComponents required dependencies installed ======================== */ | ||
if (isAllComponents) { | ||
// Check if framer-motion allComponents is installed | ||
const [isCorrectInstalled, ...missingDependencies] = checkRequiredContentInstalled( | ||
'all', | ||
allDependenciesKeys | ||
); | ||
|
||
if (!isCorrectInstalled) { | ||
problemRecord.push({ | ||
level: 'error', | ||
name: 'missingDependencies', | ||
outputFn: () => { | ||
Logger.error('you have not installed the required dependencies'); | ||
Logger.newLine(); | ||
Logger.info('The required dependencies are:'); | ||
Logger.newLine(); | ||
missingDependencies.forEach((dependency) => { | ||
Logger.info(`- ${dependency}`); | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** ======================== Return when there is no problem ======================== */ | ||
if (!problemRecord.length) { | ||
Logger.success('🌟Congratulation no problem found in your project'); | ||
|
||
return; | ||
} | ||
|
||
/** ======================== Output the problem record ======================== */ | ||
Logger.prefix( | ||
'error', | ||
`❌Sorry there are ${chalk.underline(problemRecord.length)} problem${ | ||
problemRecord.length === 1 ? '' : 's' | ||
} in your project` | ||
); | ||
Logger.newLine(); | ||
|
||
for (let index = 0; index < problemRecord.length; index++) { | ||
const problem = problemRecord[index] as ProblemRecord; | ||
|
||
Logger[problem.level](`❗️Problem ${index + 1}: ${chalk.bold(problem.name)}`); | ||
Logger.newLine(); | ||
problem.outputFn(); | ||
Logger.newLine(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const ALL_COMPONENTS = '@nextui-org/react'; | ||
export const FRAMER_MOTION = 'framer-motion'; | ||
export const ALL_COMPONENTS_REQUIRED = [ALL_COMPONENTS, FRAMER_MOTION] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { SAFE_ANY } from './type'; | ||
|
||
import { ALL_COMPONENTS, FRAMER_MOTION } from 'src/constants/required'; | ||
|
||
export type CheckType = 'all' | 'partial'; | ||
|
||
type CheckResult<T extends SAFE_ANY[] = SAFE_ANY[]> = [boolean, ...T]; | ||
/** | ||
* Check if the required content is installed | ||
* @example return result and missing required [false, '@nextui-org/react', 'framer-motion'] | ||
* @param type | ||
* @param dependenciesKeys | ||
* @returns | ||
*/ | ||
export function checkRequiredContentInstalled( | ||
type: CheckType, | ||
dependenciesKeys: Set<string> | ||
): CheckResult { | ||
if (type === 'all') { | ||
const hasAllComponents = dependenciesKeys.has(ALL_COMPONENTS); | ||
const hasFramerMotion = dependenciesKeys.has(FRAMER_MOTION); | ||
const result = [] as unknown as CheckResult; | ||
|
||
if (hasAllComponents && hasFramerMotion) { | ||
return [true]; | ||
} | ||
!hasAllComponents && result.push(ALL_COMPONENTS); | ||
!hasFramerMotion && result.push(FRAMER_MOTION); | ||
|
||
return [false, ...result]; | ||
} | ||
|
||
return [false]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters