All files / src add-remove-xref-source.js

0% Statements 0/80
0% Branches 0/33
0% Functions 0/12
0% Lines 0/80

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                                                                                                                                                                                                                                                                                                                                       
const fs = require('node:fs');
const path = require('node:path');
const Logger = require('./utils/logger');
 
// Resolve the path to specs.json in the root directory
const JSON_FILE = path.resolve(process.cwd(), 'specs.json');
 
// Check if the JSON file exists
if (!fs.existsSync(JSON_FILE)) {
    Logger.error(`Error: ${JSON_FILE} does not exist.`);
    process.exit(1);
}
 
// Ask the user for inputs
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
 
let mode = ""; // To track if user wants to add or remove
 
// Function to prompt for mode
function askMode() {
    rl.question('Would you like to add, remove, or view entries? (add/remove/view): ', (answer) => {
        mode = answer.trim().toLowerCase();
        if (mode === 'add' || mode === 'a') {
            askQuestion(0);
        } else if (mode === 'remove' || mode === 'r') {
            askRemoveEntry();
        } else if (mode === 'view' || mode === 'v') {
            showReferences();
        } else {
            Logger.warn('Invalid option. Please enter add, remove, or view.');
            askMode();
        }
    });
}
 
// Questions for adding an entry
const questions = [
    'Enter URL of the repository: ',
    'Enter URL of the GitHub page: ',
    'Enter short unique name: ',
];
 
let inputs = {};
 
// Function to ask questions for adding an entry
function askQuestion(index) {
    if (index === questions.length) {
        rl.close();
        updateJSON();
        return;
    }
 
    rl.question(questions[index], (answer) => {
        switch (index) {
            case 0:
                inputs.url = answer;
                break;
            case 1:
                inputs.gh_page = answer;
                break;
            case 2:
                inputs.external_spec = answer;
                break;
        }
        askQuestion(index + 1);
    });
}
 
// Function to prompt for removal
function askRemoveEntry() {
    rl.question('Enter the short unique name of the entry you want to remove: ', (answer) => {
        removeEntry(answer.trim());
        rl.close();
    });
}
 
// Function to show current external references
function showReferences() {
    const data = JSON.parse(fs.readFileSync(JSON_FILE, 'utf8'));
    Logger.info('Current external references (xref):');
 
    data.specs[0].external_specs.forEach(spec => {
        Logger.separator();
        Logger.highlight(`Short name: ${spec.external_spec}`);
        Logger.info(`GitHub Page: ${spec.gh_page}`);
        Logger.info(`URL: ${spec.url}`);
        Logger.separator();
    });
    rl.close();
}
 
// Update the JSON file by adding an entry
function updateJSON() {
    try {
        const data = JSON.parse(fs.readFileSync(JSON_FILE, 'utf8'));
 
        if (!data.specs || !Array.isArray(data.specs) || !data.specs[0].external_specs) {
            Logger.error('Error: Invalid JSON structure. "specs[0].external_specs" is missing.');
            process.exit(1);
        }
 
        const externalSpecs = data.specs[0].external_specs;
 
        // Check if the external_spec already exists
        const exists = externalSpecs.some(
            (entry) => entry.external_spec === inputs.external_spec
        );
 
        if (exists) {
            Logger.warn(`Entry with external_spec "${inputs.external_spec}" already exists. No changes made.`);
            return;
        }
 
        // Add the new entry if it doesn't exist
        externalSpecs.push(inputs);
 
    fs.writeFileSync(JSON_FILE, JSON.stringify(data, null, 2), 'utf8');
    Logger.success(`Updated successfully.`);
    } catch (error) {
    Logger.error(`Error: Failed to update ${JSON_FILE}.`, error.message);
        process.exit(1);
    }
}
 
// Remove an entry based on `external_spec`
function removeEntry(externalSpec) {
    try {
        const data = JSON.parse(fs.readFileSync(JSON_FILE, 'utf8'));
 
        if (!data.specs || !Array.isArray(data.specs) || !data.specs[0].external_specs) {
            Logger.error('Error: Invalid JSON structure. "specs[0].external_specs" is missing.');
            process.exit(1);
        }
 
        const externalSpecs = data.specs[0].external_specs;
 
        // Filter out the entry with the matching external_spec
        const filteredSpecs = externalSpecs.filter(
            (entry) => entry.external_spec !== externalSpec
        );
 
        if (filteredSpecs.length === externalSpecs.length) {
            Logger.warn(`No entry found with external_spec "${externalSpec}".`);
            return;
        }
 
        // Update the JSON structure
        data.specs[0].external_specs = filteredSpecs;
 
    fs.writeFileSync(JSON_FILE, JSON.stringify(data, null, 2), 'utf8');
    Logger.success(`Removed entry successfully.`);
    } catch (error) {
    Logger.error(`Error: Failed to update ${JSON_FILE}.`, error.message);
        process.exit(1);
    }
}
 
// Start by asking the mode
askMode();