Created
May 20, 2024 17:15
-
-
Save yene/3397496365b88206863e1c6bebd01c4f to your computer and use it in GitHub Desktop.
Small helper to write the most ugly shell scripts in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { exec } from 'child_process' | |
async function main() { | |
let args = process.argv.slice(2) | |
if (args.length !== 1) { | |
console.log(firstWordRed('Error: missing argument, path to Git directory.')) | |
console.log(firstWordRed('Warning: This script will reset the Git directory.')) | |
process.exit(1) | |
} | |
try { | |
let result = await $`dotnet --version` | |
console.log('dotnet version:', result) | |
} catch (e) { | |
console.log(e) | |
process.exit(1) | |
} | |
} | |
function firstWordRed(str) { | |
let FgColor = "\x1b[31m" | |
let Reset = "\x1b[0m" | |
let firstWord = str.split(' ')[0] | |
let rest = str.split(' ').slice(1).join(' ') | |
return `${FgColor}${firstWord}${Reset} ${rest}` | |
} | |
function firstWordGreen(str) { | |
let FgColor = "\x1b[92m" | |
let Reset = "\x1b[0m" | |
let firstWord = str.split(' ')[0] | |
let rest = str.split(' ').slice(1).join(' ') | |
return `${FgColor}${firstWord}${Reset} ${rest}` | |
} | |
function $(strs, ...interp) { | |
let arr = [] | |
strs.forEach((str, i) => { | |
arr.push(str, interp[i]) | |
}) | |
const cmd = arr.join('') | |
console.log('$', firstWordGreen(cmd)) | |
return new Promise(function (resolve, reject) { | |
exec(cmd, (error, stdout, stderr) => { | |
if (error) { | |
reject(error) | |
return | |
} | |
resolve(stdout.trim()) | |
}) | |
}) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment