UNPKG

@sfdx-falcon/builder-library

Version:

Collection of functions for building pre-defined SFDX-Falcon Tasks, Questions, and Task Bundles. Allows developers to quickly build common Task and Interview-driven workflows in their CLI plugins. Part of the SFDX-Falcon Library.

188 lines 13.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); //─────────────────────────────────────────────────────────────────────────────────────────────────┐ /** * @author Vivek M. Chawla <@VivekMChawla> * @copyright 2019, Vivek M. Chawla / Salesforce. All rights reserved. * @license BSD-3-Clause For full license text, see the LICENSE file in the repo root or * `https://opensource.org/licenses/BSD-3-Clause` * @file packages/builder-library/src/questions/filesystem.ts * @summary Exports `Builder` classes for filesystem related questions. * @description Exports `Builder` classes for filesystem related questions. */ //─────────────────────────────────────────────────────────────────────────────────────────────────┘ // Import External Libraries, Modules, and Types const fse = require("fs-extra"); // Module that adds a few extra file system methods that aren't included in the native fs module. It is a drop in replacement for fs. const path = require("path"); // Node's built-in path library. // Import SFDX-Falcon Libraries const validator_1 = require("@sfdx-falcon/validator"); // Library of Type Validation helper functions. const validator_2 = require("@sfdx-falcon/validator"); // Library of Yeoman-related Validation helper functions. // Import SFDX-Falcon Classes & Functions const builder_1 = require("@sfdx-falcon/builder"); // Class. Classes derived from QuestionsBuilder can be used to build an Inquirer Questions object. const debug_1 = require("@sfdx-falcon/debug"); // Class. Provides custom "debugging" services (ie. debug-style info to console.log()). const prompt_1 = require("@sfdx-falcon/prompt"); // Class. Allows easy creation of Inquirer prompts that have a "confirmation" question that can be used to restart collection of the information. // Set the File Local Debug Namespace const dbgNs = '@sfdx-falcon:builder-library:questions'; debug_1.SfdxFalconDebug.msg(`${dbgNs}:`, `Debugging initialized for ${dbgNs}(filesystem)`); //─────────────────────────────────────────────────────────────────────────────────────────────────┐ /** * @class ProvideBaseDirectory * @extends InterviewQuestionsBuilder * @summary Interview Questions Builder for providing a "base directory" (ie. a directory that * is expected to contain a specific file or directory). * @description Interview Questions Builder for providing a "base directory" (ie. a directory that * is expected to contain a specific file or directory). */ //─────────────────────────────────────────────────────────────────────────────────────────────────┘ class ProvideBaseDirectory extends builder_1.InterviewQuestionsBuilder { //───────────────────────────────────────────────────────────────────────────┐ /** * @constructs ProvideBaseDirectory * @param {ProvideBaseDirectoryOptions} opts Required. */ //───────────────────────────────────────────────────────────────────────────┘ constructor(opts) { // Call the superclass constructor. super(opts); // Initialize debug for this method. const dbgNS = this.initializeDebug(dbgNs, `constructor`, arguments); // Validate required options. validator_1.TypeValidator.throwOnEmptyNullInvalidString(opts.fileOrDirName, `${dbgNS.ext}`, `fileOrDirName`); // Validate optional options. if (opts.msgStrings.promptProvidePath) validator_1.TypeValidator.throwOnEmptyNullInvalidString(opts.msgStrings.promptProvidePath, `${dbgNS.ext}`, `msgStrings.promptProvidePath`); if (opts.msgStrings.errorNotFound) validator_1.TypeValidator.throwOnEmptyNullInvalidString(opts.msgStrings.errorNotFound, `${dbgNS.ext}`, `msgStrings.errorNotFound`); if (opts.validateFunction) validator_1.TypeValidator.throwOnInvalidFunction(opts.validateFunction, `${dbgNS.ext}`, `validateFunction`); // Initialize member variables. this.fileOrDirName = opts.fileOrDirName; this.validateFunction = opts.validateFunction || null; this.promptProvidePath = opts.msgStrings.promptProvidePath || `Path to the directory containing ${this.fileOrDirName}?`; this.errorNotFound = opts.msgStrings.errorNotFound || `Specified directory does not contain '${this.fileOrDirName}'`; } //───────────────────────────────────────────────────────────────────────────┐ /** * @method build * @returns {Questions} * @description Builds the Interview Questions. */ //───────────────────────────────────────────────────────────────────────────┘ build(buildCtx) { // Initialize debug for this method. const dbgNS = this.initializeDebug(dbgNs, `build`, arguments); // Validate the Build Context. validator_1.TypeValidator.throwOnInvalidInstance(buildCtx, prompt_1.SfdxFalconPrompt, `${dbgNS.ext}`, `BuildContext`); validator_1.TypeValidator.throwOnNullInvalidString(buildCtx.defaultAnswers.baseDirectory, `${dbgNS.ext}`, `BuildContext.defaultAnswers.baseDirectory`); // Build and return the Questions. return [ { type: 'input', name: 'baseDirectory', message: this.promptProvidePath, default: (typeof buildCtx.userAnswers.baseDirectory !== 'undefined') ? buildCtx.userAnswers.baseDirectory // Current Value : buildCtx.defaultAnswers.baseDirectory, filter: filterLocalPath, when: true, validate: async (userInput) => { // Check if a path to the specified file or directory exists when combined with the user's input. const completePath = path.join(userInput, this.fileOrDirName); const pathExists = await fse.pathExists(completePath) .catch(pathExistsError => { debug_1.SfdxFalconDebug.obj(`${dbgNS.ext}:baseDirectory:validate:pathExistsError:`, pathExistsError); return null; }); // If the Complete Path does not exist, return an error message. if (pathExists !== true) { return this.errorNotFound; } // If the caller specified a validate function, run it and return the results. if (this.validateFunction) { return await this.validateFunction(completePath); } // If we get here, all validation checks passed. Return TRUE. return true; } } ]; } } exports.ProvideBaseDirectory = ProvideBaseDirectory; //─────────────────────────────────────────────────────────────────────────────────────────────────┐ /** * @class ProvideTargetDirectory * @extends InterviewQuestionsBuilder * @summary Interview Questions Builder for providing a "target directory". * @description Interview Questions Builder for providing a "target directory". */ //─────────────────────────────────────────────────────────────────────────────────────────────────┘ class ProvideTargetDirectory extends builder_1.InterviewQuestionsBuilder { //───────────────────────────────────────────────────────────────────────────┐ /** * @constructs ProvideTargetDirectory * @param {ProvideTargetDirectoryOptions} opts Required. */ //───────────────────────────────────────────────────────────────────────────┘ constructor(opts) { // Call the superclass constructor. super(opts); // Initialize debug for this method. const dbgNS = this.initializeDebug(dbgNs, `constructor`, arguments); // Validate optional options. if (opts.msgStrings.promptProvidePath) validator_1.TypeValidator.throwOnEmptyNullInvalidString(opts.msgStrings.promptProvidePath, `${dbgNS.ext}`, `msgStrings.promptProvidePath`); // Initialize member variables. this.promptProvidePath = opts.msgStrings.promptProvidePath || `What is the path to your target directory?`; } //───────────────────────────────────────────────────────────────────────────┐ /** * @method build * @returns {Questions} * @description Builds the Interview Questions. */ //───────────────────────────────────────────────────────────────────────────┘ build(buildCtx) { // Initialize debug for this method. const dbgNS = this.initializeDebug(dbgNs, `build`, arguments); // Validate the Build Context. validator_1.TypeValidator.throwOnInvalidInstance(buildCtx, prompt_1.SfdxFalconPrompt, `${dbgNS.ext}`, `BuildContext`); validator_1.TypeValidator.throwOnNullInvalidString(buildCtx.defaultAnswers.targetDirectory, `${dbgNS.ext}`, `BuildContext.defaultAnswers.targetDirectory`); // Build and return the Questions. return [ { type: 'input', name: 'targetDirectory', message: this.promptProvidePath, default: (typeof buildCtx.userAnswers.targetDirectory !== 'undefined') ? buildCtx.userAnswers.targetDirectory // Current Value : buildCtx.defaultAnswers.targetDirectory, validate: validator_2.InteractiveValidator.targetPath, filter: filterLocalPath, when: true } ]; } } exports.ProvideTargetDirectory = ProvideTargetDirectory; // ────────────────────────────────────────────────────────────────────────────────────────────────┐ /** * @function filterLocalPath * @param {string} localPath Required. * @returns {string} A resolved version of the path string provided by `localPath`. * @description Filter function which takes a local Path value and resolves it by using * path.resolve(), and then returns that value. */ // ────────────────────────────────────────────────────────────────────────────────────────────────┘ function filterLocalPath(localPath) { // Define local debug namespace and debug arguments. const funcName = `filterLocalPath`; const dbgNsLocal = `${dbgNs}:${funcName}`; debug_1.SfdxFalconDebug.obj(`${dbgNsLocal}:arguments:`, arguments); // Validate the incoming arguments. validator_1.TypeValidator.throwOnNullInvalidString(localPath, `${dbgNsLocal}`, `localPath`); // Return a resolved version of localPath. return path.resolve(localPath); } exports.filterLocalPath = filterLocalPath; //# sourceMappingURL=filesystem.js.map