UNPKG

projex

Version:
161 lines (160 loc) 7.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectoryUtils = void 0; const _api_1 = require("../../api/index"); const logger_1 = require("../logger"); const chalk_1 = __importDefault(require("chalk")); const path = require('path'); const fs = require('fs'); const prompts = require('prompts'); class DirectoryUtils { root; list; constructor(list) { this.root = (0, _api_1.getCurrentDirectory)(); this.list = list ? list : false; } /* The `chooseFolders` method is a public method of the `DirectoryUtils` class. It takes in three parameters: `folderList`, which is an array of `IFile` objects representing the list of folders to choose from, `message`, which is a string representing the message to display to the user, and `action`, which is a string representing the action being performed. */ chooseFolders = async (folderList, message, action) => { const questions = [ { type: 'multiselect', name: 'choices', message: message, choices: folderList.map((folder) => { return { title: folder.name, value: folder.name, }; }), }, ]; const response = await prompts(questions); return response; }; /* The `getDirectories` method is a public method of the `DirectoryUtils` class. It takes in a `srcpath` parameter, which is a string representing the path of the directory to search for subdirectories. */ getDirectories = (srcpath) => { return fs .readdirSync(srcpath) .map((file) => path.join(srcpath, file)) .filter((path) => fs.statSync(path).isDirectory()); }; /* The `getFilesInDirectory` method is a public method of the `DirectoryUtils` class. It takes in two parameters: `srcpath`, which is a string representing the path of the directory to search for files, and `extension`, which is a string or null representing the file extension to filter the files by. */ getFilesInDirectory = async (extension) => { let files = []; await fs.readdirSync(this.root).map((file) => { if (extension != null && extension != undefined) { if (this.endsWith(file, extension)) { files.push({ name: file, path: path.join(this.root, file), }); } } else { files.push({ name: file, path: path.join(this.root, file), }); } }); return files; }; /* The `promptSelectElements` method is a public method of the `DirectoryUtils` class. It takes in an array of `IFile` objects (`files`), a message string (default value is "Select the files to upload"), and an action string (default value is "Select the project"). */ promptSelectElements = async (files, message = 'Select the files to upload', action = 'Select the project') => { let selection = await this.chooseFolders(files, message, action); const selectedFolders = selection.choices; const numSelectedFolders = selectedFolders.length; logger_1.log.info(`number of selected folders: ${chalk_1.default.bold.whiteBright(numSelectedFolders)}`); return selectedFolders; }; /** * The function checks if a string ends with a specific suffix. * @param {string} str - The `str` parameter is a string that represents the main string we want to * check if it ends with a specific suffix. * @param {string} suffix - The "suffix" parameter is a string that represents the ending portion of * the "str" string. The function checks if the "str" string ends with the "suffix" string. * @returns a boolean value. It returns true if the given string ends with the specified suffix, and * false otherwise. */ endsWith(str, suffix) { return str.indexOf(suffix, str.length - suffix.length) !== -1; } /* The `getCurrentDirectory` method is a public method that returns the value of the `root` property. The `root` property is set in the constructor of the `DirectoryUtils` class and represents the current directory. So, when the `getCurrentDirectory` method is called, it simply returns the current directory. */ getCurrentDirectory = () => { return this.root; }; /** * The function getCurrentDirectory returns an array containing an object with the name and path of * the current directory. * @returns An array containing an object with the name and path of the current directory. */ getCurrentDirectoryFormatted = () => { const directory = this.getCurrentDirectory(); const currentDirectory = { name: directory.split('/').pop(), path: directory, }; return [currentDirectory]; }; /* The `getFolderDirectories` method is a function that retrieves all the directories within the current directory. It uses the `getDirectories` method to get a list of directories, and then maps over each directory to create an array of `IFile` objects. Each `IFile` object contains the name of the directory (obtained by splitting the directory path and getting the last element) and the full path of the directory. */ getFolderDirectories = async () => { const directories = await this.getDirectories(this.root); const folders = directories.map((directory) => { return { name: directory.split('/').pop(), path: directory, }; }); const selected = await this.promptSelectElements(folders); return selected.map((folder) => { return folders.find((item) => item.name === folder); }); }; // Filter the original array with the string array and get only the element selected getSelectedElements(files, selected) { let toUpload = []; files.map((item) => { selected.map((sItem) => { if (item.name == sItem) { toUpload.push(item); } }); }); return toUpload; } async getFolders() { return this.list ? await this.getFolderDirectories() : this.getCurrentDirectoryFormatted(); } async runCommandInFolders(folders, method) { const folderOperations = folders.map(async (folder) => { if (!folder) { return; } logger_1.log.info(`setting up for: ${_api_1.Colors.PINK(folder.name)}`); await method(folder.path); logger_1.log.info(`setup complete for: ${_api_1.Colors.PINK(folder.name)}`); }); await Promise.all(folderOperations); } } exports.DirectoryUtils = DirectoryUtils;