UNPKG

@ts-dev-tools/core

Version:
53 lines (52 loc) 2.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginService = void 0; const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const constants_1 = require("../constants"); const CorePackageService_1 = require("./CorePackageService"); const PackageJson_1 = require("./PackageJson"); class PluginService { constructor() { } static packageNameIsPlugin(packageName) { return packageName.match(new RegExp(`^${constants_1.PACKAGE_BASE_NAME}/.*$`)); } static getInstalledPlugins(absoluteProjectDir) { const packageJson = PackageJson_1.PackageJson.fromDirPath(absoluteProjectDir); const allDependenciesPackageNames = packageJson.getAllDependenciesPackageNames(); if (!allDependenciesPackageNames.length) { return []; } const pluginsFullname = allDependenciesPackageNames.filter((packageName) => PluginService.packageNameIsPlugin(packageName)); const sortPlugins = (pluginA, pluginB) => pluginA.localeCompare(pluginB); pluginsFullname.sort(sortPlugins); const plugins = new Map(); for (const pluginFullname of pluginsFullname) { const plugin = PluginService.getPluginFromFullname(pluginFullname); plugins.set(pluginFullname, plugin); const pluginParents = PluginService.getInstalledPlugins(plugin.path); for (const pluginParent of pluginParents) { plugins.set(pluginParent.fullname, pluginParent); } } return Array.from(plugins.values()); } static getPluginShortname(fullname) { const shortname = fullname.replace(`${constants_1.PACKAGE_BASE_NAME}/`, ""); return shortname; } static getPluginFromFullname(fullname) { const corePackageRootPath = CorePackageService_1.CorePackageService.getPackageRootPath(); const shortname = PluginService.getPluginShortname(fullname); const path = (0, node_path_1.resolve)(corePackageRootPath, "../", shortname); if (!(0, node_fs_1.existsSync)(path)) { throw new Error(`Plugin "${fullname}" is required but cannot be found in ${path}.`); } return { fullname, shortname, path, }; } } exports.PluginService = PluginService;