UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

81 lines 3.36 kB
import { lt, valid, validRange } from 'semver'; import { JsonRule } from '../../JsonRule.js'; export class DependencyRule extends JsonRule { constructor(packageName, packageVersion, isDevDep = false, isOptional = false, add = true) { super(); this.packageName = packageName; this.packageVersion = packageVersion; this.isDevDep = isDevDep; this.isOptional = isOptional; this.add = add; } get title() { return this.packageName; } get description() { return `${(this.add ? 'Upgrade' : 'Remove')} SharePoint Framework ${(this.isDevDep ? 'dev ' : '')}dependency package ${this.packageName}`; } get resolution() { return this.add ? `${(this.isDevDep ? 'installDev' : 'install')} ${this.packageName}@${this.packageVersion}` : `${(this.isDevDep ? 'uninstallDev' : 'uninstall')} ${this.packageName}`; } get resolutionType() { return 'cmd'; } get severity() { return 'Required'; } get file() { return './package.json'; } // eslint-disable-next-line @typescript-eslint/no-unused-vars customCondition(project) { return false; } visit(project, findings) { if (!project.packageJson) { return; } const projectDependencies = this.isDevDep ? project.packageJson.devDependencies : project.packageJson.dependencies; const versionEntry = projectDependencies ? projectDependencies[this.packageName] : ''; const packageVersion = valid(versionEntry); const versionRange = validRange(versionEntry); if (this.add) { let jsonProperty = this.isDevDep ? 'devDependencies' : 'dependencies'; if (versionEntry) { jsonProperty += `.${this.packageName}`; if (packageVersion) { if (lt(packageVersion, this.packageVersion)) { const node = this.getAstNodeFromFile(project.packageJson, jsonProperty); this.addFindingWithPosition(findings, node); } } else { if (versionRange) { const node = this.getAstNodeFromFile(project.packageJson, jsonProperty); this.addFindingWithPosition(findings, node); } } } else { if (!this.isOptional || this.customCondition(project)) { const node = this.getAstNodeFromFile(project.packageJson, jsonProperty); this.addFindingWithCustomInfo(this.packageName, this.description.replace('Upgrade', 'Install'), [{ file: this.file, resolution: this.resolution, position: this.getPositionFromNode(node) }], findings); } } } else { const jsonProperty = `${(this.isDevDep ? 'devDependencies' : 'dependencies')}.${this.packageName}`; if (versionEntry) { const node = this.getAstNodeFromFile(project.packageJson, jsonProperty); this.addFindingWithPosition(findings, node); } } } } //# sourceMappingURL=DependencyRule.js.map