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

80 lines (79 loc) 4.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readNxProject = exports.readNxConfig = exports.findNxWorkspacePaths = void 0; const fast_glob_1 = __importDefault(require("fast-glob")); const fs_1 = require("fs"); const json5_1 = __importDefault(require("json5")); const upath_1 = __importDefault(require("upath")); const fileUtils_1 = require("../../fileUtils"); const pathResolutionMap_1 = require("../pathResolutionMap"); // Nx finds project configurations using plugins: https://github.com/nrwl/nx/blob/master/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts#L160-L163 // the first plugin figures out workspaces from package.json: https://github.com/nrwl/nx/blob/master/packages/nx/src/plugins/package-json-workspaces/create-nodes.ts#L19 // the second plugin figures out workspaces from project.json: https://github.com/nrwl/nx/blob/master/packages/nx/src/plugins/project-json/build-nodes/project-json.ts#L8 async function findNxWorkspacePaths(cwd) { const paths = await (0, fast_glob_1.default)("**/*/{package,project}.json", { cwd, ignore: ["**/node_modules/**"] }); return [...new Set(paths.map(path => upath_1.default.dirname(path)))]; } exports.findNxWorkspacePaths = findNxWorkspacePaths; async function readNxConfig(repoPath) { const nxJsonConfigFile = await (0, fileUtils_1.readIfExists)(upath_1.default.join(repoPath, "nx.json")); if (nxJsonConfigFile) { return json5_1.default.parse(nxJsonConfigFile); } } exports.readNxConfig = readNxConfig; function getScope(rootPackageName) { if (!rootPackageName) { return ""; } if (rootPackageName.startsWith("@")) { return `${rootPackageName.split("/")[0]}/`; } return ""; } function getProjectName(workspacePath, workspaceProjectName) { if (workspaceProjectName) { return workspaceProjectName; } const parts = workspacePath.split("/"); return parts[parts.length - 1]; } // From: https://github.com/nrwl/nx/blob/master/packages/workspace/src/utilities/get-import-path.ts#L4 function getPackageName(workspacePath, rootPackageJson, workspacePackageJson, workspaceProjectJson) { if (workspacePackageJson === null || workspacePackageJson === void 0 ? void 0 : workspacePackageJson.name) { return workspacePackageJson.name; } const scope = getScope(rootPackageJson.name); const projectName = getProjectName(workspacePath, workspaceProjectJson === null || workspaceProjectJson === void 0 ? void 0 : workspaceProjectJson.name); return `${scope}${projectName}`; } function getPackageVersion(rootPackageJson, workspacePackageJson) { var _a, _b; return (_b = (_a = workspacePackageJson === null || workspacePackageJson === void 0 ? void 0 : workspacePackageJson.version) !== null && _a !== void 0 ? _a : rootPackageJson.version) !== null && _b !== void 0 ? _b : "0.0.0"; } async function readNxProject(rootPath, workspacePath, tsconfig) { const rootPackageJsonPath = upath_1.default.join(rootPath, "package.json"); const rootPackageJsonContent = await fs_1.promises.readFile(rootPackageJsonPath, "utf8"); const rootPackageJson = json5_1.default.parse(rootPackageJsonContent); const workspacePackageJsonPath = upath_1.default.join(workspacePath, "package.json"); const workspacePackageJsonContent = await (0, fileUtils_1.readIfExists)(workspacePackageJsonPath); const workspacePackageJson = workspacePackageJsonContent ? json5_1.default.parse(workspacePackageJsonContent) : undefined; const workspaceProjectJsonPath = upath_1.default.join(workspacePath, "project.json"); const workspaceProjectJsonContent = await (0, fileUtils_1.readIfExists)(workspaceProjectJsonPath); const workspaceProjectJson = workspaceProjectJsonContent ? json5_1.default.parse(workspaceProjectJsonContent) : undefined; return { name: getPackageName(workspacePath, rootPackageJson, workspacePackageJson, workspaceProjectJson), version: getPackageVersion(rootPackageJson, workspacePackageJson), path: workspacePath, info: workspacePackageJson !== null && workspacePackageJson !== void 0 ? workspacePackageJson : rootPackageJson, tsconfig, aliases: new pathResolutionMap_1.PathResolutionMap(pathResolutionMap_1.PathResolutionEntryType.Alias), importMap: new pathResolutionMap_1.PathResolutionMap(pathResolutionMap_1.PathResolutionEntryType.Import), exportMap: new pathResolutionMap_1.PathResolutionMap(pathResolutionMap_1.PathResolutionEntryType.Export), dependencies: new Set(), }; } exports.readNxProject = readNxProject;