Git multi pushing w filter and verbose modes

admin

javascript

другие самплы
//@ts-check


const { execSync, exec } = require('child_process');

const root = './Preact (JSX_babel, hmr)';

/**
 * @type {string}
 */
const branchesResult = execSync('git branch', { cwd: root }).toString();

let branches = branchesResult.split('\n').map(bname => bname.replace('*', '').trim()).filter(Boolean)


/// filtering:

const filterIndex = process.argv.indexOf('-F');

if (~filterIndex && process.argv[filterIndex + 1]) {
    branches = branches.filter(b => ~b.indexOf(process.argv[filterIndex + 1]))
}

/// async: 

const isAsync = ~process.argv.indexOf('-a')

/// verbose mode

const verboseMode = ~process.argv.indexOf('-v');




let sucCount = 0;

branches.forEach(branch => {

    process.stdout.write('\x1b[33m ➣ ' + branch + '\x1b[0m pushing... ' + (isAsync ? '\n' : ''));          // ➣〥 https://pixelplus.ru/samostoyatelno/stati/vnutrennie-faktory/tablica-simvolov-unicode.html

    (isAsync ? asyncPush : syncPush)(branch)
})

!isAsync && console.log(`\n> Succesfully pushed ${sucCount} of ${branches.length}`)


/**
 * 
 * @param {string} branch 
 */
function asyncPush(branch) {


        const interval = setInterval(() => {
            // console.log('\x1b[95m > ' + branch + '\x1b[0m:')
            // process.stdout.moveCursor(0, -1);
            // process.stdout.clearLine(1);
            process.stdout.write('.')
        }, 1000)

        exec(`git push origin ${branch}`, { cwd: root }, (err, r) => {
            const resp = r.toString()

            clearInterval(interval)

            if (r) {
                process.stdout.clearLine(-1);
                console.log(`\x1b[95m ✓ \x1b[0m ${branch}`)
            }            

            if ('-v' in process.argv) {
                console.log('\x1b[90m' + r + '\033[0m')
            }
        });
}



function syncPush(branch) {    

    verboseMode && process.stdout.write('\x1b[90m\x1b[2m')

    try {
        const r = execSync(`git push origin ${branch}`, { cwd: root, stdio: verboseMode ? 'inherit' : 'ignore' });
    }
    catch (er) {
        process.stdout.clearLine(-1);
        process.stdout.cursorTo(0);
        console.log(`\x1b[31m ✗ ${branch} is not pushed \x1b[0m: ${er.message}`)    // ✗❌ - https://www.symbolscopypaste.com/cross-symbol.html
        return;
    }
    
    sucCount++;

    if (verboseMode) {
        process.stdout.cursorTo(0);
        process.stdout.moveCursor(0, -1);
        process.stdout.write(`\x1b[2m\x0b[33m ✓ \x1b[0m`)  // \x1b[2m - bold
    }
    process.stdout.clearLine(-1);
    process.stdout.cursorTo(0);

    console.log(`\x1b[33m ✓ ${branch}\x1b[0m pushed`)
}


process.on('beforeExit', async () => {
    // await something()
    // process.exit(0) // if you don't close yourself this will run forever
});
(ваш голос учтен)

Прикрепить файл