UNPKG

@omlet/cli

Version:

Omlet (https://omlet.dev) is a component analytics tool that uses a CLI to scan your codebase to detect components and their usage. Get real usage insights from customizable charts to measure adoption across all projects and identify opportunities to impr

134 lines (133 loc) • 5.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = exports.InvalidProjectSetup = exports.ResolutionConfigIssueLevel = exports.ResolutionConfigIssueType = void 0; const micromatch_1 = __importDefault(require("micromatch")); const upath_1 = __importDefault(require("upath")); const error_1 = require("../error"); const pathResolutionMap_1 = require("./pathResolutionMap"); const projectUtils_1 = require("./projectUtils"); var ResolutionConfigIssueType; (function (ResolutionConfigIssueType) { ResolutionConfigIssueType["TargetNotExist"] = "TARGET_NOT_EXIST"; ResolutionConfigIssueType["TargetNotIncluded"] = "TARGET_NOT_INCLUDED"; })(ResolutionConfigIssueType = exports.ResolutionConfigIssueType || (exports.ResolutionConfigIssueType = {})); var ResolutionConfigIssueLevel; (function (ResolutionConfigIssueLevel) { ResolutionConfigIssueLevel["Error"] = "ERROR"; ResolutionConfigIssueLevel["Warning"] = "WARNING"; })(ResolutionConfigIssueLevel = exports.ResolutionConfigIssueLevel || (exports.ResolutionConfigIssueLevel = {})); class InvalidProjectSetup extends error_1.CliError { constructor(message, data) { var _a; super(message, { context: data }); this.issues = (_a = data.issues) !== null && _a !== void 0 ? _a : []; } get level() { if (this.issues.length > 0 && !this.issues.some(issue => issue.level === ResolutionConfigIssueLevel.Error)) { return ResolutionConfigIssueLevel.Warning; } else { return ResolutionConfigIssueLevel.Error; } } } exports.InvalidProjectSetup = InvalidProjectSetup; class ProjectSetupValidator { constructor(resolver) { this.resolver = resolver; this.projectRoot = resolver.projectRoot; this.config = resolver.userConfig; } findEntrySource(pkgName, entry) { var _a, _b; const pkg = this.resolver.findPackageByName(pkgName); if (!pkg) { throw new InvalidProjectSetup(`Package ${pkgName}' not found in project setup`, { extra: { pkgName, entry } }); } let source; if (entry.type === pathResolutionMap_1.PathResolutionEntryType.Alias) { source = (_a = pkg.aliases.getEntry(entry.name)) === null || _a === void 0 ? void 0 : _a.sourcePath; } else if (entry.type === pathResolutionMap_1.PathResolutionEntryType.Export) { source = (_b = pkg.exportMap.getEntry(entry.name)) === null || _b === void 0 ? void 0 : _b.sourcePath; } if (!source) { throw new InvalidProjectSetup("Entry source not found in the project setup", { extra: { pkgName, entry } }); } return source; } async checkResolutionEntry(pkg, entry) { const { patterns, type } = entry; const globBase = type === pathResolutionMap_1.PathResolutionEntryType.Export ? pkg.path : undefined; const matches = await this.resolver.findGlobMatchesInProject(patterns.flatMap(p => (0, projectUtils_1.starPatternToGlob)(p, true)), globBase); if (!matches) { return; } if (matches.length === 0) { return { packageName: pkg.name, entry: { ...entry, sourcePath: this.findEntrySource(pkg.name, entry), }, type: ResolutionConfigIssueType.TargetNotExist, level: ResolutionConfigIssueLevel.Error, }; } const isMatchIncluded = micromatch_1.default.some(matches, this.config.include, { ignore: this.config.ignore }); if (!isMatchIncluded) { return { packageName: pkg.name, entry: { ...entry, sourcePath: this.findEntrySource(pkg.name, entry), }, type: ResolutionConfigIssueType.TargetNotIncluded, level: ResolutionConfigIssueLevel.Warning, }; } } async validateResolutionEntries(pkg, entries) { const issues = []; if (entries.length === 0) { return issues; } for (const entry of entries) { const issue = await this.checkResolutionEntry(pkg, entry); if (issue) { issues.push(issue); } } return issues; } async validatePackage(pkg) { const issues = []; const exportMapEntries = Object.entries(pkg.exportMap).map(([name, patterns]) => ({ name, patterns, type: pathResolutionMap_1.PathResolutionEntryType.Export })); issues.push(...await this.validateResolutionEntries(pkg, exportMapEntries)); const aliasEntries = Object.entries(pkg.aliases).map(([name, patterns]) => ({ name, patterns, type: pathResolutionMap_1.PathResolutionEntryType.Alias })); issues.push(...await this.validateResolutionEntries(pkg, aliasEntries)); return issues; } async validate(setup) { const isPackageIncluded = (pkg) => { return this.resolver.isGlobIncluded([upath_1.default.join(pkg.path, "**/*")]); }; const issues = []; for (const pkg of [setup.root, ...Object.values(setup.packages)]) { if (await isPackageIncluded(pkg)) { issues.push(...await this.validatePackage(pkg)); } } if (issues.length > 0) { throw new InvalidProjectSetup("Issues found in the project setup", { issues }); } } } function validate(setup, resolver) { const validator = new ProjectSetupValidator(resolver); return validator.validate(setup); } exports.validate = validate;