@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
85 lines (84 loc) • 3.82 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = resolveWorkspaces;
const node_fs_1 = __importDefault(require("node:fs"));
const promises_1 = __importDefault(require("node:fs/promises"));
const node_path_1 = __importDefault(require("node:path"));
const glob_1 = require("glob");
const js_yaml_1 = __importDefault(require("js-yaml"));
const utils_1 = require("./utils");
/**
* Search for all directories containing alova.config configuration files under the monorepo project. It will search for configuration files based on `workspaces` in `package.json` or sub packages defined in `pnpm-workspace.yaml`
* @param projectPath The project path to search, defaults to `process.cwd()`.
* @returns An array of relative paths to directories containing alova.config configuration files.
*/
async function resolveWorkspaces(projectPath = process.cwd()) {
const resultDirs = [];
// Check if there is alova.config.js in the root directory
const rootConfigPath = await (0, utils_1.resolveConfigFile)(projectPath);
if (rootConfigPath) {
resultDirs.push(projectPath);
}
// Find sub packages based on workspaces in package.json or pnpm-workspace.yaml
const packageJsonPath = node_path_1.default.join(projectPath, 'package.json');
const pnpmWorkspacePathYaml = node_path_1.default.join(projectPath, 'pnpm-workspace.yaml');
const pnpmWorkspacePathYml = node_path_1.default.join(projectPath, 'pnpm-workspace.yml');
let workspaces = [];
// If package.json exists, read workspaces
if (await (0, utils_1.existsPromise)(packageJsonPath)) {
const packageJson = JSON.parse(await promises_1.default.readFile(packageJsonPath, 'utf-8'));
if (packageJson.workspaces) {
workspaces = Array.isArray(packageJson.workspaces) ? packageJson.workspaces : packageJson.workspaces.packages;
}
}
// If pnpm-workspace.yaml exists, read the path in it
const pnpmWorkspacePath = (await (0, utils_1.existsPromise)(pnpmWorkspacePathYaml))
? pnpmWorkspacePathYaml
: (await (0, utils_1.existsPromise)(pnpmWorkspacePathYml))
? pnpmWorkspacePathYml
: undefined;
if (pnpmWorkspacePath) {
const pnpmConfig = js_yaml_1.default.load(await promises_1.default.readFile(pnpmWorkspacePath, 'utf-8'));
if (pnpmConfig.packages) {
workspaces = workspaces.concat(pnpmConfig.packages);
}
}
// Deduplication
workspaces = [...new Set(workspaces)];
// Iterate through each sub-package and check if alova.config.js exists
for (const workspace of workspaces) {
const workspaceDirs = await globPaths(projectPath, workspace);
for (const dir of workspaceDirs) {
const configFile = await (0, utils_1.resolveConfigFile)(dir);
if (configFile) {
resultDirs.push(dir);
}
}
}
return resultDirs;
}
/**
* Parse workspace path
* @param {string} rootPath root directory
* @param {string} workspacePattern Path pattern defined by workspace
* @returns {Promise<string[]>} Parsed path list
*/
async function globPaths(rootPath, workspacePattern) {
const resolvedPaths = [];
const dirs = await (0, glob_1.glob)(workspacePattern, {
ignore: 'node_modules/**',
cwd: rootPath,
fs: {
...node_fs_1.default,
promises: promises_1.default,
},
}); // Using the glob module to handle wildcards
for (const dir of dirs) {
const absDir = node_path_1.default.resolve(rootPath, dir);
resolvedPaths.push(absDir);
}
return resolvedPaths;
}