All files / lib/delete delete.js

94.23% Statements 49/52
96.29% Branches 26/27
100% Functions 4/4
94.23% Lines 49/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 1342x 2x 2x 2x 2x                   2x   7x   7x 7x 7x 7x                   7x 7x 7x 1x 1x   6x   4x               4x   3x               1x     2x 1x 1x             1x                   5x 5x 5x 5x 8x 8x 6x 6x 5x 2x 2x   3x     2x     1x 1x     5x 1x 4x 2x                           2x 2x 1x           1x       2x      
const inquirer = require('inquirer');
const { AppStaticService } = require('../../services/appstatic.service');
const CONSTANT = require('../constant');
const chalk = require('chalk');
const { getQuestionsListDynamically } = require('../helper');
 
/**
 * This function provides interactive list of libraries/versions available for org from token.
    1. List all the libray names
    2. Take a choice prompt of deleting entire lib/specific version of a lib(Only -dev version allowed to delete)
    3. Delete operation
 * @param {string} tokenParam optional B2S token for appstatic service, else it will take value from  constant.js B2STOKEN
 * @param {string} appStaticSVCUrl optional appstatic service url, else it will take value from constant.js APPSTATICURL
 */
const deleteLib = async (tokenParam, appStaticSVCUrl) => {
 
    try{
        // Make a fetch call and list all the libraries available.
        const libraryList = await AppStaticService.fetchLibraryNameOrVersions('', tokenParam, appStaticSVCUrl);
        const { SELECT_LIBRARY_NAME } = await inquirer.prompt(getQuestionsListDynamically('SELECT_LIBRARY_NAME', 'Select a library', libraryList));
        const deleteLibOrSpecificVersion = () => {
            return [
                {
                  name: 'SELECT_OPTIONS',
                  message: 'Choose any of the option',
                  type: 'list',
                  choices: CONSTANT.QUESTIONS.DELETE_LIB_OR_SPECIFIC_VERSION,
                  default: false
                }
            ];
        }
        const { SELECT_OPTIONS } = await inquirer.prompt(deleteLibOrSpecificVersion());
        const libVersionsList = await AppStaticService.fetchLibraryNameOrVersions(SELECT_LIBRARY_NAME, tokenParam, appStaticSVCUrl);
        if(libVersionsList && libVersionsList.length === 0){
            console.log(chalk.yellow(`There are no versions in library selected`));
            return;
        }
        if(SELECT_OPTIONS === 'delete_library'){
            // Take confirmation before deleting
            const confirmation = await inquirer.prompt([
                {
                    type: 'confirm',
                    name: 'confirmed',
                    message: `Are you sure you want to delete ?: ${SELECT_LIBRARY_NAME}`,
                    default: false
                }
            ]);
            if(confirmation.confirmed){
                // There is only endpoint for deleting one version at a time, hence get list of versions of a lib first.
                await deleteVersions({
                    libName: SELECT_LIBRARY_NAME,
                    libVersions: [...libVersionsList],
                    isDeleteAll: true
                }, tokenParam, appStaticSVCUrl);
            }
            else{
                // TODO: Should we go back or just exit
                return;
            }
        }
        else if(SELECT_OPTIONS === 'delete_version'){
            const { SELECT_LIBRARY_VERSION } = await inquirer.prompt(getQuestionsListDynamically('SELECT_LIBRARY_VERSION', 'Select a library version', libVersionsList));
            await deleteVersions({
                libName: SELECT_LIBRARY_NAME,
                libVersions: [SELECT_LIBRARY_VERSION],
                isDeleteAll: false
            }, tokenParam, appStaticSVCUrl);
        }
        else{
          return;
        }
    }
    catch(err){
        console.log(chalk(`Error occured while fetching the library list: ${err}`));
        process.exit(1);
    }
}
 
async function deleteVersions({libName, libVersions, isDeleteAll = false}, tokenParam, appStaticSVCUrl) {
    try {
      let deletedCount = 0;
      let notDeletedCount = 0;
      for (const version of libVersions) {
        try {
            if(version.indexOf('dev') !== -1){
                const libVersion = `${libName}:${version}`;
                const deleted = await AppStaticService.deleteLibraryOrVersions(libVersion, tokenParam, appStaticSVCUrl);
                if(deleted){
                    console.log(chalk.magenta(`Deleted version: ${libVersion}`));
                    deletedCount++;
                } else{
                    console.log(chalk.magenta(`Not able to delete: ${libVersion}`));
                }
            } else{
                console.error(chalk.yellow(`Not able to delete ${version}, Only dev version(e.g. 1.0.1-dev) can be deleted.`));
            }
        } catch (error) {
            console.error(chalk.red(`Failed to delete version: ${version}`));
            notDeletedCount++;
        }
      }
      if (deletedCount === libVersions.length && isDeleteAll) {
        console.log(chalk.green('All versions deleted successfully! ✅'));
      } else if(deletedCount !== libVersions.length && isDeleteAll) {
        console.log(chalk.yellow(`Deleted ${deletedCount} versions, ${notDeletedCount} versions not deleted.`));
      }
    } catch (error) {
      console.error(chalk.red('Error occurred while deleting versions:', error));
    }
  }
 
/**
 * This function delete lib based on library/version passed.
 * @param {string} libraryName Library name required arg.
 * @param {string} libraryVersion Library version required arg.
 * @param {string} tokenParam optional B2S token for appstatic service, else it will take value from  constant.js B2STOKEN
 * @param {string} appStaticSVCUrl optional appstatic service url, else it will take value from constant.js APPSTATICURL
 */
  const deleteLibVersion = async (libName, libVersion, tokenParam, appStaticSVCUrl) => {
    if(libName && libVersion){
        await deleteVersions({
            libName: libName,
            libVersions: [libVersion],
            isDeleteAll: false
        }, tokenParam, appStaticSVCUrl);
    } else {
        console.log(chalk.red(`Args missing Library name=${libName}, Library version=${libVersion}`));
    }
  }
 
module.exports = {
    deleteLib,
    deleteLibVersion
}