UNPKG

@sap/adp-common

Version:
207 lines 8.38 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppFoldersUtils = void 0; const fs = __importStar(require("fs")); const path_1 = require("path"); const findUp = require("find-up"); const findit = require("findit2"); const Types_1 = require("../models/Types"); const PACKAGE_JSON = "package.json"; const WEB_APP_DIRECTORY = "webapp"; class AppFoldersUtils { static isWorkspaceFolder(value) { return value && value.length > 0 && value[0].uri !== undefined; } static findAll(root, filename, stopFolders, files) { return new Promise((doResolve, reject) => { const finder = findit(root); finder.on("directory", (dir, stat, stop) => { const base = (0, path_1.basename)(dir); if (stopFolders.indexOf(base) !== -1) { stop(); } }); finder.on("file", (file) => { if (file.endsWith(filename)) { files.push((0, path_1.dirname)(file)); } }); finder.on("end", () => { doResolve(); }); finder.on("error", (error) => { reject(error); }); }); } static findProject(wsRoot, files) { return __awaiter(this, void 0, void 0, function* () { yield this.findAll(wsRoot, PACKAGE_JSON, [".git", "node_modules", "dist", "generator-adaptationproject-creation", WEB_APP_DIRECTORY], files); }); } static findAllPackageJsonFolders(wsFolders) { return __awaiter(this, void 0, void 0, function* () { let wsRoots; if (this.isWorkspaceFolder(wsFolders)) { wsRoots = []; wsFolders .filter((each) => each.uri.scheme === "file") .forEach((folder) => { wsRoots.push(folder.uri.fsPath); }); } else { wsRoots = wsFolders || []; } const projects = []; for (const root of wsRoots) { yield this.findProject(root, projects); } return projects; }); } static findAllManifest(wsFolders) { return __awaiter(this, void 0, void 0, function* () { let wsRoots; if (this.isWorkspaceFolder(wsFolders)) { wsRoots = []; wsFolders .filter((each) => each.uri.scheme === "file") .forEach((folder) => { wsRoots.push(folder.uri.fsPath); }); } else { wsRoots = []; } const manifests = []; for (const root of wsRoots) { yield this.findAll(root, "manifest.appdescr_variant", [".git", "node_modules", "dist"], manifests); } return manifests; }); } static findProjectRoot(path) { return __awaiter(this, void 0, void 0, function* () { const pckgJson = yield findUp(PACKAGE_JSON, { cwd: path }); if (!pckgJson) { throw new Error(`Could not find any project root.`); } const root = (0, path_1.dirname)(pckgJson); return root; }); } static findRootsForPath(path) { return __awaiter(this, void 0, void 0, function* () { try { const appRoot = yield this.findProjectRoot(path); return { projectRoot: appRoot }; } catch (error) { return undefined; } }); } static findAllApps(wsFolders) { return __awaiter(this, void 0, void 0, function* () { const result = []; const manifestPaths = yield this.findAllManifest(wsFolders); for (const manifestPath of manifestPaths) { const roots = yield this.findRootsForPath(manifestPath); if (roots) { result.push({ projectRoot: roots.projectRoot, manifestPath }); } } return result; }); } static findAllSubFolders(folder) { return __awaiter(this, void 0, void 0, function* () { let subFolders = []; const items = yield fs.promises.readdir(folder); for (const item of items) { const itemPath = (0, path_1.join)(folder, item); if ((yield (yield fs.promises.stat(itemPath)).isDirectory()) && !itemPath.includes("node_modules")) { subFolders.push(itemPath); subFolders = subFolders.concat(yield this.findAllSubFolders(itemPath)); } } return subFolders; }); } static getAppFolders(app) { return __awaiter(this, void 0, void 0, function* () { const appFolders = { projectRoots: new Set(), subWebappRoots: new Set(), manifestAppDescRoots: new Set() }; appFolders.projectRoots.add(app.projectRoot); appFolders.manifestAppDescRoots.add((0, path_1.join)(app.manifestPath, "manifest.appdescr_variant")); const subWebappRoots = yield this.findAllSubFolders(app.projectRoot); subWebappRoots.forEach((sub) => appFolders.subWebappRoots.add(sub)); return appFolders; }); } static mergeAppFolders(source, target) { for (const folderKey of Types_1.folderKeys) { Array.from(source[folderKey]).forEach((folder) => target[folderKey].add(folder)); } } static findAllProjectRoots(wsFolders) { return __awaiter(this, void 0, void 0, function* () { const projects = yield this.findAllPackageJsonFolders(wsFolders); return projects; }); } static getProjectRootFromPath(roots, path) { for (const root of roots) { if (root === path || (path.startsWith(root) && path.replace(root, "").startsWith(path_1.sep))) { return root; } } return undefined; } static getProjectPathByFile(filePath, workspaceRoots) { return __awaiter(this, void 0, void 0, function* () { const watchedPaths = yield this.findAllProjectRoots(workspaceRoots); return this.getProjectRootFromPath(watchedPaths, filePath); }); } } exports.AppFoldersUtils = AppFoldersUtils; //# sourceMappingURL=AppFolderUtils.js.map